--- Represent/Represent.cpp 2004/12/22 07:19:54 371 +++ Represent/Represent.cpp 2004/12/25 06:48:09 395 @@ -5,8 +5,30 @@ // $Id$ #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) { @@ -17,18 +39,451 @@ int Main(const app::Options& options) Represent::Represent() { - ext::String string("1001"); - Binary sign(string, true), unsign(string, false); + 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; + + { + xml::ScopeElement table(xhtml, "table"); + + headings(xhtml); + form(xhtml); + output(xhtml); + } + + 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"))); + + api::Cin.ReadFully(content.Begin(), content.GetSize()); + + query = content; + } + + 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(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 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() < 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); + } +} + +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; +} - api::Cerr << string << ios::NewLine << ext::String(sign) << ios::NewLine << ext::String(unsign) << ios::NewLine; +template +void Represent::normal(xml::TextWriter& xhtml, const Type& type) +{ + xhtml.OutputText(lexical_cast(type)); +} - string = "deadbeef"; +// 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)); - Hexadecimal test(string, false); + xml::ScopeElement(xhtml, "br"); - api::Cerr << string << ios::NewLine << ext::String(test) << ios::NewLine; + _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)); - Hexadecimal sign_(sign), unsign_(unsign); + xml::ScopeElement(xhtml, "br"); - api::Cerr << ext::String(sign_) << ios::NewLine << ext::String(unsign_) << ios::NewLine; + _foreach (ext::String, atom, string) + { + xml::ScopeElement(xhtml, "br"); + + xhtml.OutputText(Hexadecimal(*atom)); + xhtml.OutputText(" = '"); + xhtml.OutputText(ext::CodePoint(*atom)); + xhtml.OutputText("'"); + } }