ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Represent/Represent.cpp
(Generate patch)

Comparing Represent/represent.cpp (file contents):
Revision 368 by douglas, 2004-12-21T03:26:56-08:00 vs.
Revision 380 by douglas, 2004-12-23T01:07:33-08:00

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

Comparing Represent/represent.cpp (property svn:eol-style):
Revision 368 by douglas, 2004-12-21T03:26:56-08:00 vs.
Revision 380 by douglas, 2004-12-23T01:07:33-08:00

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines