Newer
Older
Khaled Noaman
committed
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);
Khaled Noaman
committed
actualSrc = ncActual;
}
// Insure that the temp buffer, if any, gets cleaned up via the nc pointer
ArrayJanitor<UChar> janTmp(ncActual, manager);
Khaled Noaman
committed
// Caculate a return buffer size not too big, but less likely to overflow
int32_t targetLen = (int32_t)(srcLen * 1.25);
// Allocate the return buffer
retBuf = (char*) manager->allocate((targetLen + 1) * sizeof(char));//new char[targetLen + 1];
Khaled Noaman
committed
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
//
// Lock now while we call the converter. Use a faux block to do the
// lock so that it unlocks immediately afterwards.
//
UErrorCode err = U_ZERO_ERROR;
int32_t targetCap;
{
XMLMutexLock lockConverter(&fMutex);
targetCap = ucnv_fromUChars
(
fConverter
, retBuf
, targetLen + 1
, actualSrc
, -1
, &err
);
}
// If targetLen is not enough then buffer overflow might occur
if ((err == U_BUFFER_OVERFLOW_ERROR) || (err == U_STRING_NOT_TERMINATED_WARNING))
Khaled Noaman
committed
{
//
// Reset the error, delete the old buffer, allocate a new one,
// and try again.
//
err = U_ZERO_ERROR;
manager->deallocate(retBuf);//delete [] retBuf;
retBuf = (char*) manager->allocate((targetCap + 1) * sizeof(char));//new char[targetCap + 1];
Khaled Noaman
committed
// Lock again before we retry
XMLMutexLock lockConverter(&fMutex);
targetCap = ucnv_fromUChars
(
fConverter
, retBuf
, targetCap
, actualSrc
, -1
, &err
);
}
if (U_FAILURE(err))
{
manager->deallocate(retBuf);//delete [] retBuf;
Khaled Noaman
committed
return 0;
}
return retBuf;
}
XMLCh* ICULCPTranscoder::transcode(const char* const toTranscode)
{
// Watch for a few pyscho corner cases
if (!toTranscode)
return 0;
if (!*toTranscode)
{
XMLCh* retVal = new XMLCh[1];
retVal[0] = 0;
return retVal;
}
//
// Get the length of the string to transcode. The Unicode string will
Khaled Noaman
committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
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
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
// almost always be no more chars than were in the source, so this is
// the best guess as to the storage needed.
//
const int32_t srcLen = (int32_t)strlen(toTranscode);
// We need a target buffer of UChars to fill in
UChar* targetBuf = 0;
// Now lock while we do these calculations
UErrorCode err = U_ZERO_ERROR;
int32_t targetCap;
{
XMLMutexLock lockConverter(&fMutex);
//
// Here we don't know what the target length will be so use 0 and
// expect an U_BUFFER_OVERFLOW_ERROR in which case it'd get resolved
// by the correct capacity value.
//
targetCap = ucnv_toUChars
(
fConverter
, 0
, 0
, toTranscode
, srcLen
, &err
);
if (err != U_BUFFER_OVERFLOW_ERROR)
return 0;
err = U_ZERO_ERROR;
targetBuf = new UChar[targetCap + 1];
ucnv_toUChars
(
fConverter
, targetBuf
, targetCap
, toTranscode
, srcLen
, &err
);
}
if (U_FAILURE(err))
{
// Clean up if we got anything allocated
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);
delete [] targetBuf;
}
return actualRet;
}
XMLCh* ICULCPTranscoder::transcode(const char* const toTranscode,
MemoryManager* const manager)
{
// Watch for a few pyscho corner cases
if (!toTranscode)
return 0;
if (!*toTranscode)
{
XMLCh* retVal = (XMLCh*) manager->allocate(sizeof(XMLCh));//new XMLCh[1];
Khaled Noaman
committed
retVal[0] = 0;
return retVal;
}
//
// Get the length of the string to transcode. The Unicode string will
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
// almost always be no more chars than were in the source, so this is
// the best guess as to the storage needed.
//
const int32_t srcLen = (int32_t)strlen(toTranscode);
// We need a target buffer of UChars to fill in
UChar* targetBuf = 0;
// Now lock while we do these calculations
UErrorCode err = U_ZERO_ERROR;
int32_t targetCap;
{
XMLMutexLock lockConverter(&fMutex);
//
// Here we don't know what the target length will be so use 0 and
// expect an U_BUFFER_OVERFLOW_ERROR in which case it'd get resolved
// by the correct capacity value.
//
targetCap = ucnv_toUChars
(
fConverter
, 0
, 0
, toTranscode
, srcLen
, &err
);
if (err != U_BUFFER_OVERFLOW_ERROR)
return 0;
err = U_ZERO_ERROR;
targetBuf = (UChar*) manager->allocate((targetCap+1) * sizeof(UChar));//new UChar[targetCap + 1];
ucnv_toUChars
(
fConverter
, targetBuf
, targetCap
, 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;
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
}
return actualRet;
}
bool ICULCPTranscoder::transcode(const char* const toTranscode
, XMLCh* const toFill
, const unsigned int maxChars)
{
// 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*) XMLPlatformUtils::fgMemoryManager->allocate
(
(maxChars + 1) * sizeof(UChar)
);//new UChar[maxChars + 1];
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
//
// 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)
XMLPlatformUtils::fgMemoryManager->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
XMLPlatformUtils::fgMemoryManager->deallocate(targetBuf);//delete [] targetBuf;
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
}
return true;
}
bool ICULCPTranscoder::transcode( const XMLCh* const toTranscode
, char* const toFill
, const unsigned int maxChars)
{
// 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, XMLPlatformUtils::fgMemoryManager);
actualSrc = ncActual;
}
// Insure that the temp buffer, if any, gets cleaned up via the nc pointer
ArrayJanitor<UChar> janTmp(ncActual, XMLPlatformUtils::fgMemoryManager);
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
//
// 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;
}