--- Represent/represent.cpp 2004/12/21 09:15:40 363 +++ Represent/Represent.cpp 2004/12/24 05:08:56 386 @@ -4,84 +4,174 @@ // // $Id$ -#include -#include -#include - -#include - -class Binary -{ -private: - std::vector bytes; -public: - Binary(const std::string& string); - template - Binary(const Type& type); - operator std::string() const; -}; +#include "Hexadecimal.hpp" +#include "DataType.hpp" + +#ifdef _WIN32 +#pragma warning(disable:4267) +#endif -Binary::Binary(const std::string& string) +#include + +int Main(const app::Options& options) { - throw; + Represent represent; + + 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"); - _mforeach (std::vector, byte, bytes) *byte = *type_++; + headings(xhtml); + form(xhtml); + output(xhtml); } -Binary::operator std::string() const +struct Represent::Item { - std::string string; + 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")); - _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('&')); + + _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)); + } } -std::ostream& operator<<(std::ostream& out, const Binary& binary) +void Represent::headings(xml::TextWriter& xhtml) { - return out << std::string(binary); + 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]); + } } -int main(int argc, char* argv[]) +void Represent::form(xml::TextWriter& xhtml) { - std::string hello("Hello, World!"); + xml::ScopeElement tr(xhtml, "tr"); - _foreach (std::string, atom, hello) { - std::cout << Binary(*atom) << " = " << *atom << 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_); + } } - _fori (index, -10, 11) { - 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); } - _foru (index, 4294967286, 11) { - std::cout << Binary(index) << " = " << index << std::endl; + 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_]); + } } - for (float index(-1.0); index < 1.0; index += 0.1) + 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()) { - std::cout << Binary(index) << " = " << index << std::endl; + 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; } - for (double index(-1.0); index < 1.0; index += 0.1) + _rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast(delete_->second)); + + _foreach (ext::Vector, item, items) switch (item->type) { - std::cout << Binary(index) << " = " << index << std::endl; - } + case DataType::TYPE_bool: + output(xhtml, *item); - return 0; + break; + case DataType::TYPE_char: + output(xhtml, *item); + + break; + } }