ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 201
Committed: 2004-08-30T19:24:40-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 2783 byte(s)
Log Message:
Huzzah!

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

Properties

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