8 |
|
#include "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 |
< |
random(); |
33 |
> |
daily(matcher.size() > 1 ? matcher[1] : ""); |
34 |
> |
} |
35 |
> |
else if (path == matcher("^/random/(\\d+)?$")) |
36 |
> |
{ |
37 |
> |
random(matcher.size() > 1 ? matcher[1] : ""); |
38 |
|
} |
39 |
|
else if (path == matcher("^/(" + this->matcher + ")$")) |
40 |
|
{ |
49 |
|
|
50 |
|
ext::String FeepingCreaturism::program; |
51 |
|
|
52 |
+ |
bool FeepingCreaturism::CaseLess::operator()(const std::string& one, |
53 |
+ |
const std::string& two) |
54 |
+ |
{ |
55 |
+ |
std::string one_(one), two_(two); |
56 |
+ |
|
57 |
+ |
// XXX: should be std::tolower except g++34 doesn't believe it |
58 |
+ |
std::transform(one.begin(), one.end(), one_.begin(), ::tolower); |
59 |
+ |
std::transform(two.begin(), two.end(), two_.begin(), ::tolower); |
60 |
+ |
|
61 |
+ |
if (one_ == two_) return one < two; |
62 |
+ |
|
63 |
+ |
return one_ < two_; |
64 |
+ |
} |
65 |
+ |
|
66 |
|
void FeepingCreaturism::initialize() |
67 |
|
{ |
68 |
< |
ext::Handle<xml::Document> document(xml::Parse("jargon.xml")); |
69 |
< |
ext::Handle<xml::Node> node(*document/"feepingcreaturism"); |
68 |
> |
_H<xml::Document> document(xml::Parse("jargon.xml")); |
69 |
> |
_H<xml::Node> node(*document/"feepingcreaturism"); |
70 |
|
|
71 |
|
this->path = *node/"jargon"; |
72 |
|
this->matcher = *node/"matcher"; |
73 |
|
|
74 |
< |
char* path[] = { new char[this->path.size()] }; |
74 |
> |
_L<ext::String> args(1, this->path); |
75 |
|
|
76 |
< |
std::strcpy(path[0], this->path.c_str()); |
76 |
> |
args.InsertLast("-type"); |
77 |
> |
args.InsertLast("f"); |
78 |
|
|
79 |
< |
::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL)); |
79 |
> |
_S<api::Process> find("/usr/bin/find", args); |
80 |
|
Matcher matcher("^" + this->path + "/(" + this->matcher + ")$"); |
81 |
+ |
ext::String path; |
82 |
|
|
83 |
< |
if (traversal == NULL) |
83 |
> |
while (ios::ReadLine(*find.GetReader(), path)) if (matcher == path) |
84 |
|
{ |
85 |
< |
api::Cerr << program << ": Horrible Failure!\n"; |
70 |
< |
|
71 |
< |
std::exit(1); |
85 |
> |
jargon.insert(matcher[1]); |
86 |
|
} |
73 |
– |
|
74 |
– |
for (::FTSENT* entity(::fts_read(traversal)); entity != NULL; |
75 |
– |
entity = ::fts_read(traversal)) |
76 |
– |
{ |
77 |
– |
if (entity->fts_info == FTS_F && entity->fts_path == matcher) |
78 |
– |
{ |
79 |
– |
jargon.insert(matcher[1]); |
80 |
– |
} |
81 |
– |
} |
82 |
– |
|
83 |
– |
::fts_close(traversal); |
84 |
– |
|
85 |
– |
delete [] path[0]; |
87 |
|
} |
88 |
|
|
89 |
|
void FeepingCreaturism::parse() |
116 |
|
while (query.good()); |
117 |
|
} |
118 |
|
|
119 |
< |
void FeepingCreaturism::daily() |
119 |
> |
void FeepingCreaturism::daily(const ext::String& date) |
120 |
|
{ |
121 |
|
std::time_t when(std::time(NULL)); |
122 |
< |
std::tm* now(std::localtime(&when)); |
122 |
> |
std::tm* day(std::localtime(&when)); |
123 |
> |
|
124 |
> |
day->tm_sec = 0; |
125 |
> |
day->tm_min = 0; |
126 |
> |
day->tm_hour = 0; |
127 |
|
|
128 |
< |
now->tm_sec = 0; |
124 |
< |
now->tm_min = 0; |
125 |
< |
now->tm_hour = 0; |
128 |
> |
if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
129 |
|
|
130 |
< |
std::time_t difference(mktime(now) / 86400); |
130 |
> |
std::time_t difference(mktime(day) / 86400); |
131 |
|
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
132 |
|
ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
133 |
|
|
134 |
|
select(entry); |
135 |
|
} |
136 |
|
|
137 |
< |
void FeepingCreaturism::random() |
137 |
> |
void FeepingCreaturism::random(const ext::String& number) |
138 |
|
{ |
139 |
|
::srandomdev(); |
140 |
|
|
141 |
|
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
142 |
< |
ext::String entry(jargon.size() ? jargon[::random() % jargon.size()] : ""); |
142 |
> |
std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() : |
143 |
> |
lexical_cast<std::vector<ext::String>::size_type>(number)); |
144 |
> |
|
145 |
> |
assert(random >= 0); |
146 |
> |
assert(random % jargon.size() < jargon.size()); |
147 |
> |
|
148 |
> |
ext::String entry(jargon.size() ? jargon[random % jargon.size()] : ""); |
149 |
|
|
150 |
|
select(entry); |
151 |
|
} |
156 |
|
{ |
157 |
|
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
158 |
|
|
159 |
< |
_M::iterator include(cgi.find("include")); |
160 |
< |
|
161 |
< |
if (include != cgi.end()) |
162 |
< |
{ |
154 |
< |
Jargon jargon(path + "/" + selection, |
155 |
< |
lexical_cast<bool>(include->second)); |
156 |
< |
|
157 |
< |
api::Cout << jargon; |
158 |
< |
} |
159 |
< |
else |
160 |
< |
{ |
161 |
< |
Jargon jargon(path + "/" + selection); |
159 |
> |
Jargon jargon(path, selection, cgi.find("include") != cgi.end() |
160 |
> |
&& lexical_cast<bool>(ext::String(cgi.find("include")->second)), |
161 |
> |
cgi.find("relative") != cgi.end() |
162 |
> |
? ext::String(cgi.find("relative")->second) : ext::String()); |
163 |
|
|
164 |
< |
api::Cout << jargon; |
164 |
< |
} |
164 |
> |
api::Cout << jargon; |
165 |
|
} |
166 |
|
else |
167 |
|
{ |