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 203 by Douglas Thrift, 2004-09-01T02:10:19-07:00 vs.
Revision 261 by Douglas Thrift, 2004-10-07T22:20:16-07:00

# Line 15 | Line 15 | extern "C"
15   #include <fts.h>
16   }
17  
18 < int main(int argc, char* argv[])
18 > #include <menes-api/exename.hpp>
19 > #include <menes-app/simple.hpp>
20 >
21 > int Main(const app::Options& options)
22   {
23 <        FeepingCreaturism::program = argv[0];
23 >        FeepingCreaturism::program = api::GetExecutablePath().GetName();
24  
25          FeepingCreaturism creaturism;
26  
# Line 32 | Line 35 | FeepingCreaturism::FeepingCreaturism()
35          ext::String path(env.get("PATH_INFO"));
36          Matcher matcher;
37  
38 <        if (path == "/daily/") daily(); else if (path == "/random/")
38 >        if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$"))
39 >        {
40 >                daily(matcher.size() > 1 ? matcher[1] : "");
41 >        }
42 >        else if (path == matcher("^/random/(\\d+)?$"))
43          {
44 <                random();
44 >                random(matcher.size() > 1 ? matcher[1] : "");
45          }
46          else if (path == matcher("^/(" + this->matcher + ")$"))
47          {
# Line 49 | Line 56 | FeepingCreaturism::FeepingCreaturism()
56  
57   ext::String FeepingCreaturism::program;
58  
59 + bool FeepingCreaturism::CaseLess::operator()(const std::string& one,
60 +        const std::string& two)
61 + {
62 +        std::string one_(one), two_(two);
63 +
64 +        // XXX: should be std::tolower except g++34 doesn't believe it
65 +        std::transform(one.begin(), one.end(), one_.begin(), ::tolower);
66 +        std::transform(two.begin(), two.end(), two_.begin(), ::tolower);
67 +
68 +        if (one_ == two_) return one < two;
69 +
70 +        return one_ < two_;
71 + }
72 +
73   void FeepingCreaturism::initialize()
74   {
75          ext::Handle<xml::Document> document(xml::Parse("jargon.xml"));
# Line 57 | Line 78 | void FeepingCreaturism::initialize()
78          this->path = *node/"jargon";
79          this->matcher = *node/"matcher";
80  
81 <        char* path[] = { new char[this->path.size()] };
81 >        char* path[] = { new char[this->path.GetData().GetSize()] };
82  
83 <        std::strcpy(path[0], this->path.c_str());
83 >        std::strcpy(path[0], this->path.NullTerminate());
84  
85          ::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL));
86          Matcher matcher("^" + this->path + "/(" + this->matcher + ")$");
# Line 115 | Line 136 | void FeepingCreaturism::parse()
136          while (query.good());
137   }
138  
139 < void FeepingCreaturism::daily()
139 > void FeepingCreaturism::daily(const ext::String& date)
140   {
141          std::time_t when(std::time(NULL));
142 <        std::tm* now(std::localtime(&when));
142 >        std::tm* day(std::localtime(&when));
143  
144 <        now->tm_sec = 0;
145 <        now->tm_min = 0;
146 <        now->tm_hour = 0;
144 >        day->tm_sec = 0;
145 >        day->tm_min = 0;
146 >        day->tm_hour = 0;
147  
148 <        std::time_t difference(mktime(now) / 86400);
148 >        if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day);
149 >
150 >        std::time_t difference(mktime(day) / 86400);
151          std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
152          ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : "");
153  
154          select(entry);
155   }
156  
157 < void FeepingCreaturism::random()
157 > void FeepingCreaturism::random(const ext::String& number)
158   {
159          ::srandomdev();
160  
161          std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end());
162 <        ext::String entry(jargon.size() ? jargon[::random() % jargon.size()] : "");
162 >        std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() :
163 >                lexical_cast<std::vector<ext::String>::size_type>(number));
164 >
165 >        assert(random >= 0);
166 >        assert(random % jargon.size() < jargon.size());
167 >
168 >        ext::String entry(jargon.size() ? jargon[random % jargon.size()] : "");
169  
170          select(entry);
171   }
# Line 147 | Line 176 | void FeepingCreaturism::select(const ext
176          {
177                  api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n";
178  
179 <                _M::iterator include(cgi.find("include"));
180 <
181 <                if (include != cgi.end())
182 <                {
154 <                        Jargon jargon(path + "/" + selection,
155 <                                lexical_cast<bool>(include->second));
179 >                Jargon jargon(path, selection, cgi.find("include") != cgi.end()
180 >                        && lexical_cast<bool>(ext::String(cgi.find("include")->second)),
181 >                        cgi.find("relative") != cgi.end()
182 >                        ? ext::String(cgi.find("relative")->second) : ext::String());
183  
184 <                        api::Cout << jargon;
158 <                }
159 <                else
160 <                {
161 <                        Jargon jargon(path + "/" + selection);
162 <
163 <                        api::Cout << jargon;
164 <                }
184 >                api::Cout << jargon;
185          }
186          else
187          {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines