diff options
author | Bernard Normier <bernard@zeroc.com> | 2006-01-09 22:35:30 +0000 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2006-01-09 22:35:30 +0000 |
commit | 7d8fd91fd6fd1b89ce886fb09d92607157484f11 (patch) | |
tree | 2d49ace40d9367daaffc6c5d2a8e7b5538850b90 | |
parent | Fixed unicode / string converter commit (diff) | |
download | ice-7d8fd91fd6fd1b89ce886fb09d92607157484f11.tar.bz2 ice-7d8fd91fd6fd1b89ce886fb09d92607157484f11.tar.xz ice-7d8fd91fd6fd1b89ce886fb09d92607157484f11.zip |
Added UTFConversionException to Unicode
-rw-r--r-- | cpp/include/IceUtil/Unicode.h | 26 | ||||
-rw-r--r-- | cpp/src/IceUtil/Unicode.cpp | 80 |
2 files changed, 96 insertions, 10 deletions
diff --git a/cpp/include/IceUtil/Unicode.h b/cpp/include/IceUtil/Unicode.h index 8e85c93b49e..4e78dbdfd92 100644 --- a/cpp/include/IceUtil/Unicode.h +++ b/cpp/include/IceUtil/Unicode.h @@ -11,6 +11,7 @@ #define ICE_UTIL_UNICODE_H #include <IceUtil/Config.h> +#include <IceUtil/Exception.h> namespace IceUtil { @@ -102,6 +103,7 @@ enum ConversionResult sourceIllegal /* source sequence is illegal/malformed */ }; + enum ConversionFlags { strictConversion = 0, @@ -125,6 +127,30 @@ ICE_UTIL_API ConversionResult convertUTF8ToUTFWstring(const Byte*& sourceStart, const Byte* sourceEnd, std::wstring& target, ConversionFlags flags); + + + +// +// UTFConversionException is raised by wstringToString() or stringToWstring() +// to report a conversion error +// +class ICE_UTIL_API UTFConversionException : public Exception +{ +public: + + UTFConversionException(const char*, int, ConversionResult); + virtual const std::string ice_name() const; + virtual void ice_print(std::ostream&) const; + virtual Exception* ice_clone() const; + virtual void ice_throw() const; + + ConversionResult conversionResult() const; +private: + + const ConversionResult _conversionResult; + static const char* _name; +}; + } #endif diff --git a/cpp/src/IceUtil/Unicode.cpp b/cpp/src/IceUtil/Unicode.cpp index 287ddbe1e7a..0429ee406a1 100644 --- a/cpp/src/IceUtil/Unicode.cpp +++ b/cpp/src/IceUtil/Unicode.cpp @@ -132,6 +132,60 @@ IceUtil::convertUTF8ToUTFWstring(const Byte*& sourceStart, const Byte* sourceEnd // wstringToString and stringToWstring // +const char* IceUtil::UTFConversionException::_name = "IceUtil::UTFConversionException"; + +IceUtil::UTFConversionException::UTFConversionException(const char* file, int line, + ConversionResult cr): + Exception(file, line), + _conversionResult(cr) +{} + +const string +IceUtil::UTFConversionException::ice_name() const +{ + return _name; +} + +void +IceUtil::UTFConversionException::ice_print(ostream& os) const +{ + Exception::ice_print(os); + switch(_conversionResult) + { + case sourceExhausted: + os << ": source exhausted"; + break; + case targetExhausted: + os << ": target exhausted"; + break; + case sourceIllegal: + os << ": illegal source"; + break; + default: + assert(0); + break; + }; +} + +IceUtil::Exception* +IceUtil::UTFConversionException::ice_clone() const +{ + return new UTFConversionException(*this); +} + +void +IceUtil::UTFConversionException::ice_throw() const +{ + throw *this; +} + +IceUtil::ConversionResult +IceUtil::UTFConversionException::conversionResult() const +{ + return _conversionResult; +} + + string IceUtil::wstringToString(const wstring& wstr) { @@ -145,17 +199,20 @@ IceUtil::wstringToString(const wstring& wstr) const wchar_t* sourceStart = wstr.data(); - ConversionResult result = + ConversionResult cr = convertUTFWstringToUTF8( sourceStart, sourceStart + wstr.size(), targetStart, targetEnd, lenientConversion); - if(result == conversionOK) + if(cr != conversionOK) { - string s(reinterpret_cast<char*>(outBuf), - static_cast<size_t>(targetStart - outBuf)); - s.swap(target); + delete[] outBuf; + throw UTFConversionException(__FILE__, __LINE__, cr); } + + string s(reinterpret_cast<char*>(outBuf), + static_cast<size_t>(targetStart - outBuf)); + s.swap(target); delete[] outBuf; return target; } @@ -166,11 +223,14 @@ IceUtil::stringToWstring(const string& str) wstring result; const Byte* sourceStart = reinterpret_cast<const Byte*>(str.data()); - convertUTF8ToUTFWstring(sourceStart, sourceStart + str.size(), - result, lenientConversion); - // - // TODO: check the ConversionResult and do something with it! - // + ConversionResult cr + = convertUTF8ToUTFWstring(sourceStart, sourceStart + str.size(), + result, lenientConversion); + + if(cr != conversionOK) + { + throw UTFConversionException(__FILE__, __LINE__, cr); + } return result; } |