]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/tools/genrb/wrtxml.cpp
ICU-62109.0.1.tar.gz
[apple/icu.git] / icuSources / tools / genrb / wrtxml.cpp
index 4a642b9eb8715806a9c3c3602223bb1613062a79..58e055d5718c03f5d146d0f6b4a509f0e0fb4514 100644 (file)
@@ -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.
 *
 *******************************************************************************
 *
 *******************************************************************************
 */
+
+// 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"
@@ -61,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);
 
@@ -73,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);
 
@@ -126,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;
@@ -356,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){
 
@@ -408,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 */
 
@@ -459,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(" "));
@@ -467,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){
@@ -490,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);
@@ -533,15 +551,14 @@ printComments(struct UString *src, const char *resName, UBool printTranslate, UE
  * <trans-unit id = "blah" resname = "blah" restype = "x-id-alias" translate = "no">
  * <group id "calendar_gregorian" resname = "gregorian" restype = "x-icu-array">
  */
-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 {
@@ -603,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;
@@ -619,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;
@@ -640,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;
@@ -651,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;
@@ -671,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;
 
@@ -679,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};
@@ -708,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;
@@ -717,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("<"));
@@ -757,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;
@@ -768,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));
@@ -785,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;
@@ -793,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) {
@@ -865,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 <res->u.fBinaryValue.fLength){
-            len = itostr(temp, res->u.fBinaryValue.fData[i], 16, 2);
+        while(i <res->fLength){
+            len = itostr(temp, res->fData[i], 16, 2);
             crc = computeCRC(temp, len, crc);
             i++;
         }
@@ -877,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 <res->u.fBinaryValue.fLength){
-            len = itostr(temp, res->u.fBinaryValue.fData[i], 16, 2);
+        while(i <res->fLength){
+            len = itostr(temp, res->fData[i], 16, 2);
             write_utf8_file(out, UnicodeString(temp));
             i += 1;
         }
@@ -905,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;
 
@@ -922,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) {
@@ -955,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<StringResource *>(res), id, language, status);
              return;
 
         case URES_ALIAS:
-             alias_write_xml     (res, id, language, status);
+             alias_write_xml     (static_cast<AliasResource *>(res), id, language, status);
              return;
 
         case URES_INT_VECTOR:
-             intvector_write_xml (res, id, language, status);
+             intvector_write_xml (static_cast<IntVectorResource *>(res), id, language, status);
              return;
 
         case URES_BINARY:
-             bin_write_xml       (res, id, language, status);
+             bin_write_xml       (static_cast<BinaryResource *>(res), id, language, status);
              return;
 
         case URES_INT:
-             int_write_xml       (res, id, language, status);
+             int_write_xml       (static_cast<IntResource *>(res), id, language, status);
              return;
 
         case URES_ARRAY:
-             array_write_xml     (res, id, language, status);
+             array_write_xml     (static_cast<ArrayResource *>(res), id, language, status);
              return;
 
         case URES_TABLE:
-             table_write_xml     (res, id, language, isTopLevel, status);
+             table_write_xml     (static_cast<TableResource *>(res), id, language, isTopLevel, status);
              return;
 
         default:
@@ -1121,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 */
@@ -1131,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");
@@ -1151,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"));