--- Represent/represent.cpp 2004/12/21 09:12:18 362 +++ Represent/Represent.cpp 2005/03/10 03:08:17 422 @@ -4,99 +4,509 @@ // // $Id$ -#include -#include -#include +#include "Hexadecimal.hpp" +#include "DataType.hpp" +#include "InputType.hpp" + +#ifdef _WIN32 +#pragma warning(disable:4267 4288) +#endif + +#include +#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_) {} +}; + +int Main(const app::Options& options) +{ + Represent represent; -/*template -std::string binary(const Type& type) + return 0; +} + +Represent::Represent() { - std::ostringstream string; + 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; - _rforu (index, 0, 8 * sizeof (type)) { - bool bit(1 & type >> index); + xml::ScopeElement table(xhtml, "table"); - string << bit; + headings(xhtml); + form(xhtml); + output(xhtml); } - return string.str(); -}*/ + api::Cout << after << ios::Flush; +} -class Binary +void Represent::parse() { -private: - std::vector bytes; -public: - Binary(const std::string& string); - template - Binary(const Type& type); - operator std::string() const; -}; + 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()); -Binary::Binary(const std::string& string) + query = content; + } + + ext::Vector pairs(ext::SplitAll(query, ext::String("&"))); + + _foreach (const ext::Vector, pair, pairs) + { + ext::String::ConstIterator equal(ext::FindFirstAll(*pair, ext::String("="))); + ext::String name(pair->Begin(), equal), value(equal != pair->End() ? equal + 1 : equal, pair->End()); + + cgi.insert(std::pair(decode(name), decode(value))); + } +} + +std::string Represent::decode(const ext::String& encoded) { - throw; + std::string decoded(encoded); + std::string::size_type pos(0); + + while ((pos = decoded.find_first_of("%+", pos)) != std::string::npos) switch (decoded[pos]) + { + case '%': + decoded.replace(pos, 3, 1, Hexadecimal(decoded.substr(pos + 1, 2), false).convert(false)); + break; + case '+': + decoded[pos] = ' '; + } + + return decoded; } -template -Binary::Binary(const Type& type) : bytes(sizeof (type)) +void Represent::headings(xml::TextWriter& xhtml) { - char* type_(reinterpret_cast(const_cast(&type))); + xml::ScopeElement tr(xhtml, "tr"); + ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" }; + + _for (unsigned, index, 0, sizeof (headings) / sizeof (ext::String)) + { + xml::ScopeElement th(xhtml, "th"); - _mforeach (std::vector, byte, bytes) *byte = *type_++; + xhtml.OutputText(headings[index]); + } } -Binary::operator std::string() const +void Represent::form(xml::TextWriter& xhtml) { - std::string string; + xml::ScopeElement tr(xhtml, "tr"); - _rforeach (std::vector, byte, bytes) { - _rfor (char, bit, 0, 8) + 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 (const ext::Vector, type_, DataType::enumerate()) { - string += 1 & *byte >> bit ? '1' : '0'; + xml::ScopeElement option(xhtml, "option"); + + if (*type_ == type) xhtml.SetAttribute("selected", "selected"); + + xhtml.OutputText(*type_); } } - return string; + { + 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 (const 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"); } -std::ostream& operator<<(std::ostream& out, const Binary& binary) +void Represent::output(xml::TextWriter& xhtml) { - return out << std::string(binary); + typedef std::multimap::size_type MultiMapSize; + + std::set > count; + ext::String names[] = { "type", "data", "input" }; + + _for (unsigned, 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; + + _for (unsigned, index, 0, *count.begin() < 128 ? *count.begin() : 128) + { + 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)); + + if (!items.IsEmpty() && items.First().data.IsEmpty()) items.RemoveFirst(); + + 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); + } } -int main(int argc, char* argv[]) +template +void Represent::output(xml::TextWriter& xhtml, const Item& item, size_t index) { - std::string hello("Hello, World!"); + xhtml.OpenElement("tr"); - _foreach (std::string, atom, hello) { - std::cout << Binary(*atom) << " = " << *atom << std::endl; + xml::ScopeElement td(xhtml, "td"); + + xhtml.SetAttribute("rowspan", "3"); + xhtml.OutputText(item.type); + + ext::String names[] = { "type", "data", "input" }, values[] = { item.type, item.data, item.input }; + + _for (unsigned, 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]); + } } - _fori (index, -10, 11) + Type type(input(item)); + { - std::cout << Binary(index) << " = " << index << std::endl; + xml::ScopeElement td(xhtml, "td"); + + normal(xhtml, type); } - _foru (index, -10, 11) { - std::cout << Binary(index) << " = " << index << std::endl; + xml::ScopeElement th(xhtml, "th"); + + xhtml.OutputText("Normal"); } - for (float index(-1.0); index < 1.0; index += 0.1) { - std::cout << Binary(index) << " = " << index << std::endl; + 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"); } - for (double index(-1.0); index < 1.0; index += 0.1) + xhtml.CloseElement(); + xhtml.OpenElement("tr"); + { - std::cout << Binary(index) << " = " << index << std::endl; + xml::ScopeElement td(xhtml, "td"); + + binary(xhtml, type); } - return 0; + { + 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 Represent::input(const Item& item) +{ + bool signed_(etl::Limits::IsSigned); + + 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_); + } +} + +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(item.data, false).convert(false); + } +} + +template <> +std::string Represent::input(const Item& item) +{ + std::string string; + + switch (item.input) + { + default: + return item.data; + case InputType::INPUT_Binary: + _foreach (const _L, atom, ext::SplitAll(item.data, ext::String(" "))) string += Binary(*atom, false).convert(false); + break; + case InputType::INPUT_Hexadecimal: + _foreach (const _L, atom, ext::SplitAll(item.data, ext::String(" "))) string += Hexadecimal(*atom, false).convert(false); + } + + return string; +} + +template <> +ext::String Represent::input(const Item& item) +{ + ext::String string; + + switch (item.input) + { + default: + return item.data; + case InputType::INPUT_Binary: + _foreach (const _L, atom, ext::SplitAll(item.data, ext::String(" "))) string.InsertLast(Binary(*atom, false).convert(false)); + break; + case InputType::INPUT_Hexadecimal: + _foreach (const _L, atom, ext::SplitAll(item.data, ext::String(" "))) string.InsertLast(Hexadecimal(*atom, false).convert(false)); + } + + return string; +} + +template +void Represent::normal(xml::TextWriter& xhtml, const Type& type) +{ + xhtml.OutputText(lexical_cast(type)); +} + +template <> +void Represent::normal(xml::TextWriter& xhtml, const char& char_) +{ + xhtml.OutputText("'"); + xhtml.OutputText(std::string(1, char_).c_str()); + xhtml.OutputText("'"); +} + +template <> +void Represent::normal(xml::TextWriter& xhtml, const std::string& string) +{ + xhtml.OutputText("\""); + xhtml.OutputText(string); + xhtml.OutputText("\""); +} + +template <> +void Represent::normal(xml::TextWriter& xhtml, const ext::String& string) +{ + xhtml.OutputText("\""); + xhtml.OutputText(string); + xhtml.OutputText("\""); +} + +template +void Represent::binary(xml::TextWriter& xhtml, const Type& type) +{ + xhtml.OutputText(Binary(type)); +} + +template <> +void Represent::binary(xml::TextWriter& xhtml, const std::string& string) +{ + xhtml.OutputText(Binary(string)); + + xml::ScopeElement(xhtml, "br"); + + _sforeach (std::string, atom, string) + { + xml::ScopeElement(xhtml, "br"); + + xhtml.OutputText(Binary(*atom)); + xhtml.OutputText(" = '"); + xhtml.OutputText(ext::CodePoint(*atom)); + xhtml.OutputText("'"); + } +} + +template <> +void Represent::binary(xml::TextWriter& xhtml, const ext::String& string) +{ + xhtml.OutputText(Binary(string)); + + xml::ScopeElement(xhtml, "br"); + + _foreach (const ext::String, atom, string) + { + xml::ScopeElement(xhtml, "br"); + + xhtml.OutputText(Binary(*atom)); + xhtml.OutputText(" = '"); + xhtml.OutputText(ext::CodePoint(*atom)); + xhtml.OutputText("'"); + } +} + +template +void Represent::hexadecimal(xml::TextWriter& xhtml, const Type& type) +{ + xhtml.OutputText(Hexadecimal(type)); +} + +template <> +void Represent::hexadecimal(xml::TextWriter& xhtml, const std::string& string) +{ + xhtml.OutputText(Hexadecimal(string)); + + xml::ScopeElement(xhtml, "br"); + + _sforeach (std::string, atom, string) + { + xml::ScopeElement(xhtml, "br"); + + xhtml.OutputText(Hexadecimal(*atom)); + xhtml.OutputText(" = '"); + xhtml.OutputText(ext::CodePoint(*atom)); + xhtml.OutputText("'"); + } +} + +template <> +void Represent::hexadecimal(xml::TextWriter& xhtml, const ext::String& string) +{ + xhtml.OutputText(Hexadecimal(string)); + + xml::ScopeElement(xhtml, "br"); + + _foreach (const ext::String, atom, string) + { + xml::ScopeElement(xhtml, "br"); + + xhtml.OutputText(Hexadecimal(*atom)); + xhtml.OutputText(" = '"); + xhtml.OutputText(ext::CodePoint(*atom)); + xhtml.OutputText("'"); + } }