ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 219
Committed: 2004-09-05T20:52:38-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 4485 byte(s)
Log Message:
That might work better.

File Contents

# User Rev Content
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 196 #include "Jargon.hpp"
10 Douglas Thrift 191
11 Douglas Thrift 194 extern "C"
12     {
13     #include <sys/types.h>
14     #include <sys/stat.h>
15     #include <fts.h>
16     }
17    
18 Douglas Thrift 191 int main(int argc, char* argv[])
19     {
20 Douglas Thrift 201 FeepingCreaturism::program = argv[0];
21    
22 Douglas Thrift 191 FeepingCreaturism creaturism;
23    
24     return 0;
25     }
26 Douglas Thrift 194
27     FeepingCreaturism::FeepingCreaturism()
28     {
29     initialize();
30 Douglas Thrift 203 parse();
31 Douglas Thrift 194
32 Douglas Thrift 203 ext::String path(env.get("PATH_INFO"));
33 Douglas Thrift 194 Matcher matcher;
34    
35 Douglas Thrift 211 if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))
36 Douglas Thrift 194 {
37 Douglas Thrift 211 daily(matcher.size() > 1 ? matcher[1] : "");
38 Douglas Thrift 194 }
39 Douglas Thrift 211 else if (path == matcher("^/random/(\\d+)?$"))
40     {
41     random(matcher.size() > 1 ? matcher[1] : "");
42     }
43 Douglas Thrift 194 else if (path == matcher("^/(" + this->matcher + ")$"))
44     {
45     select(matcher[1], true);
46     }
47 Douglas Thrift 198 else
48     {
49 Douglas Thrift 203 api::Cout << "Location: http://" << env.get("HTTP_HOST")
50 Douglas Thrift 202 << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n";
51 Douglas Thrift 198 }
52 Douglas Thrift 194 }
53    
54 Douglas Thrift 203 ext::String FeepingCreaturism::program;
55 Douglas Thrift 201
56 Douglas Thrift 206 bool FeepingCreaturism::CaseLess::operator()(const std::string& one,
57     const std::string& two)
58     {
59     std::string one_(one), two_(two);
60    
61     // XXX: should be std::tolower except g++34 doesn't believe it
62     std::transform(one.begin(), one.end(), one_.begin(), ::tolower);
63     std::transform(two.begin(), two.end(), two_.begin(), ::tolower);
64    
65     return one_ < two_;
66     }
67    
68 Douglas Thrift 194 void FeepingCreaturism::initialize()
69     {
70 Douglas Thrift 201 ext::Handle<xml::Document> document(xml::Parse("jargon.xml"));
71     ext::Handle<xml::Node> node(*document/"feepingcreaturism");
72 Douglas Thrift 194
73 Douglas Thrift 201 this->path = *node/"jargon";
74     this->matcher = *node/"matcher";
75 Douglas Thrift 194
76     char* path[] = { new char[this->path.size()] };
77    
78     std::strcpy(path[0], this->path.c_str());
79    
80     ::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL));
81 Douglas Thrift 203 Matcher matcher("^" + this->path + "/(" + this->matcher + ")$");
82 Douglas Thrift 194
83 Douglas Thrift 196 if (traversal == NULL)
84     {
85 Douglas Thrift 203 api::Cerr << program << ": Horrible Failure!\n";
86 Douglas Thrift 196
87     std::exit(1);
88     }
89    
90 Douglas Thrift 194 for (::FTSENT* entity(::fts_read(traversal)); entity != NULL;
91     entity = ::fts_read(traversal))
92     {
93     if (entity->fts_info == FTS_F && entity->fts_path == matcher)
94     {
95     jargon.insert(matcher[1]);
96     }
97     }
98    
99     ::fts_close(traversal);
100    
101     delete [] path[0];
102     }
103    
104 Douglas Thrift 203 void FeepingCreaturism::parse()
105     {
106     std::stringstream query(env.get("QUERY_STRING"));
107    
108     if (env.get("REQUEST_METHOD") == "POST")
109     {
110     std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_"
111     "TYPE")));
112     char* content(new char[length]);
113    
114     api::Cin.Read(content, length);
115     query.write(content, length);
116    
117     delete [] content;
118     }
119    
120     if (query.str().empty()) return;
121    
122     do
123     {
124     std::string name, value;
125    
126     std::getline(query, name, '=');
127     std::getline(query, value, '&');
128    
129     cgi.insert(_P(name, value));
130     }
131     while (query.good());
132     }
133    
134 Douglas Thrift 211 void FeepingCreaturism::daily(const ext::String& date)
135 Douglas Thrift 194 {
136     std::time_t when(std::time(NULL));
137 Douglas Thrift 211 std::tm* day(std::localtime(&when));
138 Douglas Thrift 194
139 Douglas Thrift 211 day->tm_sec = 0;
140     day->tm_min = 0;
141     day->tm_hour = 0;
142 Douglas Thrift 194
143 Douglas Thrift 211 if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
144    
145     std::time_t difference(mktime(day) / 86400);
146 Douglas Thrift 203 std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
147     ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : "");
148 Douglas Thrift 194
149     select(entry);
150     }
151    
152 Douglas Thrift 211 void FeepingCreaturism::random(const ext::String& number)
153 Douglas Thrift 194 {
154     ::srandomdev();
155    
156 Douglas Thrift 203 std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
157 Douglas Thrift 211 std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() :
158     lexical_cast<std::vector<ext::String>::size_type>(number));
159 Douglas Thrift 194
160 Douglas Thrift 211 assert(random >= 0);
161     assert(random % jargon.size() < jargon.size());
162    
163     ext::String entry(jargon.size() ? jargon[random % jargon.size()] : "");
164    
165 Douglas Thrift 194 select(entry);
166     }
167    
168 Douglas Thrift 203 void FeepingCreaturism::select(const ext::String& selection, bool validate)
169 Douglas Thrift 194 {
170     if (!validate || jargon.find(selection) != jargon.end())
171     {
172 Douglas Thrift 203 api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
173 Douglas Thrift 196
174 Douglas Thrift 203 _M::iterator include(cgi.find("include"));
175 Douglas Thrift 196
176 Douglas Thrift 203 if (include != cgi.end())
177     {
178     Jargon jargon(path + "/" + selection,
179 Douglas Thrift 219 lexical_cast<bool>(ext::String(include->second)));
180 Douglas Thrift 203
181     api::Cout << jargon;
182     }
183     else
184     {
185     Jargon jargon(path + "/" + selection);
186    
187     api::Cout << jargon;
188     }
189 Douglas Thrift 194 }
190 Douglas Thrift 195 else
191     {
192 Douglas Thrift 203 api::Cout << "Status: 404\r\n"
193     << "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"
194     << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
195 Douglas Thrift 195 << "<html><head>\n<title>404 Not Found</title>\n</head><body>\n"
196     << "<h1>Not Found</h1>\n<p>The requested URL "
197     << env.get("PATH_INFO") << " was not found on this server.</p>\n"
198     << "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n";
199     }
200 Douglas Thrift 194 }

Properties

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