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 391 by douglas, 2004-12-24T20:31: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 51 | Line 75 | void Represent::parse()
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)
# Line 58 | Line 84 | void Represent::parse()
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   }
# Line 65 | Line 93 | void Represent::parse()
93   void Represent::headings(xml::TextWriter& xhtml)
94   {
95          xml::ScopeElement tr(xhtml, "tr");
96 <        ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" };
96 >        ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" };
97  
98          _foru (index, 0, sizeof (headings) / sizeof (ext::String))
99          {
# Line 84 | Line 112 | void Represent::form(xml::TextWriter& xh
112  
113                  xhtml.SetAttribute("name", "type");
114  
115 <                DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast<unsigned>(cgi.find("type")->second)) : DataType::TYPE_bool);
115 >                DataType type(cgi.find("type") != cgi.end() ? cgi.find("type")->second : std::string());
116  
117                  _foreach (ext::Vector<DataType>, type_, DataType::enumerate())
118                  {
# Line 92 | Line 120 | void Represent::form(xml::TextWriter& xh
120  
121                          if (*type_ == type) xhtml.SetAttribute("selected", "selected");
122  
95                        xhtml.SetAttribute("value", lexical_cast<ext::String>(DataType::Type(*type_)));
123                          xhtml.OutputText(*type_);
124                  }
125          }
# Line 112 | Line 139 | void Represent::form(xml::TextWriter& xh
139          {
140                  xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
141  
142 <                ext::String inputs[] = { "Normal", "Binary", "Hexadecimal" };
116 <                Input input(cgi.find("input") != cgi.end() ? Input(lexical_cast<unsigned>(cgi.find("input")->second)) : INPUT_Normal);
142 >                xhtml.SetAttribute("name", "input");
143  
144 <                _foru (input_, INPUT_Normal, INPUT_Hexadecimal + 1)
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_) == input) xhtml.SetAttribute("selected", "selected");
150 >                        if (*input_ == input) xhtml.SetAttribute("selected", "selected");
151  
152 <                        xhtml.SetAttribute("value", lexical_cast<ext::String>(input_));
125 <                        xhtml.OutputText(inputs[input_]);
152 >                        xhtml.OutputText(*input_);
153                  }
154          }
155  
# Line 148 | Line 175 | void Represent::output(xml::TextWriter&
175  
176          _foru (index, 0, *count.begin())
177          {
178 <                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);
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  
# Line 159 | Line 186 | void Represent::output(xml::TextWriter&
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);
166 <
194 >                output<bool>(xhtml, *item, ++index);
195                  break;
196          case DataType::TYPE_char:
197 <                output<char>(xhtml, *item);
198 <
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines