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 388 by douglas, 2004-12-24T03:31:12-08:00 vs.
Revision 400 by douglas, 2004-12-27T19:19:23-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 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 <                cgi.insert(std::pair<std::string, std::string>(name, value));
89 > std::string Represent::decode(const ext::String& encoded)
90 > {
91 >        std::string decoded(encoded);
92 >        std::string::size_type pos(0);
93 >
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 181 | Line 199 | void Represent::output(xml::TextWriter&
199  
200          _rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast<size_t>(delete_->second));
201  
202 +        if (!items.IsEmpty() && items.First().data.IsEmpty()) items.RemoveFirst();
203 +
204          size_t index(0);
205  
206          _foreach (ext::Vector<Item>, item, items) switch (item->type)
# Line 312 | Line 332 | void Represent::output(xml::TextWriter&
332   template <typename Type>
333   Type Represent::input(const Item& item)
334   {
335 <        // 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
335 >        bool signed_(etl::Limits<Type>::IsSigned);
336  
337 <        try { return lexical_cast<bool>(item.data); } catch (ext::Exception) { return false; }
337 >        switch (item.input)
338 >        {
339 >        default:
340 >                try { return lexical_cast<Type>(item.data); } catch (ext::Exception) { return 0; }
341 >        case InputType::INPUT_Binary:
342 >                return Binary(item.data, signed_).convert<Type>(signed_);
343 >        case InputType::INPUT_Hexadecimal:
344 >                return Hexadecimal(item.data, signed_).convert<Type>(signed_);
345 >        }
346   }
347  
348   template <>
349   char Represent::input(const Item& item)
350   {
351 <        // XXX: implement
352 <        
353 <        return item.data.First();
351 >        switch (item.input)
352 >        {
353 >        default:
354 >                return item.data.First();
355 >        case InputType::INPUT_Binary:
356 >                return Binary(item.data, false).convert<char>(false);
357 >        case InputType::INPUT_Hexadecimal:
358 >                return Hexadecimal(item.data, false).convert<char>(false);
359 >        }
360   }
361  
336 // XXX: constructing a string from Binary or Hexadecimal seems to dangerous
362   template <>
363   std::string Represent::input(const Item& item)
364   {
365 <        return item.data;
365 >        std::string string;
366 >
367 >        switch (item.input)
368 >        {
369 >        default:
370 >                return item.data;
371 >        case InputType::INPUT_Binary:
372 >                _foreach (_L<ext::String>, atom, item.data.Split(' ')) string += Binary(*atom, false).convert<char>(false);
373 >                break;
374 >        case InputType::INPUT_Hexadecimal:
375 >                _foreach (_L<ext::String>, atom, item.data.Split(' ')) string += Hexadecimal(*atom, false).convert<char>(false);
376 >        }
377 >
378 >        return string;
379   }
380  
343 // XXX: constructing a string from Binary or Hexadecimal seems to dangerous
381   template <>
382   ext::String Represent::input(const Item& item)
383   {
384 <        return item.data;
384 >        ext::String string;
385 >
386 >        switch (item.input)
387 >        {
388 >        default:
389 >                return item.data;
390 >        case InputType::INPUT_Binary:
391 >                _foreach (_L<ext::String>, atom, item.data.Split(' ')) string.InsertLast(Binary(*atom, false).convert<uint32_t>(false));
392 >                break;
393 >        case InputType::INPUT_Hexadecimal:
394 >                _foreach (_L<ext::String>, atom, item.data.Split(' ')) string.InsertLast(Hexadecimal(*atom, false).convert<uint32_t>(false));
395 >        }
396 >
397 >        return string;
398   }
399  
400   template <typename Type>
# Line 356 | Line 406 | void Represent::normal(xml::TextWriter&
406   template <>
407   void Represent::normal(xml::TextWriter& xhtml, const char& char_)
408   {
409 <        xhtml.OutputText(ext::CodePoint(char_));
409 >        xhtml.OutputText("'");
410 >        xhtml.OutputText(std::string(1, char_).c_str());
411 >        xhtml.OutputText("'");
412   }
413  
414   template <>
415   void Represent::normal(xml::TextWriter& xhtml, const std::string& string)
416   {
417 +        xhtml.OutputText("\"");
418          xhtml.OutputText(string);
419 +        xhtml.OutputText("\"");
420   }
421  
422   template <>
423   void Represent::normal(xml::TextWriter& xhtml, const ext::String& string)
424   {
425 +        xhtml.OutputText("\"");
426          xhtml.OutputText(string);
427 +        xhtml.OutputText("\"");
428   }
429  
430   template <typename Type>
# Line 389 | Line 445 | void Represent::binary(xml::TextWriter&
445                  xml::ScopeElement(xhtml, "br");
446  
447                  xhtml.OutputText(Binary(*atom));
448 +                xhtml.OutputText(" = '");
449 +                xhtml.OutputText(ext::CodePoint(*atom));
450 +                xhtml.OutputText("'");
451          }
452   }
453  
# Line 404 | Line 463 | void Represent::binary(xml::TextWriter&
463                  xml::ScopeElement(xhtml, "br");
464  
465                  xhtml.OutputText(Binary(*atom));
466 +                xhtml.OutputText(" = '");
467 +                xhtml.OutputText(ext::CodePoint(*atom));
468 +                xhtml.OutputText("'");
469          }
470   }
471  
# Line 424 | Line 486 | void Represent::hexadecimal(xml::TextWri
486          {
487                  xml::ScopeElement(xhtml, "br");
488  
489 <                xhtml.OutputText(Hexadecimal(string));
489 >                xhtml.OutputText(Hexadecimal(*atom));
490 >                xhtml.OutputText(" = '");
491 >                xhtml.OutputText(ext::CodePoint(*atom));
492 >                xhtml.OutputText("'");
493          }
494   }
495  
# Line 439 | Line 504 | void Represent::hexadecimal(xml::TextWri
504          {
505                  xml::ScopeElement(xhtml, "br");
506  
507 <                xhtml.OutputText(Hexadecimal(string));
507 >                xhtml.OutputText(Hexadecimal(*atom));
508 >                xhtml.OutputText(" = '");
509 >                xhtml.OutputText(ext::CodePoint(*atom));
510 >                xhtml.OutputText("'");
511          }
512   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines