X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/4388f060552cc537e71e957d32f35e9d75a61233..4f1e1a09ce4daed860e35d359ce2fceccb0764e8:/icuSources/tools/genrb/wrtxml.cpp diff --git a/icuSources/tools/genrb/wrtxml.cpp b/icuSources/tools/genrb/wrtxml.cpp index 7e760534..58e055d5 100644 --- a/icuSources/tools/genrb/wrtxml.cpp +++ b/icuSources/tools/genrb/wrtxml.cpp @@ -1,7 +1,9 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* * -* Copyright (C) 2002-2012, International Business Machines +* Copyright (C) 2002-2015, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -16,6 +18,17 @@ * ******************************************************************************* */ + +// Safer use of UnicodeString. +#ifndef UNISTR_FROM_CHAR_EXPLICIT +# define UNISTR_FROM_CHAR_EXPLICIT explicit +#endif + +// Less important, but still a good idea. +#ifndef UNISTR_FROM_STRING_EXPLICIT +# define UNISTR_FROM_STRING_EXPLICIT explicit +#endif + #include "reslist.h" #include "unewdata.h" #include "unicode/ures.h" @@ -25,7 +38,6 @@ #include "unicode/ucnv.h" #include "genrb.h" #include "rle.h" -#include "ucol_tok.h" #include "uhash.h" #include "uresimp.h" #include "unicode/ustring.h" @@ -33,6 +45,8 @@ #include "ustr.h" #include "prscmnts.h" #include "unicode/unistr.h" +#include "unicode/utf8.h" +#include "unicode/utf16.h" #include U_NAMESPACE_USE @@ -59,7 +73,7 @@ static int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString) u_strToUTF8(NULL, 0, &len, - outString.getBuffer(), + toUCharPtr(outString.getBuffer()), outString.length(), &status); @@ -71,7 +85,7 @@ static int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString) u_strToUTF8(dest, len, &len, - outString.getBuffer(), + toUCharPtr(outString.getBuffer()), outString.length(), &status); @@ -124,7 +138,7 @@ static char* getID(const char* id, const char* curKey, char* result) { * conversion is not portable across platforms with different endianess. */ -uint32_t computeCRC(char *ptr, uint32_t len, uint32_t lastcrc){ +uint32_t computeCRC(const char *ptr, uint32_t len, uint32_t lastcrc){ int32_t crc; uint32_t temp1; uint32_t temp2; @@ -251,7 +265,7 @@ static char* convertAndEscape(char** pDest, int32_t destCap, int32_t* destLength return NULL; } - if((destLen+UTF8_CHAR_LENGTH(c)) < destCap){ + if((destLen+U8_LENGTH(c)) < destCap){ /* ASCII Range */ if(c <=0x007F){ @@ -354,6 +368,7 @@ static char* convertAndEscape(char** pDest, int32_t destCap, int32_t* destLength #define LF 0x000D #define AT_SIGN 0x0040 +#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 static void trim(char **src, int32_t *len){ @@ -406,8 +421,10 @@ print(UChar* src, int32_t srcLen,const char *tagStart,const char *tagEnd, UErro } } +#endif + static void -printNoteElements(struct UString *src, UErrorCode *status){ +printNoteElements(const UString *src, UErrorCode *status){ #if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */ @@ -457,6 +474,7 @@ static void printAttribute(const char *name, const char *value, int32_t /*len*/) write_utf8_file(out, UnicodeString("\"")); } +#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */ static void printAttribute(const char *name, const UnicodeString value, int32_t /*len*/) { write_utf8_file(out, UnicodeString(" ")); @@ -465,6 +483,7 @@ static void printAttribute(const char *name, const UnicodeString value, int32_t write_utf8_file(out, value); write_utf8_file(out, UnicodeString("\"")); } +#endif static void printComments(struct UString *src, const char *resName, UBool printTranslate, UErrorCode *status){ @@ -488,6 +507,7 @@ printComments(struct UString *src, const char *resName, UBool printTranslate, UE uprv_free(trans); return; } + // TODO: make src const, stop modifying it in-place, make printContainer() take const resource, etc. src->fLength = removeCmtText(src->fChars, src->fLength, status); descLen = getDescription(src->fChars,src->fLength, &desc, capacity, status); transLen = getTranslate(src->fChars,src->fLength, &trans, capacity, status); @@ -531,15 +551,14 @@ printComments(struct UString *src, const char *resName, UBool printTranslate, UE * * */ -static char *printContainer(struct SResource *res, const char *container, const char *restype, const char *mimetype, const char *id, UErrorCode *status) +static char *printContainer(SResource *res, const char *container, const char *restype, const char *mimetype, const char *id, UErrorCode *status) { - char resKeyBuffer[8]; const char *resname = NULL; char *sid = NULL; write_tabs(out); - resname = res_getKeyString(srBundle, res, resKeyBuffer); + resname = res->getKeyString(srBundle); if (resname != NULL && *resname != 0) { sid = getID(id, resname, sid); } else { @@ -601,7 +620,7 @@ static const char *intvector_restype = "x-icu-intvector"; static const char *table_restype = "x-icu-table"; static void -string_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) { +string_write_xml(StringResource *res, const char* id, const char* /*language*/, UErrorCode *status) { char *sid = NULL; char* buf = NULL; @@ -617,7 +636,7 @@ string_write_xml(struct SResource *res, const char* id, const char* /*language*/ write_utf8_file(out, UnicodeString(source)); - buf = convertAndEscape(&buf, 0, &bufLen, res->u.fString.fChars, res->u.fString.fLength, status); + buf = convertAndEscape(&buf, 0, &bufLen, res->getBuffer(), res->length(), status); if (U_FAILURE(*status)) { return; @@ -638,7 +657,7 @@ string_write_xml(struct SResource *res, const char* id, const char* /*language*/ } static void -alias_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) { +alias_write_xml(AliasResource *res, const char* id, const char* /*language*/, UErrorCode *status) { char *sid = NULL; char* buf = NULL; int32_t bufLen=0; @@ -649,7 +668,7 @@ alias_write_xml(struct SResource *res, const char* id, const char* /*language*/, write_utf8_file(out, UnicodeString(source)); - buf = convertAndEscape(&buf, 0, &bufLen, res->u.fString.fChars, res->u.fString.fLength, status); + buf = convertAndEscape(&buf, 0, &bufLen, res->getBuffer(), res->length(), status); if(U_FAILURE(*status)){ return; @@ -669,7 +688,7 @@ alias_write_xml(struct SResource *res, const char* id, const char* /*language*/, } static void -array_write_xml(struct SResource *res, const char* id, const char* language, UErrorCode *status) { +array_write_xml(ArrayResource *res, const char* id, const char* language, UErrorCode *status) { char* sid = NULL; int index = 0; @@ -677,7 +696,7 @@ array_write_xml(struct SResource *res, const char* id, const char* language, UEr sid = printContainer(res, group, array_restype, NULL, id, status); - current = res->u.fArray.fFirst; + current = res->fFirst; while (current != NULL) { char c[256] = {0}; @@ -706,7 +725,7 @@ array_write_xml(struct SResource *res, const char* id, const char* language, UEr } static void -intvector_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) { +intvector_write_xml(IntVectorResource *res, const char* id, const char* /*language*/, UErrorCode *status) { char* sid = NULL; char* ivd = NULL; uint32_t i=0; @@ -715,12 +734,12 @@ intvector_write_xml(struct SResource *res, const char* id, const char* /*languag sid = printContainer(res, group, intvector_restype, NULL, id, status); - for(i = 0; i < res->u.fIntVector.fCount; i += 1) { + for(i = 0; i < res->fCount; i += 1) { char c[256] = {0}; itostr(c, i, 10, 0); ivd = getID(sid, c, ivd); - len = itostr(buf, res->u.fIntVector.fArray[i], 10, 0); + len = itostr(buf, res->fArray[i], 10, 0); write_tabs(out); write_utf8_file(out, UnicodeString("<")); @@ -755,7 +774,7 @@ intvector_write_xml(struct SResource *res, const char* id, const char* /*languag } static void -int_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) { +int_write_xml(IntResource *res, const char* id, const char* /*language*/, UErrorCode *status) { char* sid = NULL; char buf[256] = {0}; uint32_t len = 0; @@ -766,7 +785,7 @@ int_write_xml(struct SResource *res, const char* id, const char* /*language*/, U write_utf8_file(out, UnicodeString(source)); - len = itostr(buf, res->u.fIntValue.fValue, 10, 0); + len = itostr(buf, res->fValue, 10, 0); write_utf8_file(out, UnicodeString(buf, len)); write_utf8_file(out, UnicodeString(close_source)); @@ -783,7 +802,7 @@ int_write_xml(struct SResource *res, const char* id, const char* /*language*/, U } static void -bin_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) { +bin_write_xml(BinaryResource *res, const char* id, const char* /*language*/, UErrorCode *status) { const char* m_type = application_mimetype; char* sid = NULL; uint32_t crc = 0xFFFFFFFF; @@ -791,16 +810,16 @@ bin_write_xml(struct SResource *res, const char* id, const char* /*language*/, U char fileName[1024] ={0}; int32_t tLen = ( outDir == NULL) ? 0 :(int32_t)uprv_strlen(outDir); char* fn = (char*) uprv_malloc(sizeof(char) * (tLen+1024 + - (res->u.fBinaryValue.fFileName !=NULL ? - uprv_strlen(res->u.fBinaryValue.fFileName) :0))); + (res->fFileName !=NULL ? + uprv_strlen(res->fFileName) :0))); const char* ext = NULL; char* f = NULL; fn[0]=0; - if(res->u.fBinaryValue.fFileName != NULL){ - uprv_strcpy(fileName, res->u.fBinaryValue.fFileName); + if(res->fFileName != NULL){ + uprv_strcpy(fileName, res->fFileName); f = uprv_strrchr(fileName, '\\'); if (f != NULL) { @@ -863,8 +882,8 @@ bin_write_xml(struct SResource *res, const char* id, const char* /*language*/, U write_utf8_file(out, UnicodeString(internal_file)); printAttribute("form", application_mimetype, (int32_t) uprv_strlen(application_mimetype)); - while(i u.fBinaryValue.fLength){ - len = itostr(temp, res->u.fBinaryValue.fData[i], 16, 2); + while(i fLength){ + len = itostr(temp, res->fData[i], 16, 2); crc = computeCRC(temp, len, crc); i++; } @@ -875,8 +894,8 @@ bin_write_xml(struct SResource *res, const char* id, const char* /*language*/, U write_utf8_file(out, UnicodeString(">")); i = 0; - while(i u.fBinaryValue.fLength){ - len = itostr(temp, res->u.fBinaryValue.fData[i], 16, 2); + while(i fLength){ + len = itostr(temp, res->fData[i], 16, 2); write_utf8_file(out, UnicodeString(temp)); i += 1; } @@ -903,7 +922,7 @@ bin_write_xml(struct SResource *res, const char* id, const char* /*language*/, U static void -table_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status) { +table_write_xml(TableResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status) { uint32_t i = 0; @@ -920,7 +939,7 @@ table_write_xml(struct SResource *res, const char* id, const char* language, UBo sid[0] = '\0'; } - current = res->u.fTable.fFirst; + current = res->fFirst; i = 0; while (current != NULL) { @@ -953,31 +972,31 @@ res_write_xml(struct SResource *res, const char* id, const char* language, UBoo if (res != NULL) { switch (res->fType) { case URES_STRING: - string_write_xml (res, id, language, status); + string_write_xml (static_cast(res), id, language, status); return; case URES_ALIAS: - alias_write_xml (res, id, language, status); + alias_write_xml (static_cast(res), id, language, status); return; case URES_INT_VECTOR: - intvector_write_xml (res, id, language, status); + intvector_write_xml (static_cast(res), id, language, status); return; case URES_BINARY: - bin_write_xml (res, id, language, status); + bin_write_xml (static_cast(res), id, language, status); return; case URES_INT: - int_write_xml (res, id, language, status); + int_write_xml (static_cast(res), id, language, status); return; case URES_ARRAY: - array_write_xml (res, id, language, status); + array_write_xml (static_cast(res), id, language, status); return; case URES_TABLE: - table_write_xml (res, id, language, isTopLevel, status); + table_write_xml (static_cast(res), id, language, isTopLevel, status); return; default: @@ -1119,7 +1138,7 @@ bundle_write_xml(struct SRBRoot *bundle, const char *outputDir,const char* outpu *status = U_FILE_ACCESS_ERROR; goto cleanup_bundle_write_xml; } - write_utf8_file(out, xmlHeader); + write_utf8_file(out, UnicodeString(xmlHeader)); if(outputEnc && *outputEnc!='\0'){ /* store the output encoding */ @@ -1129,9 +1148,9 @@ bundle_write_xml(struct SRBRoot *bundle, const char *outputDir,const char* outpu goto cleanup_bundle_write_xml; } } - write_utf8_file(out, bundleStart); + write_utf8_file(out, UnicodeString(bundleStart)); write_tabs(out); - write_utf8_file(out, fileStart); + write_utf8_file(out, UnicodeString(fileStart)); /* check if lang and language are the same */ if(language != NULL && uprv_strcmp(lang, srBundle->fLocale)!=0){ fprintf(stderr,"Warning: The top level tag in the resource and language specified are not the same. Please check the input.\n"); @@ -1149,12 +1168,12 @@ bundle_write_xml(struct SRBRoot *bundle, const char *outputDir,const char* outpu tabCount += 1; write_tabs(out); - write_utf8_file(out, headerStart); + write_utf8_file(out, UnicodeString(headerStart)); tabCount += 1; write_tabs(out); - write_utf8_file(out, tool_start); + write_utf8_file(out, UnicodeString(tool_start)); printAttribute("tool-id", tool_id, (int32_t) uprv_strlen(tool_id)); printAttribute("tool-name", tool_name, (int32_t) uprv_strlen(tool_name)); write_utf8_file(out, UnicodeString("/>\n"));