From: Christian P. <cp...@us...> - 2005-07-06 12:59:20
|
Update of /cvsroot/pclasses/pclasses2/src/Unicode In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25949/src/Unicode Modified Files: Converter.cpp Char.cpp Makefile.am Added Files: NumberFormat.cpp Log Message: - Renamed Unicode::Char::nl() to Unicode::Char::lf() - Added some documentation and methods to Unicode::Converter - Added Unicode::NumberFormat Index: Converter.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/Unicode/Converter.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- Converter.cpp 24 May 2005 02:32:59 -0000 1.1 +++ Converter.cpp 6 Jul 2005 12:59:10 -0000 1.2 @@ -40,6 +40,33 @@ ucnv_close((UConverter*)_private); } +std::string Converter::name() const throw(UnicodeError) +{ + UErrorCode errorCode = U_ZERO_ERROR; + + std::string ret = ucnv_getName((UConverter*)_private, &errorCode); + if(U_FAILURE(errorCode)) + throw UnicodeError(errorCode, "Could not get converter name", P_SOURCEINFO); + + return ret; +} + +String Converter::displayName(const char* locale) const throw(UnicodeError) +{ + UErrorCode errorCode = U_ZERO_ERROR; + size_t neededSize = ucnv_getDisplayName((UConverter*)_private, locale, 0, 0, &errorCode); + + ScopedArrayPtr<uchar16_t> displayName(new uchar16_t[neededSize + 1]); + + ucnv_getDisplayName((UConverter*)_private, locale, displayName.get(), neededSize + 1, &errorCode); + if(U_FAILURE(errorCode)) + throw UnicodeError(errorCode, "Could not get converter displayname", P_SOURCEINFO); + + String ret; + ret.acquire(displayName.release(), neededSize + 1, neededSize); + return ret; +} + String Converter::toUnicode(const std::string& str) throw(UnicodeError) { return toUnicode(str.c_str(), str.size()); @@ -69,19 +96,15 @@ return retStr; } -std::string Converter::fromUnicode(const String& str) throw(UnicodeError) +std::string fromUnicodeChars(UConverter* conv, const uchar16_t* str, size_t srcLen) { - if(str.empty()) - return std::string(); - UErrorCode errorCode = U_ZERO_ERROR; - size_t srcLen = str.size(); - size_t strSize = UCNV_GET_MAX_BYTES_FOR_STRING(srcLen, ucnv_getMaxCharSize((UConverter*)_private)) + 1; + size_t strSize = UCNV_GET_MAX_BYTES_FOR_STRING(srcLen, ucnv_getMaxCharSize(conv) + 1); ScopedArrayPtr<char> ret(new char[strSize]); - ucnv_fromUChars((UConverter*)_private, ret.get(), strSize, - str.data(), srcLen, &errorCode); + ucnv_fromUChars(conv, ret.get(), strSize, + str, srcLen, &errorCode); if(U_FAILURE(errorCode)) throw UnicodeError(errorCode, "Could not convert string from unicode", P_SOURCEINFO); @@ -91,6 +114,67 @@ return retStr; } +std::string Converter::fromUnicode(const String& str) throw(UnicodeError) +{ + if(str.empty()) + return std::string(); + + return fromUnicodeChars((UConverter*)_private, str.data(), str.size()); +} + +std::string Converter::fromUnicode(Char ch) throw(UnicodeError) +{ + uchar16_t tmp[2]; + size_t offset = 0; + + U16_APPEND_UNSAFE(tmp, offset, (uint32_t)ch); + return fromUnicodeChars((UConverter*)_private, tmp, offset); +} + +Converter::StringVector Converter::availableNames() throw(UnicodeError) +{ + int count = ucnv_countAvailable(); + + StringVector ret; + ret.reserve(count); + + for(int i = 0; i < count; ++i) + ret.at(i) = ucnv_getAvailableName(i); + + return ret; +} + +Converter::StringVector Converter::aliasNames(const std::string& name) throw(UnicodeError) +{ + UErrorCode errorCode = U_ZERO_ERROR; + + int count = ucnv_countAliases(name.c_str(), &errorCode); + if(U_FAILURE(errorCode)) + throw UnicodeError(errorCode, "Could not get count of alias names", P_SOURCEINFO); + + StringVector ret; + ret.reserve(count); + + for(int i = 0; i < count; ++i) + { + const char* alias = ucnv_getAlias(name.c_str(), i, &errorCode); + + if(U_FAILURE(errorCode)) + throw UnicodeError(errorCode, "Could not get alias name", P_SOURCEINFO); + + if(errorCode == U_AMBIGUOUS_ALIAS_WARNING) + { + errorCode = U_ZERO_ERROR; + continue; + } + + ret.at(i) = alias; + } + + return ret; +} + + } // !namespace Unicode } // !namespace P Index: Makefile.am =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/Unicode/Makefile.am,v retrieving revision 1.13 retrieving revision 1.14 diff -u -d -r1.13 -r1.14 --- Makefile.am 4 Jul 2005 23:36:15 -0000 1.13 +++ Makefile.am 6 Jul 2005 12:59:10 -0000 1.14 @@ -6,7 +6,7 @@ CPPFLAGS = -DPUNICODE_BUILD $(ICU_CFLAGS) libpclasses_unicode_la_SOURCES = Char.cpp String.cpp Converter.cpp \ - UnicodeError.cpp + NumberFormat.cpp UnicodeError.cpp libpclasses_unicode_la_LDFLAGS = -no-undefined Index: Char.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/Unicode/Char.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- Char.cpp 24 May 2005 02:32:59 -0000 1.8 +++ Char.cpp 6 Jul 2005 12:59:10 -0000 1.9 @@ -178,15 +178,15 @@ return _eof; } -const Char& Char::nl() throw() +const Char& Char::lf() throw() { - static Char _nl('\n'); + static Char _nl(0x000A); return _nl; } const Char& Char::cr() throw() { - static Char _cr('\r'); + static Char _cr(0x000D); return _cr; } --- NEW FILE: NumberFormat.cpp --- /*************************************************************************** * Copyright (C) 2005 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/ScopedArrayPtr.h" #include "pclasses/Unicode/NumberFormat.h" #include <unicode/unum.h> /* #include <unicode/uloc.h> #include <unicode/umisc.h> #include <unicode/parseerr.h>*/ namespace P { namespace Unicode { UNumberFormatStyle Style2UNumberFormatStyle(NumberFormat::Style s) { UNumberFormatStyle ret = UNUM_DEFAULT; switch(s) { case NumberFormat::Decimal: ret = UNUM_DECIMAL; break; case NumberFormat::Currency: ret = UNUM_CURRENCY; break; case NumberFormat::Percent: ret = UNUM_PERCENT; break; case NumberFormat::Scientific: ret = UNUM_SCIENTIFIC; break; case NumberFormat::Spellout: ret = UNUM_SPELLOUT; break; case NumberFormat::Ordinal: ret = UNUM_ORDINAL; break; default: break; } return ret; } NumberFormat::NumberFormat(Style s, const char* locale) { UErrorCode error = U_ZERO_ERROR; _private = (void*)unum_open(Style2UNumberFormatStyle(s), NULL, 0, locale, NULL, &error); if(U_FAILURE(error)) throw UnicodeError(error, "Could not init number formater for given locale", P_SOURCEINFO); } NumberFormat::~NumberFormat() throw() { unum_close((UNumberFormat*)_private); } void NumberFormat::setWidth(unsigned int width) { unum_setAttribute((UNumberFormat*)_private, UNUM_FORMAT_WIDTH, width); } unsigned int NumberFormat::width() const throw() { return unum_getAttribute((UNumberFormat*)_private, UNUM_FORMAT_WIDTH); } UNumberFormatPadPosition PadPosition2UNumberFormatPadPosition(NumberFormat::PadPosition pos) { UNumberFormatPadPosition ret; switch(pos) { case NumberFormat::BeforePrefix: ret = UNUM_PAD_BEFORE_PREFIX; break; case NumberFormat::AfterPrefix: ret = UNUM_PAD_AFTER_PREFIX; break; case NumberFormat::BeforeSuffix: ret = UNUM_PAD_BEFORE_SUFFIX; break; case NumberFormat::AfterSuffix: default: ret = UNUM_PAD_AFTER_SUFFIX; break; } return ret; } void NumberFormat::setPadPosition(PadPosition p) { unum_setAttribute((UNumberFormat*)_private, UNUM_PADDING_POSITION, PadPosition2UNumberFormatPadPosition(p)); } NumberFormat::PadPosition UNumberFormatPadPosition2PadPosition(UNumberFormatPadPosition pos) { NumberFormat::PadPosition ret; switch(pos) { case UNUM_PAD_BEFORE_PREFIX: ret = NumberFormat::BeforePrefix; break; case UNUM_PAD_AFTER_PREFIX: ret = NumberFormat::AfterPrefix; break; case UNUM_PAD_BEFORE_SUFFIX: ret = NumberFormat::BeforeSuffix; break; case UNUM_PAD_AFTER_SUFFIX: ret = NumberFormat::AfterSuffix; break; } return ret; } NumberFormat::PadPosition NumberFormat::padPosition() const throw() { return UNumberFormatPadPosition2PadPosition( (UNumberFormatPadPosition)unum_getAttribute((UNumberFormat*)_private, UNUM_PADDING_POSITION)); } void NumberFormatSetTextAttr(UNumberFormat* format, UNumberFormatTextAttribute attr, Unicode::Char ch) { uchar16_t tmp[2]; size_t offset = 0; U16_APPEND_UNSAFE(tmp, offset, (uint32_t)ch); UErrorCode errorCode = U_ZERO_ERROR; unum_setTextAttribute(format, attr, tmp, offset, &errorCode); if(U_FAILURE(errorCode)) throw UnicodeError(errorCode, "Could not set padding character", P_SOURCEINFO); } Char NumberFormatGetTextAttr(UNumberFormat* format, UNumberFormatTextAttribute attr) { uchar16_t tmp[3]; size_t len; UErrorCode errorCode = U_ZERO_ERROR; len = unum_getTextAttribute(format, attr, tmp, sizeof(tmp), &errorCode); if(U_FAILURE(errorCode)) throw UnicodeError(errorCode, "Could not set padding character", P_SOURCEINFO); size_t offset = 0; uint32_t ch; U16_GET_UNSAFE(tmp, offset, ch); return Char(ch); } void NumberFormat::setPadChar(Char ch) { NumberFormatSetTextAttr((UNumberFormat*)_private, UNUM_PADDING_CHARACTER, ch); } Char NumberFormat::padChar() const throw() { return NumberFormatGetTextAttr((UNumberFormat*)_private, UNUM_PADDING_CHARACTER); } void NumberFormat::setPositivePrefix(Char ch) { NumberFormatSetTextAttr((UNumberFormat*)_private, UNUM_POSITIVE_PREFIX, ch); } Char NumberFormat::positivePrefix() { return NumberFormatGetTextAttr((UNumberFormat*)_private, UNUM_POSITIVE_PREFIX); } void NumberFormat::setPositiveSuffix(Char ch) { NumberFormatSetTextAttr((UNumberFormat*)_private, UNUM_POSITIVE_SUFFIX, ch); } Char NumberFormat::positiveSuffix() { return NumberFormatGetTextAttr((UNumberFormat*)_private, UNUM_POSITIVE_SUFFIX); } void NumberFormat::setNegativePrefix(Char ch) { NumberFormatSetTextAttr((UNumberFormat*)_private, UNUM_NEGATIVE_PREFIX, ch); } Char NumberFormat::negativePrefix() { return NumberFormatGetTextAttr((UNumberFormat*)_private, UNUM_NEGATIVE_PREFIX); } void NumberFormat::setNegativeSuffix(Char ch) { NumberFormatSetTextAttr((UNumberFormat*)_private, UNUM_NEGATIVE_SUFFIX, ch); } Char NumberFormat::negativeSuffix() { return NumberFormatGetTextAttr((UNumberFormat*)_private, UNUM_NEGATIVE_SUFFIX); } String NumberFormat::format(Int32 val) throw(UnicodeError) { UErrorCode errorCode = U_ZERO_ERROR; size_t strLen = unum_format((UNumberFormat*)_private, val, NULL, 0, NULL, &errorCode); ScopedArrayPtr<uchar16_t> ret(new uchar16_t[strLen + 1]); errorCode = U_ZERO_ERROR; unum_format((UNumberFormat*)_private, val, ret.get(), strLen + 1, NULL, &errorCode); if(U_FAILURE(errorCode)) throw UnicodeError(errorCode, "Could not convert number to string", P_SOURCEINFO); String retStr; retStr.acquire(ret.release(), strLen + 1, strLen); return retStr; } String NumberFormat::format(Int64 val) throw(UnicodeError) { UErrorCode errorCode = U_ZERO_ERROR; size_t strLen = unum_formatInt64((UNumberFormat*)_private, val, NULL, 0, NULL, &errorCode); ScopedArrayPtr<uchar16_t> ret(new uchar16_t[strLen + 1]); errorCode = U_ZERO_ERROR; unum_formatInt64((UNumberFormat*)_private, val, ret.get(), strLen + 1, NULL, &errorCode); if(U_FAILURE(errorCode)) throw UnicodeError(errorCode, "Could not convert number to string", P_SOURCEINFO); String retStr; retStr.acquire(ret.release(), strLen + 1, strLen); return retStr; } String NumberFormat::format(double val) throw(UnicodeError) { UErrorCode errorCode = U_ZERO_ERROR; size_t strLen = unum_formatDouble((UNumberFormat*)_private, val, NULL, 0, NULL, &errorCode); ScopedArrayPtr<uchar16_t> ret(new uchar16_t[strLen + 1]); errorCode = U_ZERO_ERROR; unum_formatDouble((UNumberFormat*)_private, val, ret.get(), strLen + 1, NULL, &errorCode); if(U_FAILURE(errorCode)) throw UnicodeError(errorCode, "Could not convert number to string", P_SOURCEINFO); String retStr; retStr.acquire(ret.release(), strLen + 1, strLen); return retStr; } } // !namespace Unicode } // !namespace P |