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 371 by douglas, 2004-12-21T23:19:54-08:00 vs.
Revision 380 by douglas, 2004-12-23T01:07:33-08:00

# Line 5 | Line 5
5   // $Id$
6  
7   #include "Hexadecimal.hpp"
8 + #include "DataType.hpp"
9  
10 + #include <menes-api/environment.hpp>
11   #include <menes-app/simple.hpp>
12  
13 + struct Environment
14 + {
15 +        ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } }
16 + } env;
17 +
18   int Main(const app::Options& options)
19   {
20          Represent represent;
# Line 17 | Line 24 | int Main(const app::Options& options)
24  
25   Represent::Represent()
26   {
27 <        ext::String string("1001");
28 <        Binary sign(string, true), unsign(string, false);
27 >        parse();
28 >
29 >        api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush;
30 >
31 >        xml::TextWriter xhtml(api::Cout);
32 >        xml::ScopeElement table(xhtml, "table");
33 >
34 >        headings(xhtml);
35 >        form(xhtml);
36 >
37 >        //
38 > }
39 >
40 > void Represent::parse()
41 > {
42 >        ext::String query(env.get("QUERY_STRING"));
43 >
44 >        if (env.get("REQUEST_METHOD") == "POST")
45 >        {
46 >                ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH")));
47 >
48 >                api::Cin.ReadFully(content.Begin(), content.GetSize());
49 >
50 >                query = content;
51 >        }
52 >
53 >        ext::Vector<ext::String> pairs(query.Split('&'));
54 >
55 >        _foreach (ext::Vector<ext::String>, pair, pairs)
56 >        {
57 >                ext::String::ConstIterator equal(pair->FindFirst('='));
58 >                ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End());
59 >
60 >                cgi.insert(std::pair<std::string, std::string>(name, value));
61 >        }
62 > }
63 >
64 > void Represent::headings(xml::TextWriter& xhtml)
65 > {
66 >        xml::ScopeElement tr(xhtml, "tr");
67 >        ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" };
68 >
69 >        _foru (index, 0, sizeof (headings) / sizeof (ext::String))
70 >        {
71 >                xml::ScopeElement th(xhtml, "th");
72 >
73 >                xhtml.OutputText(headings[index]);
74 >        }
75 > }
76 >
77 > void Represent::form(xml::TextWriter& xhtml)
78 > {
79 >        xml::ScopeElement tr(xhtml, "tr");
80 >
81 >        {
82 >                xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
83 >
84 >                xhtml.SetAttribute("name", "type");
85 >
86 >                DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast<unsigned>(cgi.find("type")->second)) : DataType::TYPE_bool);
87 >
88 >                _foreach (ext::Vector<DataType>, type_, DataType::enumerate())
89 >                {
90 >                        xml::ScopeElement option(xhtml, "option");
91 >
92 >                        if (*type_ == type) xhtml.SetAttribute("selected", "selected");
93 >
94 >                        xhtml.SetAttribute("value", lexical_cast<ext::String>(DataType::Type(*type_)));
95 >                        xhtml.OutputText(*type_);
96 >                }
97 >        }
98 >
99 >        {
100 >                xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
101 >
102 >                xhtml.SetAttribute("name", "data");
103 >                xhtml.SetAttribute("size", "64");
104 >                xhtml.SetAttribute("type", "text");
105 >
106 >                ext::String data(cgi.find("data") != cgi.end() ? cgi.find("data")->second : std::string());
107 >
108 >                xhtml.SetAttribute("value", data);
109 >        }
110 >
111 >        {
112 >                xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
113  
114 <        api::Cerr << string << ios::NewLine << ext::String(sign) << ios::NewLine << ext::String(unsign) << ios::NewLine;
114 >                ext::String inputs[] = { "Binary", "Hexadecimal", "std::istream", "ios::PrintReader" };
115 >                unsigned input(cgi.find("input") != cgi.end() ? lexical_cast<unsigned>(cgi.find("input")->second) : 0);
116  
117 <        string = "deadbeef";
117 >                _foru (input_, 0, sizeof (inputs) / sizeof (ext::String))
118 >                {
119 >                        xml::ScopeElement option(xhtml, "option");
120  
121 <        Hexadecimal test(string, false);
121 >                        if (input_ == input) xhtml.SetAttribute("selected", "selected");
122  
123 <        api::Cerr << string << ios::NewLine << ext::String(test) << ios::NewLine;
123 >                        xhtml.SetAttribute("value", lexical_cast<ext::String>(input_));
124 >                        xhtml.OutputText(inputs[input_]);
125 >                }
126 >        }
127  
128 <        Hexadecimal sign_(sign), unsign_(unsign);
128 >        xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
129  
130 <        api::Cerr << ext::String(sign_) << ios::NewLine << ext::String(unsign_) << ios::NewLine;
130 >        xhtml.SetAttribute("type", "submit");
131 >        xhtml.SetAttribute("value", "Store");
132   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines