diff options
author | Martin Corino <mcorino@remedy.nl> | 2018-08-03 14:15:38 +0200 |
---|---|---|
committer | Martin Corino <mcorino@remedy.nl> | 2018-08-04 13:56:37 +0200 |
commit | 4a09b0b10cbea1fdcb16161583ee22f7d151784b (patch) | |
tree | 3a69f80021bf06e96197cd5bae164d8ebff657f3 /TAO/tao/Codeset | |
parent | 81d3d81c4cf0904cd8d5b4c0872cd1821b5d874e (diff) | |
download | ATCD-4a09b0b10cbea1fdcb16161583ee22f7d151784b.tar.gz |
add full support for streaming std::(w)string
(optimized for C++11 and onwards)
added tests for optimized std::(w)string support
extend character translators with std::(w)string support also
Diffstat (limited to 'TAO/tao/Codeset')
-rw-r--r-- | TAO/tao/Codeset/UTF16_BOM_Translator.cpp | 68 | ||||
-rw-r--r-- | TAO/tao/Codeset/UTF16_BOM_Translator.h | 4 | ||||
-rw-r--r-- | TAO/tao/Codeset/UTF8_Latin1_Translator.cpp | 49 | ||||
-rw-r--r-- | TAO/tao/Codeset/UTF8_Latin1_Translator.h | 2 |
4 files changed, 123 insertions, 0 deletions
diff --git a/TAO/tao/Codeset/UTF16_BOM_Translator.cpp b/TAO/tao/Codeset/UTF16_BOM_Translator.cpp index ad66904c07a..0d0169cdd11 100644 --- a/TAO/tao/Codeset/UTF16_BOM_Translator.cpp +++ b/TAO/tao/Codeset/UTF16_BOM_Translator.cpp @@ -165,6 +165,74 @@ TAO_UTF16_BOM_Translator::read_wstring (ACE_InputCDR &cdr, return 0; } +#if !defined(ACE_LACKS_STD_WSTRING) +ACE_CDR::Boolean +TAO_UTF16_BOM_Translator::read_wstring (ACE_InputCDR &cdr, + std::wstring &x) +{ +#if defined (ACE_HAS_CPP11) + ACE_CDR::ULong len; + if (!this->read_4 (cdr, &len)) + return false; + + // A check for the length being too great is done later in the + // call to read_char_array but we want to have it done before + // the memory is allocated. + if (len > 0 && len <= cdr.length ()) + { + if (static_cast<ACE_CDR::Short> (this->major_version (cdr)) == 1 + && static_cast<ACE_CDR::Short> (this->minor_version (cdr)) > 1) + { + len /= ACE_UTF16_CODEPOINT_SIZE; + + try + { + x.resize (len); + } + catch (const std::bad_alloc&) + { + return false; + } + + if (this->read_wchar_array_i (cdr, &x[0], len, 1)) + { + // Since reading the array may have adjusted the length, + // shrink to fit + x.resize (len); + return true; + } + } + else + { + try + { + x.resize (len); + } + catch (const std::bad_alloc&) + { + return false; + } + + if (this->read_wchar_array (cdr, &x[0], len)) + { + x.resize (len-1); // drop terminating zero wchar read from stream + return true; + } + } + } + else if (len == 0) + { + x.clear (); + return true; + } + x.clear (); + return false; +#else + return this->ACE_WChar_Codeset_Translator::read_wstring (cdr, x); +#endif +} +#endif + ACE_CDR::Boolean TAO_UTF16_BOM_Translator::read_wchar_array_i (ACE_InputCDR & cdr, ACE_CDR::WChar *x, diff --git a/TAO/tao/Codeset/UTF16_BOM_Translator.h b/TAO/tao/Codeset/UTF16_BOM_Translator.h index 20d9ac2ad1e..19c3b8eb159 100644 --- a/TAO/tao/Codeset/UTF16_BOM_Translator.h +++ b/TAO/tao/Codeset/UTF16_BOM_Translator.h @@ -52,6 +52,10 @@ public: ACE_CDR::WChar &); virtual ACE_CDR::Boolean read_wstring (ACE_InputCDR &, ACE_CDR::WChar *&); +#if !defined(ACE_LACKS_STD_WSTRING) + virtual ACE_CDR::Boolean read_wstring (ACE_InputCDR&, + std::wstring &); +#endif virtual ACE_CDR::Boolean read_wchar_array (ACE_InputCDR &, ACE_CDR::WChar *, ACE_CDR::ULong); diff --git a/TAO/tao/Codeset/UTF8_Latin1_Translator.cpp b/TAO/tao/Codeset/UTF8_Latin1_Translator.cpp index 5dfb557ff0b..6da6df8f67a 100644 --- a/TAO/tao/Codeset/UTF8_Latin1_Translator.cpp +++ b/TAO/tao/Codeset/UTF8_Latin1_Translator.cpp @@ -118,6 +118,55 @@ TAO_UTF8_Latin1_Translator::read_string (ACE_InputCDR &cdr, } ACE_CDR::Boolean +TAO_UTF8_Latin1_Translator::read_string (ACE_InputCDR &cdr, + std::string &x) +{ +#if defined (ACE_HAS_CPP11) + ACE_CDR::ULong len; + if (!cdr.read_ulong (len)) + return false; + + // A check for the length being too great is done later in the + // call to read_char_array but we want to have it done before + // the memory is allocated. + if (len > 0 && len <= cdr.length()) + { + // detract terminating '\0' from length + len--; + try + { + x.resize (len); + } + catch (const std::bad_alloc&) + { + return false; + } + + // pos keeps track of the character position, it will never be + // greater than len + size_t pos = 0; + ACE_CDR::ULong incr = 1; + for (ACE_CDR::ULong i = 0; incr > 0 && i < len; i += incr) + { + incr = this->read_char_i(cdr,x[pos++]); + } + if (incr > 0) + { + // read terminating '\0' from stream + ACE_CDR::Char c; + incr = this->read_char_i(cdr, c); + return (incr > 0); + } + } + + x.clear (); + return false; +#else + return this->ACE_Char_Codeset_Translator::read_string (cdr, x); +#endif +} + +ACE_CDR::Boolean TAO_UTF8_Latin1_Translator::read_char_array (ACE_InputCDR & cdr, ACE_CDR::Char *x, ACE_CDR::ULong length) diff --git a/TAO/tao/Codeset/UTF8_Latin1_Translator.h b/TAO/tao/Codeset/UTF8_Latin1_Translator.h index 2fac7ed1f80..6bccd0bee8c 100644 --- a/TAO/tao/Codeset/UTF8_Latin1_Translator.h +++ b/TAO/tao/Codeset/UTF8_Latin1_Translator.h @@ -56,6 +56,8 @@ public: ACE_CDR::Char &); virtual ACE_CDR::Boolean read_string (ACE_InputCDR &, ACE_CDR::Char *&); + virtual ACE_CDR::Boolean read_string (ACE_InputCDR &, + std::string &); virtual ACE_CDR::Boolean read_char_array (ACE_InputCDR &, ACE_CDR::Char *, ACE_CDR::ULong); |