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 |
> |
#include <menes-app/simple.hpp> |
11 |
|
|
12 |
< |
/*template <typename Type> |
14 |
< |
std::string binary(const Type& type) |
15 |
< |
{ |
16 |
< |
std::ostringstream string; |
17 |
< |
|
18 |
< |
_rforu (index, 0, 8 * sizeof (type)) |
19 |
< |
{ |
20 |
< |
bool bit(1 & type >> index); |
21 |
< |
|
22 |
< |
string << bit; |
23 |
< |
} |
24 |
< |
|
25 |
< |
return string.str(); |
26 |
< |
}*/ |
12 |
> |
Environment env; |
13 |
|
|
14 |
< |
class Binary |
14 |
> |
int Main(const app::Options& options) |
15 |
|
{ |
16 |
< |
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 |
< |
}; |
16 |
> |
Represent represent; |
17 |
|
|
18 |
< |
Binary::Binary(const std::string& string) |
40 |
< |
{ |
41 |
< |
throw; |
18 |
> |
return 0; |
19 |
|
} |
20 |
|
|
21 |
< |
template <typename Type> |
45 |
< |
Binary::Binary(const Type& type) : bytes(sizeof (type)) |
21 |
> |
Represent::Represent() |
22 |
|
{ |
23 |
< |
char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type))); |
23 |
> |
parse(); |
24 |
> |
|
25 |
> |
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; |
26 |
> |
|
27 |
> |
xml::TextWriter xhtml(api::Cout); |
28 |
> |
xml::ScopeElement table(xhtml, "table"); |
29 |
> |
|
30 |
> |
headings(xhtml); |
31 |
> |
form(xhtml); |
32 |
|
|
33 |
< |
_mforeach (std::vector<char>, byte, bytes) *byte = *type_++; |
33 |
> |
// |
34 |
|
} |
35 |
|
|
36 |
< |
Binary::operator std::string() const |
36 |
> |
void Represent::parse() |
37 |
|
{ |
38 |
< |
std::string string; |
38 |
> |
ext::String query(env.get("QUERY_STRING")); |
39 |
|
|
40 |
< |
_rforeach (std::vector<char>, byte, bytes) |
40 |
> |
if (env.get("REQUEST_METHOD") == "POST") |
41 |
|
{ |
42 |
< |
_rfor (char, bit, 0, 8) |
43 |
< |
{ |
44 |
< |
string += 1 & *byte >> bit ? '1' : '0'; |
45 |
< |
} |
42 |
> |
ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH"))); |
43 |
> |
|
44 |
> |
api::Cin.ReadFully(content.Begin(), content.GetSize()); |
45 |
> |
|
46 |
> |
query = content; |
47 |
|
} |
48 |
|
|
49 |
< |
return string; |
65 |
< |
} |
49 |
> |
ext::Vector<ext::String> pairs(query.Split('&')); |
50 |
|
|
51 |
< |
std::ostream& operator<<(std::ostream& out, const Binary& binary) |
52 |
< |
{ |
53 |
< |
return out << std::string(binary); |
51 |
> |
_foreach (ext::Vector<ext::String>, pair, pairs) |
52 |
> |
{ |
53 |
> |
ext::String::ConstIterator equal(pair->FindFirst('=')); |
54 |
> |
ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End()); |
55 |
> |
|
56 |
> |
cgi.insert(std::pair<std::string, std::string>(name, value)); |
57 |
> |
} |
58 |
|
} |
59 |
|
|
60 |
< |
int main(int argc, char* argv[]) |
60 |
> |
void Represent::headings(xml::TextWriter& xhtml) |
61 |
|
{ |
62 |
< |
std::string hello("Hello, World!"); |
62 |
> |
xml::ScopeElement tr(xhtml, "tr"); |
63 |
> |
ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" }; |
64 |
|
|
65 |
< |
_foreach (std::string, atom, hello) |
65 |
> |
_foru (index, 0, sizeof (headings) / sizeof (ext::String)) |
66 |
|
{ |
67 |
< |
std::cout << Binary(*atom) << " = " << *atom << std::endl; |
79 |
< |
} |
67 |
> |
xml::ScopeElement th(xhtml, "th"); |
68 |
|
|
69 |
< |
_fori (index, -10, 11) |
82 |
< |
{ |
83 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
69 |
> |
xhtml.OutputText(headings[index]); |
70 |
|
} |
71 |
+ |
} |
72 |
+ |
|
73 |
+ |
void Represent::form(xml::TextWriter& xhtml) |
74 |
+ |
{ |
75 |
+ |
xml::ScopeElement tr(xhtml, "tr"); |
76 |
|
|
86 |
– |
_foru (index, -10, 11) |
77 |
|
{ |
78 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
78 |
> |
xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); |
79 |
> |
|
80 |
> |
xhtml.SetAttribute("name", "type"); |
81 |
> |
|
82 |
> |
DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast<unsigned>(cgi.find("type")->second)) : DataType::TYPE_bool); |
83 |
> |
|
84 |
> |
_foreach (ext::Vector<DataType>, type_, DataType::enumerate()) |
85 |
> |
{ |
86 |
> |
xml::ScopeElement option(xhtml, "option"); |
87 |
> |
|
88 |
> |
if (*type_ == type) xhtml.SetAttribute("selected", "selected"); |
89 |
> |
|
90 |
> |
xhtml.SetAttribute("value", lexical_cast<ext::String>(DataType::Type(*type_))); |
91 |
> |
xhtml.OutputText(*type_); |
92 |
> |
} |
93 |
|
} |
94 |
|
|
91 |
– |
for (float index(-1.0); index < 1.0; index += 0.1) |
95 |
|
{ |
96 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
96 |
> |
xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); |
97 |
> |
|
98 |
> |
xhtml.SetAttribute("name", "data"); |
99 |
> |
xhtml.SetAttribute("size", "64"); |
100 |
> |
xhtml.SetAttribute("type", "text"); |
101 |
> |
|
102 |
> |
ext::String data(cgi.find("data") != cgi.end() ? cgi.find("data")->second : std::string()); |
103 |
> |
|
104 |
> |
xhtml.SetAttribute("value", data); |
105 |
|
} |
106 |
|
|
96 |
– |
for (double index(-1.0); index < 1.0; index += 0.1) |
107 |
|
{ |
108 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
108 |
> |
xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); |
109 |
> |
|
110 |
> |
ext::String inputs[] = { "Binary", "Hexadecimal", "std::istream", "ios::PrintReader" }; |
111 |
> |
unsigned input(cgi.find("input") != cgi.end() ? lexical_cast<unsigned>(cgi.find("input")->second) : 0); |
112 |
> |
|
113 |
> |
_foru (input_, 0, sizeof (inputs) / sizeof (ext::String)) |
114 |
> |
{ |
115 |
> |
xml::ScopeElement option(xhtml, "option"); |
116 |
> |
|
117 |
> |
if (input_ == input) xhtml.SetAttribute("selected", "selected"); |
118 |
> |
|
119 |
> |
xhtml.SetAttribute("value", lexical_cast<ext::String>(input_)); |
120 |
> |
xhtml.OutputText(inputs[input_]); |
121 |
> |
} |
122 |
|
} |
123 |
|
|
124 |
< |
return 0; |
124 |
> |
xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); |
125 |
> |
|
126 |
> |
xhtml.SetAttribute("type", "submit"); |
127 |
> |
xhtml.SetAttribute("value", "Store"); |
128 |
|
} |