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 |
|
|
42 |
|
} |
43 |
|
else |
44 |
|
{ |
45 |
< |
api::Cout << "Location: http://" << env.get("HTTP_HOST") |
50 |
< |
<< 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) |
84 |
< |
{ |
85 |
< |
api::Cerr << program << ": Horrible Failure!\n"; |
86 |
< |
|
87 |
< |
std::exit(1); |
88 |
< |
} |
89 |
< |
|
90 |
< |
for (::FTSENT* entity(::fts_read(traversal)); entity != NULL; |
91 |
< |
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) |
94 |
< |
{ |
95 |
< |
jargon.insert(matcher[1]); |
96 |
< |
} |
84 |
> |
jargon.insert(matcher[1]); |
85 |
|
} |
86 |
|
|
87 |
< |
::fts_close(traversal); |
100 |
< |
|
101 |
< |
delete [] path[0]; |
87 |
> |
find.Join(); |
88 |
|
} |
89 |
|
|
90 |
|
void FeepingCreaturism::parse() |
93 |
|
|
94 |
|
if (env.get("REQUEST_METHOD") == "POST") |
95 |
|
{ |
96 |
< |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_" |
111 |
< |
"TYPE"))); |
96 |
> |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_TYPE"))); |
97 |
|
char* content(new char[length]); |
98 |
|
|
99 |
|
api::Cin.Read(content, length); |
128 |
|
if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
129 |
|
|
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()] : ""); |
131 |
> |
ext::Vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
132 |
> |
ext::String entry(jargon.GetSize() ? jargon[difference % jargon.GetSize()] : ""); |
133 |
|
|
134 |
|
select(entry); |
135 |
|
} |
138 |
|
{ |
139 |
|
::srandomdev(); |
140 |
|
|
141 |
< |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
142 |
< |
std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() : |
158 |
< |
lexical_cast<std::vector<ext::String>::size_type>(number)); |
141 |
> |
ext::Vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
142 |
> |
size_t random(number.IsEmpty() ? ::random() : lexical_cast<size_t>(number)); |
143 |
|
|
144 |
|
assert(random >= 0); |
145 |
< |
assert(random % jargon.size() < jargon.size()); |
145 |
> |
assert(random % jargon.GetSize() < jargon.GetSize()); |
146 |
|
|
147 |
< |
ext::String entry(jargon.size() ? jargon[random % jargon.size()] : ""); |
147 |
> |
ext::String entry(jargon.GetSize() ? jargon[random % jargon.GetSize()] : ""); |
148 |
|
|
149 |
|
select(entry); |
150 |
|
} |
155 |
|
{ |
156 |
|
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
157 |
|
|
158 |
< |
Jargon jargon(path + "/" + selection, cgi.find("include") != cgi.end() |
175 |
< |
&& lexical_cast<bool>(ext::String(cgi.find("include")->second)), |
176 |
< |
cgi.find("relative") != cgi.end() |
177 |
< |
? ext::String(cgi.find("relative")->second) : ext::String()); |
158 |
> |
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()); |
159 |
|
|
160 |
|
api::Cout << jargon; |
161 |
|
} |