--- Represent/represent.cpp 2004/12/21 11:26:56 368 +++ Represent/Represent.cpp 2004/12/23 04:42:52 379 @@ -4,98 +4,65 @@ // // $Id$ -#include -#include -#include +#include "Hexadecimal.hpp" -#include +#include +#include -class Binary +struct Environment { -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; -}; + ext::String get(const ext::String& name) { try { return api::TheEnvironment.Get(name); } catch (ext::Exception) { return ext::String(); } } +} env; -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; - if (off != 0) - { - bytes.push_back(0); - string.insert(0, 8 - off, signed_ && string[0] == '1' ? '1' : '0'); - } - - std::string::size_type index(0); - - _rmforeach (std::vector, byte, bytes) _rfor (char, bit, 0, 8) *byte |= (string[index++] == '1') << bit; -} - -template -Binary::Binary(const Type& type) : bytes(sizeof (type)) -{ - char* type_(reinterpret_cast(const_cast(&type))); - - _mforeach (std::vector, byte, bytes) *byte = *type_++; + return 0; } -template -Type Binary::convert(bool signed_) +Represent::Represent() { - Type type; - - if (sizeof (type) != bytes.size()) bytes.resize(sizeof (type), signed_ && bytes.back() >> 7 ? ~0 : 0); - - char* type_(reinterpret_cast(&type)); + parse(); - _foreach (std::vector, byte, bytes) *type_++ = *byte; + api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n" << ios::Flush; - return type; -} - -Binary::operator std::string() const -{ - std::string string; + xml::TextWriter xhtml(api::Cout); + xml::ScopeElement table(xhtml, "table"); - _rforeach (std::vector, byte, bytes) _rfor (char, bit, 0, 8) string +=1 & *byte >> bit ? '1' : '0'; + { + xml::ScopeElement tr(xhtml, "tr"); + ext::String headings[] = { "Type", "Data Representation", "Storage" }; - return string; -} + _foru (index, 0, sizeof (headings) / sizeof (ext::String)) + { + xml::ScopeElement th(xhtml, "th"); -inline std::ostream& operator<<(std::ostream& out, const Binary& binary) -{ - return out << std::string(binary); + xhtml.OutputText(headings[index]); + } + } } -int main(int argc, char* argv[]) +void Represent::parse() { - std::string hello("Hello, World!"); - - _foreach (std::string, atom, hello) std::cout << Binary(*atom) << " = " << *atom << std::endl; - - _fori (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; - - _foru (index, -10, 11) std::cout << Binary(index) << " = " << index << std::endl; + ext::String query(env.get("QUERY_STRING")); - for (float index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; - - for (double index(-1.0); index < 1.0; index += 0.1) std::cout << Binary(index) << " = " << index << std::endl; + if (env.get("REQUEST_METHOD") == "POST") + { + ext::Buffer content(lexical_cast(env.get("CONTENT_LENGTH"))); - std::string string("101001101001"); - Binary binary(string, true); + api::Cin.ReadFully(content.Begin(), content.GetSize()); - std::cout << binary << " = " << string << std::endl; + query = content; + } - float value(binary.convert()); + ext::Vector pairs(query.Split('&')); - std::cout << binary << " = " << value << std::endl << Binary(value) << " = " << value << std::endl; + _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 0; + cgi.insert(std::pair(name, value)); + } }