ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Represent.cpp
(Generate patch)

Comparing Represent/Represent.cpp (file contents):
Revision 373 by douglas, 2004-12-22T17:27:28-08:00 vs.
Revision 388 by douglas, 2004-12-24T03:31:12-08:00

# Line 5 | Line 5
5   // $Id$
6  
7   #include "Hexadecimal.hpp"
8 + #include "DataType.hpp"
9 + #include "InputType.hpp"
10 +
11 + #ifdef _WIN32
12 + #pragma warning(disable:4267)
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   int Main(const app::Options& options)
21   {
# Line 17 | Line 26 | int Main(const app::Options& options)
26  
27   Represent::Represent()
28   {
29 <        api::Cerr << Binary("Hello, World!") << " = " << Hexadecimal("Hello, World") << " = Hello, World!" << ios::NewLine;
29 >        api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush;
30 >
31 >        xml::TextWriter xhtml(api::Cout);
32 >
33 >        parse();
34 >
35 >        _H<xml::Document> document(xml::Parse("represent.xml"));
36 >        _H<xml::Node> node(*document/"represent");
37 >        ext::String before(*node/"before"), after(*node/"after");
38 >
39 >        api::Cout << ios::NewLine << before << ios::Flush;
40 >
41 >        {
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 > }
51 >
52 > struct Represent::Item
53 > {
54 >        DataType type;
55 >        ext::String data;
56 >        InputType input;
57 >        Item(const DataType& type, const ext::String& data, const InputType& input) : type(type), data(data), input(input) {}
58 > };
59 >
60 > void Represent::parse()
61 > {
62 >        ext::String query(env.get("QUERY_STRING"));
63 >
64 >        if (env.get("REQUEST_METHOD") == "POST")
65 >        {
66 >                ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH")));
67 >
68 >                api::Cin.ReadFully(content.Begin(), content.GetSize());
69 >
70 >                query = content;
71 >        }
72 >
73 > //      api::Cout << query << ios::NewLine;
74 >
75 >        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 >                // XXX: clean up %20, etc.
83 >
84 >                cgi.insert(std::pair<std::string, std::string>(name, value));
85 >        }
86 > }
87 >
88 > void Represent::headings(xml::TextWriter& xhtml)
89 > {
90 >        xml::ScopeElement tr(xhtml, "tr");
91 >        ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" };
92 >
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 >                DataType type(cgi.find("type") != cgi.end() ? cgi.find("type")->second : std::string());
111 >
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 >                xhtml.SetAttribute("name", "input");
138 >
139 >                InputType input(cgi.find("input") != cgi.end() ? cgi.find("input")->second : std::string());
140 >
141 >                _foreach (ext::Vector<InputType>, input_, InputType::enumerate())
142 >                {
143 >                        xml::ScopeElement option(xhtml, "option");
144 >
145 >                        if (*input_ == input) xhtml.SetAttribute("selected", "selected");
146 >
147 >                        xhtml.OutputText(*input_);
148 >                }
149 >        }
150 >
151 >        xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
152 >
153 >        xhtml.SetAttribute("type", "submit");
154 >        xhtml.SetAttribute("value", "Store");
155 > }
156 >
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 >                Item item(DataType(type != type_ ? type->second : std::string()), data != data_ ? data->second : std::string(), InputType(input != input_ ? input->second : std::string()));
174 >
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 >        size_t index(0);
185 >
186 >        _foreach (ext::Vector<Item>, item, items) switch (item->type)
187 >        {
188 >        case DataType::TYPE_bool:
189 >                output<bool>(xhtml, *item, ++index);
190 >                break;
191 >        case DataType::TYPE_char:
192 >                output<char>(xhtml, *item, ++index);
193 >                break;
194 >        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 >        }
224 > }
225 >
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 <        std::string hello("Hello, World!");
320 > template <>
321 > bool Represent::input(const Item& item)
322 > {
323 >        // XXX: implement
324  
325 <        api::Cerr << Binary(hello) << " = " << Hexadecimal(hello) << " = " << ext::String(hello) << ios::NewLine;
325 >        try { return lexical_cast<bool>(item.data); } catch (ext::Exception) { return false; }
326 > }
327  
328 <        _sforeach (std::string, atom, hello) api::Cerr << Binary(*atom) << " = " << Hexadecimal(*atom) << " = " << ext::String(atom, 1) << ios::NewLine;
328 > template <>
329 > char Represent::input(const Item& item)
330 > {
331 >        // XXX: implement
332 >        
333 >        return item.data.First();
334 > }
335  
336 <        ext::String hello_(hello);
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 <        api::Cerr << Binary(hello_) << " = " << Hexadecimal(hello_) << " = " << hello_ << ios::NewLine;
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 <        _foreach (ext::String, atom, hello_) api::Cerr << Binary(*atom) << " = " << Hexadecimal(*atom) << " = " << ext::String(*atom) << ios::NewLine;
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 <        _fori (index, -10, 11) api::Cerr << Binary(index) << " = " << Hexadecimal(index) << " = " << index << ios::NewLine;
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 <        _foru (index, 0xFFFFFFF6, 11) api::Cerr << Binary(index) << " = " << Hexadecimal(index) << " = " << index << ios::NewLine;
436 >        xml::ScopeElement(xhtml, "br");
437  
438 <        for (float index(-1); index < 1.1; index += 0.1) api::Cerr << Binary(index) << " = " << Hexadecimal(index) << " = " << index << ios::NewLine;
438 >        _foreach (ext::String, atom, string)
439 >        {
440 >                xml::ScopeElement(xhtml, "br");
441  
442 <        for (double index(-1); index < 1.1; index += 0.1) api::Cerr << Binary(index) << " = " << Hexadecimal(index) << " = " << index << ios::NewLine;
442 >                xhtml.OutputText(Hexadecimal(string));
443 >        }
444   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines