5 |
|
// $Id$ |
6 |
|
|
7 |
|
#include "Hexadecimal.hpp" |
8 |
+ |
#include "DataType.hpp" |
9 |
|
|
9 |
– |
#include <menes-api/environment.hpp> |
10 |
|
#include <menes-app/simple.hpp> |
11 |
|
|
12 |
– |
struct Environment |
13 |
– |
{ |
14 |
– |
ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } } |
15 |
– |
} env; |
16 |
– |
|
12 |
|
int Main(const app::Options& options) |
13 |
|
{ |
14 |
|
Represent represent; |
25 |
|
xml::TextWriter xhtml(api::Cout); |
26 |
|
xml::ScopeElement table(xhtml, "table"); |
27 |
|
|
28 |
< |
{ |
29 |
< |
xml::ScopeElement tr(xhtml, "tr"); |
30 |
< |
ext::String headings[] = { "Type", "Data Representation", "Storage" }; |
36 |
< |
|
37 |
< |
_foru (index, 0, sizeof (headings) / sizeof (ext::String)) |
38 |
< |
{ |
39 |
< |
xml::ScopeElement th(xhtml, "th"); |
40 |
< |
|
41 |
< |
xhtml.OutputText(headings[index]); |
42 |
< |
} |
43 |
< |
} |
28 |
> |
headings(xhtml); |
29 |
> |
form(xhtml); |
30 |
> |
output(xhtml); |
31 |
|
} |
32 |
|
|
33 |
+ |
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 |
|
void Represent::parse() |
42 |
|
{ |
43 |
|
ext::String query(env.get("QUERY_STRING")); |
61 |
|
cgi.insert(std::pair<std::string, std::string>(name, value)); |
62 |
|
} |
63 |
|
} |
64 |
+ |
|
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 |
+ |
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 |
+ |
|
118 |
+ |
_foru (input_, INPUT_Normal, INPUT_Hexadecimal + 1) |
119 |
+ |
{ |
120 |
+ |
xml::ScopeElement option(xhtml, "option"); |
121 |
+ |
|
122 |
+ |
if (Input(input_) == input) xhtml.SetAttribute("selected", "selected"); |
123 |
+ |
|
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 |
+ |
|
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 |
+ |
} |