From 76d23e639004517db8f9469d64ac1789f8449365 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 7 Jan 2010 13:50:11 +0200 Subject: Add support for ISO-8859-1 as application encoding New runtime configuration parameter, XSDE_ENCODING. New option, --char-encoding. New test, tests/cxx/hybrid/iso8859-1. --- libxsde/xsde/cxx/serializer/context.cxx | 702 ++++++++++++++++++++++++++++++++ 1 file changed, 702 insertions(+) (limited to 'libxsde/xsde/cxx/serializer/context.cxx') 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 + #include #include @@ -11,6 +13,10 @@ # include #endif +#ifdef XSDE_ENCODING_ISO8859_1 +# include +#endif + #include 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 (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 (uns), + reinterpret_cast (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 (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 (uns), + reinterpret_cast (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 (uname), + reinterpret_cast (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 (uns), + reinterpret_cast (uname), + reinterpret_cast (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 (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 (us), + static_cast (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 (uns), + reinterpret_cast (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 (uns), + reinterpret_cast ("")); + +#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 (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 ( + 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 } } } -- cgit v1.1