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 |
370 |
#include <menes-app/simple.hpp> |
11 |
douglas |
362 |
|
12 |
douglas |
370 |
int Main(const app::Options& options) |
13 |
douglas |
362 |
{ |
14 |
douglas |
370 |
Represent represent; |
15 |
douglas |
362 |
|
16 |
douglas |
370 |
return 0; |
17 |
douglas |
362 |
} |
18 |
|
|
|
19 |
douglas |
370 |
Represent::Represent() |
20 |
douglas |
362 |
{ |
21 |
douglas |
379 |
parse(); |
22 |
douglas |
371 |
|
23 |
douglas |
379 |
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; |
24 |
douglas |
371 |
|
25 |
douglas |
379 |
xml::TextWriter xhtml(api::Cout); |
26 |
|
|
xml::ScopeElement table(xhtml, "table"); |
27 |
douglas |
371 |
|
28 |
douglas |
380 |
headings(xhtml); |
29 |
|
|
form(xhtml); |
30 |
douglas |
385 |
output(xhtml); |
31 |
douglas |
379 |
} |
32 |
douglas |
373 |
|
33 |
douglas |
385 |
struct Represent::Item |
34 |
|
|
{ |
35 |
|
|
DataType type; |
36 |
|
|
ext::String data; |
37 |
|
|
Input input; |
38 |
|
|
Item(const DataType& type, const ext::String& data, Input input) : type(type), data(data), input(input) {} |
39 |
|
|
}; |
40 |
|
|
|
41 |
douglas |
379 |
void Represent::parse() |
42 |
|
|
{ |
43 |
|
|
ext::String query(env.get("QUERY_STRING")); |
44 |
douglas |
373 |
|
45 |
douglas |
379 |
if (env.get("REQUEST_METHOD") == "POST") |
46 |
|
|
{ |
47 |
|
|
ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH"))); |
48 |
douglas |
373 |
|
49 |
douglas |
379 |
api::Cin.ReadFully(content.Begin(), content.GetSize()); |
50 |
douglas |
373 |
|
51 |
douglas |
379 |
query = content; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
ext::Vector<ext::String> pairs(query.Split('&')); |
55 |
|
|
|
56 |
|
|
_foreach (ext::Vector<ext::String>, pair, pairs) |
57 |
|
|
{ |
58 |
|
|
ext::String::ConstIterator equal(pair->FindFirst('=')); |
59 |
|
|
ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End()); |
60 |
|
|
|
61 |
|
|
cgi.insert(std::pair<std::string, std::string>(name, value)); |
62 |
|
|
} |
63 |
douglas |
362 |
} |
64 |
douglas |
380 |
|
65 |
|
|
void Represent::headings(xml::TextWriter& xhtml) |
66 |
|
|
{ |
67 |
|
|
xml::ScopeElement tr(xhtml, "tr"); |
68 |
|
|
ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" }; |
69 |
|
|
|
70 |
|
|
_foru (index, 0, sizeof (headings) / sizeof (ext::String)) |
71 |
|
|
{ |
72 |
|
|
xml::ScopeElement th(xhtml, "th"); |
73 |
|
|
|
74 |
|
|
xhtml.OutputText(headings[index]); |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
void Represent::form(xml::TextWriter& xhtml) |
79 |
|
|
{ |
80 |
|
|
xml::ScopeElement tr(xhtml, "tr"); |
81 |
|
|
|
82 |
|
|
{ |
83 |
|
|
xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); |
84 |
|
|
|
85 |
|
|
xhtml.SetAttribute("name", "type"); |
86 |
|
|
|
87 |
|
|
DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast<unsigned>(cgi.find("type")->second)) : DataType::TYPE_bool); |
88 |
|
|
|
89 |
|
|
_foreach (ext::Vector<DataType>, type_, DataType::enumerate()) |
90 |
|
|
{ |
91 |
|
|
xml::ScopeElement option(xhtml, "option"); |
92 |
|
|
|
93 |
|
|
if (*type_ == type) xhtml.SetAttribute("selected", "selected"); |
94 |
|
|
|
95 |
|
|
xhtml.SetAttribute("value", lexical_cast<ext::String>(DataType::Type(*type_))); |
96 |
|
|
xhtml.OutputText(*type_); |
97 |
|
|
} |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
{ |
101 |
|
|
xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); |
102 |
|
|
|
103 |
|
|
xhtml.SetAttribute("name", "data"); |
104 |
|
|
xhtml.SetAttribute("size", "64"); |
105 |
|
|
xhtml.SetAttribute("type", "text"); |
106 |
|
|
|
107 |
|
|
ext::String data(cgi.find("data") != cgi.end() ? cgi.find("data")->second : std::string()); |
108 |
|
|
|
109 |
|
|
xhtml.SetAttribute("value", data); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
{ |
113 |
|
|
xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); |
114 |
|
|
|
115 |
douglas |
385 |
ext::String inputs[] = { "Normal", "Binary", "Hexadecimal" }; |
116 |
|
|
Input input(cgi.find("input") != cgi.end() ? Input(lexical_cast<unsigned>(cgi.find("input")->second)) : INPUT_Normal); |
117 |
douglas |
380 |
|
118 |
douglas |
385 |
_foru (input_, INPUT_Normal, INPUT_Hexadecimal + 1) |
119 |
douglas |
380 |
{ |
120 |
|
|
xml::ScopeElement option(xhtml, "option"); |
121 |
|
|
|
122 |
douglas |
385 |
if (Input(input_) == input) xhtml.SetAttribute("selected", "selected"); |
123 |
douglas |
380 |
|
124 |
|
|
xhtml.SetAttribute("value", lexical_cast<ext::String>(input_)); |
125 |
|
|
xhtml.OutputText(inputs[input_]); |
126 |
|
|
} |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); |
130 |
|
|
|
131 |
|
|
xhtml.SetAttribute("type", "submit"); |
132 |
|
|
xhtml.SetAttribute("value", "Store"); |
133 |
|
|
} |
134 |
douglas |
385 |
|
135 |
|
|
void Represent::output(xml::TextWriter& xhtml) |
136 |
|
|
{ |
137 |
|
|
typedef std::multimap<std::string, std::string>::size_type MultiMapSize; |
138 |
|
|
|
139 |
|
|
std::set<MultiMapSize, std::greater<MultiMapSize> > count; |
140 |
|
|
ext::String names[] = { "type", "data", "input" }; |
141 |
|
|
|
142 |
|
|
_foru (index, 0, sizeof (names) / sizeof (ext::String)) count.insert(cgi.count(names[index])); |
143 |
|
|
|
144 |
|
|
typedef std::multimap<std::string, std::string>::const_iterator MultiMapConstIterator; |
145 |
|
|
|
146 |
|
|
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")); |
147 |
|
|
ext::Vector<Item> items; |
148 |
|
|
|
149 |
|
|
_foru (index, 0, *count.begin()) |
150 |
|
|
{ |
151 |
|
|
Item item(type != type_ ? DataType::Type(lexical_cast<unsigned>(type->second)) : DataType::TYPE_bool, data != data_ ? data->second : std::string(), input != input_ ? Input(lexical_cast<unsigned>(input->second)) : INPUT_Normal); |
152 |
|
|
|
153 |
|
|
items.InsertLast(item); |
154 |
|
|
|
155 |
|
|
if (type != type_) ++type; |
156 |
|
|
if (data != data_) ++data; |
157 |
|
|
if (input != input_) ++input; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
_rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast<size_t>(delete_->second)); |
161 |
|
|
|
162 |
|
|
_foreach (ext::Vector<Item>, item, items) switch (item->type) |
163 |
|
|
{ |
164 |
|
|
case DataType::TYPE_bool: |
165 |
|
|
output<bool>(xhtml, *item); |
166 |
|
|
|
167 |
|
|
break; |
168 |
|
|
case DataType::TYPE_char: |
169 |
|
|
output<char>(xhtml, *item); |
170 |
|
|
|
171 |
|
|
break; |
172 |
|
|
} |
173 |
|
|
} |