1 |
douglas |
362 |
// Represent |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
douglas |
371 |
#include "Hexadecimal.hpp" |
8 |
douglas |
380 |
#include "DataType.hpp" |
9 |
douglas |
362 |
|
10 |
douglas |
379 |
#include <menes-api/environment.hpp> |
11 |
douglas |
370 |
#include <menes-app/simple.hpp> |
12 |
douglas |
362 |
|
13 |
douglas |
379 |
struct Environment |
14 |
|
|
{ |
15 |
|
|
ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } } |
16 |
|
|
} env; |
17 |
|
|
|
18 |
douglas |
370 |
int Main(const app::Options& options) |
19 |
douglas |
362 |
{ |
20 |
douglas |
370 |
Represent represent; |
21 |
douglas |
362 |
|
22 |
douglas |
370 |
return 0; |
23 |
douglas |
362 |
} |
24 |
|
|
|
25 |
douglas |
370 |
Represent::Represent() |
26 |
douglas |
362 |
{ |
27 |
douglas |
379 |
parse(); |
28 |
douglas |
371 |
|
29 |
douglas |
379 |
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; |
30 |
douglas |
371 |
|
31 |
douglas |
379 |
xml::TextWriter xhtml(api::Cout); |
32 |
|
|
xml::ScopeElement table(xhtml, "table"); |
33 |
douglas |
371 |
|
34 |
douglas |
380 |
headings(xhtml); |
35 |
|
|
form(xhtml); |
36 |
douglas |
371 |
|
37 |
douglas |
380 |
// |
38 |
douglas |
379 |
} |
39 |
douglas |
373 |
|
40 |
douglas |
379 |
void Represent::parse() |
41 |
|
|
{ |
42 |
|
|
ext::String query(env.get("QUERY_STRING")); |
43 |
douglas |
373 |
|
44 |
douglas |
379 |
if (env.get("REQUEST_METHOD") == "POST") |
45 |
|
|
{ |
46 |
|
|
ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH"))); |
47 |
douglas |
373 |
|
48 |
douglas |
379 |
api::Cin.ReadFully(content.Begin(), content.GetSize()); |
49 |
douglas |
373 |
|
50 |
douglas |
379 |
query = content; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
ext::Vector<ext::String> pairs(query.Split('&')); |
54 |
|
|
|
55 |
|
|
_foreach (ext::Vector<ext::String>, pair, pairs) |
56 |
|
|
{ |
57 |
|
|
ext::String::ConstIterator equal(pair->FindFirst('=')); |
58 |
|
|
ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End()); |
59 |
|
|
|
60 |
|
|
cgi.insert(std::pair<std::string, std::string>(name, value)); |
61 |
|
|
} |
62 |
douglas |
362 |
} |
63 |
douglas |
380 |
|
64 |
|
|
void Represent::headings(xml::TextWriter& xhtml) |
65 |
|
|
{ |
66 |
|
|
xml::ScopeElement tr(xhtml, "tr"); |
67 |
|
|
ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" }; |
68 |
|
|
|
69 |
|
|
_foru (index, 0, sizeof (headings) / sizeof (ext::String)) |
70 |
|
|
{ |
71 |
|
|
xml::ScopeElement th(xhtml, "th"); |
72 |
|
|
|
73 |
|
|
xhtml.OutputText(headings[index]); |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
void Represent::form(xml::TextWriter& xhtml) |
78 |
|
|
{ |
79 |
|
|
xml::ScopeElement tr(xhtml, "tr"); |
80 |
|
|
|
81 |
|
|
{ |
82 |
|
|
xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); |
83 |
|
|
|
84 |
|
|
xhtml.SetAttribute("name", "type"); |
85 |
|
|
|
86 |
|
|
DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast<unsigned>(cgi.find("type")->second)) : DataType::TYPE_bool); |
87 |
|
|
|
88 |
|
|
_foreach (ext::Vector<DataType>, type_, DataType::enumerate()) |
89 |
|
|
{ |
90 |
|
|
xml::ScopeElement option(xhtml, "option"); |
91 |
|
|
|
92 |
|
|
if (*type_ == type) xhtml.SetAttribute("selected", "selected"); |
93 |
|
|
|
94 |
|
|
xhtml.SetAttribute("value", lexical_cast<ext::String>(DataType::Type(*type_))); |
95 |
|
|
xhtml.OutputText(*type_); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
{ |
100 |
|
|
xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); |
101 |
|
|
|
102 |
|
|
xhtml.SetAttribute("name", "data"); |
103 |
|
|
xhtml.SetAttribute("size", "64"); |
104 |
|
|
xhtml.SetAttribute("type", "text"); |
105 |
|
|
|
106 |
|
|
ext::String data(cgi.find("data") != cgi.end() ? cgi.find("data")->second : std::string()); |
107 |
|
|
|
108 |
|
|
xhtml.SetAttribute("value", data); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
{ |
112 |
|
|
xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); |
113 |
|
|
|
114 |
|
|
ext::String inputs[] = { "Binary", "Hexadecimal", "std::istream", "ios::PrintReader" }; |
115 |
|
|
unsigned input(cgi.find("input") != cgi.end() ? lexical_cast<unsigned>(cgi.find("input")->second) : 0); |
116 |
|
|
|
117 |
|
|
_foru (input_, 0, sizeof (inputs) / sizeof (ext::String)) |
118 |
|
|
{ |
119 |
|
|
xml::ScopeElement option(xhtml, "option"); |
120 |
|
|
|
121 |
|
|
if (input_ == input) xhtml.SetAttribute("selected", "selected"); |
122 |
|
|
|
123 |
|
|
xhtml.SetAttribute("value", lexical_cast<ext::String>(input_)); |
124 |
|
|
xhtml.OutputText(inputs[input_]); |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); |
129 |
|
|
|
130 |
|
|
xhtml.SetAttribute("type", "submit"); |
131 |
|
|
xhtml.SetAttribute("value", "Store"); |
132 |
|
|
} |