4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
< |
#include <iostream> |
8 |
< |
#include <string> |
9 |
< |
#include <vector> |
7 |
> |
#include "Hexadecimal.hpp" |
8 |
|
|
9 |
< |
#include <foreach.hpp> |
9 |
> |
#include <menes-api/environment.hpp> |
10 |
> |
#include <menes-app/simple.hpp> |
11 |
|
|
12 |
< |
class Binary |
12 |
> |
struct Environment |
13 |
|
{ |
14 |
< |
private: |
15 |
< |
std::vector<char> bytes; |
17 |
< |
public: |
18 |
< |
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 |
< |
}; |
14 |
> |
ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } } |
15 |
> |
} env; |
16 |
|
|
17 |
< |
Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0) |
17 |
> |
int Main(const app::Options& options) |
18 |
|
{ |
19 |
< |
std::string::size_type off(string.size() % 8); |
19 |
> |
Represent represent; |
20 |
|
|
21 |
< |
if (off != 0) |
31 |
< |
{ |
32 |
< |
bytes.push_back(0); |
33 |
< |
string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); |
34 |
< |
} |
35 |
< |
|
36 |
< |
std::string::size_type index(0); |
37 |
< |
|
38 |
< |
_rmforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit; |
39 |
< |
} |
40 |
< |
|
41 |
< |
template <typename Type> |
42 |
< |
Binary::Binary(const Type& type) : bytes(sizeof (type)) |
43 |
< |
{ |
44 |
< |
char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type))); |
45 |
< |
|
46 |
< |
_mforeach (std::vector<char>, byte, bytes) *byte = *type_++; |
21 |
> |
return 0; |
22 |
|
} |
23 |
|
|
24 |
< |
template <typename Type> |
50 |
< |
Type Binary::convert(bool signed_) |
24 |
> |
Represent::Represent() |
25 |
|
{ |
26 |
< |
Type type; |
53 |
< |
|
54 |
< |
if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); |
55 |
< |
|
56 |
< |
char* type_(reinterpret_cast<char*>(&type)); |
57 |
< |
|
58 |
< |
_foreach (std::vector<char>, byte, bytes) *type_++ = *byte; |
26 |
> |
parse(); |
27 |
|
|
28 |
< |
return type; |
61 |
< |
} |
28 |
> |
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; |
29 |
|
|
30 |
< |
Binary::operator std::string() const |
31 |
< |
{ |
65 |
< |
std::string string; |
30 |
> |
xml::TextWriter xhtml(api::Cout); |
31 |
> |
xml::ScopeElement table(xhtml, "table"); |
32 |
|
|
33 |
< |
_rforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; |
33 |
> |
{ |
34 |
> |
xml::ScopeElement tr(xhtml, "tr"); |
35 |
> |
ext::String headings[] = { "Type", "Data Representation", "Storage" }; |
36 |
|
|
37 |
< |
return string; |
38 |
< |
} |
37 |
> |
_foru (index, 0, sizeof (headings) / sizeof (ext::String)) |
38 |
> |
{ |
39 |
> |
xml::ScopeElement th(xhtml, "th"); |
40 |
|
|
41 |
< |
inline std::ostream& operator<<(std::ostream& out, const Binary& binary) |
42 |
< |
{ |
43 |
< |
return out << std::string(binary); |
41 |
> |
xhtml.OutputText(headings[index]); |
42 |
> |
} |
43 |
> |
} |
44 |
|
} |
45 |
|
|
46 |
< |
int main(int argc, char* argv[]) |
46 |
> |
void Represent::parse() |
47 |
|
{ |
48 |
< |
std::string hello("Hello, World!"); |
48 |
> |
ext::String query(env.get("QUERY_STRING")); |
49 |
|
|
50 |
< |
_foreach (std::string, atom, hello) |
50 |
> |
if (env.get("REQUEST_METHOD") == "POST") |
51 |
|
{ |
52 |
< |
std::cout << Binary(*atom) << " = " << *atom << std::endl; |
84 |
< |
} |
52 |
> |
ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH"))); |
53 |
|
|
54 |
< |
_fori (index, -10, 11) |
87 |
< |
{ |
88 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
89 |
< |
} |
54 |
> |
api::Cin.ReadFully(content.Begin(), content.GetSize()); |
55 |
|
|
56 |
< |
_foru (index, -10, 11) |
92 |
< |
{ |
93 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
56 |
> |
query = content; |
57 |
|
} |
58 |
|
|
59 |
< |
for (float index(-1.0); index < 1.0; index += 0.1) |
97 |
< |
{ |
98 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
99 |
< |
} |
59 |
> |
ext::Vector<ext::String> pairs(query.Split('&')); |
60 |
|
|
61 |
< |
for (double index(-1.0); index < 1.0; index += 0.1) |
61 |
> |
_foreach (ext::Vector<ext::String>, pair, pairs) |
62 |
|
{ |
63 |
< |
std::cout << Binary(index) << " = " << index << std::endl; |
64 |
< |
} |
105 |
< |
|
106 |
< |
std::string string("101001101001"); |
107 |
< |
Binary binary(string, true); |
108 |
< |
|
109 |
< |
std::cout << binary << " = " << string << std::endl; |
110 |
< |
|
111 |
< |
float value(binary.convert<float>()); |
63 |
> |
ext::String::ConstIterator equal(pair->FindFirst('=')); |
64 |
> |
ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End()); |
65 |
|
|
66 |
< |
std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; |
67 |
< |
|
115 |
< |
return 0; |
66 |
> |
cgi.insert(std::pair<std::string, std::string>(name, value)); |
67 |
> |
} |
68 |
|
} |