5 |
|
// $Id$ |
6 |
|
|
7 |
|
#include "Environment.hpp" |
8 |
< |
#include "Matcher.hpp" |
8 |
> |
#include "Matcher/Matcher.hpp" |
9 |
|
#include "Jargon.hpp" |
10 |
|
|
11 |
< |
extern "C" |
12 |
< |
{ |
13 |
< |
#include <sys/types.h> |
14 |
< |
#include <sys/stat.h> |
15 |
< |
#include <fts.h> |
16 |
< |
} |
11 |
> |
#include <menes-api/exename.hpp> |
12 |
> |
#include <menes-app/simple.hpp> |
13 |
|
|
14 |
< |
int main(int argc, char* argv[]) |
14 |
> |
int Main(const app::Options& options) |
15 |
|
{ |
16 |
< |
FeepingCreaturism::program = argv[0]; |
16 |
> |
FeepingCreaturism::program = api::GetExecutablePath().GetName(); |
17 |
|
|
18 |
|
FeepingCreaturism creaturism; |
19 |
|
|
28 |
|
ext::String path(env.get("PATH_INFO")); |
29 |
|
Matcher matcher; |
30 |
|
|
31 |
< |
if (path == "/daily/") daily(); else if (path == "/random/") |
31 |
> |
if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$")) |
32 |
> |
{ |
33 |
> |
daily(matcher.size() > 1 ? matcher[1] : ""); |
34 |
> |
} |
35 |
> |
else if (path == matcher("^/random/(\\d+)?$")) |
36 |
|
{ |
37 |
< |
random(); |
37 |
> |
random(matcher.size() > 1 ? matcher[1] : ""); |
38 |
|
} |
39 |
|
else if (path == matcher("^/(" + this->matcher + ")$")) |
40 |
|
{ |
42 |
|
} |
43 |
|
else |
44 |
|
{ |
45 |
< |
api::Cout << "Location: http://" << env.get("HTTP_HOST") |
46 |
< |
<< env.get("SCRIPT_NAME") << "/daily/\r\n\r\n"; |
45 |
> |
api::Cout << "Location: http://" << env.get("HTTP_HOST") << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n"; |
46 |
|
} |
47 |
|
} |
48 |
|
|
57 |
|
std::transform(one.begin(), one.end(), one_.begin(), ::tolower); |
58 |
|
std::transform(two.begin(), two.end(), two_.begin(), ::tolower); |
59 |
|
|
60 |
+ |
if (one_ == two_) return one < two; |
61 |
+ |
|
62 |
|
return one_ < two_; |
63 |
|
} |
64 |
|
|
65 |
|
void FeepingCreaturism::initialize() |
66 |
|
{ |
67 |
< |
ext::Handle<xml::Document> document(xml::Parse("jargon.xml")); |
68 |
< |
ext::Handle<xml::Node> node(*document/"feepingcreaturism"); |
67 |
> |
_H<xml::Document> document(xml::Parse("jargon.xml")); |
68 |
> |
_H<xml::Node> node(*document/"feepingcreaturism"); |
69 |
|
|
70 |
|
this->path = *node/"jargon"; |
71 |
|
this->matcher = *node/"matcher"; |
72 |
|
|
73 |
< |
char* path[] = { new char[this->path.size()] }; |
73 |
> |
_L<ext::String> args(1, this->path); |
74 |
|
|
75 |
< |
std::strcpy(path[0], this->path.c_str()); |
75 |
> |
args.InsertLast("-type"); |
76 |
> |
args.InsertLast("f"); |
77 |
|
|
78 |
< |
::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL)); |
78 |
> |
_S<api::Process> find("/usr/bin/find", args); |
79 |
|
Matcher matcher("^" + this->path + "/(" + this->matcher + ")$"); |
80 |
+ |
ext::String path; |
81 |
|
|
82 |
< |
if (traversal == NULL) |
80 |
< |
{ |
81 |
< |
api::Cerr << program << ": Horrible Failure!\n"; |
82 |
< |
|
83 |
< |
std::exit(1); |
84 |
< |
} |
85 |
< |
|
86 |
< |
for (::FTSENT* entity(::fts_read(traversal)); entity != NULL; |
87 |
< |
entity = ::fts_read(traversal)) |
82 |
> |
while (ios::ReadLine(*find.GetReader(), path)) if (matcher == path) |
83 |
|
{ |
84 |
< |
if (entity->fts_info == FTS_F && entity->fts_path == matcher) |
90 |
< |
{ |
91 |
< |
jargon.insert(matcher[1]); |
92 |
< |
} |
84 |
> |
jargon.insert(matcher[1]); |
85 |
|
} |
94 |
– |
|
95 |
– |
::fts_close(traversal); |
96 |
– |
|
97 |
– |
delete [] path[0]; |
86 |
|
} |
87 |
|
|
88 |
|
void FeepingCreaturism::parse() |
91 |
|
|
92 |
|
if (env.get("REQUEST_METHOD") == "POST") |
93 |
|
{ |
94 |
< |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_" |
107 |
< |
"TYPE"))); |
94 |
> |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_TYPE"))); |
95 |
|
char* content(new char[length]); |
96 |
|
|
97 |
|
api::Cin.Read(content, length); |
109 |
|
std::getline(query, name, '='); |
110 |
|
std::getline(query, value, '&'); |
111 |
|
|
112 |
< |
cgi.insert(_P(name, value)); |
112 |
> |
cgi.insert(std::pair<std::string, std::string>(name, value)); |
113 |
|
} |
114 |
|
while (query.good()); |
115 |
|
} |
116 |
|
|
117 |
< |
void FeepingCreaturism::daily() |
117 |
> |
void FeepingCreaturism::daily(const ext::String& date) |
118 |
|
{ |
119 |
|
std::time_t when(std::time(NULL)); |
120 |
< |
std::tm* now(std::localtime(&when)); |
120 |
> |
std::tm* day(std::localtime(&when)); |
121 |
> |
|
122 |
> |
day->tm_sec = 0; |
123 |
> |
day->tm_min = 0; |
124 |
> |
day->tm_hour = 0; |
125 |
|
|
126 |
< |
now->tm_sec = 0; |
127 |
< |
now->tm_min = 0; |
128 |
< |
now->tm_hour = 0; |
129 |
< |
|
130 |
< |
std::time_t difference(mktime(now) / 86400); |
140 |
< |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
141 |
< |
ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
126 |
> |
if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
127 |
> |
|
128 |
> |
std::time_t difference(mktime(day) / 86400); |
129 |
> |
ext::Vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
130 |
> |
ext::String entry(jargon.GetSize() ? jargon[difference % jargon.GetSize()] : ""); |
131 |
|
|
132 |
|
select(entry); |
133 |
|
} |
134 |
|
|
135 |
< |
void FeepingCreaturism::random() |
135 |
> |
void FeepingCreaturism::random(const ext::String& number) |
136 |
|
{ |
137 |
|
::srandomdev(); |
138 |
|
|
139 |
< |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
140 |
< |
ext::String entry(jargon.size() ? jargon[::random() % jargon.size()] : ""); |
139 |
> |
ext::Vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
140 |
> |
size_t random(number.IsEmpty() ? ::random() : lexical_cast<size_t>(number)); |
141 |
> |
|
142 |
> |
assert(random >= 0); |
143 |
> |
assert(random % jargon.GetSize() < jargon.GetSize()); |
144 |
> |
|
145 |
> |
ext::String entry(jargon.GetSize() ? jargon[random % jargon.GetSize()] : ""); |
146 |
|
|
147 |
|
select(entry); |
148 |
|
} |
153 |
|
{ |
154 |
|
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
155 |
|
|
156 |
< |
_M::iterator include(cgi.find("include")); |
163 |
< |
|
164 |
< |
if (include != cgi.end()) |
165 |
< |
{ |
166 |
< |
Jargon jargon(path + "/" + selection, |
167 |
< |
lexical_cast<bool>(include->second)); |
168 |
< |
|
169 |
< |
api::Cout << jargon; |
170 |
< |
} |
171 |
< |
else |
172 |
< |
{ |
173 |
< |
Jargon jargon(path + "/" + selection); |
156 |
> |
Jargon jargon(path, selection, cgi.find("include") != cgi.end() && lexical_cast<bool>(ext::String(cgi.find("include")->second)), cgi.find("relative") != cgi.end() ? ext::String(cgi.find("relative")->second) : ext::String()); |
157 |
|
|
158 |
< |
api::Cout << jargon; |
176 |
< |
} |
158 |
> |
api::Cout << jargon; |
159 |
|
} |
160 |
|
else |
161 |
|
{ |