--- Represent/Represent.cpp 2004/12/24 05:01:12 385 +++ Represent/Represent.cpp 2004/12/25 06:32:04 393 @@ -6,8 +6,29 @@ #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_) {} +}; int Main(const app::Options& options) { @@ -18,25 +39,28 @@ int Main(const app::Options& options) Represent::Represent() { - 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); - output(xhtml); -} + parse(); -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) {} -}; + _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; + + { + xml::ScopeElement table(xhtml, "table"); + + headings(xhtml); + form(xhtml); + output(xhtml); + } + + api::Cout << after << ios::Flush; +} void Represent::parse() { @@ -58,14 +82,31 @@ void Represent::parse() 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)); + cgi.insert(std::pair(decode(name), decode(value))); + } +} + +std::string Represent::decode(const ext::String& encoded) +{ + 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; } void Represent::headings(xml::TextWriter& xhtml) { xml::ScopeElement tr(xhtml, "tr"); - ext::String headings[] = { "Data Type", "Data Representation", "Input", "Storage" }; + ext::String headings[] = { "Data Type", "Data Representation", "Input Type", "Storage" }; _foru (index, 0, sizeof (headings) / sizeof (ext::String)) { @@ -84,7 +125,7 @@ void Represent::form(xml::TextWriter& xh xhtml.SetAttribute("name", "type"); - DataType type(cgi.find("type") != cgi.end() ? DataType::Type(lexical_cast(cgi.find("type")->second)) : DataType::TYPE_bool); + DataType type(cgi.find("type") != cgi.end() ? cgi.find("type")->second : std::string()); _foreach (ext::Vector, type_, DataType::enumerate()) { @@ -92,7 +133,6 @@ void Represent::form(xml::TextWriter& xh if (*type_ == type) xhtml.SetAttribute("selected", "selected"); - xhtml.SetAttribute("value", lexical_cast(DataType::Type(*type_))); xhtml.OutputText(*type_); } } @@ -112,17 +152,17 @@ void Represent::form(xml::TextWriter& xh { 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); + xhtml.SetAttribute("name", "input"); - _foru (input_, INPUT_Normal, INPUT_Hexadecimal + 1) + 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_) == input) xhtml.SetAttribute("selected", "selected"); + if (*input_ == input) xhtml.SetAttribute("selected", "selected"); - xhtml.SetAttribute("value", lexical_cast(input_)); - xhtml.OutputText(inputs[input_]); + xhtml.OutputText(*input_); } } @@ -146,9 +186,9 @@ void Represent::output(xml::TextWriter& 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()) + _foru (index, 0, *count.begin() < 128 ? *count.begin() : 128) { - 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); + 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); @@ -159,15 +199,289 @@ void Represent::output(xml::TextWriter& _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); - + output(xhtml, *item, ++index); break; case DataType::TYPE_char: - output(xhtml, *item); - + 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 +void Represent::output(xml::TextWriter& xhtml, const Item& item, size_t index) +{ + xhtml.OpenElement("tr"); + + { + 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 }; + + _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 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); + } +} + +// 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; +} + +template +void Represent::normal(xml::TextWriter& xhtml, const Type& type) +{ + xhtml.OutputText(lexical_cast(type)); +} + +// XXX: damn, this one is really quite screwy +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 (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 (ext::String, atom, string) + { + xml::ScopeElement(xhtml, "br"); + + xhtml.OutputText(Hexadecimal(*atom)); + xhtml.OutputText(" = '"); + xhtml.OutputText(ext::CodePoint(*atom)); + xhtml.OutputText("'"); } }