--- Represent/represent.cpp 2004/12/21 11:24:26 367 +++ Represent/Represent.cpp 2004/12/24 05:08:56 386 @@ -4,98 +4,174 @@ // // $Id$ -#include -#include -#include - -#include - -class Binary -{ -private: - std::vector bytes; -public: - Binary(std::string& string, bool signed_ = false); - template - Binary(const Type& type); - template - Type convert(bool signed_ = false); - operator std::string() const; -}; +#include "Hexadecimal.hpp" +#include "DataType.hpp" -Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0) -{ - std::string::size_type off(string.size() % 8); +#ifdef _WIN32 +#pragma warning(disable:4267) +#endif - if (off != 0) - { - bytes.push_back(0); - string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); - } +#include - std::string::size_type index(0); +int Main(const app::Options& options) +{ + Represent represent; - _rmforeach (std::vector, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit; + return 0; } -template -Binary::Binary(const Type& type) : bytes(sizeof (type)) +Represent::Represent() { - char* type_(reinterpret_cast(const_cast(&type))); + parse(); - _mforeach (std::vector, byte, bytes) *byte = *type_++; + 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); + output(xhtml); } -template -Type Binary::convert(bool signed_) +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() { - Type type; + 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()); - if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); + query = content; + } - char* type_(reinterpret_cast(&type)); + ext::Vector pairs(query.Split('&')); - _foreach (std::vector, byte, bytes) *type_++ = *byte; + _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()); - return type; + cgi.insert(std::pair(name, value)); + } } -Binary::operator std::string() const +void Represent::headings(xml::TextWriter& xhtml) { - std::string string; + xml::ScopeElement tr(xhtml, "tr"); + ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" }; - _rforeach (std::vector, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; + _foru (index, 0, sizeof (headings) / sizeof (ext::String)) + { + xml::ScopeElement th(xhtml, "th"); - return string; + xhtml.OutputText(headings[index]); + } } -inline std::ostream& operator<<(std::ostream& out, const Binary& binary) +void Represent::form(xml::TextWriter& xhtml) { - return out << std::string(binary); + 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"); } -int main(int argc, char* argv[]) +void Represent::output(xml::TextWriter& xhtml) { - std::string hello("Hello, World!"); + typedef std::multimap::size_type MultiMapSize; - _foreach (std::string, atom, hello) std::cout << Binary(*atom) << " = " << *atom << std::endl; + std::set > count; + ext::String names[] = { "type", "data", "input" }; - _fori (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; + _foru (index, 0, sizeof (names) / sizeof (ext::String)) count.insert(cgi.count(names[index])); - _foru (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; + typedef std::multimap::const_iterator MultiMapConstIterator; - for (float index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; + 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; - for (double index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; + _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); - std::string string("101001101001"); - Binary binary(string, true); + if (type != type_) ++type; + if (data != data_) ++data; + if (input != input_) ++input; + } - std::cout << binary << " = " << string << std::endl; + _rfor (MultiMapConstIterator, delete_, cgi.lower_bound("delete"), cgi.upper_bound("delete")) items.RemoveAt(lexical_cast(delete_->second)); - float value(binary.convert()); + _foreach (ext::Vector, item, items) switch (item->type) + { + case DataType::TYPE_bool: + output(xhtml, *item); - std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; + break; + case DataType::TYPE_char: + output(xhtml, *item); - return 0; + break; + } }