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

# 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 int main(int argc, char* argv[])
19 {
20 FeepingCreaturism::program = argv[0];
21
22 FeepingCreaturism creaturism;
23
24 return 0;
25 }
26
27 FeepingCreaturism::FeepingCreaturism()
28 {
29 initialize();
30
31 std::string path(env.get("PATH_INFO"));
32 Matcher matcher;
33
34 if (path == "/daily/") daily(); else if (path == "/random/")
35 {
36 random();
37 }
38 else if (path == matcher("^/(" + this->matcher + ")$"))
39 {
40 select(matcher[1], true);
41 }
42 else
43 {
44 cout << "Status: 301\r\n";
45 }
46 }
47
48 std::string FeepingCreaturism::program;
49
50 void FeepingCreaturism::initialize()
51 {
52 ext::Handle<xml::Document> document(xml::Parse("jargon.xml"));
53 ext::Handle<xml::Node> node(*document/"feepingcreaturism");
54
55 this->path = *node/"jargon";
56 this->matcher = *node/"matcher";
57
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 if (traversal == NULL)
66 {
67 cerr << program << ": Horrible Failure!\n";
68
69 std::exit(1);
70 }
71
72 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 cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
117
118 Jargon jargon(path + '/' + selection);
119
120 cout << jargon;
121 }
122 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 }

Properties

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