ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
Revision: 196
Committed: 2004-08-27T19:31:26-07:00 (20 years, 9 months ago) by Douglas Thrift
File size: 2687 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 int main(int argc, char* argv[])
19 {
20 FeepingCreaturism creaturism;
21
22 return 0;
23 }
24
25 FeepingCreaturism::FeepingCreaturism()
26 {
27 initialize();
28
29 std::string path(env.get("PATH_INFO"));
30 Matcher matcher;
31
32 if (path.empty()) daily(); else if (path == "/random")
33 {
34 random();
35 }
36 else if (path == matcher("^/(" + this->matcher + ")$"))
37 {
38 select(matcher[1], true);
39 }
40 else daily();
41 }
42
43 void FeepingCreaturism::initialize()
44 {
45 ext::Handle<xml::Document> document(xml::Parse("FeepingCreaturism.xml"));
46 ext::Handle<xml::Node> node(*document/"FeepingCreaturism");
47
48 this->path = *node/"Jargon";
49 this->matcher = *node/"Matcher";
50
51 char* path[] = { new char[this->path.size()] };
52
53 std::strcpy(path[0], this->path.c_str());
54
55 ::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL));
56 Matcher matcher('^' + this->path + "/(" + this->matcher + ")$");
57
58 if (traversal == NULL)
59 {
60 cerr << "FeepingCreaturism: Horrible Failure!\n";
61
62 std::exit(1);
63 }
64
65 for (::FTSENT* entity(::fts_read(traversal)); entity != NULL;
66 entity = ::fts_read(traversal))
67 {
68 if (entity->fts_info == FTS_F && entity->fts_path == matcher)
69 {
70 jargon.insert(matcher[1]);
71 }
72 }
73
74 ::fts_close(traversal);
75
76 delete [] path[0];
77 }
78
79 void FeepingCreaturism::daily()
80 {
81 std::time_t when(std::time(NULL));
82 std::tm* now(std::localtime(&when));
83
84 now->tm_sec = 0;
85 now->tm_min = 0;
86 now->tm_hour = 0;
87
88 std::time_t difference(mktime(now) / 86400);
89 std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end());
90 std::string entry(jargon.size() ? jargon[difference % jargon.size()] : "");
91
92 select(entry);
93 }
94
95 void FeepingCreaturism::random()
96 {
97 ::srandomdev();
98
99 std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end());
100 std::string entry(jargon.size() ? jargon[::random() % jargon.size()] : "");
101
102 select(entry);
103 }
104
105 void FeepingCreaturism::select(const std::string& selection, bool validate)
106 {
107 if (!validate || jargon.find(selection) != jargon.end())
108 {
109 cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
110
111 Jargon jargon(path + '/' + selection);
112
113 cout << jargon;
114 }
115 else
116 {
117 cout << "Status: 404\r\nContent-Type: text/html; charset=ISO-8859-1\r\n"
118 << "\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
119 << "<html><head>\n<title>404 Not Found</title>\n</head><body>\n"
120 << "<h1>Not Found</h1>\n<p>The requested URL "
121 << env.get("PATH_INFO") << " was not found on this server.</p>\n"
122 << "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n";
123 }
124 }

Properties

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