scuffed-code/icu4c/source/common/convert.cpp

473 lines
14 KiB
C++
Raw Normal View History

/*
**********************************************************************
* Copyright (C) 1998-1999, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*/
1999-08-16 21:50:52 +00:00
/* C++ wrappers for the ICUs Codeset Conversion Routines*/
class Locale;
class UnicodeString;
class Mutex;
#include "unicode/utypes.h"
#include "unicode/resbund.h"
1999-08-16 21:50:52 +00:00
#include "cmemory.h"
#include "mutex.h"
extern "C" {
#include "ucnv_io.h"
#include "unicode/ucnv_err.h"
#include "ucnv_bld.h"
#include "unicode/ucnv.h"
1999-08-16 21:50:52 +00:00
}
#include "unicode/convert.h"
1999-08-16 21:50:52 +00:00
/* list of converter and alias names */
const char **UnicodeConverter::availableConverterNames=NULL;
int32_t UnicodeConverter::availableConverterNamesCount=0;
1999-08-16 21:50:52 +00:00
UnicodeConverter::UnicodeConverter()
1999-08-16 21:50:52 +00:00
{
UErrorCode err = U_ZERO_ERROR;
1999-08-16 21:50:52 +00:00
myUnicodeConverter = ucnv_open(NULL, &err);
}
UnicodeConverter::UnicodeConverter(const char* name, UErrorCode& err)
1999-08-16 21:50:52 +00:00
{
myUnicodeConverter = ucnv_open(name, &err);
}
UnicodeConverter::UnicodeConverter(const UnicodeString& name, UErrorCode& err)
1999-08-16 21:50:52 +00:00
{
char myName[UCNV_MAX_CONVERTER_NAME_LENGTH];
1999-08-16 21:50:52 +00:00
int i;
name.extract(0, i = name.length(), myName);
1999-08-16 21:50:52 +00:00
myName[i]='\0';
myUnicodeConverter = ucnv_open(myName, &err);
}
UnicodeConverter::UnicodeConverter(int32_t codepageNumber,
UConverterPlatform platform,
1999-08-16 21:50:52 +00:00
UErrorCode& err)
{
myUnicodeConverter = ucnv_openCCSID(codepageNumber,
platform,
&err);
}
UnicodeConverter& UnicodeConverter::operator=(const UnicodeConverter& that)
1999-08-16 21:50:52 +00:00
{
{
/*Decrements the overwritten converter's ref count
*Increments the assigner converter's ref count
*/
Mutex updateReferenceCounters;
if (myUnicodeConverter->sharedData->referenceCounter != 0 && myUnicodeConverter->sharedData->referenceCounter != (uint32_t) ~0) {
myUnicodeConverter->sharedData->referenceCounter--;
}
if (that.myUnicodeConverter->sharedData->referenceCounter != (uint32_t) ~0) {
that.myUnicodeConverter->sharedData->referenceCounter++;
}
1999-08-16 21:50:52 +00:00
}
*myUnicodeConverter = *(that.myUnicodeConverter);
return *this;
}
UBool UnicodeConverter::operator==(const UnicodeConverter& that) const
1999-08-16 21:50:52 +00:00
{
return
(myUnicodeConverter->sharedData == that.myUnicodeConverter->sharedData) &&
1999-08-16 21:50:52 +00:00
(myUnicodeConverter->fromCharErrorBehaviour == that.myUnicodeConverter->fromCharErrorBehaviour) &&
(myUnicodeConverter->toUContext == that.myUnicodeConverter->toUContext) &&
1999-08-16 21:50:52 +00:00
(myUnicodeConverter->toUnicodeStatus == that.myUnicodeConverter->toUnicodeStatus) &&
(myUnicodeConverter->subChar1 == that.myUnicodeConverter->subChar1) &&
1999-08-16 21:50:52 +00:00
(myUnicodeConverter->subCharLen == that.myUnicodeConverter->subCharLen) &&
(uprv_memcmp(myUnicodeConverter->subChar, that.myUnicodeConverter->subChar, myUnicodeConverter->subCharLen) == 0) &&
1999-08-16 21:50:52 +00:00
(myUnicodeConverter->UCharErrorBufferLength == that.myUnicodeConverter->UCharErrorBufferLength) &&
(myUnicodeConverter->charErrorBufferLength == that.myUnicodeConverter->charErrorBufferLength) &&
(uprv_memcmp(myUnicodeConverter->UCharErrorBuffer, that.myUnicodeConverter->UCharErrorBuffer, myUnicodeConverter->UCharErrorBufferLength) == 0) &&
(uprv_memcmp(myUnicodeConverter->charErrorBuffer, that.myUnicodeConverter->charErrorBuffer, myUnicodeConverter->charErrorBufferLength) == 0) &&
(myUnicodeConverter->fromUCharErrorBehaviour == that.myUnicodeConverter->fromUCharErrorBehaviour) &&
(myUnicodeConverter->fromUContext == that.myUnicodeConverter->fromUContext);
1999-08-16 21:50:52 +00:00
}
UBool UnicodeConverter::operator!=(const UnicodeConverter& that) const
1999-08-16 21:50:52 +00:00
{
return !(*this == that);
}
UnicodeConverter::UnicodeConverter(const UnicodeConverter& that)
1999-08-16 21:50:52 +00:00
{
/*increments the referenceCounter to let the static table know
*it has one more client
*/
myUnicodeConverter = (UConverter *)uprv_malloc(sizeof(UConverter)); //new UConverter;
1999-08-16 21:50:52 +00:00
{
Mutex updateReferenceCounter;
if (that.myUnicodeConverter->sharedData->referenceCounter != (uint32_t) ~0) {
that.myUnicodeConverter->sharedData->referenceCounter++;
}
1999-08-16 21:50:52 +00:00
}
*myUnicodeConverter = *(that.myUnicodeConverter);
}
UnicodeConverter::~UnicodeConverter()
1999-08-16 21:50:52 +00:00
{
ucnv_close(myUnicodeConverter);
}
void
UnicodeConverter::fromUnicodeString(char* target,
1999-08-16 21:50:52 +00:00
int32_t& targetSize,
const UnicodeString& source,
UErrorCode& err) const
{
const UChar* mySource = NULL;
int32_t mySourceLength = 0;
UConverter myConverter;
char *myTarget = NULL;
if (U_FAILURE(err)) return;
1999-08-16 21:50:52 +00:00
if ((myUnicodeConverter == NULL) || source.isBogus() || (targetSize <= 0))
{
err = U_ILLEGAL_ARGUMENT_ERROR;
1999-08-16 21:50:52 +00:00
return;
}
/*makes a local copy of the UnicodeConverter*/
myConverter = *myUnicodeConverter;
/*Removes all state info on the UnicodeConverter*/
ucnv_reset(&myConverter);
mySourceLength = source.length();
mySource = source.getArrayStart();
1999-08-16 21:50:52 +00:00
myTarget = target;
ucnv_fromUnicode(&myConverter,
&myTarget,
target + targetSize,
&mySource,
mySource + mySourceLength,
NULL,
TRUE,
&err);
targetSize = myTarget - target;
return;
}
void
UnicodeConverter::toUnicodeString(UnicodeString& target,
1999-08-16 21:50:52 +00:00
const char* source,
int32_t sourceSize,
UErrorCode& err) const
{
const char* mySource = source;
const char* mySourceLimit = source + sourceSize;
UChar* myTargetUChars = NULL;
UChar* myTargetUCharsAlias = NULL;
int32_t myTargetUCharsLength = 0;
UConverter myConverter;
if (U_FAILURE(err)) return;
1999-08-16 21:50:52 +00:00
if ((myUnicodeConverter == NULL) || target.isBogus() || (sourceSize <= 0))
{
err = U_ILLEGAL_ARGUMENT_ERROR;
1999-08-16 21:50:52 +00:00
return;
}
/*makes a local bitwise copy of the UnicodeConverter*/
myConverter = *myUnicodeConverter;
/*Removes all state info on the UnicodeConverter*/
ucnv_reset(&myConverter);
/*Allocates the theoritically (Not counting added bytes from the error functions) max buffer
*on a "normal" call, only one iteration will be necessary.
*/
myTargetUChars =
(UChar*)uprv_malloc(sizeof(UChar)*(myTargetUCharsLength = (sourceSize/(int32_t)getMinBytesPerChar())));
1999-08-16 21:50:52 +00:00
if (myTargetUChars == NULL)
{
err = U_MEMORY_ALLOCATION_ERROR;
1999-08-16 21:50:52 +00:00
return;
}
/*renders the target clean*/
target.remove();
/*Will loop until (re-use the same buffer) until no more memory is requested
*or an error (other than INDEX_OUTOF_BOUNDS) is encountered
*/
do
{
err = U_ZERO_ERROR;
1999-08-16 21:50:52 +00:00
myTargetUCharsAlias = myTargetUChars;
ucnv_toUnicode(&myConverter,
&myTargetUCharsAlias,
myTargetUChars + myTargetUCharsLength,
&mySource,
mySourceLimit,
NULL,
TRUE,
&err);
/*appends what we got thus far to the UnicodeString*/
target.replace((UTextOffset)target.length(),
1999-08-16 21:50:52 +00:00
myTargetUCharsAlias - myTargetUChars,
myTargetUChars,
myTargetUCharsAlias - myTargetUChars);
/*Checks for the integrity of target (UnicodeString) as it adds data to it*/
if (target.isBogus()) err = U_MEMORY_ALLOCATION_ERROR;
} while (err == U_BUFFER_OVERFLOW_ERROR);
1999-08-16 21:50:52 +00:00
uprv_free(myTargetUChars);
1999-08-16 21:50:52 +00:00
return;
}
void
UnicodeConverter::fromUnicode(char*& target,
1999-08-16 21:50:52 +00:00
const char* targetLimit,
const UChar*& source,
const UChar* sourceLimit,
int32_t *offsets,
UBool flush,
1999-08-16 21:50:52 +00:00
UErrorCode& err)
{
ucnv_fromUnicode(myUnicodeConverter,
&target,
targetLimit,
&source,
sourceLimit,
offsets,
flush,
&err);
}
void
UnicodeConverter::toUnicode(UChar*& target,
1999-08-16 21:50:52 +00:00
const UChar* targetLimit,
const char*& source,
const char* sourceLimit,
int32_t* offsets,
UBool flush,
1999-08-16 21:50:52 +00:00
UErrorCode& err)
{
ucnv_toUnicode(myUnicodeConverter,
&target,
targetLimit,
&source,
sourceLimit,
offsets,
flush,
&err);
}
const char*
UnicodeConverter::getName(UErrorCode& err) const
1999-08-16 21:50:52 +00:00
{
return ucnv_getName(myUnicodeConverter, &err);
}
int8_t
UnicodeConverter::getMaxBytesPerChar() const
1999-08-16 21:50:52 +00:00
{
return ucnv_getMaxCharSize(myUnicodeConverter);
}
int8_t
UnicodeConverter::getMinBytesPerChar() const
1999-08-16 21:50:52 +00:00
{
return ucnv_getMinCharSize(myUnicodeConverter);
}
void
UnicodeConverter::getSubstitutionChars(char* subChars,
1999-08-16 21:50:52 +00:00
int8_t& len,
UErrorCode& err) const
{
ucnv_getSubstChars(myUnicodeConverter,
subChars,
&len,
&err);
}
void
UnicodeConverter::setSubstitutionChars(const char* subChars,
1999-08-16 21:50:52 +00:00
int8_t len,
UErrorCode& err)
{
ucnv_setSubstChars(myUnicodeConverter,
subChars,
len,
&err);
}
void
UnicodeConverter::resetState()
1999-08-16 21:50:52 +00:00
{
ucnv_reset(myUnicodeConverter);
}
int32_t
UnicodeConverter::getCodepage(UErrorCode& err) const
1999-08-16 21:50:52 +00:00
{
return ucnv_getCCSID(myUnicodeConverter, &err);
}
void
UnicodeConverter::getMissingCharAction(UConverterToUCallback *action,
void **context) const
1999-08-16 21:50:52 +00:00
{
ucnv_getToUCallBack(myUnicodeConverter, action, context);
1999-08-16 21:50:52 +00:00
}
void
UnicodeConverter::getMissingUnicodeAction(UConverterFromUCallback *action,
void **context) const
1999-08-16 21:50:52 +00:00
{
ucnv_getFromUCallBack(myUnicodeConverter, action, context);
1999-08-16 21:50:52 +00:00
}
void
UnicodeConverter::setMissingCharAction(UConverterToUCallback newAction,
void *newContext,
UConverterToUCallback *oldAction,
void **oldContext,
1999-08-16 21:50:52 +00:00
UErrorCode& err)
{
ucnv_setToUCallBack(myUnicodeConverter, newAction, newContext, oldAction, oldContext, &err);
1999-08-16 21:50:52 +00:00
}
void
UnicodeConverter::setMissingUnicodeAction(UConverterFromUCallback newAction,
void* newContext,
UConverterFromUCallback *oldAction,
void** oldContext,
1999-08-16 21:50:52 +00:00
UErrorCode& err)
{
ucnv_setFromUCallBack(myUnicodeConverter, newAction, newContext, oldAction, oldContext, &err);
1999-08-16 21:50:52 +00:00
}
void
UnicodeConverter::getDisplayName(const Locale& displayLocale,
1999-08-16 21:50:52 +00:00
UnicodeString& displayName) const
{
UErrorCode err = U_ZERO_ERROR;
UChar name[UCNV_MAX_CONVERTER_NAME_LENGTH];
int32_t length = ucnv_getDisplayName(myUnicodeConverter, displayLocale.getName(), name, sizeof(name), &err);
if (U_SUCCESS(err))
1999-08-16 21:50:52 +00:00
{
displayName.replace(0, 0x7fffffff, name, length);
1999-08-16 21:50:52 +00:00
}
else
1999-08-16 21:50:52 +00:00
{
/*Error While creating the resource bundle use the internal name instead*/
displayName.remove();
displayName = getName(err); /*Get the raw ASCII name*/
}
return;
}
UConverterPlatform
UnicodeConverter::getCodepagePlatform(UErrorCode &err) const
1999-08-16 21:50:52 +00:00
{
return ucnv_getPlatform(myUnicodeConverter, &err);
}
UConverterType UnicodeConverter::getType() const
1999-08-16 21:50:52 +00:00
{
return ucnv_getType(myUnicodeConverter);
}
void UnicodeConverter::getStarters(UBool starters[256],
1999-08-16 21:50:52 +00:00
UErrorCode& err) const
{
ucnv_getStarters(myUnicodeConverter,
starters,
&err);
return;
}
const char* const*
UnicodeConverter::getAvailableNames(int32_t& num, UErrorCode& err)
1999-08-16 21:50:52 +00:00
{
if(U_FAILURE(err)) {
num = 0;
return NULL;
}
if (availableConverterNames==NULL) {
int32_t count = ucnv_io_countAvailableConverters(&err);
if (count > 0) {
const char **names = new const char *[count];
if (names != NULL) {
ucnv_io_fillAvailableConverters(names, &err);
/* in the mutex block, set the data for this process */
umtx_lock(0);
if (availableConverterNames == NULL) {
availableConverterNamesCount = count;
availableConverterNames = names;
names = 0;
}
umtx_unlock(0);
/* if a different thread set it first, then delete the extra data */
if (names != 0) {
delete [] names;
}
} else {
num = 0;
err = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
}
}
num = availableConverterNamesCount;
return availableConverterNames;
1999-08-16 21:50:52 +00:00
}
int32_t UnicodeConverter::flushCache()
1999-08-16 21:50:52 +00:00
{
return ucnv_flushCache();
}
/* HSYS: To be cleaned up. The usage of UChar* and UnicodeString in
the C++ APIs need to be revisited. */
void UnicodeConverter::fixFileSeparator(UnicodeString& source) const {
if(this==NULL || &source==NULL || source.length()<=0) {
return;
}
ucnv_fixFileSeparator(myUnicodeConverter, source.getArrayStart(), source.length());
}
UBool UnicodeConverter::isAmbiguous(void) const
{
return ucnv_isAmbiguous(myUnicodeConverter);
}