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 366 by douglas, 2004-12-21T03:22:38-08:00 vs.
Revision 385 by douglas, 2004-12-23T21:01:12-08:00

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

Comparing Represent/represent.cpp (property svn:eol-style):
Revision 366 by douglas, 2004-12-21T03:22:38-08:00 vs.
Revision 385 by douglas, 2004-12-23T21:01:12-08:00

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines