--- Represent/represent.cpp 2004/12/21 11:26:56 368 +++ Represent/Represent.cpp 2004/12/25 04:31:04 391 @@ -4,98 +4,452 @@ // // $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" +#include "InputType.hpp" + +#ifdef _WIN32 +#pragma warning(disable:4267 4288) +#endif + +#include +#include +#include +#include + +struct Environment +{ + ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } } +} env; + +struct Item +{ + DataType type; + ext::String data; + InputType input; + Item(const DataType& type, const ext::String& data, const InputType& input_) : type(type), data(data), input(input_) {} }; -Binary::Binary(std::string& string, bool signed_) : bytes(string.size() / 8, 0) +int Main(const app::Options& options) { - std::string::size_type off(string.size() % 8); + Represent represent; + + return 0; +} + +Represent::Represent() +{ + api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; + + xml::TextWriter xhtml(api::Cout); + + parse(); + + _H document(xml::Parse("represent.xml")); + _H node(*document/"represent"); + ext::String before(*node/"before"), after(*node/"after"); + + api::Cout << ios::NewLine << before << ios::Flush; - if (off != 0) { - bytes.push_back(0); - string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); + xml::ScopeElement table(xhtml, "table"); + + headings(xhtml); + form(xhtml); + output(xhtml); } - std::string::size_type index(0); + api::Cout << after << ios::Flush; +} + +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"))); - _rmforeach (std::vector, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit; + api::Cin.ReadFully(content.Begin(), content.GetSize()); + + query = content; + } + +// api::Cout << query << ios::NewLine; + + 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()); + + // XXX: clean up %20, etc. + + 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 Type", "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() ? cgi.find("type")->second : std::string()); + + _foreach (ext::Vector, type_, DataType::enumerate()) + { + xml::ScopeElement option(xhtml, "option"); + + if (*type_ == type) xhtml.SetAttribute("selected", "selected"); + + 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"); + + xhtml.SetAttribute("name", "input"); + + InputType input(cgi.find("input") != cgi.end() ? cgi.find("input")->second : std::string()); + + _foreach (ext::Vector, input_, InputType::enumerate()) + { + xml::ScopeElement option(xhtml, "option"); + + if (*input_ == input) xhtml.SetAttribute("selected", "selected"); + + xhtml.OutputText(*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(DataType(type != type_ ? type->second : std::string()), data != data_ ? data->second : std::string(), InputType(input != input_ ? input->second : std::string())); + + 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)); + + size_t index(0); + + _foreach (ext::Vector, item, items) switch (item->type) + { + case DataType::TYPE_bool: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_char: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_short: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_unsigned_short: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_int: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_unsigned_int: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_long: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_unsigned_long: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_float: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_double: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_std_string: + output(xhtml, *item, ++index); + break; + case DataType::TYPE_ext_String: + output(xhtml, *item, ++index); + } } template -Binary::Binary(const Type& type) : bytes(sizeof (type)) +void Represent::output(xml::TextWriter& xhtml, const Item& item, size_t index) { - char* type_(reinterpret_cast(const_cast(&type))); + xhtml.OpenElement("tr"); + + { + xml::ScopeElement td(xhtml, "td"); - _mforeach (std::vector, byte, bytes) *byte = *type_++; + xhtml.SetAttribute("rowspan", "3"); + xhtml.OutputText(item.type); + + ext::String names[] = { "type", "data", "input" }, values[] = { item.type, item.data, item.input }; + + _foru (index, 0, sizeof (names) / sizeof (ext::String)) + { + xml::ScopeElement input(xhtml, "input"); + + xhtml.SetAttribute("name", names[index]); + xhtml.SetAttribute("type", "hidden"); + xhtml.SetAttribute("value", values[index]); + } + } + + Type type(input(item)); + + { + xml::ScopeElement td(xhtml, "td"); + + normal(xhtml, type); + } + + { + xml::ScopeElement th(xhtml, "th"); + + xhtml.OutputText("Normal"); + } + + { + xml::ScopeElement td(xhtml, "td"); + + xhtml.SetAttribute("rowspan", "3"); + + { + xml::ScopeElement input(xhtml, "input"); + + xhtml.SetAttribute("name", "delete"); + xhtml.SetAttribute("type", "checkbox"); + xhtml.SetAttribute("value", lexical_cast(index)); + } + + xhtml.OutputText("Delete"); + } + + xhtml.CloseElement(); + xhtml.OpenElement("tr"); + + { + xml::ScopeElement td(xhtml, "td"); + + binary(xhtml, type); + } + + { + xml::ScopeElement th(xhtml, "th"); + + xhtml.OutputText("Binary"); + } + + xhtml.CloseElement(); + xhtml.OpenElement("tr"); + + { + xml::ScopeElement td(xhtml, "td"); + + hexadecimal(xhtml, type); + } + + { + xml::ScopeElement th(xhtml, "th"); + + xhtml.OutputText("Hexadecimal"); + } + + xhtml.CloseElement(); } template -Type Binary::convert(bool signed_) +Type Represent::input(const Item& item) { - Type type; + bool signed_(etl::Limits::IsSigned); - if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); + switch (item.input) + { + default: + try { return lexical_cast(item.data); } catch (ext::Exception) { return 0; } + case InputType::INPUT_Binary: + return Binary(item.data, signed_).convert(signed_); + case InputType::INPUT_Hexadecimal: + return Hexadecimal(item.data, signed_).convert(signed_); + } +} - char* type_(reinterpret_cast(&type)); +template <> +char Represent::input(const Item& item) +{ + switch (item.input) + { + default: + return item.data.First(); + case InputType::INPUT_Binary: + return Binary(item.data, false).convert(false); + case InputType::INPUT_Hexadecimal: + return Hexadecimal(input.data, false).convert(false); + } +} + +// XXX: constructing a string from Binary or Hexadecimal seems too dangerous +template <> +std::string Represent::input(const Item& item) +{ + return item.data; +} + +// XXX: constructing a string from Binary or Hexadecimal seems too dangerous +template <> +ext::String Represent::input(const Item& item) +{ + return item.data; +} - _foreach (std::vector, byte, bytes) *type_++ = *byte; +template +void Represent::normal(xml::TextWriter& xhtml, const Type& type) +{ + xhtml.OutputText(lexical_cast(type)); +} - return type; +template <> +void Represent::normal(xml::TextWriter& xhtml, const char& char_) +{ + xhtml.OutputText(ext::CodePoint(char_)); } -Binary::operator std::string() const +template <> +void Represent::normal(xml::TextWriter& xhtml, const std::string& string) { - std::string string; + xhtml.OutputText(string); +} - _rforeach (std::vector, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; +template <> +void Represent::normal(xml::TextWriter& xhtml, const ext::String& string) +{ + xhtml.OutputText(string); +} - return string; +template +void Represent::binary(xml::TextWriter& xhtml, const Type& type) +{ + xhtml.OutputText(Binary(type)); } -inline std::ostream& operator<<(std::ostream& out, const Binary& binary) +template <> +void Represent::binary(xml::TextWriter& xhtml, const std::string& string) { - return out << std::string(binary); + xhtml.OutputText(Binary(string)); + + xml::ScopeElement(xhtml, "br"); + + _sforeach (std::string, atom, string) + { + xml::ScopeElement(xhtml, "br"); + + xhtml.OutputText(Binary(*atom)); + } } -int main(int argc, char* argv[]) +template <> +void Represent::binary(xml::TextWriter& xhtml, const ext::String& string) { - std::string hello("Hello, World!"); + xhtml.OutputText(Binary(string)); + + xml::ScopeElement(xhtml, "br"); - _foreach (std::string, atom, hello) std::cout << Binary(*atom) << " = " << *atom << std::endl; + _foreach (ext::String, atom, string) + { + xml::ScopeElement(xhtml, "br"); - _fori (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; + xhtml.OutputText(Binary(*atom)); + } +} + +template +void Represent::hexadecimal(xml::TextWriter& xhtml, const Type& type) +{ + xhtml.OutputText(Hexadecimal(type)); +} - _foru (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; +template <> +void Represent::hexadecimal(xml::TextWriter& xhtml, const std::string& string) +{ + xhtml.OutputText(Hexadecimal(string)); - for (float index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; + xml::ScopeElement(xhtml, "br"); - for (double index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; + _sforeach (std::string, atom, string) + { + xml::ScopeElement(xhtml, "br"); - std::string string("101001101001"); - Binary binary(string, true); + xhtml.OutputText(Hexadecimal(string)); + } +} - std::cout << binary << " = " << string << std::endl; +template <> +void Represent::hexadecimal(xml::TextWriter& xhtml, const ext::String& string) +{ + xhtml.OutputText(Hexadecimal(string)); - float value(binary.convert()); + xml::ScopeElement(xhtml, "br"); - std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; + _foreach (ext::String, atom, string) + { + xml::ScopeElement(xhtml, "br"); - return 0; + xhtml.OutputText(Hexadecimal(string)); + } }