Newer
Older
, toTranscode
, srcLen
, &err
);
}
if (U_FAILURE(err))
{
// Clean up if we got anything allocated
manager->deallocate(targetBuf);//delete [] targetBuf;
return 0;
}
// Cap it off to make sure
targetBuf[targetCap] = 0;
//
// If XMLCh and UChar are the same size, then we can return retVal
// as is. Else, we have to allocate another buffer and copy the data
// over to it.
//
XMLCh* actualRet;
if (sizeof(XMLCh) == sizeof(UChar))
{
actualRet = (XMLCh*)targetBuf;
}
else
{
actualRet = convertToXMLCh(targetBuf, manager);
manager->deallocate(targetBuf);//delete [] targetBuf;
}
return actualRet;
}
bool ICULCPTranscoder::transcode(const char* const toTranscode
, XMLCh* const toFill
, const unsigned int maxChars
, MemoryManager* const manager)
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
{
// Check for a couple of psycho corner cases
if (!toTranscode || !maxChars)
{
toFill[0] = 0;
return true;
}
if (!*toTranscode)
{
toFill[0] = 0;
return true;
}
// We'll need this in a couple of places below
const unsigned int srcLen = strlen(toTranscode);
//
// Set up the target buffer. If XMLCh and UChar are not the same size
// then we have to use a temp buffer and convert over.
//
UChar* targetBuf;
if (sizeof(XMLCh) == sizeof(UChar))
targetBuf = (UChar*)toFill;
else
targetBuf = (UChar*) manager->allocate
(
(maxChars + 1) * sizeof(UChar)
);//new UChar[maxChars + 1];
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
//
// Use a faux block to enforce a lock on the converter, which will
// unlock immediately after its completed.
//
UErrorCode err = U_ZERO_ERROR;
{
XMLMutexLock lockConverter(&fMutex);
ucnv_toUChars
(
fConverter
, targetBuf
, maxChars + 1
, toTranscode
, srcLen
, &err
);
}
if (U_FAILURE(err))
{
if (targetBuf != (UChar*)toFill)
manager->deallocate(targetBuf);//delete [] targetBuf;
return false;
}
// If the sizes are not the same, then copy the data over
if (sizeof(XMLCh) != sizeof(UChar))
{
UChar* srcPtr = targetBuf;
XMLCh* outPtr = toFill;
while (*srcPtr)
*outPtr++ = XMLCh(*srcPtr++);
*outPtr = 0;
// And delete the temp buffer
manager->deallocate(targetBuf);//delete [] targetBuf;
}
return true;
}
bool ICULCPTranscoder::transcode( const XMLCh* const toTranscode
, char* const toFill
, const unsigned int maxChars
, MemoryManager* const manager)
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
{
// Watch for a few psycho corner cases
if (!toTranscode || !maxChars)
{
toFill[0] = 0;
return true;
}
if (!*toTranscode)
{
toFill[0] = 0;
return true;
}
//
// If XMLCh and UChar are not the same size, then we have to make a
// temp copy of the text to pass to ICU.
//
const UChar* actualSrc;
UChar* ncActual = 0;
if (sizeof(XMLCh) == sizeof(UChar))
{
actualSrc = (const UChar*)toTranscode;
}
else
{
// Allocate a non-const temp buf, but store it also in the actual
ncActual = convertToUChar(toTranscode, 0, manager);
actualSrc = ncActual;
}
// Insure that the temp buffer, if any, gets cleaned up via the nc pointer
ArrayJanitor<UChar> janTmp(ncActual, manager);
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
//
// Use a faux block to enforce a lock on the converter while we do this.
// It will be released immediately after its done.
//
UErrorCode err = U_ZERO_ERROR;
int32_t targetCap;
{
XMLMutexLock lockConverter(&fMutex);
targetCap = ucnv_fromUChars
(
fConverter
, toFill
, maxChars
, actualSrc
, -1
, &err
);
}
if (U_FAILURE(err))
return false;
toFill[targetCap] = 0;
return true;
}