ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Represent.cpp
Revision: 387
Committed: 2004-12-24T03:17:21-08:00 (20 years, 5 months ago) by douglas
File size: 9816 byte(s)
Log Message:
Getting somewhere, but I've hit some weird stuff.

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

Properties

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