ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Represent.cpp
Revision: 385
Committed: 2004-12-23T21:01:12-08:00 (20 years, 5 months ago) by douglas
File size: 4606 byte(s)
Log Message:
This going to be easier to finish in VC++.

File Contents

# Content
1 // Represent
2 //
3 // Douglas Thrift
4 //
5 // $Id$
6
7 #include "Hexadecimal.hpp"
8 #include "DataType.hpp"
9
10 #include <menes-app/simple.hpp>
11
12 int Main(const app::Options& options)
13 {
14 Represent represent;
15
16 return 0;
17 }
18
19 Represent::Represent()
20 {
21 parse();
22
23 api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush;
24
25 xml::TextWriter xhtml(api::Cout);
26 xml::ScopeElement table(xhtml, "table");
27
28 headings(xhtml);
29 form(xhtml);
30 output(xhtml);
31 }
32
33 struct Represent::Item
34 {
35 DataType type;
36 ext::String data;
37 Input input;
38 Item(const DataType& type, const ext::String& data, Input input) : type(type), data(data), input(input) {}
39 };
40
41 void Represent::parse()
42 {
43 ext::String query(env.get("QUERY_STRING"));
44
45 if (env.get("REQUEST_METHOD") == "POST")
46 {
47 ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH")));
48
49 api::Cin.ReadFully(content.Begin(), content.GetSize());
50
51 query = content;
52 }
53
54 ext::Vector<ext::String> pairs(query.Split('&'));
55
56 _foreach (ext::Vector<ext::String>, pair, pairs)
57 {
58 ext::String::ConstIterator equal(pair->FindFirst('='));
59 ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End());
60
61 cgi.insert(std::pair<std::string, std::string>(name, value));
62 }
63 }
64
65 void Represent::headings(xml::TextWriter& xhtml)
66 {
67 xml::ScopeElement tr(xhtml, "tr");
68 ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" };
69
70 _foru (index, 0, sizeof (headings) / sizeof (ext::String))
71 {
72 xml::ScopeElement th(xhtml, "th");
73
74 xhtml.OutputText(headings[index]);
75 }
76 }
77
78 void Represent::form(xml::TextWriter& xhtml)
79 {
80 xml::ScopeElement tr(xhtml, "tr");
81
82 {
83 xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
84
85 xhtml.SetAttribute("name", "type");
86
87 DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast<unsigned>(cgi.find("type")->second)) : DataType::TYPE_bool);
88
89 _foreach (ext::Vector<DataType>, type_, DataType::enumerate())
90 {
91 xml::ScopeElement option(xhtml, "option");
92
93 if (*type_ == type) xhtml.SetAttribute("selected", "selected");
94
95 xhtml.SetAttribute("value", lexical_cast<ext::String>(DataType::Type(*type_)));
96 xhtml.OutputText(*type_);
97 }
98 }
99
100 {
101 xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
102
103 xhtml.SetAttribute("name", "data");
104 xhtml.SetAttribute("size", "64");
105 xhtml.SetAttribute("type", "text");
106
107 ext::String data(cgi.find("data") != cgi.end() ? cgi.find("data")->second : std::string());
108
109 xhtml.SetAttribute("value", data);
110 }
111
112 {
113 xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
114
115 ext::String inputs[] = { "Normal", "Binary", "Hexadecimal" };
116 Input input(cgi.find("input") != cgi.end() ? Input(lexical_cast<unsigned>(cgi.find("input")->second)) : INPUT_Normal);
117
118 _foru (input_, INPUT_Normal, INPUT_Hexadecimal + 1)
119 {
120 xml::ScopeElement option(xhtml, "option");
121
122 if (Input(input_) == input) xhtml.SetAttribute("selected", "selected");
123
124 xhtml.SetAttribute("value", lexical_cast<ext::String>(input_));
125 xhtml.OutputText(inputs[input_]);
126 }
127 }
128
129 xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
130
131 xhtml.SetAttribute("type", "submit");
132 xhtml.SetAttribute("value", "Store");
133 }
134
135 void Represent::output(xml::TextWriter& xhtml)
136 {
137 typedef std::multimap<std::string, std::string>::size_type MultiMapSize;
138
139 std::set<MultiMapSize, std::greater<MultiMapSize> > count;
140 ext::String names[] = { "type", "data", "input" };
141
142 _foru (index, 0, sizeof (names) / sizeof (ext::String)) count.insert(cgi.count(names[index]));
143
144 typedef std::multimap<std::string, std::string>::const_iterator MultiMapConstIterator;
145
146 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"));
147 ext::Vector<Item> items;
148
149 _foru (index, 0, *count.begin())
150 {
151 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);
152
153 items.InsertLast(item);
154
155 if (type != type_) ++type;
156 if (data != data_) ++data;
157 if (input != input_) ++input;
158 }
159
160 _rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast<size_t>(delete_->second));
161
162 _foreach (ext::Vector<Item>, item, items) switch (item->type)
163 {
164 case DataType::TYPE_bool:
165 output<bool>(xhtml, *item);
166
167 break;
168 case DataType::TYPE_char:
169 output<char>(xhtml, *item);
170
171 break;
172 }
173 }

Properties

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