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

# User Rev Content
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 387 #include "InputType.hpp"
10 douglas 362
11 douglas 386 #ifdef _WIN32
12 douglas 389 #pragma warning(disable:4267 4288)
13 douglas 386 #endif
14    
15 douglas 370 #include <menes-app/simple.hpp>
16 douglas 387 #include <menes-xml/document.hpp>
17     #include <menes-xml/nodeset.hpp>
18     #include <menes-xml/parse.hpp>
19 douglas 362
20 douglas 389 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 douglas 370 int Main(const app::Options& options)
34 douglas 362 {
35 douglas 370 Represent represent;
36 douglas 362
37 douglas 370 return 0;
38 douglas 362 }
39    
40 douglas 370 Represent::Represent()
41 douglas 362 {
42 douglas 387 api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush;
43    
44 douglas 388 xml::TextWriter xhtml(api::Cout);
45    
46 douglas 379 parse();
47 douglas 371
48 douglas 387 _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 douglas 371
52 douglas 388 api::Cout << ios::NewLine << before << ios::Flush;
53 douglas 387
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 douglas 379 }
64 douglas 373
65 douglas 379 void Represent::parse()
66     {
67     ext::String query(env.get("QUERY_STRING"));
68 douglas 373
69 douglas 379 if (env.get("REQUEST_METHOD") == "POST")
70     {
71     ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH")));
72 douglas 373
73 douglas 379 api::Cin.ReadFully(content.Begin(), content.GetSize());
74 douglas 373
75 douglas 379 query = content;
76     }
77    
78 douglas 387 // api::Cout << query << ios::NewLine;
79    
80 douglas 379 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 douglas 387 // XXX: clean up %20, etc.
88    
89 douglas 379 cgi.insert(std::pair<std::string, std::string>(name, value));
90     }
91 douglas 362 }
92 douglas 380
93     void Represent::headings(xml::TextWriter& xhtml)
94     {
95     xml::ScopeElement tr(xhtml, "tr");
96 douglas 387 ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" };
97 douglas 380
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 douglas 387 DataType type(cgi.find("type") != cgi.end() ? cgi.find("type")->second : std::string());
116 douglas 380
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 douglas 387 xhtml.SetAttribute("name", "input");
143 douglas 380
144 douglas 387 InputType input(cgi.find("input") != cgi.end() ? cgi.find("input")->second : std::string());
145    
146     _foreach (ext::Vector<InputType>, input_, InputType::enumerate())
147 douglas 380 {
148     xml::ScopeElement option(xhtml, "option");
149    
150 douglas 387 if (*input_ == input) xhtml.SetAttribute("selected", "selected");
151 douglas 380
152 douglas 387 xhtml.OutputText(*input_);
153 douglas 380 }
154     }
155    
156     xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
157    
158     xhtml.SetAttribute("type", "submit");
159     xhtml.SetAttribute("value", "Store");
160     }
161 douglas 385
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 douglas 387 Item item(DataType(type != type_ ? type->second : std::string()), data != data_ ? data->second : std::string(), InputType(input != input_ ? input->second : std::string()));
179 douglas 385
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 douglas 387 size_t index(0);
190    
191 douglas 385 _foreach (ext::Vector<Item>, item, items) switch (item->type)
192     {
193     case DataType::TYPE_bool:
194 douglas 387 output<bool>(xhtml, *item, ++index);
195 douglas 385 break;
196     case DataType::TYPE_char:
197 douglas 387 output<char>(xhtml, *item, ++index);
198 douglas 385 break;
199 douglas 387 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 douglas 385 }
229     }
230 douglas 387
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 douglas 391 bool signed_(etl::Limits<Type>::IsSigned);
321 douglas 387
322 douglas 391 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 douglas 387 }
332    
333     template <>
334     char Represent::input(const Item& item)
335     {
336 douglas 391 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 douglas 387 }
346    
347 douglas 391 // XXX: constructing a string from Binary or Hexadecimal seems too dangerous
348 douglas 387 template <>
349     std::string Represent::input(const Item& item)
350     {
351     return item.data;
352     }
353    
354 douglas 391 // XXX: constructing a string from Binary or Hexadecimal seems too dangerous
355 douglas 387 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