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 389 by douglas, 2004-12-24T04:02:30-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;
7 > #include "Hexadecimal.hpp"
8 > #include "DataType.hpp"
9 > #include "InputType.hpp"
10 >
11 > #ifdef _WIN32
12 > #pragma warning(disable:4267 4288)
13 > #endif
14 >
15 > #include <menes-app/simple.hpp>
16 > #include <menes-xml/document.hpp>
17 > #include <menes-xml/nodeset.hpp>
18 > #include <menes-xml/parse.hpp>
19 >
20 > struct Environment
21 > {
22 >        ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } }
23 > } env;
24 >
25 > struct Item
26 > {
27 >        DataType type;
28 >        ext::String data;
29 >        InputType input;
30 >        Item(const DataType& type, const ext::String& data, const InputType& input_) : type(type), data(data), input(input_) {}
31   };
32  
33 < Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0)
33 > int Main(const app::Options& options)
34   {
35 <        std::string::size_type off(string.size() % 8);
35 >        Represent represent;
36 >
37 >        return 0;
38 > }
39 >
40 > Represent::Represent()
41 > {
42 >        api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush;
43 >
44 >        xml::TextWriter xhtml(api::Cout);
45 >
46 >        parse();
47 >
48 >        _H<xml::Document> document(xml::Parse("represent.xml"));
49 >        _H<xml::Node> node(*document/"represent");
50 >        ext::String before(*node/"before"), after(*node/"after");
51 >
52 >        api::Cout << ios::NewLine << before << ios::Flush;
53  
30        if (off != 0)
54          {
55 <                bytes.push_back(0);
56 <                string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0');
55 >                xml::ScopeElement table(xhtml, "table");
56 >
57 >                headings(xhtml);
58 >                form(xhtml);
59 >                output(xhtml);
60          }
61  
62 <        std::string::size_type index(0);
62 >        api::Cout << after << ios::Flush;
63 > }
64 >
65 > void Represent::parse()
66 > {
67 >        ext::String query(env.get("QUERY_STRING"));
68 >
69 >        if (env.get("REQUEST_METHOD") == "POST")
70 >        {
71 >                ext::Buffer content(lexical_cast<size_t>(env.get("CONTENT_LENGTH")));
72  
73 <        _rmforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit;
73 >                api::Cin.ReadFully(content.Begin(), content.GetSize());
74 >
75 >                query = content;
76 >        }
77 >
78 > //      api::Cout << query << ios::NewLine;
79 >
80 >        ext::Vector<ext::String> pairs(query.Split('&'));
81 >
82 >        _foreach (ext::Vector<ext::String>, pair, pairs)
83 >        {
84 >                ext::String::ConstIterator equal(pair->FindFirst('='));
85 >                ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End());
86 >
87 >                // XXX: clean up %20, etc.
88 >
89 >                cgi.insert(std::pair<std::string, std::string>(name, value));
90 >        }
91 > }
92 >
93 > void Represent::headings(xml::TextWriter& xhtml)
94 > {
95 >        xml::ScopeElement tr(xhtml, "tr");
96 >        ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" };
97 >
98 >        _foru (index, 0, sizeof (headings) / sizeof (ext::String))
99 >        {
100 >                xml::ScopeElement th(xhtml, "th");
101 >
102 >                xhtml.OutputText(headings[index]);
103 >        }
104 > }
105 >
106 > void Represent::form(xml::TextWriter& xhtml)
107 > {
108 >        xml::ScopeElement tr(xhtml, "tr");
109 >
110 >        {
111 >                xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
112 >
113 >                xhtml.SetAttribute("name", "type");
114 >
115 >                DataType type(cgi.find("type") != cgi.end() ? cgi.find("type")->second : std::string());
116 >
117 >                _foreach (ext::Vector<DataType>, type_, DataType::enumerate())
118 >                {
119 >                        xml::ScopeElement option(xhtml, "option");
120 >
121 >                        if (*type_ == type) xhtml.SetAttribute("selected", "selected");
122 >
123 >                        xhtml.OutputText(*type_);
124 >                }
125 >        }
126 >
127 >        {
128 >                xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
129 >
130 >                xhtml.SetAttribute("name", "data");
131 >                xhtml.SetAttribute("size", "64");
132 >                xhtml.SetAttribute("type", "text");
133 >
134 >                ext::String data(cgi.find("data") != cgi.end() ? cgi.find("data")->second : std::string());
135 >
136 >                xhtml.SetAttribute("value", data);
137 >        }
138 >
139 >        {
140 >                xml::ScopeElement td(xhtml, "td"), select(xhtml, "select");
141 >
142 >                xhtml.SetAttribute("name", "input");
143 >
144 >                InputType input(cgi.find("input") != cgi.end() ? cgi.find("input")->second : std::string());
145 >
146 >                _foreach (ext::Vector<InputType>, input_, InputType::enumerate())
147 >                {
148 >                        xml::ScopeElement option(xhtml, "option");
149 >
150 >                        if (*input_ == input) xhtml.SetAttribute("selected", "selected");
151 >
152 >                        xhtml.OutputText(*input_);
153 >                }
154 >        }
155 >
156 >        xml::ScopeElement td(xhtml, "td"), input(xhtml, "input");
157 >
158 >        xhtml.SetAttribute("type", "submit");
159 >        xhtml.SetAttribute("value", "Store");
160 > }
161 >
162 > void Represent::output(xml::TextWriter& xhtml)
163 > {
164 >        typedef std::multimap<std::string, std::string>::size_type MultiMapSize;
165 >
166 >        std::set<MultiMapSize, std::greater<MultiMapSize> > count;
167 >        ext::String names[] = { "type", "data", "input" };
168 >
169 >        _foru (index, 0, sizeof (names) / sizeof (ext::String)) count.insert(cgi.count(names[index]));
170 >
171 >        typedef std::multimap<std::string, std::string>::const_iterator MultiMapConstIterator;
172 >
173 >        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"));
174 >        ext::Vector<Item> items;
175 >
176 >        _foru (index, 0, *count.begin())
177 >        {
178 >                Item item(DataType(type != type_ ? type->second : std::string()), data != data_ ? data->second : std::string(), InputType(input != input_ ? input->second : std::string()));
179 >
180 >                items.InsertLast(item);
181 >
182 >                if (type != type_) ++type;
183 >                if (data != data_) ++data;
184 >                if (input != input_) ++input;
185 >        }
186 >
187 >        _rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast<size_t>(delete_->second));
188 >
189 >        size_t index(0);
190 >
191 >        _foreach (ext::Vector<Item>, item, items) switch (item->type)
192 >        {
193 >        case DataType::TYPE_bool:
194 >                output<bool>(xhtml, *item, ++index);
195 >                break;
196 >        case DataType::TYPE_char:
197 >                output<char>(xhtml, *item, ++index);
198 >                break;
199 >        case DataType::TYPE_short:
200 >                output<short>(xhtml, *item, ++index);
201 >                break;
202 >        case DataType::TYPE_unsigned_short:
203 >                output<unsigned short>(xhtml, *item, ++index);
204 >                break;
205 >        case DataType::TYPE_int:
206 >                output<int>(xhtml, *item, ++index);
207 >                break;
208 >        case DataType::TYPE_unsigned_int:
209 >                output<unsigned int>(xhtml, *item, ++index);
210 >                break;
211 >        case DataType::TYPE_long:
212 >                output<long>(xhtml, *item, ++index);
213 >                break;
214 >        case DataType::TYPE_unsigned_long:
215 >                output<unsigned long>(xhtml, *item, ++index);
216 >                break;
217 >        case DataType::TYPE_float:
218 >                output<float>(xhtml, *item, ++index);
219 >                break;
220 >        case DataType::TYPE_double:
221 >                output<double>(xhtml, *item, ++index);
222 >                break;
223 >        case DataType::TYPE_std_string:
224 >                output<std::string>(xhtml, *item, ++index);
225 >                break;
226 >        case DataType::TYPE_ext_String:
227 >                output<ext::String>(xhtml, *item, ++index);
228 >        }
229   }
230  
231   template <typename Type>
232 < Binary::Binary(const Type& type) : bytes(sizeof (type))
232 > void Represent::output(xml::TextWriter& xhtml, const Item& item, size_t index)
233   {
234 <        char* type_(reinterpret_cast<char*>(const_cast<Type*>(&type)));
234 >        xhtml.OpenElement("tr");
235 >
236 >        {
237 >                xml::ScopeElement td(xhtml, "td");
238  
239 <        _mforeach (std::vector<char>, byte, bytes) *byte = *type_++;
239 >                xhtml.SetAttribute("rowspan", "3");
240 >                xhtml.OutputText(item.type);
241 >
242 >                ext::String names[] = { "type", "data", "input" }, values[] = { item.type, item.data, item.input };
243 >
244 >                _foru (index, 0, sizeof (names) / sizeof (ext::String))
245 >                {
246 >                        xml::ScopeElement input(xhtml, "input");
247 >
248 >                        xhtml.SetAttribute("name", names[index]);
249 >                        xhtml.SetAttribute("type", "hidden");
250 >                        xhtml.SetAttribute("value", values[index]);
251 >                }
252 >        }
253 >
254 >        Type type(input<Type>(item));
255 >
256 >        {
257 >                xml::ScopeElement td(xhtml, "td");
258 >
259 >                normal(xhtml, type);
260 >        }
261 >
262 >        {
263 >                xml::ScopeElement th(xhtml, "th");
264 >
265 >                xhtml.OutputText("Normal");
266 >        }
267 >
268 >        {
269 >                xml::ScopeElement td(xhtml, "td");
270 >
271 >                xhtml.SetAttribute("rowspan", "3");
272 >
273 >                {
274 >                        xml::ScopeElement input(xhtml, "input");
275 >
276 >                        xhtml.SetAttribute("name", "delete");
277 >                        xhtml.SetAttribute("type", "checkbox");
278 >                        xhtml.SetAttribute("value", lexical_cast<ext::String>(index));
279 >                }
280 >
281 >                xhtml.OutputText("Delete");
282 >        }
283 >
284 >        xhtml.CloseElement();
285 >        xhtml.OpenElement("tr");
286 >
287 >        {
288 >                xml::ScopeElement td(xhtml, "td");
289 >
290 >                binary(xhtml, type);
291 >        }
292 >
293 >        {
294 >                xml::ScopeElement th(xhtml, "th");
295 >
296 >                xhtml.OutputText("Binary");
297 >        }
298 >
299 >        xhtml.CloseElement();
300 >        xhtml.OpenElement("tr");
301 >
302 >        {
303 >                xml::ScopeElement td(xhtml, "td");
304 >        
305 >                hexadecimal(xhtml, type);
306 >        }
307 >
308 >        {
309 >                xml::ScopeElement th(xhtml, "th");
310 >
311 >                xhtml.OutputText("Hexadecimal");
312 >        }
313 >
314 >        xhtml.CloseElement();
315   }
316  
317   template <typename Type>
318 < Type Binary::convert(bool signed_)
318 > Type Represent::input(const Item& item)
319 > {
320 >        // XXX: implement
321 >        
322 >        return lexical_cast<Type>(item.data);
323 > }
324 >
325 > template <>
326 > bool Represent::input(const Item& item)
327 > {
328 >        // XXX: implement
329 >
330 >        try { return lexical_cast<bool>(item.data); } catch (ext::Exception) { return false; }
331 > }
332 >
333 > template <>
334 > char Represent::input(const Item& item)
335   {
336 <        Type type;
336 >        // XXX: implement
337 >        
338 >        return item.data.First();
339 > }
340  
341 <        if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0);
341 > // XXX: constructing a string from Binary or Hexadecimal seems to dangerous
342 > template <>
343 > std::string Represent::input(const Item& item)
344 > {
345 >        return item.data;
346 > }
347  
348 <        char* type_(reinterpret_cast<char*>(&type));
348 > // XXX: constructing a string from Binary or Hexadecimal seems to dangerous
349 > template <>
350 > ext::String Represent::input(const Item& item)
351 > {
352 >        return item.data;
353 > }
354  
355 <        _foreach (std::vector<char>, byte, bytes) *type_++ = *byte;
355 > template <typename Type>
356 > void Represent::normal(xml::TextWriter& xhtml, const Type& type)
357 > {
358 >        xhtml.OutputText(lexical_cast<ext::String>(type));
359 > }
360  
361 <        return type;
361 > template <>
362 > void Represent::normal(xml::TextWriter& xhtml, const char& char_)
363 > {
364 >        xhtml.OutputText(ext::CodePoint(char_));
365   }
366  
367 < Binary::operator std::string() const
367 > template <>
368 > void Represent::normal(xml::TextWriter& xhtml, const std::string& string)
369   {
370 <        std::string string;
370 >        xhtml.OutputText(string);
371 > }
372  
373 <        _rforeach (std::vector<char>, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0';
373 > template <>
374 > void Represent::normal(xml::TextWriter& xhtml, const ext::String& string)
375 > {
376 >        xhtml.OutputText(string);
377 > }
378  
379 <        return string;
379 > template <typename Type>
380 > void Represent::binary(xml::TextWriter& xhtml, const Type& type)
381 > {
382 >        xhtml.OutputText(Binary(type));
383   }
384  
385 < inline std::ostream& operator<<(std::ostream& out, const Binary& binary)
385 > template <>
386 > void Represent::binary(xml::TextWriter& xhtml, const std::string& string)
387   {
388 <        return out << std::string(binary);
388 >        xhtml.OutputText(Binary(string));
389 >
390 >        xml::ScopeElement(xhtml, "br");
391 >
392 >        _sforeach (std::string, atom, string)
393 >        {
394 >                xml::ScopeElement(xhtml, "br");
395 >
396 >                xhtml.OutputText(Binary(*atom));
397 >        }
398   }
399  
400 < int main(int argc, char* argv[])
400 > template <>
401 > void Represent::binary(xml::TextWriter& xhtml, const ext::String& string)
402   {
403 <        std::string hello("Hello, World!");
403 >        xhtml.OutputText(Binary(string));
404 >
405 >        xml::ScopeElement(xhtml, "br");
406  
407 <        _foreach (std::string, atom, hello) std::cout << Binary(*atom) << " = " << *atom << std::endl;
407 >        _foreach (ext::String, atom, string)
408 >        {
409 >                xml::ScopeElement(xhtml, "br");
410  
411 <        _fori (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl;
411 >                xhtml.OutputText(Binary(*atom));
412 >        }
413 > }
414 >
415 > template <typename Type>
416 > void Represent::hexadecimal(xml::TextWriter& xhtml, const Type& type)
417 > {
418 >        xhtml.OutputText(Hexadecimal(type));
419 > }
420  
421 <        _foru (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl;
421 > template <>
422 > void Represent::hexadecimal(xml::TextWriter& xhtml, const std::string& string)
423 > {
424 >        xhtml.OutputText(Hexadecimal(string));
425  
426 <        for (float index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl;
426 >        xml::ScopeElement(xhtml, "br");
427  
428 <        for (double index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl;
428 >        _sforeach (std::string, atom, string)
429 >        {
430 >                xml::ScopeElement(xhtml, "br");
431  
432 <        std::string string("101001101001");
433 <        Binary binary(string, true);
432 >                xhtml.OutputText(Hexadecimal(string));
433 >        }
434 > }
435  
436 <        std::cout << binary << " = " << string << std::endl;
436 > template <>
437 > void Represent::hexadecimal(xml::TextWriter& xhtml, const ext::String& string)
438 > {
439 >        xhtml.OutputText(Hexadecimal(string));
440  
441 <        float value(binary.convert<float>());
441 >        xml::ScopeElement(xhtml, "br");
442  
443 <        std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl;
443 >        _foreach (ext::String, atom, string)
444 >        {
445 >                xml::ScopeElement(xhtml, "br");
446  
447 <        return 0;
447 >                xhtml.OutputText(Hexadecimal(string));
448 >        }
449   }

Comparing Represent/represent.cpp (property svn:eol-style):
Revision 368 by douglas, 2004-12-21T03:26:56-08:00 vs.
Revision 389 by douglas, 2004-12-24T04:02:30-08:00

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines