char *aescstrdup(const UChar* unichars,int32_t length){ char *newString,*targetLimit,*target; UConverterFromUCallback cb; const void *p; UErrorCode errorCode = U_ZERO_ERROR; #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY # if U_PLATFORM == U_PF_OS390 static const char convName[] = "ibm-1047"; # else static const char convName[] = "ibm-37"; # endif #else static const char convName[] = "US-ASCII"; #endif UConverter* conv = ucnv_open(convName, &errorCode); if(length==-1){ length = u_strlen( unichars); } newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1)); target = newString; targetLimit = newString+sizeof(char) * 8 * (length +1); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode); ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode); ucnv_close(conv); *target = '\0'; return newString; }
UChar* myFormatit(UDateFormat* datdef, UDate d1) { UChar *result1=NULL; int32_t resultlength, resultlengthneeded; UErrorCode status = U_ZERO_ERROR; resultlength=0; resultlengthneeded=udat_format(datdef, d1, NULL, resultlength, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthneeded+1; /*result1=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /*this leaks*/ result1=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/ udat_format(datdef, d1, result1, resultlength, NULL, &status); } if(U_FAILURE(status)) { log_err("FAIL: Error in formatting using udat_format(.....): %s\n", myErrorName(status)); return 0; } return result1; }
char *austrdup(const UChar* unichars) { int length; char *newString; length = u_strlen ( unichars ); /*newString = (char*)malloc ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */ newString = (char*)ctst_malloc ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */ if ( newString == NULL ) return NULL; u_austrcpy ( newString, unichars ); return newString; }
const char* loadTestData(UErrorCode* err){ if( _testDataPath == NULL){ const char* directory=NULL; UResourceBundle* test =NULL; char* tdpath=NULL; const char* tdrelativepath; #if defined (U_TOPBUILDDIR) tdrelativepath = "test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING; directory = U_TOPBUILDDIR; #else tdrelativepath = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING; directory= ctest_dataOutDir(); #endif tdpath = (char*) ctst_malloc(sizeof(char) *(( strlen(directory) * strlen(tdrelativepath)) + 10)); /* u_getDataDirectory shoul return \source\data ... set the * directory to ..\source\data\..\test\testdata\out\testdata * * Fallback: When Memory mapped file is built * ..\source\data\out\..\..\test\testdata\out\testdata */ strcpy(tdpath, directory); strcat(tdpath, tdrelativepath); strcat(tdpath,"testdata"); test=ures_open(tdpath, "testtypes", err); /* Fall back did not succeed either so return */ if(U_FAILURE(*err)){ *err = U_FILE_ACCESS_ERROR; log_data_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err)); return ""; } ures_close(test); _testDataPath = tdpath; return _testDataPath; } return _testDataPath; }
static UChar* myNumformat(const UNumberFormat* numfor, double d) { UChar *result2=NULL; int32_t resultlength, resultlengthneeded; UErrorCode status = U_ZERO_ERROR; resultlength=0; resultlengthneeded=unum_formatDouble(numfor, d, NULL, resultlength, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthneeded+1; /*result2=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /* this leaks */ result2=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/ unum_formatDouble(numfor, d, result2, resultlength, NULL, &status); } if(U_FAILURE(status)) { log_err("FAIL: Error in formatting using unum_format(.....) %s\n", myErrorName(status) ); return 0; } return result2; }
static void TestUDataSetAppData(){ /* UDataMemory *dataItem;*/ UErrorCode status=U_ZERO_ERROR; int fileHandle = 0; /* We are going to read the testdata.dat file */ struct stat statBuf; size_t fileSize = 0; char *fileBuf = 0; size_t i; /* Open the testdata.dat file, using normal */ const char* tdrelativepath = loadTestData(&status); char* filePath=(char*)malloc(sizeof(char) * (strlen(tdrelativepath) + strlen(".dat") +1 +strlen(tdrelativepath)) ); strcpy(filePath, tdrelativepath); strcat(filePath, ".dat"); log_verbose("Testing udata_setAppData() with %s\n", filePath); #if defined(WIN32) || defined(U_CYGWIN) fileHandle = open( filePath, O_RDONLY | O_BINARY ); #else fileHandle = open( filePath, O_RDONLY); #endif if( fileHandle == -1 ) { log_err("FAIL: TestUDataSetAppData() can not open(\"%s\", O_RDONLY)\n", filePath); goto cleanupAndReturn; } /* *Find the size of testdata.dat, and read the whole thing into memory */ if (fstat(fileHandle, &statBuf) == 0) { fileSize = statBuf.st_size; } if (fileSize == 0) { log_err("FAIL: TestUDataSetAppData() can not find size of file \"%s\".\n", filePath); goto cleanupAndReturn; } fileBuf = (char *)ctst_malloc(fileSize); if (fileBuf == 0) { log_err("FAIL: TestUDataSetAppData() can not malloc(%d) for file \"%s\".\n", fileSize, filePath); goto cleanupAndReturn; } i = read(fileHandle, fileBuf, fileSize); if (i != fileSize) { log_err("FAIL: TestUDataSetAppData() error reading file \"%s\" size=%d read=%d.\n", filePath, fileSize, i); goto cleanupAndReturn; } /* * Got testdata.dat into memory, now we try setAppData using the memory image. */ status=U_ZERO_ERROR; udata_setAppData("appData1", fileBuf, &status); if (status != U_ZERO_ERROR) { log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData1\", fileBuf, status) " " returned status of %s\n", u_errorName(status)); goto cleanupAndReturn; } udata_setAppData("appData2", fileBuf, &status); if (status != U_ZERO_ERROR) { log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) " " returned status of %s\n", u_errorName(status)); goto cleanupAndReturn; } /* If we try to setAppData with the same name a second time, we should get a * a using default warning. */ udata_setAppData("appData2", fileBuf, &status); if (status != U_USING_DEFAULT_WARNING) { log_err("FAIL: TestUDataSetAppData(): udata_setAppData(\"appData2\", fileBuf, status) " " returned status of %s, expected U_USING_DEFAULT_WARNING.\n", u_errorName(status)); } /** It is no longer correct to use udata_setAppData to change the package of a contained item. dataItem = udata_open("appData1", "res", "te_IN", &status); **/ cleanupAndReturn: /* Note: fileBuf is not deleted because ICU retains a pointer to it * forever (until ICU is shut down). */ if (fileHandle > 0) { close(fileHandle); } free(filePath); return; }