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 385 by douglas, 2004-12-23T21:01:12-08:00 vs.
Revision 393 by douglas, 2004-12-24T22:32:04-08:00

# Line 6 | Line 6
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   {
# Line 18 | Line 39 | int Main(const app::Options& options)
39  
40   Represent::Represent()
41   {
21        parse();
22
42          api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush;
43  
44          xml::TextWriter xhtml(api::Cout);
26        xml::ScopeElement table(xhtml, "table");
45  
46 <        headings(xhtml);
29 <        form(xhtml);
30 <        output(xhtml);
31 < }
46 >        parse();
47  
48 < struct Represent::Item
49 < {
50 <        DataType type;
51 <        ext::String data;
52 <        Input input;
53 <        Item(const DataType& type, const ext::String& data, Input input) : type(type), data(data), input(input) {}
54 < };
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   {
# Line 58 | Line 82 | void Represent::parse()
82                  ext::String::ConstIterator equal(pair->FindFirst('='));
83                  ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End());
84  
85 <                cgi.insert(std::pair<std::string, std::string>(name, value));
85 >                cgi.insert(std::pair<std::string, std::string>(decode(name), decode(value)));
86 >        }
87 > }
88 >
89 > std::string Represent::decode(const ext::String& encoded)
90 > {
91 >        std::string decoded(encoded);
92 >        std::string::size_type pos(0);
93 >
94 >        while ((pos = decoded.find_first_of("%+", pos)) != std::string::npos) switch (decoded[pos])
95 >        {
96 >        case '%':
97 >                decoded.replace(pos, 3, 1, Hexadecimal(decoded.substr(pos + 1, 2), false).convert<char>(false));
98 >                break;
99 >        case '+':
100 >                decoded[pos] = ' ';
101          }
102 +
103 +        return decoded;
104   }
105  
106   void Represent::headings(xml::TextWriter& xhtml)
107   {
108          xml::ScopeElement tr(xhtml, "tr");
109 <        ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" };
109 >        ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" };
110  
111          _foru (index, 0, sizeof (headings) / sizeof (ext::String))
112          {
# Line 84 | Line 125 | void Represent::form(xml::TextWriter& xh
125  
126                  xhtml.SetAttribute("name", "type");
127  
128 <                DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast<unsigned>(cgi.find("type")->second)) : DataType::TYPE_bool);
128 >                DataType type(cgi.find("type") != cgi.end() ? cgi.find("type")->second : std::string());
129  
130                  _foreach (ext::Vector<DataType>, type_, DataType::enumerate())
131                  {
# Line 92 | Line 133 | void Represent::form(xml::TextWriter& xh
133  
134                          if (*type_ == type) xhtml.SetAttribute("selected", "selected");
135  
95                        xhtml.SetAttribute("value", lexical_cast<ext::String>(DataType::Type(*type_)));
136                          xhtml.OutputText(*type_);
137                  }
138          }
# Line 112 | Line 152 | void Represent::form(xml::TextWriter& xh
152          {
153                  xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
154  
155 <                ext::String inputs[] = { "Normal", "Binary", "Hexadecimal" };
116 <                Input input(cgi.find("input") != cgi.end() ? Input(lexical_cast<unsigned>(cgi.find("input")->second)) : INPUT_Normal);
155 >                xhtml.SetAttribute("name", "input");
156  
157 <                _foru (input_, INPUT_Normal, INPUT_Hexadecimal + 1)
157 >                InputType input(cgi.find("input") != cgi.end() ? cgi.find("input")->second : std::string());
158 >
159 >                _foreach (ext::Vector<InputType>, input_, InputType::enumerate())
160                  {
161                          xml::ScopeElement option(xhtml, "option");
162  
163 <                        if (Input(input_) == input) xhtml.SetAttribute("selected", "selected");
163 >                        if (*input_ == input) xhtml.SetAttribute("selected", "selected");
164  
165 <                        xhtml.SetAttribute("value", lexical_cast<ext::String>(input_));
125 <                        xhtml.OutputText(inputs[input_]);
165 >                        xhtml.OutputText(*input_);
166                  }
167          }
168  
# Line 146 | Line 186 | void Represent::output(xml::TextWriter&
186          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"));
187          ext::Vector<Item> items;
188  
189 <        _foru (index, 0, *count.begin())
189 >        _foru (index, 0, *count.begin() < 128 ? *count.begin() : 128)
190          {
191 <                Item item(type != type_ ? DataType::Type(lexical_cast<unsigned>(type->second)) : DataType::TYPE_bool, data != data_ ? data->second : std::string(), input != input_ ? Input(lexical_cast<unsigned>(input->second)) : INPUT_Normal);
191 >                Item item(DataType(type != type_ ? type->second : std::string()), data != data_ ? data->second : std::string(), InputType(input != input_ ? input->second : std::string()));
192  
193                  items.InsertLast(item);
194  
# Line 159 | Line 199 | void Represent::output(xml::TextWriter&
199  
200          _rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast<size_t>(delete_->second));
201  
202 +        size_t index(0);
203 +
204          _foreach (ext::Vector<Item>, item, items) switch (item->type)
205          {
206          case DataType::TYPE_bool:
207 <                output<bool>(xhtml, *item);
166 <
207 >                output<bool>(xhtml, *item, ++index);
208                  break;
209          case DataType::TYPE_char:
210 <                output<char>(xhtml, *item);
211 <
210 >                output<char>(xhtml, *item, ++index);
211 >                break;
212 >        case DataType::TYPE_short:
213 >                output<short>(xhtml, *item, ++index);
214 >                break;
215 >        case DataType::TYPE_unsigned_short:
216 >                output<unsigned short>(xhtml, *item, ++index);
217 >                break;
218 >        case DataType::TYPE_int:
219 >                output<int>(xhtml, *item, ++index);
220 >                break;
221 >        case DataType::TYPE_unsigned_int:
222 >                output<unsigned int>(xhtml, *item, ++index);
223 >                break;
224 >        case DataType::TYPE_long:
225 >                output<long>(xhtml, *item, ++index);
226 >                break;
227 >        case DataType::TYPE_unsigned_long:
228 >                output<unsigned long>(xhtml, *item, ++index);
229                  break;
230 +        case DataType::TYPE_float:
231 +                output<float>(xhtml, *item, ++index);
232 +                break;
233 +        case DataType::TYPE_double:
234 +                output<double>(xhtml, *item, ++index);
235 +                break;
236 +        case DataType::TYPE_std_string:
237 +                output<std::string>(xhtml, *item, ++index);
238 +                break;
239 +        case DataType::TYPE_ext_String:
240 +                output<ext::String>(xhtml, *item, ++index);
241 +        }
242 + }
243 +
244 + template <typename Type>
245 + void Represent::output(xml::TextWriter& xhtml, const Item& item, size_t index)
246 + {
247 +        xhtml.OpenElement("tr");
248 +
249 +        {
250 +                xml::ScopeElement td(xhtml, "td");
251 +
252 +                xhtml.SetAttribute("rowspan", "3");
253 +                xhtml.OutputText(item.type);
254 +
255 +                ext::String names[] = { "type", "data", "input" }, values[] = { item.type, item.data, item.input };
256 +
257 +                _foru (index, 0, sizeof (names) / sizeof (ext::String))
258 +                {
259 +                        xml::ScopeElement input(xhtml, "input");
260 +
261 +                        xhtml.SetAttribute("name", names[index]);
262 +                        xhtml.SetAttribute("type", "hidden");
263 +                        xhtml.SetAttribute("value", values[index]);
264 +                }
265 +        }
266 +
267 +        Type type(input<Type>(item));
268 +
269 +        {
270 +                xml::ScopeElement td(xhtml, "td");
271 +
272 +                normal(xhtml, type);
273 +        }
274 +
275 +        {
276 +                xml::ScopeElement th(xhtml, "th");
277 +
278 +                xhtml.OutputText("Normal");
279 +        }
280 +
281 +        {
282 +                xml::ScopeElement td(xhtml, "td");
283 +
284 +                xhtml.SetAttribute("rowspan", "3");
285 +
286 +                {
287 +                        xml::ScopeElement input(xhtml, "input");
288 +
289 +                        xhtml.SetAttribute("name", "delete");
290 +                        xhtml.SetAttribute("type", "checkbox");
291 +                        xhtml.SetAttribute("value", lexical_cast<ext::String>(index));
292 +                }
293 +
294 +                xhtml.OutputText("Delete");
295 +        }
296 +
297 +        xhtml.CloseElement();
298 +        xhtml.OpenElement("tr");
299 +
300 +        {
301 +                xml::ScopeElement td(xhtml, "td");
302 +
303 +                binary(xhtml, type);
304 +        }
305 +
306 +        {
307 +                xml::ScopeElement th(xhtml, "th");
308 +
309 +                xhtml.OutputText("Binary");
310 +        }
311 +
312 +        xhtml.CloseElement();
313 +        xhtml.OpenElement("tr");
314 +
315 +        {
316 +                xml::ScopeElement td(xhtml, "td");
317 +        
318 +                hexadecimal(xhtml, type);
319 +        }
320 +
321 +        {
322 +                xml::ScopeElement th(xhtml, "th");
323 +
324 +                xhtml.OutputText("Hexadecimal");
325 +        }
326 +
327 +        xhtml.CloseElement();
328 + }
329 +
330 + template <typename Type>
331 + Type Represent::input(const Item& item)
332 + {
333 +        bool signed_(etl::Limits<Type>::IsSigned);
334 +
335 +        switch (item.input)
336 +        {
337 +        default:
338 +                try { return lexical_cast<Type>(item.data); } catch (ext::Exception) { return 0; }
339 +        case InputType::INPUT_Binary:
340 +                return Binary(item.data, signed_).convert<Type>(signed_);
341 +        case InputType::INPUT_Hexadecimal:
342 +                return Hexadecimal(item.data, signed_).convert<Type>(signed_);
343 +        }
344 + }
345 +
346 + template <>
347 + char Represent::input(const Item& item)
348 + {
349 +        switch (item.input)
350 +        {
351 +        default:
352 +                return item.data.First();
353 +        case InputType::INPUT_Binary:
354 +                return Binary(item.data, false).convert<char>(false);
355 +        case InputType::INPUT_Hexadecimal:
356 +                return Hexadecimal(item.data, false).convert<char>(false);
357 +        }
358 + }
359 +
360 + // XXX: constructing a string from Binary or Hexadecimal seems too dangerous
361 + template <>
362 + std::string Represent::input(const Item& item)
363 + {
364 +        return item.data;
365 + }
366 +
367 + // XXX: constructing a string from Binary or Hexadecimal seems too dangerous
368 + template <>
369 + ext::String Represent::input(const Item& item)
370 + {
371 +        return item.data;
372 + }
373 +
374 + template <typename Type>
375 + void Represent::normal(xml::TextWriter& xhtml, const Type& type)
376 + {
377 +        xhtml.OutputText(lexical_cast<ext::String>(type));
378 + }
379 +
380 + // XXX: damn, this one is really quite screwy
381 + template <>
382 + void Represent::normal(xml::TextWriter& xhtml, const char& char_)
383 + {
384 +        xhtml.OutputText("'");
385 +        xhtml.OutputText(std::string(1, char_).c_str());
386 +        xhtml.OutputText("'");
387 + }
388 +
389 + template <>
390 + void Represent::normal(xml::TextWriter& xhtml, const std::string& string)
391 + {
392 +        xhtml.OutputText("\"");
393 +        xhtml.OutputText(string);
394 +        xhtml.OutputText("\"");
395 + }
396 +
397 + template <>
398 + void Represent::normal(xml::TextWriter& xhtml, const ext::String& string)
399 + {
400 +        xhtml.OutputText("\"");
401 +        xhtml.OutputText(string);
402 +        xhtml.OutputText("\"");
403 + }
404 +
405 + template <typename Type>
406 + void Represent::binary(xml::TextWriter& xhtml, const Type& type)
407 + {
408 +        xhtml.OutputText(Binary(type));
409 + }
410 +
411 + template <>
412 + void Represent::binary(xml::TextWriter& xhtml, const std::string& string)
413 + {
414 +        xhtml.OutputText(Binary(string));
415 +
416 +        xml::ScopeElement(xhtml, "br");
417 +
418 +        _sforeach (std::string, atom, string)
419 +        {
420 +                xml::ScopeElement(xhtml, "br");
421 +
422 +                xhtml.OutputText(Binary(*atom));
423 +                xhtml.OutputText(" = '");
424 +                xhtml.OutputText(ext::CodePoint(*atom));
425 +                xhtml.OutputText("'");
426 +        }
427 + }
428 +
429 + template <>
430 + void Represent::binary(xml::TextWriter& xhtml, const ext::String& string)
431 + {
432 +        xhtml.OutputText(Binary(string));
433 +
434 +        xml::ScopeElement(xhtml, "br");
435 +
436 +        _foreach (ext::String, atom, string)
437 +        {
438 +                xml::ScopeElement(xhtml, "br");
439 +
440 +                xhtml.OutputText(Binary(*atom));
441 +                xhtml.OutputText(" = '");
442 +                xhtml.OutputText(ext::CodePoint(*atom));
443 +                xhtml.OutputText("'");
444 +        }
445 + }
446 +
447 + template <typename Type>
448 + void Represent::hexadecimal(xml::TextWriter& xhtml, const Type& type)
449 + {
450 +        xhtml.OutputText(Hexadecimal(type));
451 + }
452 +
453 + template <>
454 + void Represent::hexadecimal(xml::TextWriter& xhtml, const std::string& string)
455 + {
456 +        xhtml.OutputText(Hexadecimal(string));
457 +
458 +        xml::ScopeElement(xhtml, "br");
459 +
460 +        _sforeach (std::string, atom, string)
461 +        {
462 +                xml::ScopeElement(xhtml, "br");
463 +
464 +                xhtml.OutputText(Hexadecimal(*atom));
465 +                xhtml.OutputText(" = '");
466 +                xhtml.OutputText(ext::CodePoint(*atom));
467 +                xhtml.OutputText("'");
468 +        }
469 + }
470 +
471 + template <>
472 + void Represent::hexadecimal(xml::TextWriter& xhtml, const ext::String& string)
473 + {
474 +        xhtml.OutputText(Hexadecimal(string));
475 +
476 +        xml::ScopeElement(xhtml, "br");
477 +
478 +        _foreach (ext::String, atom, string)
479 +        {
480 +                xml::ScopeElement(xhtml, "br");
481 +
482 +                xhtml.OutputText(Hexadecimal(*atom));
483 +                xhtml.OutputText(" = '");
484 +                xhtml.OutputText(ext::CodePoint(*atom));
485 +                xhtml.OutputText("'");
486          }
487   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines