ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 202
Committed: 2004-08-30T23:18:58-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 2874 byte(s)
Log Message:
Yeah!

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    
31     std::string path(env.get("PATH_INFO"));
32     Matcher matcher;
33    
34 Douglas Thrift 198 if (path == "/daily/") daily(); else if (path == "/random/")
35 Douglas Thrift 194 {
36     random();
37     }
38     else if (path == matcher("^/(" + this->matcher + ")$"))
39     {
40     select(matcher[1], true);
41     }
42 Douglas Thrift 198 else
43     {
44 Douglas Thrift 202 cout << "Status: 301\r\nLocation: http://" << env.get("HTTP_HOST")
45     << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n";
46 Douglas Thrift 198 }
47 Douglas Thrift 194 }
48    
49 Douglas Thrift 201 std::string FeepingCreaturism::program;
50    
51 Douglas Thrift 194 void FeepingCreaturism::initialize()
52     {
53 Douglas Thrift 201 ext::Handle<xml::Document> document(xml::Parse("jargon.xml"));
54     ext::Handle<xml::Node> node(*document/"feepingcreaturism");
55 Douglas Thrift 194
56 Douglas Thrift 201 this->path = *node/"jargon";
57     this->matcher = *node/"matcher";
58 Douglas Thrift 194
59     char* path[] = { new char[this->path.size()] };
60    
61     std::strcpy(path[0], this->path.c_str());
62    
63     ::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL));
64     Matcher matcher('^' + this->path + "/(" + this->matcher + ")$");
65    
66 Douglas Thrift 196 if (traversal == NULL)
67     {
68 Douglas Thrift 201 cerr << program << ": Horrible Failure!\n";
69 Douglas Thrift 196
70     std::exit(1);
71     }
72    
73 Douglas Thrift 194 for (::FTSENT* entity(::fts_read(traversal)); entity != NULL;
74     entity = ::fts_read(traversal))
75     {
76     if (entity->fts_info == FTS_F && entity->fts_path == matcher)
77     {
78     jargon.insert(matcher[1]);
79     }
80     }
81    
82     ::fts_close(traversal);
83    
84     delete [] path[0];
85     }
86    
87     void FeepingCreaturism::daily()
88     {
89     std::time_t when(std::time(NULL));
90     std::tm* now(std::localtime(&when));
91    
92     now->tm_sec = 0;
93     now->tm_min = 0;
94     now->tm_hour = 0;
95    
96     std::time_t difference(mktime(now) / 86400);
97     std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end());
98     std::string entry(jargon.size() ? jargon[difference % jargon.size()] : "");
99    
100     select(entry);
101     }
102    
103     void FeepingCreaturism::random()
104     {
105     ::srandomdev();
106    
107     std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end());
108     std::string entry(jargon.size() ? jargon[::random() % jargon.size()] : "");
109    
110     select(entry);
111     }
112    
113     void FeepingCreaturism::select(const std::string& selection, bool validate)
114     {
115     if (!validate || jargon.find(selection) != jargon.end())
116     {
117 Douglas Thrift 196 cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
118    
119     Jargon jargon(path + '/' + selection);
120    
121     cout << jargon;
122 Douglas Thrift 194 }
123 Douglas Thrift 195 else
124     {
125     cout << "Status: 404\r\nContent-Type: text/html; charset=ISO-8859-1\r\n"
126     << "\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
127     << "<html><head>\n<title>404 Not Found</title>\n</head><body>\n"
128     << "<h1>Not Found</h1>\n<p>The requested URL "
129     << env.get("PATH_INFO") << " was not found on this server.</p>\n"
130     << "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n";
131     }
132 Douglas Thrift 194 }

Properties

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