static UPlugData *uplug_allocatePlug(UPlugEntrypoint *entrypoint, const char *config, void *lib, const char *symName, UErrorCode *status) { UPlugData *plug; if(U_FAILURE(*status)) { return NULL; } plug = uplug_allocateEmptyPlug(status); if(config!=NULL) { uprv_strncpy(plug->config, config, UPLUG_NAME_MAX); } else { plug->config[0] = 0; } if(symName!=NULL) { uprv_strncpy(plug->sym, symName, UPLUG_NAME_MAX); } else { plug->sym[0] = 0; } plug->entrypoint = entrypoint; plug->lib = lib; uplug_queryPlug(plug, status); return plug; }
static UPlugData* uplug_initErrorPlug(const char *libName, const char *sym, const char *config, const char *nameOrError, UErrorCode loadStatus, UErrorCode *status) { UPlugData *plug = uplug_allocateEmptyPlug(status); if(U_FAILURE(*status)) return NULL; plug->pluginStatus = loadStatus; plug->awaitingLoad = FALSE; /* Won't load. */ plug->dontUnload = TRUE; /* cannot unload. */ if(sym!=NULL) { uprv_strncpy(plug->sym, sym, UPLUG_NAME_MAX); } if(libName!=NULL) { uprv_strncpy(plug->libName, libName, UPLUG_NAME_MAX); } if(nameOrError!=NULL) { uprv_strncpy(plug->name, nameOrError, UPLUG_NAME_MAX); } if(config!=NULL) { uprv_strncpy(plug->config, config, UPLUG_NAME_MAX); } return plug; }
void LocaleBased::setLocaleIDs(const char* validID, const char* actualID) { if (validID != 0) { uprv_strncpy(valid, validID, ULOC_FULLNAME_CAPACITY); valid[ULOC_FULLNAME_CAPACITY-1] = 0; // always terminate } if (actualID != 0) { uprv_strncpy(actual, actualID, ULOC_FULLNAME_CAPACITY); actual[ULOC_FULLNAME_CAPACITY-1] = 0; // always terminate } }
static CharList * pkg_appendFromStrings(CharList *l, CharList** end, const char *s, int32_t len) { CharList *endptr = NULL; const char *p; char *t; const char *targ; if(end == NULL) { end = &endptr; } if(len==-1) { len = uprv_strlen(s); } targ = s+len; while(*s && s<targ) { while(s<targ&&isspace(*s)) s++; for(p=s;s<targ&&!isspace(*p);p++); if(p!=s) { t = uprv_malloc(p-s+1); uprv_strncpy(t,s,p-s); t[p-s]=0; l=pkg_appendToList(l,end,t); fprintf(stderr, " P %s\n", t); } s=p; } return l; }
U_CAPI int32_t U_EXPORT2 unum_parseDecimal(const UNumberFormat* fmt, const UChar* text, int32_t textLength, int32_t *parsePos /* 0 = start */, char *outBuf, int32_t outBufLength, UErrorCode *status) { if (U_FAILURE(*status)) { return -1; } if ((outBuf == NULL && outBufLength != 0) || outBufLength < 0) { *status = U_ILLEGAL_ARGUMENT_ERROR; return -1; } Formattable res; parseRes(res, fmt, text, textLength, parsePos, status); StringPiece sp = res.getDecimalNumber(*status); if (U_FAILURE(*status)) { return -1; } else if (sp.size() > outBufLength) { *status = U_BUFFER_OVERFLOW_ERROR; } else if (sp.size() == outBufLength) { uprv_strncpy(outBuf, sp.data(), sp.size()); *status = U_STRING_NOT_TERMINATED_WARNING; } else { U_ASSERT(outBufLength > 0); uprv_strcpy(outBuf, sp.data()); } return sp.size(); }
U_CAPI const char * U_EXPORT2 findDirname(const char *path, char *buffer, int32_t bufLen, UErrorCode* status) { if(U_FAILURE(*status)) return NULL; const char *resultPtr = NULL; int32_t resultLen = 0; const char *basename=uprv_strrchr(path, U_FILE_SEP_CHAR); #if U_FILE_ALT_SEP_CHAR!=U_FILE_SEP_CHAR const char *basenameAlt=uprv_strrchr(path, U_FILE_ALT_SEP_CHAR); if(basenameAlt && (!basename || basename<basenameAlt)) { basename = basenameAlt; } #endif if(!basename) { /* no basename - return ''. */ resultPtr = ""; resultLen = 0; } else { resultPtr = path; resultLen = basename - path; if(resultLen<1) { resultLen = 1; /* '/' or '/a' -> '/' */ } } if((resultLen+1) <= bufLen) { uprv_strncpy(buffer, resultPtr, resultLen); buffer[resultLen]=0; return buffer; } else { *status = U_BUFFER_OVERFLOW_ERROR; return NULL; } }
U_CAPI UCalendar* U_EXPORT2 ucal_open( const UChar* zoneID, int32_t len, const char* locale, UCalendarType caltype, UErrorCode* status) { if(U_FAILURE(*status)) return 0; TimeZone* zone = (zoneID==NULL) ? TimeZone::createDefault() : _createTimeZone(zoneID, len, status); if (U_FAILURE(*status)) { return NULL; } if ( caltype == UCAL_GREGORIAN ) { char localeBuf[ULOC_LOCALE_IDENTIFIER_CAPACITY]; if ( locale == NULL ) { locale = uloc_getDefault(); } uprv_strncpy(localeBuf, locale, ULOC_LOCALE_IDENTIFIER_CAPACITY); uloc_setKeywordValue("calendar", "gregorian", localeBuf, ULOC_LOCALE_IDENTIFIER_CAPACITY, status); if (U_FAILURE(*status)) { return NULL; } return (UCalendar*)Calendar::createInstance(zone, Locale(localeBuf), *status); } return (UCalendar*)Calendar::createInstance(zone, Locale(locale), *status); }
void NumberingSystem::setName(const char *n) { if ( n == NULL ) { name[0] = (char) 0; } else { uprv_strncpy(name,n,NUMSYS_NAME_CAPACITY); name[NUMSYS_NAME_CAPACITY] = (char)0; // Make sure it is null terminated. } }
static const char* getCodepageFromPOSIXID(const char *localeName, char * buffer, int32_t buffCapacity) { char localeBuf[100]; const char *name = NULL; char *variant = NULL; if (localeName != NULL && (name = (uprv_strchr(localeName, '.'))) != NULL) { size_t localeCapacity = uprv_min(sizeof(localeBuf), (name-localeName)+1); uprv_strncpy(localeBuf, localeName, localeCapacity); localeBuf[localeCapacity-1] = 0; /* ensure NULL termination */ name = uprv_strncpy(buffer, name+1, buffCapacity); buffer[buffCapacity-1] = 0; /* ensure NULL termination */ if ((variant = (uprv_strchr(name, '@'))) != NULL) { *variant = 0; } name = remapPlatformDependentCodepage(localeBuf, name); } return name; }
void MeasureUnit::initCurrency(const char *isoCurrency) { int32_t result = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), "currency"); U_ASSERT(result != -1); fTypeId = result; result = binarySearch( gSubTypes, gOffsets[fTypeId], gOffsets[fTypeId + 1], isoCurrency); if (result != -1) { fSubTypeId = result - gOffsets[fTypeId]; } else { uprv_strncpy(fCurrency, isoCurrency, UPRV_LENGTHOF(fCurrency)); } }
CReg(const UChar* _iso, const char* _id) : next(0) { int32_t len = uprv_strlen(_id); if (len > (int32_t)(sizeof(id)-1)) { len = (sizeof(id)-1); } uprv_strncpy(id, _id, len); id[len] = 0; uprv_memcpy(iso, _iso, ISO_COUNTRY_CODE_LENGTH * sizeof(const UChar)); iso[ISO_COUNTRY_CODE_LENGTH] = 0; }
/* go from "/usr/local/include/curses.h" to "/usr/local/include" */ void get_dirname(char *dirname, const char *filename) { const char *lastSlash = uprv_strrchr(filename, U_FILE_SEP_CHAR) + 1; if(lastSlash>filename) { uprv_strncpy(dirname, filename, (lastSlash - filename)); *(dirname + (lastSlash - filename)) = '\0'; } else { *dirname = '\0'; } }
DigitList& DigitList::operator=(const DigitList& other) { if (this != &other) { fDecimalAt = other.fDecimalAt; fCount = other.fCount; fIsPositive = other.fIsPositive; fRoundingMode = other.fRoundingMode; uprv_strncpy(fDigits, other.fDigits, fCount); } return *this; }
void InputText::setDeclaredEncoding(const char* encoding, int32_t len) { if(encoding) { if (len == -1) { len = (int32_t)uprv_strlen(encoding); } len += 1; // to make place for the \0 at the end. uprv_free(fDeclaredEncoding); fDeclaredEncoding = NEW_ARRAY(char, len); uprv_strncpy(fDeclaredEncoding, encoding, len); } }
U_NAMESPACE_END // The following must at least allow for rg key value (6) plus terminator (1). #define ULOC_RG_BUFLEN 8 U_CAPI int32_t U_EXPORT2 ulocimp_getRegionForSupplementalData(const char *localeID, UBool inferRegion, char *region, int32_t regionCapacity, UErrorCode* status) { if (U_FAILURE(*status)) { return 0; } char rgBuf[ULOC_RG_BUFLEN]; UErrorCode rgStatus = U_ZERO_ERROR; // First check for rg keyword value int32_t rgLen = uloc_getKeywordValue(localeID, "rg", rgBuf, ULOC_RG_BUFLEN, &rgStatus); if (U_FAILURE(rgStatus) || rgLen != 6) { rgLen = 0; } else { // rgBuf guaranteed to be zero terminated here, with text len 6 char *rgPtr = rgBuf; for (; *rgPtr!= 0; rgPtr++) { *rgPtr = uprv_toupper(*rgPtr); } rgLen = (uprv_strcmp(rgBuf+2, "ZZZZ") == 0)? 2: 0; } if (rgLen == 0) { // No valid rg keyword value, try for unicode_region_subtag rgLen = uloc_getCountry(localeID, rgBuf, ULOC_RG_BUFLEN, status); if (U_FAILURE(*status)) { rgLen = 0; } else if (rgLen == 0 && inferRegion) { // no unicode_region_subtag but inferRegion TRUE, try likely subtags char locBuf[ULOC_FULLNAME_CAPACITY]; rgStatus = U_ZERO_ERROR; (void)uloc_addLikelySubtags(localeID, locBuf, ULOC_FULLNAME_CAPACITY, &rgStatus); if (U_SUCCESS(rgStatus)) { rgLen = uloc_getCountry(locBuf, rgBuf, ULOC_RG_BUFLEN, status); if (U_FAILURE(*status)) { rgLen = 0; } } } } rgBuf[rgLen] = 0; uprv_strncpy(region, rgBuf, regionCapacity); return u_terminateChars(region, regionCapacity, rgLen, status); }
static const char* U_CALLCONV _processRFC3066Locale(CollatorSpec *spec, uint32_t, const char* string, UErrorCode *status) { char terminator = *string; string++; const char *end = uprv_strchr(string+1, terminator); if(end == NULL || end - string >= loc3066Capacity) { *status = U_BUFFER_OVERFLOW_ERROR; return string; } else { uprv_strncpy(spec->locale, string, end-string); return end+1; } }
U_INTERNAL void * U_EXPORT2 uplug_openLibrary(const char *libName, UErrorCode *status) { int32_t libEntry = -1; void *lib = NULL; if(U_FAILURE(*status)) return NULL; libEntry = searchForLibraryName(libName); if(libEntry == -1) { libEntry = libraryCount++; if(libraryCount >= libraryMax) { /* Ran out of library slots. Statically allocated because we can't depend on allocating memory.. */ *status = U_MEMORY_ALLOCATION_ERROR; #if UPLUG_TRACE DBG((stderr, "uplug_openLibrary() - out of library slots (max %d)\n", libraryMax)); #endif return NULL; } /* Some operating systems don't want DL operations from multiple threads. */ libraryList[libEntry].lib = uprv_dl_open(libName, status); #if UPLUG_TRACE DBG((stderr, "uplug_openLibrary(%s,%s) libEntry %d, lib %p\n", libName, u_errorName(*status), libEntry, lib)); #endif if(libraryList[libEntry].lib == NULL || U_FAILURE(*status)) { /* cleanup. */ libraryList[libEntry].lib = NULL; /* failure with open */ libraryList[libEntry].name[0] = 0; #if UPLUG_TRACE DBG((stderr, "uplug_openLibrary(%s,%s) libEntry %d, lib %p\n", libName, u_errorName(*status), libEntry, lib)); #endif /* no need to free - just won't increase the count. */ libraryCount--; } else { /* is it still there? */ /* link it in */ uprv_strncpy(libraryList[libEntry].name,libName,UPLUG_NAME_MAX); libraryList[libEntry].ref=1; lib = libraryList[libEntry].lib; } } else { lib = libraryList[libEntry].lib; libraryList[libEntry].ref++; } return lib; }
const char *pkg_writeCharListWrap(FileStream *s, CharList *l, const char *delim, const char *brk, int32_t quote) { int32_t ln = 0; char buffer[1024]; while(l != NULL) { if(l->str) { uprv_strncpy(buffer, l->str, 1020); buffer[1019]=0; if(quote < 0) { /* remove quotes */ if(buffer[uprv_strlen(buffer)-1] == '"') { buffer[uprv_strlen(buffer)-1] = '\0'; } if(buffer[0] == '"') { uprv_strcpy(buffer, buffer+1); } } else if(quote > 0) { /* add quotes */ if(l->str[0] != '"') { uprv_strcpy(buffer, "\""); uprv_strncat(buffer, l->str,1020); } if(l->str[uprv_strlen(l->str)-1] != '"') { uprv_strcat(buffer, "\""); } } T_FileStream_write(s, buffer, (int32_t)uprv_strlen(buffer)); ln += (int32_t)uprv_strlen(l->str); } if(l->next && delim) { if(ln > 60 && brk) { ln = 0; T_FileStream_write(s, brk, (int32_t)uprv_strlen(brk)); } T_FileStream_write(s, delim, (int32_t)uprv_strlen(delim)); } l = l->next; } return NULL; }
const char *pkg_writeCharList(FileStream *s, CharList *l, const char *delim, int32_t quote) { char buffer[1024]; while(l != NULL) { if(l->str) { uprv_strncpy(buffer, l->str, 1023); buffer[1023]=0; if(uprv_strlen(l->str) >= 1023) { fprintf(stderr, "%s:%d: Internal error, line too long (greater than 1023 chars)\n", __FILE__, __LINE__); exit(0); } if(quote < 0) { /* remove quotes */ if(buffer[uprv_strlen(buffer)-1] == '"') { buffer[uprv_strlen(buffer)-1] = '\0'; } if(buffer[0] == '"') { uprv_strcpy(buffer, buffer+1); } } else if(quote > 0) { /* add quotes */ if(l->str[0] != '"') { uprv_strcpy(buffer, "\""); uprv_strcat(buffer, l->str); } if(l->str[uprv_strlen(l->str)-1] != '"') { uprv_strcat(buffer, "\""); } } T_FileStream_write(s, buffer, (int32_t)uprv_strlen(buffer)); } if(l->next && delim) { T_FileStream_write(s, delim, (int32_t)uprv_strlen(delim)); } l = l->next; } return NULL; }
/* NOTE: The caller should handle thread safety */ U_CAPI const char* U_EXPORT2 uprv_getDefaultLocaleID() { #if U_POSIX_LOCALE /* Note that: (a '!' means the ID is improper somehow) LC_ALL ----> default_loc codepage -------------------------------------------------------- ab.CD ab CD ab@CD ab__CD - [email protected] ab__CD EF ab_CD.EF@GH ab_CD_GH EF Some 'improper' ways to do the same as above: ! [email protected] ab_CD_GH EF ! [email protected] ab_CD_GH EF ! [email protected]@GH.IJ ab_CD_GH EF _CD@GH _CD_GH - _CD.EF@GH _CD_GH EF The variant cannot have dots in it. The 'rightmost' variant (@xxx) wins. The leftmost codepage (.xxx) wins. */ char *correctedPOSIXLocale = 0; const char* posixID = uprv_getPOSIXID(); const char *p; const char *q; int32_t len; /* Format: (no spaces) ll [ _CC ] [ . MM ] [ @ VV] l = lang, C = ctry, M = charmap, V = variant */ if (gCorrectedPOSIXLocale != NULL) { return gCorrectedPOSIXLocale; } if ((p = uprv_strchr(posixID, '.')) != NULL) { /* assume new locale can't be larger than old one? */ correctedPOSIXLocale = (char*) uprv_malloc(uprv_strlen(posixID)+1); uprv_strncpy(correctedPOSIXLocale, posixID, p-posixID); correctedPOSIXLocale[p-posixID] = 0; /* do not copy after the @ */ if ((p = uprv_strchr(correctedPOSIXLocale, '@')) != NULL) { correctedPOSIXLocale[p-correctedPOSIXLocale] = 0; } } /* Note that we scan the *uncorrected* ID. */ if ((p = uprv_strrchr(posixID, '@')) != NULL) { if (correctedPOSIXLocale == NULL) { correctedPOSIXLocale = (char*) uprv_malloc(uprv_strlen(posixID)+1); uprv_strncpy(correctedPOSIXLocale, posixID, p-posixID); correctedPOSIXLocale[p-posixID] = 0; } p++; /* Take care of any special cases here.. */ if (!uprv_strcmp(p, "nynorsk")) { p = "NY"; /* Don't worry about no__NY. In practice, it won't appear. */ } if (uprv_strchr(correctedPOSIXLocale,'_') == NULL) { uprv_strcat(correctedPOSIXLocale, "__"); /* aa@b -> aa__b */ } else { uprv_strcat(correctedPOSIXLocale, "_"); /* aa_CC@b -> aa_CC_b */ } if ((q = uprv_strchr(p, '.')) != NULL) { /* How big will the resulting string be? */ len = (int32_t)(uprv_strlen(correctedPOSIXLocale) + (q-p)); uprv_strncat(correctedPOSIXLocale, p, q-p); correctedPOSIXLocale[len] = 0; } else { /* Anything following the @ sign */ uprv_strcat(correctedPOSIXLocale, p); } /* Should there be a map from 'no@nynorsk' -> no_NO_NY here? * How about 'russian' -> 'ru'? * Many of the other locales using ISO codes will be handled by the * canonicalization functions in uloc_getDefault. */ } /* Was a correction made? */ if (correctedPOSIXLocale != NULL) { posixID = correctedPOSIXLocale; } else { /* copy it, just in case the original pointer goes away. See j2395 */ correctedPOSIXLocale = (char *)uprv_malloc(uprv_strlen(posixID) + 1); posixID = uprv_strcpy(correctedPOSIXLocale, posixID); } if (gCorrectedPOSIXLocale == NULL) { gCorrectedPOSIXLocale = correctedPOSIXLocale; ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup); correctedPOSIXLocale = NULL; } if (correctedPOSIXLocale != NULL) { /* Was already set - clean up. */ uprv_free(correctedPOSIXLocale); } return posixID; #elif defined(U_WINDOWS) UErrorCode status = U_ZERO_ERROR; LCID id = GetThreadLocale(); const char* locID = uprv_convertToPosix(id, &status); if (U_FAILURE(status)) { locID = "en_US"; } return locID; #elif defined(XP_MAC) int32_t script = MAC_LC_INIT_NUMBER; /* = IntlScript(); or GetScriptManagerVariable(smSysScript);*/ int32_t region = MAC_LC_INIT_NUMBER; /* = GetScriptManagerVariable(smRegionCode);*/ int32_t lang = MAC_LC_INIT_NUMBER; /* = GetScriptManagerVariable(smScriptLang);*/ int32_t date_region = MAC_LC_INIT_NUMBER; const char* posixID = 0; int32_t count = sizeof(mac_lc_recs) / sizeof(mac_lc_rec); int32_t i; Intl1Hndl ih; ih = (Intl1Hndl) GetIntlResource(1); if (ih) date_region = ((uint16_t)(*ih)->intl1Vers) >> 8; for (i = 0; i < count; i++) { if ( ((mac_lc_recs[i].script == MAC_LC_MAGIC_NUMBER) || (mac_lc_recs[i].script == script)) && ((mac_lc_recs[i].region == MAC_LC_MAGIC_NUMBER) || (mac_lc_recs[i].region == region)) && ((mac_lc_recs[i].lang == MAC_LC_MAGIC_NUMBER) || (mac_lc_recs[i].lang == lang)) && ((mac_lc_recs[i].date_region == MAC_LC_MAGIC_NUMBER) || (mac_lc_recs[i].date_region == date_region)) ) { posixID = mac_lc_recs[i].posixID; break; } } return posixID; #elif defined(OS400) /* locales are process scoped and are by definition thread safe */ static char correctedLocale[64]; const char *localeID = getenv("LC_ALL"); char *p; if (localeID == NULL) localeID = getenv("LANG"); if (localeID == NULL) localeID = setlocale(LC_ALL, NULL); /* Make sure we have something... */ if (localeID == NULL) return "en_US_POSIX"; /* Extract the locale name from the path. */ if((p = uprv_strrchr(localeID, '/')) != NULL) { /* Increment p to start of locale name. */ p++; localeID = p; } /* Copy to work location. */ uprv_strcpy(correctedLocale, localeID); /* Strip off the '.locale' extension. */ if((p = uprv_strchr(correctedLocale, '.')) != NULL) { *p = 0; } /* Upper case the locale name. */ T_CString_toUpperCase(correctedLocale); /* See if we are using the POSIX locale. Any of the * following are equivalent and use the same QLGPGCMA * (POSIX) locale. * QLGPGCMA2 means UCS2 * QLGPGCMA_4 means UTF-32 * QLGPGCMA_8 means UTF-8 */ if ((uprv_strcmp("C", correctedLocale) == 0) || (uprv_strcmp("POSIX", correctedLocale) == 0) || (uprv_strncmp("QLGPGCMA", correctedLocale, 8) == 0)) { uprv_strcpy(correctedLocale, "en_US_POSIX"); } else { int16_t LocaleLen; /* Lower case the lang portion. */ for(p = correctedLocale; *p != 0 && *p != '_'; p++) { *p = uprv_tolower(*p); } /* Adjust for Euro. After '_E' add 'URO'. */ LocaleLen = uprv_strlen(correctedLocale); if (correctedLocale[LocaleLen - 2] == '_' && correctedLocale[LocaleLen - 1] == 'E') { uprv_strcat(correctedLocale, "URO"); } /* If using Lotus-based locale then convert to * equivalent non Lotus. */ else if (correctedLocale[LocaleLen - 2] == '_' && correctedLocale[LocaleLen - 1] == 'L') { correctedLocale[LocaleLen - 2] = 0; } /* There are separate simplified and traditional * locales called zh_HK_S and zh_HK_T. */ else if (uprv_strncmp(correctedLocale, "zh_HK", 5) == 0) { uprv_strcpy(correctedLocale, "zh_HK"); } /* A special zh_CN_GBK locale... */ else if (uprv_strcmp(correctedLocale, "zh_CN_GBK") == 0) { uprv_strcpy(correctedLocale, "zh_CN"); } } return correctedLocale; #endif }
/* Process a file */ void processFile(const char *filename, const char *cp, const char *inputDir, const char *outputDir, const char *packageName, SRBRoot *newPoolBundle, UBool omitBinaryCollation, UErrorCode &status) { LocalPointer<SRBRoot> data; UCHARBUF *ucbuf = NULL; char *rbname = NULL; char *openFileName = NULL; char *inputDirBuf = NULL; char outputFileName[256]; int32_t dirlen = 0; int32_t filelen = 0; if (U_FAILURE(status)) { return; } if(filename==NULL){ status=U_ILLEGAL_ARGUMENT_ERROR; return; }else{ filelen = (int32_t)uprv_strlen(filename); } if(inputDir == NULL) { const char *filenameBegin = uprv_strrchr(filename, U_FILE_SEP_CHAR); openFileName = (char *) uprv_malloc(dirlen + filelen + 2); openFileName[0] = '\0'; if (filenameBegin != NULL) { /* * When a filename ../../../data/root.txt is specified, * we presume that the input directory is ../../../data * This is very important when the resource file includes * another file, like UCARules.txt or thaidict.brk. */ int32_t filenameSize = (int32_t)(filenameBegin - filename + 1); inputDirBuf = uprv_strncpy((char *)uprv_malloc(filenameSize), filename, filenameSize); /* test for NULL */ if(inputDirBuf == NULL) { status = U_MEMORY_ALLOCATION_ERROR; goto finish; } inputDirBuf[filenameSize - 1] = 0; inputDir = inputDirBuf; dirlen = (int32_t)uprv_strlen(inputDir); } }else{ dirlen = (int32_t)uprv_strlen(inputDir); if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) { openFileName = (char *) uprv_malloc(dirlen + filelen + 2); /* test for NULL */ if(openFileName == NULL) { status = U_MEMORY_ALLOCATION_ERROR; goto finish; } openFileName[0] = '\0'; /* * append the input dir to openFileName if the first char in * filename is not file seperation char and the last char input directory is not '.'. * This is to support : * genrb -s. /home/icu/data * genrb -s. icu/data * The user cannot mix notations like * genrb -s. /icu/data --- the absolute path specified. -s redundant * user should use * genrb -s. icu/data --- start from CWD and look in icu/data dir */ if( (filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){ uprv_strcpy(openFileName, inputDir); openFileName[dirlen] = U_FILE_SEP_CHAR; } openFileName[dirlen + 1] = '\0'; } else { openFileName = (char *) uprv_malloc(dirlen + filelen + 1); /* test for NULL */ if(openFileName == NULL) { status = U_MEMORY_ALLOCATION_ERROR; goto finish; } uprv_strcpy(openFileName, inputDir); } } uprv_strcat(openFileName, filename); ucbuf = ucbuf_open(openFileName, &cp,getShowWarning(),TRUE, &status); if(status == U_FILE_ACCESS_ERROR) { fprintf(stderr, "couldn't open file %s\n", openFileName == NULL ? filename : openFileName); goto finish; } if (ucbuf == NULL || U_FAILURE(status)) { fprintf(stderr, "An error occured processing file %s. Error: %s\n", openFileName == NULL ? filename : openFileName, u_errorName(status)); goto finish; } /* auto detected popular encodings? */ if (cp!=NULL && isVerbose()) { printf("autodetected encoding %s\n", cp); } /* Parse the data into an SRBRoot */ data.adoptInstead(parse(ucbuf, inputDir, outputDir, filename, !omitBinaryCollation, options[NO_COLLATION_RULES].doesOccur, &status)); if (data.isNull() || U_FAILURE(status)) { fprintf(stderr, "couldn't parse the file %s. Error:%s\n", filename, u_errorName(status)); goto finish; } if(options[WRITE_POOL_BUNDLE].doesOccur) { data->fWritePoolBundle = newPoolBundle; data->compactKeys(status); int32_t newKeysLength; const char *newKeys = data->getKeyBytes(&newKeysLength); newPoolBundle->addKeyBytes(newKeys, newKeysLength, status); if(U_FAILURE(status)) { fprintf(stderr, "bundle_compactKeys(%s) or bundle_getKeyBytes() failed: %s\n", filename, u_errorName(status)); goto finish; } /* count the number of just-added key strings */ for(const char *newKeysLimit = newKeys + newKeysLength; newKeys < newKeysLimit; ++newKeys) { if(*newKeys == 0) { ++newPoolBundle->fKeysCount; } } } if(options[USE_POOL_BUNDLE].doesOccur) { data->fUsePoolBundle = &poolBundle; } /* Determine the target rb filename */ rbname = make_res_filename(filename, outputDir, packageName, status); if(U_FAILURE(status)) { fprintf(stderr, "couldn't make the res fileName for bundle %s. Error:%s\n", filename, u_errorName(status)); goto finish; } if(write_java== TRUE){ bundle_write_java(data.getAlias(), outputDir, outputEnc, outputFileName, sizeof(outputFileName), options[JAVA_PACKAGE].value, options[BUNDLE_NAME].value, &status); }else if(write_xliff ==TRUE){ bundle_write_xml(data.getAlias(), outputDir, outputEnc, filename, outputFileName, sizeof(outputFileName), language, xliffOutputFileName, &status); }else{ /* Write the data to the file */ data->write(outputDir, packageName, outputFileName, sizeof(outputFileName), status); } if (U_FAILURE(status)) { fprintf(stderr, "couldn't write bundle %s. Error:%s\n", outputFileName, u_errorName(status)); } finish: if (inputDirBuf != NULL) { uprv_free(inputDirBuf); } if (openFileName != NULL) { uprv_free(openFileName); } if(ucbuf) { ucbuf_close(ucbuf); } if (rbname) { uprv_free(rbname); } }
static UDataMemory * openCommonData(const char *path, /* Path from OpenChoice? */ UBool isICUData, /* ICU Data true if path == NULL */ UErrorCode *pErrorCode) { UDataMemory tData; UDataPathIterator iter; const char *pathBuffer; const char *inBasename; if (U_FAILURE(*pErrorCode)) { return NULL; } UDataMemory_init(&tData); /* ??????? TODO revisit this */ if (isICUData) { /* "mini-cache" for common ICU data */ if(gCommonICUData != NULL) { return gCommonICUData; } tData.pHeader = &U_ICUDATA_ENTRY_POINT.hdr; udata_checkCommonData(&tData, pErrorCode); setCommonICUData(&tData, NULL, FALSE, pErrorCode); return gCommonICUData; } /* request is NOT for ICU Data. */ /* Find the base name portion of the supplied path. */ /* inBasename will be left pointing somewhere within the original path string. */ inBasename = findBasename(path); #ifdef UDATA_DEBUG fprintf(stderr, "inBasename = %s\n", inBasename); #endif if(*inBasename==0) { /* no basename. This will happen if the original path was a directory name, */ /* like "a/b/c/". (Fallback to separate files will still work.) */ #ifdef UDATA_DEBUG fprintf(stderr, "ocd: no basename in %s, bailing.\n", path); #endif *pErrorCode=U_FILE_ACCESS_ERROR; return NULL; } /* Is the requested common data file already open and cached? */ /* Note that the cache is keyed by the base name only. The rest of the path, */ /* if any, is not considered. */ { UDataMemory *dataToReturn = udata_findCachedData(inBasename); if (dataToReturn != NULL) { return dataToReturn; } } /* Requested item is not in the cache. * Hunt it down, trying all the path locations */ udata_pathiter_init(&iter, u_getDataDirectory(), inBasename, path, ".dat", TRUE); while((UDataMemory_isLoaded(&tData)==FALSE) && (pathBuffer = udata_pathiter_next(&iter)) != NULL) { #ifdef UDATA_DEBUG fprintf(stderr, "ocd: trying path %s - ", pathBuffer); #endif uprv_mapFile(&tData, pathBuffer); #ifdef UDATA_DEBUG fprintf(stderr, "%s\n", UDataMemory_isLoaded(&tData)?"LOADED":"not loaded"); #endif } udata_pathiter_dt(&iter); /* Note: this call may invalidate "pathBuffer" */ #if defined(OS390_STUBDATA) && defined(OS390BATCH) if (!UDataMemory_isLoaded(&tData)) { char ourPathBuffer[1024]; /* One more chance, for extendCommonData() */ uprv_strncpy(ourPathBuffer, path, 1019); ourPathBuffer[1019]=0; uprv_strcat(ourPathBuffer, ".dat"); uprv_mapFile(&tData, ourPathBuffer); } #endif if (!UDataMemory_isLoaded(&tData)) { /* no common data */ *pErrorCode=U_FILE_ACCESS_ERROR; return NULL; } /* we have mapped a file, check its header */ udata_checkCommonData(&tData, pErrorCode); /* Cache the UDataMemory struct for this .dat file, * so we won't need to hunt it down and map it again next time * something is needed from it. */ return udata_cacheDataItem(inBasename, &tData, pErrorCode); }
U_CAPI void U_EXPORT2 uplug_setPlugName(UPlugData *data, const char *name) { uprv_strncpy(data->name, name, UPLUG_NAME_MAX); }
void bundle_write_java(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, char *writtenFilename, int writtenFilenameLen, const char* packageName, const char* bundleName, UErrorCode *status) { char fileName[256] = {'\0'}; char className[256]={'\0'}; /*char constructor[1000] = { 0 };*/ /*UBool j1 =FALSE;*/ outDir = outputDir; start = TRUE; /* Reset the start indictor*/ bName = (bundleName==NULL) ? "LocaleElements" : bundleName; pName = (packageName==NULL)? "com.ibm.icu.impl.data" : packageName; uprv_strcpy(className, bName); srBundle = bundle; if(uprv_strcmp(srBundle->fLocale,"root")!=0){ uprv_strcat(className,"_"); uprv_strcat(className,srBundle->fLocale); } if(outputDir){ uprv_strcpy(fileName, outputDir); if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){ uprv_strcat(fileName,U_FILE_SEP_STRING); } uprv_strcat(fileName,className); uprv_strcat(fileName,".java"); }else{ uprv_strcat(fileName,className); uprv_strcat(fileName,".java"); } if (writtenFilename) { uprv_strncpy(writtenFilename, fileName, writtenFilenameLen); } if (U_FAILURE(*status)) { return; } out= T_FileStream_open(fileName,"w"); if(out==NULL){ *status = U_FILE_ACCESS_ERROR; return; } if(getIncludeCopyright()){ T_FileStream_write(out, copyRight, (int32_t)uprv_strlen(copyRight)); T_FileStream_write(out, warningMsg, (int32_t)uprv_strlen(warningMsg)); } T_FileStream_write(out,"package ",(int32_t)uprv_strlen("package ")); T_FileStream_write(out,pName,(int32_t)uprv_strlen(pName)); T_FileStream_write(out,";\n\n",3); T_FileStream_write(out, javaClass, (int32_t)uprv_strlen(javaClass)); T_FileStream_write(out, className, (int32_t)uprv_strlen(className)); T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1)); /* if(j1){ T_FileStream_write(out, javaClass1, (int32_t)uprv_strlen(javaClass1)); }else{ sprintf(constructor,javaClassICU,className); T_FileStream_write(out, constructor, (int32_t)uprv_strlen(constructor)); } */ if(outputEnc && *outputEnc!='\0'){ /* store the output encoding */ enc = outputEnc; conv=ucnv_open(enc,status); if(U_FAILURE(*status)){ return; } } res_write_java(bundle->fRoot, status); T_FileStream_write(out, closeClass, (int32_t)uprv_strlen(closeClass)); T_FileStream_close(out); ucnv_close(conv); }
extern int main(int argc, char* argv[]) { const char *encoding = NULL; const char *outputDir = NULL; /* NULL = no output directory, use current */ const char *inputDir = "."; int tostdout = 0; int prbom = 0; const char *pname; UResourceBundle *bundle = NULL; UErrorCode status = U_ZERO_ERROR; int32_t i = 0; UConverter *converter; const char* arg; /* Get the name of tool. */ pname = uprv_strrchr(*argv, U_FILE_SEP_CHAR); #if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR if (!pname) { pname = uprv_strrchr(*argv, U_FILE_ALT_SEP_CHAR); } #endif if (!pname) { pname = *argv; } else { ++pname; } /* error handling, printing usage message */ argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); /* error handling, printing usage message */ if(argc<0) { fprintf(stderr, "%s: error in command line argument \"%s\"\n", pname, argv[-argc]); } if(argc<0 || options[0].doesOccur || options[1].doesOccur) { fprintf(argc < 0 ? stderr : stdout, "%csage: %s [ -h, -?, --help ] [ -V, --version ]\n" " [ -v, --verbose ] [ -e, --encoding encoding ] [ --bom ]\n" " [ -t, --truncate [ size ] ]\n" " [ -s, --sourcedir source ] [ -d, --destdir destination ]\n" " [ -i, --icudatadir directory ] [ -c, --to-stdout ]\n" " [ -A, --suppressAliases]\n" " bundle ...\n", argc < 0 ? 'u' : 'U', pname); return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; } if(options[10].doesOccur) { fprintf(stderr, "%s version %s (ICU version %s).\n" "%s\n", pname, DERB_VERSION, U_ICU_VERSION, U_COPYRIGHT_STRING); return U_ZERO_ERROR; } if(options[2].doesOccur) { encoding = options[2].value; } if (options[3].doesOccur) { tostdout = 1; } if(options[4].doesOccur) { trunc = TRUE; if(options[4].value != NULL) { truncsize = atoi(options[4].value); /* user defined printable size */ } else { truncsize = DERB_DEFAULT_TRUNC; /* we'll use default omitting size */ } } else { trunc = FALSE; } if(options[5].doesOccur) { verbose = TRUE; } if (options[6].doesOccur) { outputDir = options[6].value; } if(options[7].doesOccur) { inputDir = options[7].value; /* we'll use users resources */ } if (options[8].doesOccur) { prbom = 1; } if (options[9].doesOccur) { u_setDataDirectory(options[9].value); } if (options[11].doesOccur) { suppressAliases = TRUE; } converter = ucnv_open(encoding, &status); if (U_FAILURE(status)) { fprintf(stderr, "%s: couldn't create %s converter for encoding\n", pname, encoding ? encoding : ucnv_getDefaultName()); return 2; } ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, 0, 0, &status); if (U_FAILURE(status)) { fprintf(stderr, "%s: couldn't configure converter for encoding\n", pname); return 3; } defaultConverter = ucnv_open(0, &status); if (U_FAILURE(status)) { fprintf(stderr, "%s: couldn't create %s converter for encoding\n", ucnv_getDefaultName(), pname); return 2; } for (i = 1; i < argc; ++i) { static const UChar sp[] = { 0x0020 }; /* " " */ char infile[4096]; /* XXX Sloppy. */ char locale[64]; const char *thename = 0, *p, *q; UBool fromICUData = FALSE; arg = getLongPathname(argv[i]); if (verbose) { printf("processing bundle \"%s\"\n", argv[i]); } p = uprv_strrchr(arg, U_FILE_SEP_CHAR); #if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR if (p == NULL) { p = uprv_strrchr(arg, U_FILE_ALT_SEP_CHAR); } #endif if (!p) { p = arg; } else { p++; } q = uprv_strrchr(p, '.'); if (!q) { for (q = p; *q; ++q) ; } uprv_strncpy(locale, p, q - p); locale[q - p] = 0; if (!(fromICUData = !uprv_strcmp(inputDir, "-"))) { UBool absfilename = *arg == U_FILE_SEP_CHAR; #ifdef U_WINDOWS if (!absfilename) { absfilename = (uprv_strlen(arg) > 2 && isalpha(arg[0]) && arg[1] == ':' && arg[2] == U_FILE_SEP_CHAR); } #endif if (absfilename) { thename = arg; } else { q = uprv_strrchr(arg, U_FILE_SEP_CHAR); #if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR if (q == NULL) { q = uprv_strrchr(arg, U_FILE_ALT_SEP_CHAR); } #endif uprv_strcpy(infile, inputDir); if(q != NULL) { uprv_strcat(infile, U_FILE_SEP_STRING); strncat(infile, arg, q-arg); } thename = infile; } } status = U_ZERO_ERROR; if (thename) { bundle = ures_openDirect(thename, locale, &status); } else { bundle = ures_open(fromICUData ? 0 : inputDir, locale, &status); } if (status == U_ZERO_ERROR) { FILE *out; const char *filename = 0; const char *ext = 0; if (!locale || !tostdout) { filename = uprv_strrchr(arg, U_FILE_SEP_CHAR); #if U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR if (!filename) { filename = uprv_strrchr(arg, U_FILE_ALT_SEP_CHAR); } #endif if (!filename) { filename = arg; } else { ++filename; } ext = uprv_strrchr(arg, '.'); if (!ext) { ext = filename + uprv_strlen(filename); } } if (tostdout) { out = stdout; #if defined(U_WINDOWS) || defined(U_CYGWIN) if (setmode(fileno(out), O_BINARY) == -1) { fprintf(stderr, "%s: couldn't set standard output to binary mode\n", pname); return 4; } #endif } else { char thefile[4096], *tp; int32_t len; if (outputDir) { uprv_strcpy(thefile, outputDir); uprv_strcat(thefile, U_FILE_SEP_STRING); } else { *thefile = 0; } uprv_strcat(thefile, filename); tp = thefile + uprv_strlen(thefile); len = (int32_t)uprv_strlen(ext); if (len) { tp -= len - 1; } else { *tp++ = '.'; } uprv_strcpy(tp, "txt"); out = fopen(thefile, "w"); if (!out) { fprintf(stderr, "%s: couldn't create %s\n", pname, thefile); return 4; } } if (prbom) { /* XXX: Should be done only for UTFs */ static const UChar bom[] = { 0xFEFF }; printString(out, converter, bom, (int32_t)(sizeof(bom)/sizeof(*bom))); } printCString(out, converter, "// -*- Coding: ", -1); printCString(out, converter, encoding ? encoding : getEncodingName(ucnv_getDefaultName()), -1); printCString(out, converter, "; -*-\n//\n", -1); printCString(out, converter, "// This file was dumped by derb(8) from ", -1); if (thename) { printCString(out, converter, thename, -1); } else if (fromICUData) { printCString(out, converter, "the ICU internal ", -1); printCString(out, converter, locale, -1); printCString(out, converter, " locale", -1); } printCString(out, converter, "\n// derb(8) by Vladimir Weinstein and Yves Arrouye\n\n", -1); if (locale) { printCString(out, converter, locale, -1); } else { printCString(out, converter, filename, (int32_t)(ext - filename)); printString(out, converter, sp, (int32_t)(sizeof(sp)/sizeof(*sp))); } printOutBundle(out, converter, bundle, 0, pname, &status); if (out != stdout) { fclose(out); } } else { reportError(pname, &status, "opening resource file"); } ures_close(bundle); } ucnv_close(defaultConverter); ucnv_close(converter); return 0; }
U_CAPI void U_EXPORT2 uplug_init(UErrorCode *status) { #if !U_ENABLE_DYLOAD (void)status; /* unused */ #else const char *plugin_dir; if(U_FAILURE(*status)) return; plugin_dir = getenv("ICU_PLUGINS"); #if defined(DEFAULT_ICU_PLUGINS) if(plugin_dir == NULL || !*plugin_dir) { plugin_dir = DEFAULT_ICU_PLUGINS; } #endif #if UPLUG_TRACE DBG((stderr, "ICU_PLUGINS=%s\n", plugin_dir)); #endif if(plugin_dir != NULL && *plugin_dir) { FILE *f; #ifdef OS390BATCH /* There are potentially a lot of ways to implement a plugin directory on OS390/zOS */ /* Keeping in mind that unauthorized file access is logged, monitored, and enforced */ /* I've chosen to open a DDNAME if BATCH and leave it alone for (presumably) UNIX */ /* System Services. Alternative techniques might be allocating a member in */ /* SYS1.PARMLIB or setting an environment variable "ICU_PLUGIN_PATH" (?). The */ /* DDNAME can be connected to a file in the HFS if need be. */ uprv_strncpy(plugin_file,"//DD:ICUPLUG", 2047); /* JAM 20 Oct 2011 */ #else uprv_strncpy(plugin_file, plugin_dir, 2047); uprv_strncat(plugin_file, U_FILE_SEP_STRING,2047); uprv_strncat(plugin_file, "icuplugins",2047); uprv_strncat(plugin_file, U_ICU_VERSION_SHORT ,2047); uprv_strncat(plugin_file, ".txt" ,2047); #endif #if UPLUG_TRACE DBG((stderr, "pluginfile= %s\n", plugin_file)); #endif #ifdef __MVS__ if (iscics()) /* 12 Nov 2011 JAM */ { f = NULL; } else #endif { f = fopen(plugin_file, "r"); } if(f != NULL) { char linebuf[1024]; char *p, *libName=NULL, *symName=NULL, *config=NULL; int32_t line = 0; while(fgets(linebuf,1023,f)) { line++; if(!*linebuf || *linebuf=='#') { continue; } else { p = linebuf; while(*p&&isspace((int)*p)) p++; if(!*p || *p=='#') continue; libName = p; while(*p&&!isspace((int)*p)) { p++; } if(!*p || *p=='#') continue; /* no tab after libname */ *p=0; /* end of libname */ p++; while(*p&&isspace((int)*p)) { p++; } if(!*p||*p=='#') continue; /* no symname after libname +tab */ symName = p; while(*p&&!isspace((int)*p)) { p++; } if(*p) { /* has config */ *p=0; ++p; while(*p&&isspace((int)*p)) { p++; } if(*p) { config = p; } } /* chop whitespace at the end of the config */ if(config!=NULL&&*config!=0) { p = config+strlen(config); while(p>config&&isspace((int)*(--p))) { *p=0; } } /* OK, we're good. */ { UErrorCode subStatus = U_ZERO_ERROR; UPlugData *plug = uplug_initPlugFromLibrary(libName, symName, config, &subStatus); if(U_FAILURE(subStatus) && U_SUCCESS(*status)) { *status = subStatus; } #if UPLUG_TRACE DBG((stderr, "PLUGIN libName=[%s], sym=[%s], config=[%s]\n", libName, symName, config)); DBG((stderr, " -> %p, %s\n", (void*)plug, u_errorName(subStatus))); #else (void)plug; /* unused */ #endif } } } fclose(f); } else { #if UPLUG_TRACE DBG((stderr, "Can't open plugin file %s\n", plugin_file)); #endif } } uplug_loadWaitingPlugs(status); #endif /* U_ENABLE_DYLOAD */ ucln_registerCleanup(UCLN_UPLUG, uplug_cleanup); }
/* Process a file */ static void processFile(const char *filename, const char *cp, const char *inputDir, const char *outputDir, const char *packageName, UErrorCode *status) { /*FileStream *in = NULL;*/ struct SRBRoot *data = NULL; UCHARBUF *ucbuf = NULL; char *rbname = NULL; char *openFileName = NULL; char *inputDirBuf = NULL; char outputFileName[256]; int32_t dirlen = 0; int32_t filelen = 0; if (status==NULL || U_FAILURE(*status)) { return; } if(filename==NULL){ *status=U_ILLEGAL_ARGUMENT_ERROR; return; }else{ filelen = (int32_t)uprv_strlen(filename); } if(inputDir == NULL) { const char *filenameBegin = uprv_strrchr(filename, U_FILE_SEP_CHAR); openFileName = (char *) uprv_malloc(dirlen + filelen + 2); openFileName[0] = '\0'; if (filenameBegin != NULL) { /* * When a filename ../../../data/root.txt is specified, * we presume that the input directory is ../../../data * This is very important when the resource file includes * another file, like UCARules.txt or thaidict.brk. */ int32_t filenameSize = (int32_t)(filenameBegin - filename + 1); inputDirBuf = uprv_strncpy((char *)uprv_malloc(filenameSize), filename, filenameSize); /* test for NULL */ if(inputDirBuf == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; goto finish; } inputDirBuf[filenameSize - 1] = 0; inputDir = inputDirBuf; dirlen = (int32_t)uprv_strlen(inputDir); } }else{ dirlen = (int32_t)uprv_strlen(inputDir); if(inputDir[dirlen-1] != U_FILE_SEP_CHAR) { openFileName = (char *) uprv_malloc(dirlen + filelen + 2); /* test for NULL */ if(openFileName == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; goto finish; } openFileName[0] = '\0'; /* * append the input dir to openFileName if the first char in * filename is not file seperation char and the last char input directory is not '.'. * This is to support : * genrb -s. /home/icu/data * genrb -s. icu/data * The user cannot mix notations like * genrb -s. /icu/data --- the absolute path specified. -s redundant * user should use * genrb -s. icu/data --- start from CWD and look in icu/data dir */ if( (filename[0] != U_FILE_SEP_CHAR) && (inputDir[dirlen-1] !='.')){ uprv_strcpy(openFileName, inputDir); openFileName[dirlen] = U_FILE_SEP_CHAR; } openFileName[dirlen + 1] = '\0'; } else { openFileName = (char *) uprv_malloc(dirlen + filelen + 1); /* test for NULL */ if(openFileName == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; goto finish; } uprv_strcpy(openFileName, inputDir); } } uprv_strcat(openFileName, filename); ucbuf = ucbuf_open(openFileName, &cp,getShowWarning(),TRUE, status); if(*status == U_FILE_ACCESS_ERROR) { fprintf(stderr, "couldn't open file %s\n", openFileName == NULL ? filename : openFileName); goto finish; } if (ucbuf == NULL || U_FAILURE(*status)) { fprintf(stderr, "An error occured processing file %s. Error: %s\n", openFileName == NULL ? filename : openFileName,u_errorName(*status)); goto finish; } /* auto detected popular encodings? */ if (cp!=NULL && isVerbose()) { printf("autodetected encoding %s\n", cp); } /* Parse the data into an SRBRoot */ data = parse(ucbuf, inputDir, outputDir, status); if (data == NULL || U_FAILURE(*status)) { fprintf(stderr, "couldn't parse the file %s. Error:%s\n", filename,u_errorName(*status)); goto finish; } /* Determine the target rb filename */ rbname = make_res_filename(filename, outputDir, packageName, status); if(U_FAILURE(*status)) { fprintf(stderr, "couldn't make the res fileName for bundle %s. Error:%s\n", filename,u_errorName(*status)); goto finish; } if(write_java== TRUE){ bundle_write_java(data,outputDir,outputEnc, outputFileName, sizeof(outputFileName),packageName,bundleName,status); }else if(write_xliff ==TRUE){ bundle_write_xml(data,outputDir,outputEnc, filename, outputFileName, sizeof(outputFileName),language, xliffOutputFileName,status); }else{ /* Write the data to the file */ bundle_write(data, outputDir, packageName, outputFileName, sizeof(outputFileName), status); } if (U_FAILURE(*status)) { fprintf(stderr, "couldn't write bundle %s. Error:%s\n", outputFileName,u_errorName(*status)); } bundle_close(data, status); finish: if (inputDirBuf != NULL) { uprv_free(inputDirBuf); } if (openFileName != NULL) { uprv_free(openFileName); } if(ucbuf) { ucbuf_close(ucbuf); } if (rbname) { uprv_free(rbname); } }
static void udata_pathiter_init(UDataPathIterator *iter, const char *path, const char *pkg, const char *item, const char *suffix, UBool doCheckLastFour) { #ifdef UDATA_DEBUG fprintf(stderr, "SUFFIX1=%s PATH=%s\n", suffix, path); #endif /** Path **/ if(path == NULL) { iter->path = u_getDataDirectory(); } else { iter->path = path; } /** Package **/ if(pkg == NULL) { iter->packageStubLen = 0; iter->packageStub=iter->packageStubBuf; iter->packageStub[0] = 0; } else { if(uprv_strlen(pkg) + 2 > U_DATA_PATHITER_BUFSIZ) { iter->packageStub = uprv_malloc(uprv_strlen(pkg)+2); /* Check for null pointer. */ if (iter->packageStub == NULL) { return; } } else { iter->packageStub = iter->packageStubBuf; } iter->packageStub[0] = U_FILE_SEP_CHAR; uprv_strcpy(iter->packageStub+1, pkg); iter->packageStubLen = (int32_t)uprv_strlen(iter->packageStub); #ifdef UDATA_DEBUG fprintf(stderr, "STUB=%s [%d]\n", iter->packageStub, iter->packageStubLen); #endif } /** Item **/ iter->basename = findBasename(item); iter->basenameLen = (int32_t)uprv_strlen(iter->basename); /** Item path **/ iter->itemPath = iter->itemPathBuf; if(iter->basename == item) { iter->itemPath[0] = 0; iter->nextPath = iter->path; } else { int32_t itemPathLen = (int32_t)(iter->basename-item); if (itemPathLen >= U_DATA_PATHITER_BUFSIZ) { char *t = (char *)uprv_malloc(itemPathLen+1); if (t != NULL) { iter->itemPath = t; } else { /* Malloc failed. Ignore the itemPath. */ itemPathLen = 0; } } uprv_strncpy(iter->itemPath, item, itemPathLen); iter->itemPath[itemPathLen]=0; iter->nextPath = iter->itemPath; } #ifdef UDATA_DEBUG fprintf(stderr, "SUFFIX=%s [%p]\n", suffix, suffix); #endif /** Suffix **/ if(suffix != NULL) { iter->suffix = suffix; } else { iter->suffix = ""; } iter->checkLastFour = doCheckLastFour; /* pathBuffer will hold the output path strings returned by the this iterator * Get an upper bound of possible string size, and make sure that the buffer * is big enough (sum of length of each piece, 2 extra delimiters, + trailing NULL) */ { int32_t maxPathLen = (int32_t)(uprv_strlen(iter->path) + uprv_strlen(item) + uprv_strlen(iter->suffix) + iter->packageStubLen + 3); iter->pathBuffer = iter->pathBufferA; if (maxPathLen >= U_DATA_PATHITER_BUFSIZ) { iter->pathBuffer = (char *)uprv_malloc(maxPathLen); if (iter->pathBuffer == NULL) { iter->pathBuffer = iter->pathBufferA; iter->path = ""; } } } #ifdef UDATA_DEBUG fprintf(stderr, "%p: init %s -> [path=%s], [base=%s], [suff=%s], [itempath=%s], [nextpath=%s], [checklast4=%s]\n", iter, item, iter->path, iter->basename, iter->suffix, iter->itemPath, iter->nextPath, iter->checkLastFour?"TRUE":"false"); #endif }
U_NAMESPACE_BEGIN // ------------------------------------- BreakIterator* BreakIterator::buildInstance(const Locale& loc, const char *type, int32_t kind, UErrorCode &status) { char fnbuff[256]; char ext[4]={'\0'}; char actualLocale[ULOC_FULLNAME_CAPACITY]; int32_t size; const UChar* brkfname = NULL; UResourceBundle brkRulesStack; UResourceBundle brkNameStack; UResourceBundle *brkRules = &brkRulesStack; UResourceBundle *brkName = &brkNameStack; RuleBasedBreakIterator *result = NULL; if (U_FAILURE(status)) return NULL; ures_initStackObject(brkRules); ures_initStackObject(brkName); // Get the locale UResourceBundle *b = ures_open(U_ICUDATA_BRKITR, loc.getName(), &status); /* this is a hack for now. Should be fixed when the data is fetched from brk_index.txt */ if(status==U_USING_DEFAULT_WARNING){ status=U_ZERO_ERROR; ures_openFillIn(b, U_ICUDATA_BRKITR, "", &status); } // Get the "boundaries" array. if (U_SUCCESS(status)) { brkRules = ures_getByKeyWithFallback(b, "boundaries", brkRules, &status); // Get the string object naming the rules file brkName = ures_getByKeyWithFallback(brkRules, type, brkName, &status); // Get the actual string brkfname = ures_getString(brkName, &size, &status); U_ASSERT((size_t)size<sizeof(fnbuff)); if ((size_t)size>=sizeof(fnbuff)) { size=0; if (U_SUCCESS(status)) { status = U_BUFFER_OVERFLOW_ERROR; } } // Use the string if we found it if (U_SUCCESS(status) && brkfname) { uprv_strncpy(actualLocale, ures_getLocale(brkName, &status), sizeof(actualLocale)/sizeof(actualLocale[0])); UChar* extStart=u_strchr(brkfname, 0x002e); int len = 0; if(extStart!=NULL){ len = (int)(extStart-brkfname); u_UCharsToChars(extStart+1, ext, sizeof(ext)); // nul terminates the buff u_UCharsToChars(brkfname, fnbuff, len); } fnbuff[len]=0; // nul terminate } } ures_close(brkRules); ures_close(brkName); UDataMemory* file = udata_open(U_ICUDATA_BRKITR, ext, fnbuff, &status); if (U_FAILURE(status)) { ures_close(b); return NULL; } // Create a RuleBasedBreakIterator result = new RuleBasedBreakIterator(file, status); // If there is a result, set the valid locale and actual locale, and the kind if (U_SUCCESS(status) && result != NULL) { U_LOCALE_BASED(locBased, *(BreakIterator*)result); locBased.setLocaleIDs(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status), actualLocale); result->setBreakType(kind); } ures_close(b); if (U_FAILURE(status) && result != NULL) { // Sometimes redundant check, but simple delete result; return NULL; } if (result == NULL) { udata_close(file); if (U_SUCCESS(status)) { status = U_MEMORY_ALLOCATION_ERROR; } } return result; }
static const char *udata_pathiter_next(UDataPathIterator *iter) { const char *path = NULL; uint32_t pathLen = 0; const char *pathBasename; do { if( iter->nextPath == NULL ) { break; } path = iter->nextPath; if(iter->nextPath == iter->itemPath) { /* we were processing item's path. */ iter->nextPath = iter->path; /* start with regular path next tm. */ pathLen = (int32_t)uprv_strlen(path); } else { /* fix up next for next time */ iter->nextPath = uprv_strchr(path, U_PATH_SEP_CHAR); if(iter->nextPath == NULL) { /* segment: entire path */ pathLen = (int32_t)uprv_strlen(path); } else { /* segment: until next segment */ pathLen = (int32_t)(iter->nextPath - path); if(*iter->nextPath) { /* skip divider */ iter->nextPath ++; } } } if(pathLen == 0) { continue; } #ifdef UDATA_DEBUG fprintf(stderr, "rest of path (IDD) = %s\n", path); fprintf(stderr, " "); { uint32_t qqq; for(qqq=0;qqq<pathLen;qqq++) { fprintf(stderr, " "); } fprintf(stderr, "^\n"); } #endif uprv_strncpy(iter->pathBuffer, path, pathLen); iter->pathBuffer[pathLen] = 0; /* check for .dat files */ pathBasename = findBasename(iter->pathBuffer); if(iter->checkLastFour == TRUE && (pathLen>=4) && uprv_strncmp(iter->pathBuffer +(pathLen-4),iter->suffix,4)==0 && /* suffix matches */ uprv_strncmp(findBasename(iter->pathBuffer),iter->basename,iter->basenameLen)==0 && /* base matches */ uprv_strlen(pathBasename)==(iter->basenameLen+4)) { /* base+suffix = full len */ #ifdef UDATA_DEBUG fprintf(stderr, "Have %s file on the path: %s\n", iter->suffix, iter->pathBuffer); #endif /* do nothing */ } else { /* regular dir path */ if(iter->pathBuffer[pathLen-1] != U_FILE_SEP_CHAR) { if((pathLen>=4) && uprv_strncmp(iter->pathBuffer+(pathLen-4), ".dat", 4) == 0) { #ifdef UDATA_DEBUG fprintf(stderr, "skipping non-directory .dat file %s\n", iter->pathBuffer); #endif continue; } /* Check if it is a directory with the same name as our package */ if(iter->packageStubLen && (pathLen > iter->packageStubLen) && !uprv_strcmp(iter->pathBuffer + pathLen - iter->packageStubLen, iter->packageStub)) { #ifdef UDATA_DEBUG fprintf(stderr, "Found stub %s ( will add package %s of len %d)\n", iter->packageStub, iter->basename, iter->basenameLen); #endif pathLen -= iter->packageStubLen; } iter->pathBuffer[pathLen++] = U_FILE_SEP_CHAR; } uprv_strncpy(iter->pathBuffer + pathLen, /* + basename */ iter->packageStub+1, iter->packageStubLen-1); pathLen += iter->packageStubLen-1; if(*iter->suffix) /* tack on suffix */ { uprv_strcpy(iter->pathBuffer + pathLen, iter->suffix); pathLen += (int32_t)uprv_strlen(iter->suffix); } } #ifdef UDATA_DEBUG fprintf(stderr, " --> %s\n", iter->pathBuffer); #endif return iter->pathBuffer; } while(iter->path); /* fell way off the end */ return NULL; }