1 |
Douglas Thrift |
191 |
// Feeping Creaturism |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
Douglas Thrift |
194 |
#include "Environment.hpp" |
8 |
|
|
#include "Matcher.hpp" |
9 |
Douglas Thrift |
191 |
|
10 |
Douglas Thrift |
194 |
extern "C" |
11 |
|
|
{ |
12 |
|
|
#include <sys/types.h> |
13 |
|
|
#include <sys/stat.h> |
14 |
|
|
#include <fts.h> |
15 |
|
|
} |
16 |
|
|
|
17 |
Douglas Thrift |
191 |
int main(int argc, char* argv[]) |
18 |
|
|
{ |
19 |
|
|
FeepingCreaturism creaturism; |
20 |
|
|
|
21 |
|
|
return 0; |
22 |
|
|
} |
23 |
Douglas Thrift |
194 |
|
24 |
|
|
FeepingCreaturism::FeepingCreaturism() |
25 |
|
|
{ |
26 |
|
|
initialize(); |
27 |
|
|
|
28 |
|
|
std::string path(env.get("PATH_INFO")); |
29 |
|
|
Matcher matcher; |
30 |
|
|
|
31 |
|
|
if (path.empty()) daily(); else if (path == "/random") |
32 |
|
|
{ |
33 |
|
|
random(); |
34 |
|
|
} |
35 |
|
|
else if (path == matcher("^/(" + this->matcher + ")$")) |
36 |
|
|
{ |
37 |
|
|
select(matcher[1], true); |
38 |
|
|
} |
39 |
|
|
else daily(); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
void FeepingCreaturism::initialize() |
43 |
|
|
{ |
44 |
|
|
ext::Handle<xml::Document> document(xml::Parse("FeepingCreaturism.xml")); |
45 |
|
|
ext::Handle<xml::Node> node(*document/"FeepingCreaturism"); |
46 |
|
|
|
47 |
|
|
this->path = *node/"Jargon"; |
48 |
|
|
this->matcher = *node/"Matcher"; |
49 |
|
|
|
50 |
|
|
char* path[] = { new char[this->path.size()] }; |
51 |
|
|
|
52 |
|
|
std::strcpy(path[0], this->path.c_str()); |
53 |
|
|
|
54 |
|
|
::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL)); |
55 |
|
|
Matcher matcher('^' + this->path + "/(" + this->matcher + ")$"); |
56 |
|
|
|
57 |
|
|
for (::FTSENT* entity(::fts_read(traversal)); entity != NULL; |
58 |
|
|
entity = ::fts_read(traversal)) |
59 |
|
|
{ |
60 |
|
|
if (entity->fts_info == FTS_F && entity->fts_path == matcher) |
61 |
|
|
{ |
62 |
|
|
jargon.insert(matcher[1]); |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
::fts_close(traversal); |
67 |
|
|
|
68 |
|
|
delete [] path[0]; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
void FeepingCreaturism::daily() |
72 |
|
|
{ |
73 |
|
|
std::time_t when(std::time(NULL)); |
74 |
|
|
std::tm* now(std::localtime(&when)); |
75 |
|
|
|
76 |
|
|
now->tm_sec = 0; |
77 |
|
|
now->tm_min = 0; |
78 |
|
|
now->tm_hour = 0; |
79 |
|
|
|
80 |
|
|
std::time_t difference(mktime(now) / 86400); |
81 |
|
|
std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end()); |
82 |
|
|
std::string entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
83 |
|
|
|
84 |
|
|
select(entry); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
void FeepingCreaturism::random() |
88 |
|
|
{ |
89 |
|
|
::srandomdev(); |
90 |
|
|
|
91 |
|
|
std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end()); |
92 |
|
|
std::string entry(jargon.size() ? jargon[::random() % jargon.size()] : ""); |
93 |
|
|
|
94 |
|
|
select(entry); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
void FeepingCreaturism::select(const std::string& selection, bool validate) |
98 |
|
|
{ |
99 |
|
|
if (!validate || jargon.find(selection) != jargon.end()) |
100 |
|
|
{ |
101 |
|
|
cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << selection |
102 |
|
|
<< "\r\n"; |
103 |
|
|
} |
104 |
|
|
else cout << "Status: 404\r\n\r\n"; |
105 |
|
|
} |