ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Represent.cpp
Revision: 391
Committed: 2004-12-24T20:31:04-08:00 (20 years, 5 months ago) by douglas
File size: 10336 byte(s)
Log Message:
Damn, this should compile!

File Contents

# Content
1 // Represent
2 //
3 // Douglas Thrift
4 //
5 // $Id$
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 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;
36
37 return 0;
38 }
39
40 Represent::Represent()
41 {
42 api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush;
43
44 xml::TextWriter xhtml(api::Cout);
45
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 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()
66 {
67 ext::String query(env.get("QUERY_STRING"));
68
69 if (env.get("REQUEST_METHOD") == "POST")
70 {
71 ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH")));
72
73 api::Cin.ReadFully(content.Begin(), content.GetSize());
74
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)
83 {
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 }
92
93 void Represent::headings(xml::TextWriter& xhtml)
94 {
95 xml::ScopeElement tr(xhtml, "tr");
96 ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" };
97
98 _foru (index, 0, sizeof (headings) / sizeof (ext::String))
99 {
100 xml::ScopeElement th(xhtml, "th");
101
102 xhtml.OutputText(headings[index]);
103 }
104 }
105
106 void Represent::form(xml::TextWriter& xhtml)
107 {
108 xml::ScopeElement tr(xhtml, "tr");
109
110 {
111 xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
112
113 xhtml.SetAttribute("name", "type");
114
115 DataType type(cgi.find("type") != cgi.end() ? cgi.find("type")->second : std::string());
116
117 _foreach (ext::Vector<DataType>, type_, DataType::enumerate())
118 {
119 xml::ScopeElement option(xhtml, "option");
120
121 if (*type_ == type) xhtml.SetAttribute("selected", "selected");
122
123 xhtml.OutputText(*type_);
124 }
125 }
126
127 {
128 xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
129
130 xhtml.SetAttribute("name", "data");
131 xhtml.SetAttribute("size", "64");
132 xhtml.SetAttribute("type", "text");
133
134 ext::String data(cgi.find("data") != cgi.end() ? cgi.find("data")->second : std::string());
135
136 xhtml.SetAttribute("value", data);
137 }
138
139 {
140 xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
141
142 xhtml.SetAttribute("name", "input");
143
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");
151
152 xhtml.OutputText(*input_);
153 }
154 }
155
156 xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
157
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 bool signed_(etl::Limits<Type>::IsSigned);
321
322 switch (item.input)
323 {
324 default:
325 try { return lexical_cast<Type>(item.data); } catch (ext::Exception) { return 0; }
326 case InputType::INPUT_Binary:
327 return Binary(item.data, signed_).convert<Type>(signed_);
328 case InputType::INPUT_Hexadecimal:
329 return Hexadecimal(item.data, signed_).convert<Type>(signed_);
330 }
331 }
332
333 template <>
334 char Represent::input(const Item& item)
335 {
336 switch (item.input)
337 {
338 default:
339 return item.data.First();
340 case InputType::INPUT_Binary:
341 return Binary(item.data, false).convert<char>(false);
342 case InputType::INPUT_Hexadecimal:
343 return Hexadecimal(input.data, false).convert<char>(false);
344 }
345 }
346
347 // XXX: constructing a string from Binary or Hexadecimal seems too dangerous
348 template <>
349 std::string Represent::input(const Item& item)
350 {
351 return item.data;
352 }
353
354 // XXX: constructing a string from Binary or Hexadecimal seems too dangerous
355 template <>
356 ext::String Represent::input(const Item& item)
357 {
358 return item.data;
359 }
360
361 template <typename Type>
362 void Represent::normal(xml::TextWriter& xhtml, const Type& type)
363 {
364 xhtml.OutputText(lexical_cast<ext::String>(type));
365 }
366
367 template <>
368 void Represent::normal(xml::TextWriter& xhtml, const char& char_)
369 {
370 xhtml.OutputText(ext::CodePoint(char_));
371 }
372
373 template <>
374 void Represent::normal(xml::TextWriter& xhtml, const std::string& string)
375 {
376 xhtml.OutputText(string);
377 }
378
379 template <>
380 void Represent::normal(xml::TextWriter& xhtml, const ext::String& string)
381 {
382 xhtml.OutputText(string);
383 }
384
385 template <typename Type>
386 void Represent::binary(xml::TextWriter& xhtml, const Type& type)
387 {
388 xhtml.OutputText(Binary(type));
389 }
390
391 template <>
392 void Represent::binary(xml::TextWriter& xhtml, const std::string& string)
393 {
394 xhtml.OutputText(Binary(string));
395
396 xml::ScopeElement(xhtml, "br");
397
398 _sforeach (std::string, atom, string)
399 {
400 xml::ScopeElement(xhtml, "br");
401
402 xhtml.OutputText(Binary(*atom));
403 }
404 }
405
406 template <>
407 void Represent::binary(xml::TextWriter& xhtml, const ext::String& string)
408 {
409 xhtml.OutputText(Binary(string));
410
411 xml::ScopeElement(xhtml, "br");
412
413 _foreach (ext::String, atom, string)
414 {
415 xml::ScopeElement(xhtml, "br");
416
417 xhtml.OutputText(Binary(*atom));
418 }
419 }
420
421 template <typename Type>
422 void Represent::hexadecimal(xml::TextWriter& xhtml, const Type& type)
423 {
424 xhtml.OutputText(Hexadecimal(type));
425 }
426
427 template <>
428 void Represent::hexadecimal(xml::TextWriter& xhtml, const std::string& string)
429 {
430 xhtml.OutputText(Hexadecimal(string));
431
432 xml::ScopeElement(xhtml, "br");
433
434 _sforeach (std::string, atom, string)
435 {
436 xml::ScopeElement(xhtml, "br");
437
438 xhtml.OutputText(Hexadecimal(string));
439 }
440 }
441
442 template <>
443 void Represent::hexadecimal(xml::TextWriter& xhtml, const ext::String& string)
444 {
445 xhtml.OutputText(Hexadecimal(string));
446
447 xml::ScopeElement(xhtml, "br");
448
449 _foreach (ext::String, atom, string)
450 {
451 xml::ScopeElement(xhtml, "br");
452
453 xhtml.OutputText(Hexadecimal(string));
454 }
455 }

Properties

Name Value
svn:eol-style native
svn:keywords Id