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