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

Comparing Represent/Represent.cpp (file contents):
Revision 387 by douglas, 2004-12-24T03:17:21-08:00 vs.
Revision 393 by douglas, 2004-12-24T22:32:04-08:00

# Line 9 | Line 9
9   #include "InputType.hpp"
10  
11   #ifdef _WIN32
12 < #pragma warning(disable:4267)
12 > #pragma warning(disable:4267 4288)
13   #endif
14  
15   #include <menes-app/simple.hpp>
# Line 17 | Line 17
17   #include <menes-xml/nodeset.hpp>
18   #include <menes-xml/parse.hpp>
19  
20 + struct Environment
21 + {
22 +        ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } }
23 + } env;
24 +
25 + struct Item
26 + {
27 +        DataType type;
28 +        ext::String data;
29 +        InputType input;
30 +        Item(const DataType& type, const ext::String& data, const InputType& input_) : type(type), data(data), input(input_) {}
31 + };
32 +
33   int Main(const app::Options& options)
34   {
35          Represent represent;
# Line 28 | Line 41 | Represent::Represent()
41   {
42          api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush;
43  
44 +        xml::TextWriter xhtml(api::Cout);
45 +
46          parse();
47  
48          _H<xml::Document> document(xml::Parse("represent.xml"));
49          _H<xml::Node> node(*document/"represent");
50          ext::String before(*node/"before"), after(*node/"after");
51  
52 <        api::Cout << before << ios::Flush;
38 <
39 <        xml::TextWriter xhtml(api::Cout);
52 >        api::Cout << ios::NewLine << before << ios::Flush;
53  
54          {
55                  xml::ScopeElement table(xhtml, "table");
# Line 49 | Line 62 | Represent::Represent()
62          api::Cout << after << ios::Flush;
63   }
64  
52 struct Represent::Item
53 {
54        DataType type;
55        ext::String data;
56        InputType input;
57        Item(const DataType& type, const ext::String& data, const InputType& input) : type(type), data(data), input(input) {}
58 };
59
65   void Represent::parse()
66   {
67          ext::String query(env.get("QUERY_STRING"));
# Line 70 | Line 75 | void Represent::parse()
75                  query = content;
76          }
77  
73 //      api::Cout << query << ios::NewLine;
74
78          ext::Vector<ext::String> pairs(query.Split('&'));
79  
80          _foreach (ext::Vector<ext::String>, pair, pairs)
# Line 79 | Line 82 | void Represent::parse()
82                  ext::String::ConstIterator equal(pair->FindFirst('='));
83                  ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End());
84  
85 <                // XXX: clean up %20, etc.
85 >                cgi.insert(std::pair<std::string, std::string>(decode(name), decode(value)));
86 >        }
87 > }
88 >
89 > std::string Represent::decode(const ext::String& encoded)
90 > {
91 >        std::string decoded(encoded);
92 >        std::string::size_type pos(0);
93  
94 <                cgi.insert(std::pair<std::string, std::string>(name, value));
94 >        while ((pos = decoded.find_first_of("%+", pos)) != std::string::npos) switch (decoded[pos])
95 >        {
96 >        case '%':
97 >                decoded.replace(pos, 3, 1, Hexadecimal(decoded.substr(pos + 1, 2), false).convert<char>(false));
98 >                break;
99 >        case '+':
100 >                decoded[pos] = ' ';
101          }
102 +
103 +        return decoded;
104   }
105  
106   void Represent::headings(xml::TextWriter& xhtml)
# Line 168 | Line 186 | void Represent::output(xml::TextWriter&
186          MultiMapConstIterator type(cgi.lower_bound("type")), type_(cgi.upper_bound("type")), data(cgi.lower_bound("data")), data_(cgi.upper_bound("data")), input(cgi.lower_bound("input")), input_(cgi.upper_bound("input"));
187          ext::Vector<Item> items;
188  
189 <        _foru (index, 0, *count.begin())
189 >        _foru (index, 0, *count.begin() < 128 ? *count.begin() : 128)
190          {
191                  Item item(DataType(type != type_ ? type->second : std::string()), data != data_ ? data->second : std::string(), InputType(input != input_ ? input->second : std::string()));
192  
# Line 312 | Line 330 | void Represent::output(xml::TextWriter&
330   template <typename Type>
331   Type Represent::input(const Item& item)
332   {
333 <        // XXX: implement
316 <        
317 <        return lexical_cast<Type>(item.data);
318 < }
319 <
320 < template <>
321 < bool Represent::input(const Item& item)
322 < {
323 <        // XXX: implement
333 >        bool signed_(etl::Limits<Type>::IsSigned);
334  
335 <        try { return lexical_cast<bool>(item.data); } catch (ext::Exception) { return false; }
335 >        switch (item.input)
336 >        {
337 >        default:
338 >                try { return lexical_cast<Type>(item.data); } catch (ext::Exception) { return 0; }
339 >        case InputType::INPUT_Binary:
340 >                return Binary(item.data, signed_).convert<Type>(signed_);
341 >        case InputType::INPUT_Hexadecimal:
342 >                return Hexadecimal(item.data, signed_).convert<Type>(signed_);
343 >        }
344   }
345  
346   template <>
347   char Represent::input(const Item& item)
348   {
349 <        // XXX: implement
350 <        
351 <        return item.data.First();
349 >        switch (item.input)
350 >        {
351 >        default:
352 >                return item.data.First();
353 >        case InputType::INPUT_Binary:
354 >                return Binary(item.data, false).convert<char>(false);
355 >        case InputType::INPUT_Hexadecimal:
356 >                return Hexadecimal(item.data, false).convert<char>(false);
357 >        }
358   }
359  
360 < // XXX: constructing a string from Binary or Hexadecimal seems to dangerous
360 > // XXX: constructing a string from Binary or Hexadecimal seems too dangerous
361   template <>
362   std::string Represent::input(const Item& item)
363   {
364          return item.data;
365   }
366  
367 < // XXX: constructing a string from Binary or Hexadecimal seems to dangerous
367 > // XXX: constructing a string from Binary or Hexadecimal seems too dangerous
368   template <>
369   ext::String Represent::input(const Item& item)
370   {
# Line 353 | Line 377 | void Represent::normal(xml::TextWriter&
377          xhtml.OutputText(lexical_cast<ext::String>(type));
378   }
379  
380 + // XXX: damn, this one is really quite screwy
381   template <>
382   void Represent::normal(xml::TextWriter& xhtml, const char& char_)
383   {
384 <        xhtml.OutputText(ext::CodePoint(char_));
384 >        xhtml.OutputText("'");
385 >        xhtml.OutputText(std::string(1, char_).c_str());
386 >        xhtml.OutputText("'");
387   }
388  
389   template <>
390   void Represent::normal(xml::TextWriter& xhtml, const std::string& string)
391   {
392 +        xhtml.OutputText("\"");
393          xhtml.OutputText(string);
394 +        xhtml.OutputText("\"");
395   }
396  
397   template <>
398   void Represent::normal(xml::TextWriter& xhtml, const ext::String& string)
399   {
400 +        xhtml.OutputText("\"");
401          xhtml.OutputText(string);
402 +        xhtml.OutputText("\"");
403   }
404  
405   template <typename Type>
# Line 389 | Line 420 | void Represent::binary(xml::TextWriter&
420                  xml::ScopeElement(xhtml, "br");
421  
422                  xhtml.OutputText(Binary(*atom));
423 +                xhtml.OutputText(" = '");
424 +                xhtml.OutputText(ext::CodePoint(*atom));
425 +                xhtml.OutputText("'");
426          }
427   }
428  
# Line 404 | Line 438 | void Represent::binary(xml::TextWriter&
438                  xml::ScopeElement(xhtml, "br");
439  
440                  xhtml.OutputText(Binary(*atom));
441 +                xhtml.OutputText(" = '");
442 +                xhtml.OutputText(ext::CodePoint(*atom));
443 +                xhtml.OutputText("'");
444          }
445   }
446  
# Line 424 | Line 461 | void Represent::hexadecimal(xml::TextWri
461          {
462                  xml::ScopeElement(xhtml, "br");
463  
464 <                xhtml.OutputText(Hexadecimal(string));
464 >                xhtml.OutputText(Hexadecimal(*atom));
465 >                xhtml.OutputText(" = '");
466 >                xhtml.OutputText(ext::CodePoint(*atom));
467 >                xhtml.OutputText("'");
468          }
469   }
470  
# Line 439 | Line 479 | void Represent::hexadecimal(xml::TextWri
479          {
480                  xml::ScopeElement(xhtml, "br");
481  
482 <                xhtml.OutputText(Hexadecimal(string));
482 >                xhtml.OutputText(Hexadecimal(*atom));
483 >                xhtml.OutputText(" = '");
484 >                xhtml.OutputText(ext::CodePoint(*atom));
485 >                xhtml.OutputText("'");
486          }
487   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines