diff options
Diffstat (limited to 'libxsde/xsde/cxx/serializer')
-rw-r--r-- | libxsde/xsde/cxx/serializer/context.cxx | 702 | ||||
-rw-r--r-- | libxsde/xsde/cxx/serializer/context.hxx | 18 | ||||
-rw-r--r-- | libxsde/xsde/cxx/serializer/context.ixx | 274 |
3 files changed, 720 insertions, 274 deletions
diff --git a/libxsde/xsde/cxx/serializer/context.cxx b/libxsde/xsde/cxx/serializer/context.cxx index 650bed2..faa1ed4 100644 --- a/libxsde/xsde/cxx/serializer/context.cxx +++ b/libxsde/xsde/cxx/serializer/context.cxx @@ -3,6 +3,8 @@ // copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC // license : GNU GPL v2 + exceptions; see accompanying LICENSE file +#include <assert.h> + #include <xsde/cxx/config.hxx> #include <xsde/cxx/ro-string.hxx> @@ -11,6 +13,10 @@ # include <xsde/cxx/serializer/exceptions.hxx> #endif +#ifdef XSDE_ENCODING_ISO8859_1 +# include <xsde/cxx/iso8859-1.hxx> +#endif + #include <xsde/cxx/serializer/context.hxx> namespace xsde @@ -19,6 +25,625 @@ namespace xsde { namespace serializer { + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + start_element (const char* name) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uname = name; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uname; + size_t un; + string buf; + + if (iso8859_1::ascii_iso (name, un)) + uname = name; + else + { + uname = conv_name (name, un, name_buf1_, buf); + +#ifndef XSDE_EXCEPTIONS + if (uname == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxStartElementLiteral ( + xml_serializer_, + 0, + reinterpret_cast<constUtf8> (uname)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + start_element (const char* ns, const char* name) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uns = ns; + const char* uname = name; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uns; + const char* uname; + size_t un; + string buf1, buf2; + + if (iso8859_1::ascii_iso (ns, un)) + uns = ns; + else + { + uns = conv_name (ns, un, name_buf1_, buf1); + +#ifndef XSDE_EXCEPTIONS + if (uns == 0) + return false; +#endif + } + + if (iso8859_1::ascii_iso (name, un)) + uname = name; + else + { + uname = conv_name (name, un, name_buf2_, buf2); + +#ifndef XSDE_EXCEPTIONS + if (uname == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxStartElementLiteral ( + xml_serializer_, + reinterpret_cast<constUtf8> (uns), + reinterpret_cast<constUtf8> (uname)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + start_attribute (const char* name) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uname = name; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uname; + size_t un; + string buf; + + if (iso8859_1::ascii_iso (name, un)) + uname = name; + else + { + uname = conv_name (name, un, name_buf1_, buf); + +#ifndef XSDE_EXCEPTIONS + if (uname == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxStartAttributeLiteral ( + xml_serializer_, + 0, + reinterpret_cast<constUtf8> (uname)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + start_attribute (const char* ns, const char* name) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uns = ns; + const char* uname = name; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uns; + const char* uname; + size_t un; + string buf1, buf2; + + if (iso8859_1::ascii_iso (ns, un)) + uns = ns; + else + { + uns = conv_name (ns, un, name_buf1_, buf1); + +#ifndef XSDE_EXCEPTIONS + if (uns == 0) + return false; +#endif + } + + if (iso8859_1::ascii_iso (name, un)) + uname = name; + else + { + uname = conv_name (name, un, name_buf2_, buf2); + +#ifndef XSDE_EXCEPTIONS + if (uname == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxStartAttributeLiteral ( + xml_serializer_, + reinterpret_cast<constUtf8> (uns), + reinterpret_cast<constUtf8> (uname)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + attribute (const char* name, const char* value) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uname = name; + const char* uvalue = value; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uname; + const char* uvalue; + size_t un; + string buf1, buf2; + + if (iso8859_1::ascii_iso (name, un)) + uname = name; + else + { + uname = conv_name (name, un, name_buf1_, buf1); + +#ifndef XSDE_EXCEPTIONS + if (uname == 0) + return false; +#endif + } + + if (iso8859_1::ascii_iso (value, un)) + uvalue = value; + else + { + uvalue = conv_data (value, un, buf2); + +#ifndef XSDE_EXCEPTIONS + if (uvalue == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxAddAttributeLiteral ( + xml_serializer_, + 0, + reinterpret_cast<constUtf8> (uname), + reinterpret_cast<constUtf8> (uvalue)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + attribute (const char* ns, const char* name, const char* value) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uns = ns; + const char* uname = name; + const char* uvalue = value; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uns; + const char* uname; + const char* uvalue; + size_t un; + string buf1, buf2, buf3; + + if (iso8859_1::ascii_iso (ns, un)) + uns = ns; + else + { + uns = conv_name (ns, un, name_buf1_, buf1); + +#ifndef XSDE_EXCEPTIONS + if (uns == 0) + return false; +#endif + } + + if (iso8859_1::ascii_iso (name, un)) + uname = name; + else + { + uname = conv_name (name, un, name_buf2_, buf2); + +#ifndef XSDE_EXCEPTIONS + if (uname == 0) + return false; +#endif + } + + if (iso8859_1::ascii_iso (value, un)) + uvalue = value; + else + { + uvalue = conv_data (value, un, buf3); + +#ifndef XSDE_EXCEPTIONS + if (uvalue == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxAddAttributeLiteral ( + xml_serializer_, + reinterpret_cast<constUtf8> (uns), + reinterpret_cast<constUtf8> (uname), + reinterpret_cast<constUtf8> (uvalue)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + characters (const char* s) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* us = s; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* us; + size_t un; + string buf; + + if (iso8859_1::ascii_iso (s, un)) + us = s; + else + { + us = conv_data (s, un, buf); + +#ifndef XSDE_EXCEPTIONS + if (us == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxAddText ( + xml_serializer_, + reinterpret_cast<constUtf8> (us)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + characters (const char* s, size_t n) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* us = s; + size_t un = n; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* us; + size_t un; + string buf; + + if (iso8859_1::ascii_iso (s, n, un)) + { + us = s; + un = n; + } + else + { + us = conv_data (s, n, un, buf); + +#ifndef XSDE_EXCEPTIONS + if (us == 0) + return false; +#endif + --un; // Discount trailing zero. + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxAddCountedText ( + xml_serializer_, + reinterpret_cast<constUtf8> (us), + static_cast<int> (un)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + declare_namespace (const char* ns, const char* prefix) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uns = ns; + const char* uprefix = prefix; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uns; + const char* uprefix; + size_t un; + string buf1, buf2; + + if (iso8859_1::ascii_iso (ns, un)) + uns = ns; + else + { + uns = conv_name (ns, un, name_buf1_, buf1); + +#ifndef XSDE_EXCEPTIONS + if (uns == 0) + return false; +#endif + } + + if (iso8859_1::ascii_iso (prefix, un)) + uprefix = prefix; + else + { + uprefix = conv_name (prefix, un, name_buf2_, buf2); + +#ifndef XSDE_EXCEPTIONS + if (uprefix == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxAddNamespaceLiteral ( + xml_serializer_, + reinterpret_cast<constUtf8> (uns), + reinterpret_cast<constUtf8> (uprefix)); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // +#ifdef XSDE_EXCEPTIONS + void context:: +#else + bool context:: +#endif + declare_default_namespace (const char* ns) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uns = ns; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uns; + size_t un; + string buf; + + if (iso8859_1::ascii_iso (ns, un)) + uns = ns; + else + { + uns = conv_name (ns, un, name_buf1_, buf); + +#ifndef XSDE_EXCEPTIONS + if (uns == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e = genxAddNamespaceLiteral ( + xml_serializer_, + reinterpret_cast<constUtf8> (uns), + reinterpret_cast<constUtf8> ("")); + +#ifdef XSDE_EXCEPTIONS + if (e != GENX_SUCCESS) + throw_xml_error (e); +#else + if (e != GENX_SUCCESS) + xml_error (e); + + return e == GENX_SUCCESS; +#endif + } + + // + // + const char* context:: + lookup_namespace_prefix (const char* ns) + { + // Convert from application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + const char* uns = ns; +#elif defined(XSDE_ENCODING_ISO8859_1) + const char* uns; + size_t un; + string buf; + + if (iso8859_1::ascii_iso (ns, un)) + uns = ns; + else + { + uns = conv_name (ns, un, name_buf1_, buf); + +#ifndef XSDE_EXCEPTIONS + if (uns == 0) + return false; +#endif + } +#endif // XSDE_ENCODING_* + + genxStatus e; + genxNamespace gns = genxDeclareNamespace ( + xml_serializer_, reinterpret_cast<constUtf8> (uns), 0, &e); + + if (e != GENX_SUCCESS) + { +#ifdef XSDE_EXCEPTIONS + throw_xml_error (e); +#else + xml_error (e); +#endif + return 0; + } + + const char* p = reinterpret_cast<const char*> ( + genxGetNamespacePrefix (gns)); + + // Convert to application encoding. + // +#ifdef XSDE_ENCODING_UTF8 + return p; +#elif defined(XSDE_ENCODING_ISO8859_1) + // At the moment we can only support ASCII prefixes since there is + // no good way to store the converted string. + // + assert (iso8859_1::ascii_utf (p, un)); + return p; +#endif // XSDE_ENCODING_* + } + + // + // #ifdef XSDE_POLYMORPHIC #ifdef XSDE_EXCEPTIONS void context:: @@ -102,6 +727,83 @@ namespace xsde } } #endif + +#ifdef XSDE_ENCODING_ISO8859_1 + const char* context:: + conv_data (const char* iso_s, size_t utf_n, string& var) + { + char* buf; + + if (utf_n <= sizeof (data_buf_)) + buf = data_buf_; + else + { + buf = new char[utf_n]; + +#ifndef XSDE_EXCEPTIONS + if (buf == 0) + { + sys_error (sys_error::no_memory); + return 0; + } +#endif + var.attach (buf, utf_n - 1); + } + + iso8859_1::from (iso_s, buf); + return buf; + } + + const char* context:: + conv_data (const char* iso_s, size_t iso_n, size_t utf_n, string& var) + { + char* buf; + + if (utf_n <= sizeof (data_buf_)) + buf = data_buf_; + else + { + buf = new char[utf_n]; + +#ifndef XSDE_EXCEPTIONS + if (buf == 0) + { + sys_error (sys_error::no_memory); + return 0; + } +#endif + var.attach (buf, utf_n - 1); + } + + iso8859_1::from (iso_s, iso_n, buf); + return buf; + } + + const char* context:: + conv_name (const char* iso_s, size_t utf_n, char* fix, string& var) + { + char* buf; + + if (utf_n <= sizeof (name_buf1_)) + buf = fix; + else + { + buf = new char[utf_n]; + +#ifndef XSDE_EXCEPTIONS + if (buf == 0) + { + sys_error (sys_error::no_memory); + return 0; + } +#endif + var.attach (buf, utf_n - 1); + } + + iso8859_1::from (iso_s, buf); + return buf; + } +#endif // XSDE_ENCODING_ISO8859_1 } } } diff --git a/libxsde/xsde/cxx/serializer/context.hxx b/libxsde/xsde/cxx/serializer/context.hxx index d09bb4d..d2cec00 100644 --- a/libxsde/xsde/cxx/serializer/context.hxx +++ b/libxsde/xsde/cxx/serializer/context.hxx @@ -11,6 +11,7 @@ #include <stddef.h> // size_t #include <xsde/c/genx/genx.h> +#include <xsde/cxx/string.hxx> #ifndef XSDE_EXCEPTIONS # include <xsde/cxx/sys-error.hxx> @@ -251,6 +252,23 @@ namespace xsde const void* type_id_; #endif + // Support for ISO-8859-1 conversion. + // +#ifdef XSDE_ENCODING_ISO8859_1 + protected: + const char* + conv_data (const char* iso_s, size_t utf_n, string& var); + + const char* + conv_data (const char* iso_s, size_t iso_n, size_t utf_n, string& var); + + const char* + conv_name (const char* iso_s, size_t utf_n, char* fix, string& var); + + char data_buf_[256]; + char name_buf1_[128]; + char name_buf2_[128]; // Keep buf1 and buf2 sizes the same. +#endif }; } } diff --git a/libxsde/xsde/cxx/serializer/context.ixx b/libxsde/xsde/cxx/serializer/context.ixx index 04cfe87..3612f0a 100644 --- a/libxsde/xsde/cxx/serializer/context.ixx +++ b/libxsde/xsde/cxx/serializer/context.ixx @@ -45,56 +45,6 @@ namespace xsde #else inline bool context:: #endif - start_element (const char* name) - { - genxStatus e = genxStartElementLiteral ( - xml_serializer_, - 0, - reinterpret_cast<constUtf8> (name)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif - start_element (const char* ns, const char* name) - { - genxStatus e = genxStartElementLiteral ( - xml_serializer_, - reinterpret_cast<constUtf8> (ns), - reinterpret_cast<constUtf8> (name)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif end_element () { genxStatus e = genxEndElement (xml_serializer_); @@ -117,56 +67,6 @@ namespace xsde #else inline bool context:: #endif - start_attribute (const char* name) - { - genxStatus e = genxStartAttributeLiteral ( - xml_serializer_, - 0, - reinterpret_cast<constUtf8> (name)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif - start_attribute (const char* ns, const char* name) - { - genxStatus e = genxStartAttributeLiteral ( - xml_serializer_, - reinterpret_cast<constUtf8> (ns), - reinterpret_cast<constUtf8> (name)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif end_attribute () { genxStatus e = genxEndAttribute (xml_serializer_); @@ -189,157 +89,6 @@ namespace xsde #else inline bool context:: #endif - attribute (const char* name, const char* value) - { - genxStatus e = genxAddAttributeLiteral ( - xml_serializer_, - 0, - reinterpret_cast<constUtf8> (name), - reinterpret_cast<constUtf8> (value)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif - attribute (const char* ns, const char* name, const char* value) - { - genxStatus e = genxAddAttributeLiteral ( - xml_serializer_, - reinterpret_cast<constUtf8> (ns), - reinterpret_cast<constUtf8> (name), - reinterpret_cast<constUtf8> (value)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif - characters (const char* s) - { - genxStatus e = genxAddText ( - xml_serializer_, - reinterpret_cast<constUtf8> (s)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif - characters (const char* s, size_t n) - { - genxStatus e = genxAddCountedText ( - xml_serializer_, - reinterpret_cast<constUtf8> (s), - static_cast<int> (n)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif - declare_namespace (const char* ns, const char* prefix) - { - genxStatus e = genxAddNamespaceLiteral ( - xml_serializer_, - reinterpret_cast<constUtf8> (ns), - reinterpret_cast<constUtf8> (prefix)); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif - declare_default_namespace (const char* ns) - { - genxStatus e = genxAddNamespaceLiteral ( - xml_serializer_, - reinterpret_cast<constUtf8> (ns), - reinterpret_cast<constUtf8> ("")); - -#ifdef XSDE_EXCEPTIONS - if (e != GENX_SUCCESS) - throw_xml_error (e); -#else - if (e != GENX_SUCCESS) - xml_error (e); - - return e == GENX_SUCCESS; -#endif - } - - // - // -#ifdef XSDE_EXCEPTIONS - inline void context:: -#else - inline bool context:: -#endif clear_default_namespace () { genxStatus e = genxUnsetDefaultNamespace (xml_serializer_); @@ -357,29 +106,6 @@ namespace xsde // // - inline const char* context:: - lookup_namespace_prefix (const char* ns) - { - genxStatus e; - genxNamespace gns = genxDeclareNamespace ( - xml_serializer_, reinterpret_cast<constUtf8> (ns), 0, &e); - - if (e != GENX_SUCCESS) - { -#ifdef XSDE_EXCEPTIONS - throw_xml_error (e); -#else - xml_error (e); -#endif - return 0; - } - - return reinterpret_cast<const char*> ( - genxGetNamespacePrefix (gns)); - } - - // - // #ifndef XSDE_EXCEPTIONS inline int context:: app_error () const |