6 |
|
|
7 |
|
#include "Hexadecimal.hpp" |
8 |
|
#include "DataType.hpp" |
9 |
+ |
#include "InputType.hpp" |
10 |
+ |
|
11 |
+ |
#ifdef _WIN32 |
12 |
+ |
#pragma warning(disable:4267 4288) |
13 |
+ |
#endif |
14 |
|
|
15 |
|
#include <menes-app/simple.hpp> |
16 |
+ |
#include <menes-xml/document.hpp> |
17 |
+ |
#include <menes-xml/nodeset.hpp> |
18 |
+ |
#include <menes-xml/parse.hpp> |
19 |
|
|
20 |
< |
Environment env; |
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 |
|
{ |
39 |
|
|
40 |
|
Represent::Represent() |
41 |
|
{ |
23 |
– |
parse(); |
24 |
– |
|
42 |
|
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; |
43 |
|
|
44 |
|
xml::TextWriter xhtml(api::Cout); |
28 |
– |
xml::ScopeElement table(xhtml, "table"); |
45 |
|
|
46 |
< |
headings(xhtml); |
47 |
< |
form(xhtml); |
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 |
< |
// |
52 |
> |
api::Cout << ios::NewLine << before << ios::Flush; |
53 |
> |
|
54 |
> |
{ |
55 |
> |
xml::ScopeElement table(xhtml, "table"); |
56 |
> |
|
57 |
> |
headings(xhtml); |
58 |
> |
form(xhtml); |
59 |
> |
output(xhtml); |
60 |
> |
} |
61 |
> |
|
62 |
> |
api::Cout << after << ios::Flush; |
63 |
|
} |
64 |
|
|
65 |
|
void Represent::parse() |
75 |
|
query = content; |
76 |
|
} |
77 |
|
|
78 |
+ |
// api::Cout << query << ios::NewLine; |
79 |
+ |
|
80 |
|
ext::Vector<ext::String> pairs(query.Split('&')); |
81 |
|
|
82 |
|
_foreach (ext::Vector<ext::String>, pair, pairs) |
84 |
|
ext::String::ConstIterator equal(pair->FindFirst('=')); |
85 |
|
ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End()); |
86 |
|
|
87 |
+ |
// XXX: clean up %20, etc. |
88 |
+ |
|
89 |
|
cgi.insert(std::pair<std::string, std::string>(name, value)); |
90 |
|
} |
91 |
|
} |
93 |
|
void Represent::headings(xml::TextWriter& xhtml) |
94 |
|
{ |
95 |
|
xml::ScopeElement tr(xhtml, "tr"); |
96 |
< |
ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" }; |
96 |
> |
ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" }; |
97 |
|
|
98 |
|
_foru (index, 0, sizeof (headings) / sizeof (ext::String)) |
99 |
|
{ |
112 |
|
|
113 |
|
xhtml.SetAttribute("name", "type"); |
114 |
|
|
115 |
< |
DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast<unsigned>(cgi.find("type")->second)) : DataType::TYPE_bool); |
115 |
> |
DataType type(cgi.find("type") != cgi.end() ? cgi.find("type")->second : std::string()); |
116 |
|
|
117 |
|
_foreach (ext::Vector<DataType>, type_, DataType::enumerate()) |
118 |
|
{ |
120 |
|
|
121 |
|
if (*type_ == type) xhtml.SetAttribute("selected", "selected"); |
122 |
|
|
90 |
– |
xhtml.SetAttribute("value", lexical_cast<ext::String>(DataType::Type(*type_))); |
123 |
|
xhtml.OutputText(*type_); |
124 |
|
} |
125 |
|
} |
139 |
|
{ |
140 |
|
xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); |
141 |
|
|
142 |
< |
ext::String inputs[] = { "Binary", "Hexadecimal", "std::istream", "ios::PrintReader" }; |
111 |
< |
unsigned input(cgi.find("input") != cgi.end() ? lexical_cast<unsigned>(cgi.find("input")->second) : 0); |
142 |
> |
xhtml.SetAttribute("name", "input"); |
143 |
|
|
144 |
< |
_foru (input_, 0, sizeof (inputs) / sizeof (ext::String)) |
144 |
> |
InputType input(cgi.find("input") != cgi.end() ? cgi.find("input")->second : std::string()); |
145 |
> |
|
146 |
> |
_foreach (ext::Vector<InputType>, input_, InputType::enumerate()) |
147 |
|
{ |
148 |
|
xml::ScopeElement option(xhtml, "option"); |
149 |
|
|
150 |
< |
if (input_ == input) xhtml.SetAttribute("selected", "selected"); |
150 |
> |
if (*input_ == input) xhtml.SetAttribute("selected", "selected"); |
151 |
|
|
152 |
< |
xhtml.SetAttribute("value", lexical_cast<ext::String>(input_)); |
120 |
< |
xhtml.OutputText(inputs[input_]); |
152 |
> |
xhtml.OutputText(*input_); |
153 |
|
} |
154 |
|
} |
155 |
|
|
158 |
|
xhtml.SetAttribute("type", "submit"); |
159 |
|
xhtml.SetAttribute("value", "Store"); |
160 |
|
} |
161 |
+ |
|
162 |
+ |
void Represent::output(xml::TextWriter& xhtml) |
163 |
+ |
{ |
164 |
+ |
typedef std::multimap<std::string, std::string>::size_type MultiMapSize; |
165 |
+ |
|
166 |
+ |
std::set<MultiMapSize, std::greater<MultiMapSize> > count; |
167 |
+ |
ext::String names[] = { "type", "data", "input" }; |
168 |
+ |
|
169 |
+ |
_foru (index, 0, sizeof (names) / sizeof (ext::String)) count.insert(cgi.count(names[index])); |
170 |
+ |
|
171 |
+ |
typedef std::multimap<std::string, std::string>::const_iterator MultiMapConstIterator; |
172 |
+ |
|
173 |
+ |
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")); |
174 |
+ |
ext::Vector<Item> items; |
175 |
+ |
|
176 |
+ |
_foru (index, 0, *count.begin()) |
177 |
+ |
{ |
178 |
+ |
Item item(DataType(type != type_ ? type->second : std::string()), data != data_ ? data->second : std::string(), InputType(input != input_ ? input->second : std::string())); |
179 |
+ |
|
180 |
+ |
items.InsertLast(item); |
181 |
+ |
|
182 |
+ |
if (type != type_) ++type; |
183 |
+ |
if (data != data_) ++data; |
184 |
+ |
if (input != input_) ++input; |
185 |
+ |
} |
186 |
+ |
|
187 |
+ |
_rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast<size_t>(delete_->second)); |
188 |
+ |
|
189 |
+ |
size_t index(0); |
190 |
+ |
|
191 |
+ |
_foreach (ext::Vector<Item>, item, items) switch (item->type) |
192 |
+ |
{ |
193 |
+ |
case DataType::TYPE_bool: |
194 |
+ |
output<bool>(xhtml, *item, ++index); |
195 |
+ |
break; |
196 |
+ |
case DataType::TYPE_char: |
197 |
+ |
output<char>(xhtml, *item, ++index); |
198 |
+ |
break; |
199 |
+ |
case DataType::TYPE_short: |
200 |
+ |
output<short>(xhtml, *item, ++index); |
201 |
+ |
break; |
202 |
+ |
case DataType::TYPE_unsigned_short: |
203 |
+ |
output<unsigned short>(xhtml, *item, ++index); |
204 |
+ |
break; |
205 |
+ |
case DataType::TYPE_int: |
206 |
+ |
output<int>(xhtml, *item, ++index); |
207 |
+ |
break; |
208 |
+ |
case DataType::TYPE_unsigned_int: |
209 |
+ |
output<unsigned int>(xhtml, *item, ++index); |
210 |
+ |
break; |
211 |
+ |
case DataType::TYPE_long: |
212 |
+ |
output<long>(xhtml, *item, ++index); |
213 |
+ |
break; |
214 |
+ |
case DataType::TYPE_unsigned_long: |
215 |
+ |
output<unsigned long>(xhtml, *item, ++index); |
216 |
+ |
break; |
217 |
+ |
case DataType::TYPE_float: |
218 |
+ |
output<float>(xhtml, *item, ++index); |
219 |
+ |
break; |
220 |
+ |
case DataType::TYPE_double: |
221 |
+ |
output<double>(xhtml, *item, ++index); |
222 |
+ |
break; |
223 |
+ |
case DataType::TYPE_std_string: |
224 |
+ |
output<std::string>(xhtml, *item, ++index); |
225 |
+ |
break; |
226 |
+ |
case DataType::TYPE_ext_String: |
227 |
+ |
output<ext::String>(xhtml, *item, ++index); |
228 |
+ |
} |
229 |
+ |
} |
230 |
+ |
|
231 |
+ |
template <typename Type> |
232 |
+ |
void Represent::output(xml::TextWriter& xhtml, const Item& item, size_t index) |
233 |
+ |
{ |
234 |
+ |
xhtml.OpenElement("tr"); |
235 |
+ |
|
236 |
+ |
{ |
237 |
+ |
xml::ScopeElement td(xhtml, "td"); |
238 |
+ |
|
239 |
+ |
xhtml.SetAttribute("rowspan", "3"); |
240 |
+ |
xhtml.OutputText(item.type); |
241 |
+ |
|
242 |
+ |
ext::String names[] = { "type", "data", "input" }, values[] = { item.type, item.data, item.input }; |
243 |
+ |
|
244 |
+ |
_foru (index, 0, sizeof (names) / sizeof (ext::String)) |
245 |
+ |
{ |
246 |
+ |
xml::ScopeElement input(xhtml, "input"); |
247 |
+ |
|
248 |
+ |
xhtml.SetAttribute("name", names[index]); |
249 |
+ |
xhtml.SetAttribute("type", "hidden"); |
250 |
+ |
xhtml.SetAttribute("value", values[index]); |
251 |
+ |
} |
252 |
+ |
} |
253 |
+ |
|
254 |
+ |
Type type(input<Type>(item)); |
255 |
+ |
|
256 |
+ |
{ |
257 |
+ |
xml::ScopeElement td(xhtml, "td"); |
258 |
+ |
|
259 |
+ |
normal(xhtml, type); |
260 |
+ |
} |
261 |
+ |
|
262 |
+ |
{ |
263 |
+ |
xml::ScopeElement th(xhtml, "th"); |
264 |
+ |
|
265 |
+ |
xhtml.OutputText("Normal"); |
266 |
+ |
} |
267 |
+ |
|
268 |
+ |
{ |
269 |
+ |
xml::ScopeElement td(xhtml, "td"); |
270 |
+ |
|
271 |
+ |
xhtml.SetAttribute("rowspan", "3"); |
272 |
+ |
|
273 |
+ |
{ |
274 |
+ |
xml::ScopeElement input(xhtml, "input"); |
275 |
+ |
|
276 |
+ |
xhtml.SetAttribute("name", "delete"); |
277 |
+ |
xhtml.SetAttribute("type", "checkbox"); |
278 |
+ |
xhtml.SetAttribute("value", lexical_cast<ext::String>(index)); |
279 |
+ |
} |
280 |
+ |
|
281 |
+ |
xhtml.OutputText("Delete"); |
282 |
+ |
} |
283 |
+ |
|
284 |
+ |
xhtml.CloseElement(); |
285 |
+ |
xhtml.OpenElement("tr"); |
286 |
+ |
|
287 |
+ |
{ |
288 |
+ |
xml::ScopeElement td(xhtml, "td"); |
289 |
+ |
|
290 |
+ |
binary(xhtml, type); |
291 |
+ |
} |
292 |
+ |
|
293 |
+ |
{ |
294 |
+ |
xml::ScopeElement th(xhtml, "th"); |
295 |
+ |
|
296 |
+ |
xhtml.OutputText("Binary"); |
297 |
+ |
} |
298 |
+ |
|
299 |
+ |
xhtml.CloseElement(); |
300 |
+ |
xhtml.OpenElement("tr"); |
301 |
+ |
|
302 |
+ |
{ |
303 |
+ |
xml::ScopeElement td(xhtml, "td"); |
304 |
+ |
|
305 |
+ |
hexadecimal(xhtml, type); |
306 |
+ |
} |
307 |
+ |
|
308 |
+ |
{ |
309 |
+ |
xml::ScopeElement th(xhtml, "th"); |
310 |
+ |
|
311 |
+ |
xhtml.OutputText("Hexadecimal"); |
312 |
+ |
} |
313 |
+ |
|
314 |
+ |
xhtml.CloseElement(); |
315 |
+ |
} |
316 |
+ |
|
317 |
+ |
template <typename Type> |
318 |
+ |
Type Represent::input(const Item& item) |
319 |
+ |
{ |
320 |
+ |
// XXX: implement |
321 |
+ |
|
322 |
+ |
return lexical_cast<Type>(item.data); |
323 |
+ |
} |
324 |
+ |
|
325 |
+ |
template <> |
326 |
+ |
bool Represent::input(const Item& item) |
327 |
+ |
{ |
328 |
+ |
// XXX: implement |
329 |
+ |
|
330 |
+ |
try { return lexical_cast<bool>(item.data); } catch (ext::Exception) { return false; } |
331 |
+ |
} |
332 |
+ |
|
333 |
+ |
template <> |
334 |
+ |
char Represent::input(const Item& item) |
335 |
+ |
{ |
336 |
+ |
// XXX: implement |
337 |
+ |
|
338 |
+ |
return item.data.First(); |
339 |
+ |
} |
340 |
+ |
|
341 |
+ |
// XXX: constructing a string from Binary or Hexadecimal seems to dangerous |
342 |
+ |
template <> |
343 |
+ |
std::string Represent::input(const Item& item) |
344 |
+ |
{ |
345 |
+ |
return item.data; |
346 |
+ |
} |
347 |
+ |
|
348 |
+ |
// XXX: constructing a string from Binary or Hexadecimal seems to dangerous |
349 |
+ |
template <> |
350 |
+ |
ext::String Represent::input(const Item& item) |
351 |
+ |
{ |
352 |
+ |
return item.data; |
353 |
+ |
} |
354 |
+ |
|
355 |
+ |
template <typename Type> |
356 |
+ |
void Represent::normal(xml::TextWriter& xhtml, const Type& type) |
357 |
+ |
{ |
358 |
+ |
xhtml.OutputText(lexical_cast<ext::String>(type)); |
359 |
+ |
} |
360 |
+ |
|
361 |
+ |
template <> |
362 |
+ |
void Represent::normal(xml::TextWriter& xhtml, const char& char_) |
363 |
+ |
{ |
364 |
+ |
xhtml.OutputText(ext::CodePoint(char_)); |
365 |
+ |
} |
366 |
+ |
|
367 |
+ |
template <> |
368 |
+ |
void Represent::normal(xml::TextWriter& xhtml, const std::string& string) |
369 |
+ |
{ |
370 |
+ |
xhtml.OutputText(string); |
371 |
+ |
} |
372 |
+ |
|
373 |
+ |
template <> |
374 |
+ |
void Represent::normal(xml::TextWriter& xhtml, const ext::String& string) |
375 |
+ |
{ |
376 |
+ |
xhtml.OutputText(string); |
377 |
+ |
} |
378 |
+ |
|
379 |
+ |
template <typename Type> |
380 |
+ |
void Represent::binary(xml::TextWriter& xhtml, const Type& type) |
381 |
+ |
{ |
382 |
+ |
xhtml.OutputText(Binary(type)); |
383 |
+ |
} |
384 |
+ |
|
385 |
+ |
template <> |
386 |
+ |
void Represent::binary(xml::TextWriter& xhtml, const std::string& string) |
387 |
+ |
{ |
388 |
+ |
xhtml.OutputText(Binary(string)); |
389 |
+ |
|
390 |
+ |
xml::ScopeElement(xhtml, "br"); |
391 |
+ |
|
392 |
+ |
_sforeach (std::string, atom, string) |
393 |
+ |
{ |
394 |
+ |
xml::ScopeElement(xhtml, "br"); |
395 |
+ |
|
396 |
+ |
xhtml.OutputText(Binary(*atom)); |
397 |
+ |
} |
398 |
+ |
} |
399 |
+ |
|
400 |
+ |
template <> |
401 |
+ |
void Represent::binary(xml::TextWriter& xhtml, const ext::String& string) |
402 |
+ |
{ |
403 |
+ |
xhtml.OutputText(Binary(string)); |
404 |
+ |
|
405 |
+ |
xml::ScopeElement(xhtml, "br"); |
406 |
+ |
|
407 |
+ |
_foreach (ext::String, atom, string) |
408 |
+ |
{ |
409 |
+ |
xml::ScopeElement(xhtml, "br"); |
410 |
+ |
|
411 |
+ |
xhtml.OutputText(Binary(*atom)); |
412 |
+ |
} |
413 |
+ |
} |
414 |
+ |
|
415 |
+ |
template <typename Type> |
416 |
+ |
void Represent::hexadecimal(xml::TextWriter& xhtml, const Type& type) |
417 |
+ |
{ |
418 |
+ |
xhtml.OutputText(Hexadecimal(type)); |
419 |
+ |
} |
420 |
+ |
|
421 |
+ |
template <> |
422 |
+ |
void Represent::hexadecimal(xml::TextWriter& xhtml, const std::string& string) |
423 |
+ |
{ |
424 |
+ |
xhtml.OutputText(Hexadecimal(string)); |
425 |
+ |
|
426 |
+ |
xml::ScopeElement(xhtml, "br"); |
427 |
+ |
|
428 |
+ |
_sforeach (std::string, atom, string) |
429 |
+ |
{ |
430 |
+ |
xml::ScopeElement(xhtml, "br"); |
431 |
+ |
|
432 |
+ |
xhtml.OutputText(Hexadecimal(string)); |
433 |
+ |
} |
434 |
+ |
} |
435 |
+ |
|
436 |
+ |
template <> |
437 |
+ |
void Represent::hexadecimal(xml::TextWriter& xhtml, const ext::String& string) |
438 |
+ |
{ |
439 |
+ |
xhtml.OutputText(Hexadecimal(string)); |
440 |
+ |
|
441 |
+ |
xml::ScopeElement(xhtml, "br"); |
442 |
+ |
|
443 |
+ |
_foreach (ext::String, atom, string) |
444 |
+ |
{ |
445 |
+ |
xml::ScopeElement(xhtml, "br"); |
446 |
+ |
|
447 |
+ |
xhtml.OutputText(Hexadecimal(string)); |
448 |
+ |
} |
449 |
+ |
} |