--- Represent/represent.cpp 2004/12/21 09:12:18 362 +++ Represent/Represent.cpp 2004/12/23 09:40:10 384 @@ -4,99 +4,125 @@ // // $Id$ -#include -#include -#include +#include "Hexadecimal.hpp" +#include "DataType.hpp" -#include +#include -/*template -std::string binary(const Type& type) -{ - std::ostringstream string; - - _rforu (index, 0, 8 * sizeof (type)) - { - bool bit(1 & type >> index); - - string << bit; - } - - return string.str(); -}*/ +Environment env; -class Binary +int Main(const app::Options& options) { -private: - std::vector bytes; -public: - Binary(const std::string& string); - template - Binary(const Type& type); - operator std::string() const; -}; + Represent represent; -Binary::Binary(const std::string& string) -{ - throw; + return 0; } -template -Binary::Binary(const Type& type) : bytes(sizeof (type)) +Represent::Represent() { - char* type_(reinterpret_cast(const_cast(&type))); + 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); - _mforeach (std::vector, byte, bytes) *byte = *type_++; + // } -Binary::operator std::string() const +void Represent::parse() { - std::string string; + ext::String query(env.get("QUERY_STRING")); - _rforeach (std::vector, byte, bytes) + if (env.get("REQUEST_METHOD") == "POST") { - _rfor (char, bit, 0, 8) - { - string += 1 & *byte >> bit ? '1' : '0'; - } + ext::Buffer content(lexical_cast(env.get("CONTENT_LENGTH"))); + + api::Cin.ReadFully(content.Begin(), content.GetSize()); + + query = content; } - return string; -} + ext::Vector pairs(query.Split('&')); -std::ostream& operator<<(std::ostream& out, const Binary& binary) -{ - return out << std::string(binary); + _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)); + } } -int main(int argc, char* argv[]) +void Represent::headings(xml::TextWriter& xhtml) { - std::string hello("Hello, World!"); + xml::ScopeElement tr(xhtml, "tr"); + ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" }; - _foreach (std::string, atom, hello) + _foru (index, 0, sizeof (headings) / sizeof (ext::String)) { - std::cout << Binary(*atom) << " = " << *atom << std::endl; - } + xml::ScopeElement th(xhtml, "th"); - _fori (index, -10, 11) - { - std::cout << Binary(index) << " = " << index << std::endl; + xhtml.OutputText(headings[index]); } +} + +void Represent::form(xml::TextWriter& xhtml) +{ + xml::ScopeElement tr(xhtml, "tr"); - _foru (index, -10, 11) { - std::cout << Binary(index) << " = " << index << std::endl; + 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_); + } } - for (float index(-1.0); index < 1.0; index += 0.1) { - std::cout << Binary(index) << " = " << index << std::endl; + 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); } - for (double index(-1.0); index < 1.0; index += 0.1) { - std::cout << Binary(index) << " = " << index << std::endl; + 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_]); + } } - return 0; + xml::ScopeElement td(xhtml, "td"), input(xhtml, "input"); + + xhtml.SetAttribute("type", "submit"); + xhtml.SetAttribute("value", "Store"); }