// Represent // // Douglas Thrift // // $Id$ #include "Hexadecimal.hpp" #include "DataType.hpp" #include #include struct Environment { ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } } } env; int Main(const app::Options& options) { Represent represent; return 0; } Represent::Represent() { parse(); api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; xml::TextWriter xhtml(api::Cout); xml::ScopeElement table(xhtml, "table"); headings(xhtml); form(xhtml); // } 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[] = { "Binary", "Hexadecimal", "std::istream", "ios::PrintReader" }; unsigned input(cgi.find("input") != cgi.end() ? lexical_cast(cgi.find("input")->second) : 0); _foru (input_, 0, sizeof (inputs) / sizeof (ext::String)) { xml::ScopeElement option(xhtml, "option"); if (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"); }