15 |
|
#include <fts.h> |
16 |
|
} |
17 |
|
|
18 |
< |
int main(int argc, char* argv[]) |
18 |
> |
#include <menes-api/exename.hpp> |
19 |
> |
#include <menes-app/application.hpp> |
20 |
> |
|
21 |
> |
struct FeepingCreaturismCommand : public app::Application |
22 |
|
{ |
23 |
< |
FeepingCreaturism::program = argv[0]; |
23 |
> |
virtual int Run(const app::ArgumentList& args) |
24 |
> |
{ |
25 |
> |
FeepingCreaturism::program = api::GetExecutableName(); |
26 |
|
|
27 |
< |
FeepingCreaturism creaturism; |
27 |
> |
FeepingCreaturism creaturism; |
28 |
|
|
29 |
< |
return 0; |
30 |
< |
} |
29 |
> |
return 0; |
30 |
> |
} |
31 |
> |
} creaturism; |
32 |
|
|
33 |
|
FeepingCreaturism::FeepingCreaturism() |
34 |
|
{ |
38 |
|
ext::String path(env.get("PATH_INFO")); |
39 |
|
Matcher matcher; |
40 |
|
|
41 |
< |
if (path == "/daily/") daily(); else if (path == "/random/") |
41 |
> |
if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$")) |
42 |
> |
{ |
43 |
> |
daily(matcher.size() > 1 ? matcher[1] : ""); |
44 |
> |
} |
45 |
> |
else if (path == matcher("^/random/(\\d+)?$")) |
46 |
|
{ |
47 |
< |
random(); |
47 |
> |
random(matcher.size() > 1 ? matcher[1] : ""); |
48 |
|
} |
49 |
|
else if (path == matcher("^/(" + this->matcher + ")$")) |
50 |
|
{ |
59 |
|
|
60 |
|
ext::String FeepingCreaturism::program; |
61 |
|
|
62 |
+ |
bool FeepingCreaturism::CaseLess::operator()(const std::string& one, |
63 |
+ |
const std::string& two) |
64 |
+ |
{ |
65 |
+ |
std::string one_(one), two_(two); |
66 |
+ |
|
67 |
+ |
// XXX: should be std::tolower except g++34 doesn't believe it |
68 |
+ |
std::transform(one.begin(), one.end(), one_.begin(), ::tolower); |
69 |
+ |
std::transform(two.begin(), two.end(), two_.begin(), ::tolower); |
70 |
+ |
|
71 |
+ |
return one_ < two_; |
72 |
+ |
} |
73 |
+ |
|
74 |
|
void FeepingCreaturism::initialize() |
75 |
|
{ |
76 |
|
ext::Handle<xml::Document> document(xml::Parse("jargon.xml")); |
79 |
|
this->path = *node/"jargon"; |
80 |
|
this->matcher = *node/"matcher"; |
81 |
|
|
82 |
< |
char* path[] = { new char[this->path.size()] }; |
82 |
> |
char* path[] = { new char[this->path.GetData().GetSize()] }; |
83 |
|
|
84 |
< |
std::strcpy(path[0], this->path.c_str()); |
84 |
> |
std::strcpy(path[0], this->path.NullTerminate()); |
85 |
|
|
86 |
|
::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL)); |
87 |
|
Matcher matcher("^" + this->path + "/(" + this->matcher + ")$"); |
137 |
|
while (query.good()); |
138 |
|
} |
139 |
|
|
140 |
< |
void FeepingCreaturism::daily() |
140 |
> |
void FeepingCreaturism::daily(const ext::String& date) |
141 |
|
{ |
142 |
|
std::time_t when(std::time(NULL)); |
143 |
< |
std::tm* now(std::localtime(&when)); |
143 |
> |
std::tm* day(std::localtime(&when)); |
144 |
> |
|
145 |
> |
day->tm_sec = 0; |
146 |
> |
day->tm_min = 0; |
147 |
> |
day->tm_hour = 0; |
148 |
|
|
149 |
< |
now->tm_sec = 0; |
124 |
< |
now->tm_min = 0; |
125 |
< |
now->tm_hour = 0; |
149 |
> |
if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
150 |
|
|
151 |
< |
std::time_t difference(mktime(now) / 86400); |
151 |
> |
std::time_t difference(mktime(day) / 86400); |
152 |
|
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
153 |
|
ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
154 |
|
|
155 |
|
select(entry); |
156 |
|
} |
157 |
|
|
158 |
< |
void FeepingCreaturism::random() |
158 |
> |
void FeepingCreaturism::random(const ext::String& number) |
159 |
|
{ |
160 |
|
::srandomdev(); |
161 |
|
|
162 |
|
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
163 |
< |
ext::String entry(jargon.size() ? jargon[::random() % jargon.size()] : ""); |
163 |
> |
std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() : |
164 |
> |
lexical_cast<std::vector<ext::String>::size_type>(number)); |
165 |
> |
|
166 |
> |
assert(random >= 0); |
167 |
> |
assert(random % jargon.size() < jargon.size()); |
168 |
> |
|
169 |
> |
ext::String entry(jargon.size() ? jargon[random % jargon.size()] : ""); |
170 |
|
|
171 |
|
select(entry); |
172 |
|
} |
177 |
|
{ |
178 |
|
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
179 |
|
|
180 |
< |
_M::iterator include(cgi.find("include")); |
181 |
< |
|
182 |
< |
if (include != cgi.end()) |
183 |
< |
{ |
154 |
< |
Jargon jargon(path + "/" + selection, |
155 |
< |
lexical_cast<bool>(include->second)); |
180 |
> |
Jargon jargon(path, selection, cgi.find("include") != cgi.end() |
181 |
> |
&& lexical_cast<bool>(ext::String(cgi.find("include")->second)), |
182 |
> |
cgi.find("relative") != cgi.end() |
183 |
> |
? ext::String(cgi.find("relative")->second) : ext::String()); |
184 |
|
|
185 |
< |
api::Cout << jargon; |
158 |
< |
} |
159 |
< |
else |
160 |
< |
{ |
161 |
< |
Jargon jargon(path + "/" + selection); |
162 |
< |
|
163 |
< |
api::Cout << jargon; |
164 |
< |
} |
185 |
> |
api::Cout << jargon; |
186 |
|
} |
187 |
|
else |
188 |
|
{ |