--- Represent/Represent.cpp 2004/12/22 07:19:54 371 +++ Represent/Represent.cpp 2004/12/24 05:01:12 385 @@ -5,6 +5,7 @@ // $Id$ #include "Hexadecimal.hpp" +#include "DataType.hpp" #include @@ -17,18 +18,156 @@ int Main(const app::Options& options) Represent::Represent() { - ext::String string("1001"); - Binary sign(string, true), unsign(string, false); + parse(); - api::Cerr << string << ios::NewLine << ext::String(sign) << ios::NewLine << ext::String(unsign) << ios::NewLine; + api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; - string = "deadbeef"; + xml::TextWriter xhtml(api::Cout); + xml::ScopeElement table(xhtml, "table"); - Hexadecimal test(string, false); + headings(xhtml); + form(xhtml); + output(xhtml); +} + +struct Represent::Item +{ + DataType type; + ext::String data; + Input input; + Item(const DataType& type, const ext::String& data, Input input) : type(type), data(data), input(input) {} +}; + +void Represent::parse() +{ + ext::String query(env.get("QUERY_STRING")); + + if (env.get("REQUEST_METHOD") == "POST") + { + ext::Buffer content(lexical_cast(env.get("CONTENT_LENGTH"))); + + api::Cin.ReadFully(content.Begin(), content.GetSize()); + + query = content; + } + + ext::Vector pairs(query.Split('&')); + + _foreach (ext::Vector, pair, pairs) + { + ext::String::ConstIterator equal(pair->FindFirst('=')); + ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End()); + + cgi.insert(std::pair(name, value)); + } +} + +void Represent::headings(xml::TextWriter& xhtml) +{ + xml::ScopeElement tr(xhtml, "tr"); + ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" }; + + _foru (index, 0, sizeof (headings) / sizeof (ext::String)) + { + xml::ScopeElement th(xhtml, "th"); + + xhtml.OutputText(headings[index]); + } +} + +void Represent::form(xml::TextWriter& xhtml) +{ + xml::ScopeElement tr(xhtml, "tr"); + + { + xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); + + xhtml.SetAttribute("name", "type"); + + DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast(cgi.find("type")->second)) : DataType::TYPE_bool); + + _foreach (ext::Vector, type_, DataType::enumerate()) + { + xml::ScopeElement option(xhtml, "option"); + + if (*type_ == type) xhtml.SetAttribute("selected", "selected"); + + xhtml.SetAttribute("value", lexical_cast(DataType::Type(*type_))); + xhtml.OutputText(*type_); + } + } + + { + xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); + + xhtml.SetAttribute("name", "data"); + xhtml.SetAttribute("size", "64"); + xhtml.SetAttribute("type", "text"); + + ext::String data(cgi.find("data") != cgi.end() ? cgi.find("data")->second : std::string()); + + xhtml.SetAttribute("value", data); + } + + { + xml::ScopeElement td(xhtml, "td"), select(xhtml, "select"); + + ext::String inputs[] = { "Normal", "Binary", "Hexadecimal" }; + Input input(cgi.find("input") != cgi.end() ? Input(lexical_cast(cgi.find("input")->second)) : INPUT_Normal); + + _foru (input_, INPUT_Normal, INPUT_Hexadecimal + 1) + { + xml::ScopeElement option(xhtml, "option"); + + if (Input(input_) == input) xhtml.SetAttribute("selected", "selected"); + + xhtml.SetAttribute("value", lexical_cast(input_)); + xhtml.OutputText(inputs[input_]); + } + } + + xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); + + xhtml.SetAttribute("type", "submit"); + xhtml.SetAttribute("value", "Store"); +} + +void Represent::output(xml::TextWriter& xhtml) +{ + typedef std::multimap::size_type MultiMapSize; + + std::set > count; + ext::String names[] = { "type", "data", "input" }; + + _foru (index, 0, sizeof (names) / sizeof (ext::String)) count.insert(cgi.count(names[index])); + + typedef std::multimap::const_iterator MultiMapConstIterator; + + 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")); + ext::Vector items; + + _foru (index, 0, *count.begin()) + { + Item item(type != type_ ? DataType::Type(lexical_cast(type->second)) : DataType::TYPE_bool, data != data_ ? data->second : std::string(), input != input_ ? Input(lexical_cast(input->second)) : INPUT_Normal); + + items.InsertLast(item); + + if (type != type_) ++type; + if (data != data_) ++data; + if (input != input_) ++input; + } + + _rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast(delete_->second)); - api::Cerr << string << ios::NewLine << ext::String(test) << ios::NewLine; + _foreach (ext::Vector, item, items) switch (item->type) + { + case DataType::TYPE_bool: + output(xhtml, *item); - Hexadecimal sign_(sign), unsign_(unsign); + break; + case DataType::TYPE_char: + output(xhtml, *item); - api::Cerr << ext::String(sign_) << ios::NewLine << ext::String(unsign_) << ios::NewLine; + break; + } }