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