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 196 by Douglas Thrift, 2004-08-27T19:31:26-07:00 vs.
Revision 352 by douglas, 2004-12-17T17:50:49-08:00

# Line 5 | Line 5
5   // $Id$
6  
7   #include "Environment.hpp"
8 < #include "Matcher.hpp"
8 > #include "Matcher/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 < }
11 > #include <menes-api/exename.hpp>
12 > #include <menes-app/simple.hpp>
13  
14 < int main(int argc, char* argv[])
14 > int Main(const app::Options& options)
15   {
16 +        FeepingCreaturism::program = api::GetExecutablePath().GetName();
17 +
18          FeepingCreaturism creaturism;
19  
20          return 0;
# Line 25 | Line 23 | int main(int argc, char* argv[])
23   FeepingCreaturism::FeepingCreaturism()
24   {
25          initialize();
26 +        parse();
27  
28 <        std::string path(env.get("PATH_INFO"));
28 >        ext::String path(env.get("PATH_INFO"));
29          Matcher matcher;
30  
31 <        if (path.empty()) daily(); else if (path == "/random")
31 >        if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))
32 >        {
33 >                daily(matcher.size() > 1 ? matcher[1] : "");
34 >        }
35 >        else if (path == matcher("^/random/(\\d+)?$"))
36          {
37 <                random();
37 >                random(matcher.size() > 1 ? matcher[1] : "");
38          }
39          else if (path == matcher("^/(" + this->matcher + ")$"))
40          {
41                  select(matcher[1], true);
42          }
43 <        else daily();
43 >        else
44 >        {
45 >                api::Cout << "Location: http://" << env.get("HTTP_HOST") << env.get("SCRIPT_NAME") << "/daily/\r\n\r\n";
46 >        }
47 > }
48 >
49 > ext::String FeepingCreaturism::program;
50 >
51 > bool FeepingCreaturism::CaseLess::operator()(const std::string& one,
52 >        const std::string& two)
53 > {
54 >        std::string one_(one), two_(two);
55 >
56 >        // XXX: should be std::tolower except g++34 doesn't believe it
57 >        std::transform(one.begin(), one.end(), one_.begin(), ::tolower);
58 >        std::transform(two.begin(), two.end(), two_.begin(), ::tolower);
59 >
60 >        if (one_ == two_) return one < two;
61 >
62 >        return one_ < two_;
63   }
64  
65   void FeepingCreaturism::initialize()
66   {
67 <        ext::Handle<xml::Document> document(xml::Parse("FeepingCreaturism.xml"));
68 <        ext::Handle<xml::Node> node(*document/"FeepingCreaturism");
67 >        _H<xml::Document> document(xml::Parse("jargon.xml"));
68 >        _H<xml::Node> node(*document/"feepingcreaturism");
69  
70 <        this->path = *node/"Jargon";
71 <        this->matcher = *node/"Matcher";
70 >        this->path = *node/"jargon";
71 >        this->matcher = *node/"matcher";
72  
73 <        char* path[] = { new char[this->path.size()] };
73 >        _L<ext::String> args(1, this->path);
74  
75 <        std::strcpy(path[0], this->path.c_str());
75 >        args.InsertLast("-type");
76 >        args.InsertLast("f");
77  
78 <        ::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL));
79 <        Matcher matcher('^' + this->path + "/(" + this->matcher + ")$");
78 >        _S<api::Process> find("/usr/bin/find", args);
79 >        Matcher matcher("^" + this->path + "/(" + this->matcher + ")$");
80 >        ext::String path;
81  
82 <        if (traversal == NULL)
82 >        while (ios::ReadLine(*find.GetReader(), path)) if (matcher == path)
83          {
84 <                cerr << "FeepingCreaturism: Horrible Failure!\n";
61 <
62 <                std::exit(1);
84 >                jargon.insert(matcher[1]);
85          }
86 + }
87 +
88 + void FeepingCreaturism::parse()
89 + {
90 +        std::stringstream query(env.get("QUERY_STRING"));
91  
92 <        for (::FTSENT* entity(::fts_read(traversal)); entity != NULL;
66 <                entity = ::fts_read(traversal))
92 >        if (env.get("REQUEST_METHOD") == "POST")
93          {
94 <                if (entity->fts_info == FTS_F && entity->fts_path == matcher)
95 <                {
96 <                        jargon.insert(matcher[1]);
97 <                }
94 >                std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_TYPE")));
95 >                char* content(new char[length]);
96 >
97 >                api::Cin.Read(content, length);
98 >                query.write(content, length);
99 >
100 >                delete [] content;
101          }
102  
103 <        ::fts_close(traversal);
103 >        if (query.str().empty()) return;
104 >
105 >        do
106 >        {
107 >                std::string name, value;
108 >
109 >                std::getline(query, name, '=');
110 >                std::getline(query, value, '&');
111  
112 <        delete [] path[0];
112 >                cgi.insert(_P(name, value));
113 >        }
114 >        while (query.good());
115   }
116  
117 < void FeepingCreaturism::daily()
117 > void FeepingCreaturism::daily(const ext::String& date)
118   {
119          std::time_t when(std::time(NULL));
120 <        std::tm* now(std::localtime(&when));
120 >        std::tm* day(std::localtime(&when));
121 >
122 >        day->tm_sec = 0;
123 >        day->tm_min = 0;
124 >        day->tm_hour = 0;
125  
126 <        now->tm_sec = 0;
127 <        now->tm_min = 0;
128 <        now->tm_hour = 0;
129 <
130 <        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()] : "");
126 >        if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
127 >
128 >        std::time_t difference(mktime(day) / 86400);
129 >        ext::Vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
130 >        ext::String entry(jargon.GetSize() ? jargon[difference % jargon.GetSize()] : "");
131  
132          select(entry);
133   }
134  
135 < void FeepingCreaturism::random()
135 > void FeepingCreaturism::random(const ext::String& number)
136   {
137          ::srandomdev();
138  
139 <        std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end());
140 <        std::string entry(jargon.size() ? jargon[::random() % jargon.size()] : "");
139 >        ext::Vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
140 >        size_t random(number.IsEmpty() ? ::random() : lexical_cast<size_t>(number));
141 >
142 >        assert(random >= 0);
143 >        assert(random % jargon.GetSize() < jargon.GetSize());
144 >
145 >        ext::String entry(jargon.GetSize() ? jargon[random % jargon.GetSize()] : "");
146  
147          select(entry);
148   }
149  
150 < void FeepingCreaturism::select(const std::string& selection, bool validate)
150 > void FeepingCreaturism::select(const ext::String& selection, bool validate)
151   {
152          if (!validate || jargon.find(selection) != jargon.end())
153          {
154 <                cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
154 >                api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
155  
156 <                Jargon jargon(path + '/' + selection);
156 >                Jargon jargon(path, selection, cgi.find("include") != cgi.end() && lexical_cast<bool>(ext::String(cgi.find("include")->second)), cgi.find("relative") != cgi.end() ? ext::String(cgi.find("relative")->second) : ext::String());
157  
158 <                cout << jargon;
158 >                api::Cout << jargon;
159          }
160          else
161          {
162 <                cout << "Status: 404\r\nContent-Type: text/html; charset=ISO-8859-1\r\n"
163 <                        << "\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
162 >                api::Cout << "Status: 404\r\n"
163 >                        << "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n"
164 >                        << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
165                          << "<html><head>\n<title>404 Not Found</title>\n</head><body>\n"
166                          << "<h1>Not Found</h1>\n<p>The requested URL "
167                          << env.get("PATH_INFO") << " was not found on this server.</p>\n"

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines