ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FeepingCreaturism/FeepingCreaturism.cpp
(Generate patch)

Comparing FeepingCreaturism/FeepingCreaturism.cpp (file contents):
Revision 191 by Douglas Thrift, 2004-08-20T02:44:20-07:00 vs.
Revision 198 by Douglas Thrift, 2004-08-30T18:09:53-07:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines