ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 243
Committed: 2004-09-11T21:27:10-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 4663 byte(s)
Log Message:
Meep!

File Contents

# Content
1 // Feeping Creaturism
2 //
3 // Douglas Thrift
4 //
5 // $Id$
6
7 #include "Environment.hpp"
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 }
17
18 #include <menes-api/exename.hpp>
19 #include <menes-app/application.hpp>
20
21 struct FeepingCreaturismCommand : public app::Application
22 {
23 virtual int Run(const app::ArgumentList& args)
24 {
25 FeepingCreaturism::program = api::GetExecutableName();
26
27 FeepingCreaturism creaturism;
28
29 return 0;
30 }
31 } creaturism;
32
33 FeepingCreaturism::FeepingCreaturism()
34 {
35 initialize();
36 parse();
37
38 ext::String path(env.get("PATH_INFO"));
39 Matcher matcher;
40
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(matcher.size() > 1 ? matcher[1] : "");
48 }
49 else if (path == matcher("^/(" + this->matcher + ")$"))
50 {
51 select(matcher[1], true);
52 }
53 else
54 {
55 api::Cout << "Location: http://" << env.get("HTTP_HOST")
56 << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n";
57 }
58 }
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"));
77 ext::Handle<xml::Node> node(*document/"feepingcreaturism");
78
79 this->path = *node/"jargon";
80 this->matcher = *node/"matcher";
81
82 char* path[] = { new char[this->path.GetData().GetSize()] };
83
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 + ")$");
88
89 if (traversal == NULL)
90 {
91 api::Cerr << program << ": Horrible Failure!\n";
92
93 std::exit(1);
94 }
95
96 for (::FTSENT* entity(::fts_read(traversal)); entity != NULL;
97 entity = ::fts_read(traversal))
98 {
99 if (entity->fts_info == FTS_F && entity->fts_path == matcher)
100 {
101 jargon.insert(matcher[1]);
102 }
103 }
104
105 ::fts_close(traversal);
106
107 delete [] path[0];
108 }
109
110 void FeepingCreaturism::parse()
111 {
112 std::stringstream query(env.get("QUERY_STRING"));
113
114 if (env.get("REQUEST_METHOD") == "POST")
115 {
116 std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_"
117 "TYPE")));
118 char* content(new char[length]);
119
120 api::Cin.Read(content, length);
121 query.write(content, length);
122
123 delete [] content;
124 }
125
126 if (query.str().empty()) return;
127
128 do
129 {
130 std::string name, value;
131
132 std::getline(query, name, '=');
133 std::getline(query, value, '&');
134
135 cgi.insert(_P(name, value));
136 }
137 while (query.good());
138 }
139
140 void FeepingCreaturism::daily(const ext::String& date)
141 {
142 std::time_t when(std::time(NULL));
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 if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
150
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(const ext::String& number)
159 {
160 ::srandomdev();
161
162 std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
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 }
173
174 void FeepingCreaturism::select(const ext::String& selection, bool validate)
175 {
176 if (!validate || jargon.find(selection) != jargon.end())
177 {
178 api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
179
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;
186 }
187 else
188 {
189 api::Cout << "Status: 404\r\n"
190 << "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"
191 << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
192 << "<html><head>\n<title>404 Not Found</title>\n</head><body>\n"
193 << "<h1>Not Found</h1>\n<p>The requested URL "
194 << env.get("PATH_INFO") << " was not found on this server.</p>\n"
195 << "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n";
196 }
197 }

Properties

Name Value
svn:eol-style native
svn:keywords Id