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 201 by Douglas Thrift, 2004-08-30T19:24:40-07:00 vs.
Revision 255 by Douglas Thrift, 2004-09-15T21:38:10-07:00

# Line 27 | Line 27 | int main(int argc, char* argv[])
27   FeepingCreaturism::FeepingCreaturism()
28   {
29          initialize();
30 +        parse();
31  
32 <        std::string path(env.get("PATH_INFO"));
32 >        ext::String path(env.get("PATH_INFO"));
33          Matcher matcher;
34  
35 <        if (path == "/daily/") daily(); else if (path == "/random/")
35 >        if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))
36          {
37 <                random();
37 >                daily(matcher.size() > 1 ? matcher[1] : "");
38 >        }
39 >        else if (path == matcher("^/random/(\\d+)?$"))
40 >        {
41 >                random(matcher.size() > 1 ? matcher[1] : "");
42          }
43          else if (path == matcher("^/(" + this->matcher + ")$"))
44          {
# Line 41 | Line 46 | FeepingCreaturism::FeepingCreaturism()
46          }
47          else
48          {
49 <                cout << "Status: 301\r\n";
49 >                api::Cout << "Location: http://" << env.get("HTTP_HOST")
50 >                        << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n";
51          }
52   }
53  
54 < std::string FeepingCreaturism::program;
54 > ext::String FeepingCreaturism::program;
55 >
56 > bool FeepingCreaturism::CaseLess::operator()(const std::string& one,
57 >        const std::string& two)
58 > {
59 >        std::string one_(one), two_(two);
60 >
61 >        // XXX: should be std::tolower except g++34 doesn't believe it
62 >        std::transform(one.begin(), one.end(), one_.begin(), ::tolower);
63 >        std::transform(two.begin(), two.end(), two_.begin(), ::tolower);
64 >
65 >        if (one_ == two_) return one < two;
66 >
67 >        return one_ < two_;
68 > }
69  
70   void FeepingCreaturism::initialize()
71   {
# Line 55 | Line 75 | void FeepingCreaturism::initialize()
75          this->path = *node/"jargon";
76          this->matcher = *node/"matcher";
77  
78 <        char* path[] = { new char[this->path.size()] };
78 >        char* path[] = { new char[this->path.GetData().GetSize()] };
79  
80 <        std::strcpy(path[0], this->path.c_str());
80 >        std::strcpy(path[0], this->path.NullTerminate());
81  
82          ::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL));
83 <        Matcher matcher('^' + this->path + "/(" + this->matcher + ")$");
83 >        Matcher matcher("^" + this->path + "/(" + this->matcher + ")$");
84  
85          if (traversal == NULL)
86          {
87 <                cerr << program << ": Horrible Failure!\n";
87 >                api::Cerr << program << ": Horrible Failure!\n";
88  
89                  std::exit(1);
90          }
# Line 83 | Line 103 | void FeepingCreaturism::initialize()
103          delete [] path[0];
104   }
105  
106 < void FeepingCreaturism::daily()
106 > void FeepingCreaturism::parse()
107 > {
108 >        std::stringstream query(env.get("QUERY_STRING"));
109 >
110 >        if (env.get("REQUEST_METHOD") == "POST")
111 >        {
112 >                std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_"
113 >                        "TYPE")));
114 >                char* content(new char[length]);
115 >
116 >                api::Cin.Read(content, length);
117 >                query.write(content, length);
118 >
119 >                delete [] content;
120 >        }
121 >
122 >        if (query.str().empty()) return;
123 >
124 >        do
125 >        {
126 >                std::string name, value;
127 >
128 >                std::getline(query, name, '=');
129 >                std::getline(query, value, '&');
130 >
131 >                cgi.insert(_P(name, value));
132 >        }
133 >        while (query.good());
134 > }
135 >
136 > void FeepingCreaturism::daily(const ext::String& date)
137   {
138          std::time_t when(std::time(NULL));
139 <        std::tm* now(std::localtime(&when));
139 >        std::tm* day(std::localtime(&when));
140 >
141 >        day->tm_sec = 0;
142 >        day->tm_min = 0;
143 >        day->tm_hour = 0;
144  
145 <        now->tm_sec = 0;
146 <        now->tm_min = 0;
147 <        now->tm_hour = 0;
148 <
149 <        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()] : "");
145 >        if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
146 >
147 >        std::time_t difference(mktime(day) / 86400);
148 >        std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
149 >        ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : "");
150  
151          select(entry);
152   }
153  
154 < void FeepingCreaturism::random()
154 > void FeepingCreaturism::random(const ext::String& number)
155   {
156          ::srandomdev();
157  
158 <        std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end());
159 <        std::string entry(jargon.size() ? jargon[::random() % jargon.size()] : "");
158 >        std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
159 >        std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() :
160 >                lexical_cast<std::vector<ext::String>::size_type>(number));
161 >
162 >        assert(random >= 0);
163 >        assert(random % jargon.size() < jargon.size());
164 >
165 >        ext::String entry(jargon.size() ? jargon[random % jargon.size()] : "");
166  
167          select(entry);
168   }
169  
170 < void FeepingCreaturism::select(const std::string& selection, bool validate)
170 > void FeepingCreaturism::select(const ext::String& selection, bool validate)
171   {
172          if (!validate || jargon.find(selection) != jargon.end())
173          {
174 <                cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
174 >                api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
175  
176 <                Jargon jargon(path + '/' + selection);
176 >                Jargon jargon(path, selection, cgi.find("include") != cgi.end()
177 >                        && lexical_cast<bool>(ext::String(cgi.find("include")->second)),
178 >                        cgi.find("relative") != cgi.end()
179 >                        ? ext::String(cgi.find("relative")->second) : ext::String());
180  
181 <                cout << jargon;
181 >                api::Cout << jargon;
182          }
183          else
184          {
185 <                cout << "Status: 404\r\nContent-Type: text/html; charset=ISO-8859-1\r\n"
186 <                        << "\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
185 >                api::Cout << "Status: 404\r\n"
186 >                        << "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"
187 >                        << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
188                          << "<html><head>\n<title>404 Not Found</title>\n</head><body>\n"
189                          << "<h1>Not Found</h1>\n<p>The requested URL "
190                          << env.get("PATH_INFO") << " was not found on this server.</p>\n"

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines