intl/icu/source/tools/genrb/wrtxml.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/genrb/wrtxml.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1200 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 2002-2012, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +*******************************************************************************
    1.11 +*
    1.12 +* File wrtxml.cpp
    1.13 +*
    1.14 +* Modification History:
    1.15 +*
    1.16 +*   Date        Name        Description
    1.17 +*   10/01/02    Ram         Creation.
    1.18 +*   02/07/08    Spieth      Correct XLIFF generation on EBCDIC platform
    1.19 +*
    1.20 +*******************************************************************************
    1.21 +*/
    1.22 +#include "reslist.h"
    1.23 +#include "unewdata.h"
    1.24 +#include "unicode/ures.h"
    1.25 +#include "errmsg.h"
    1.26 +#include "filestrm.h"
    1.27 +#include "cstring.h"
    1.28 +#include "unicode/ucnv.h"
    1.29 +#include "genrb.h"
    1.30 +#include "rle.h"
    1.31 +#include "ucol_tok.h"
    1.32 +#include "uhash.h"
    1.33 +#include "uresimp.h"
    1.34 +#include "unicode/ustring.h"
    1.35 +#include "unicode/uchar.h"
    1.36 +#include "ustr.h"
    1.37 +#include "prscmnts.h"
    1.38 +#include "unicode/unistr.h"
    1.39 +#include "unicode/utf8.h"
    1.40 +#include "unicode/utf16.h"
    1.41 +#include <time.h>
    1.42 +
    1.43 +U_NAMESPACE_USE
    1.44 +
    1.45 +static int tabCount = 0;
    1.46 +
    1.47 +static FileStream* out=NULL;
    1.48 +static struct SRBRoot* srBundle ;
    1.49 +static const char* outDir = NULL;
    1.50 +static const char* enc ="";
    1.51 +static UConverter* conv = NULL;
    1.52 +
    1.53 +const char* const* ISOLanguages;
    1.54 +const char* const* ISOCountries;
    1.55 +const char* textExt = ".txt";
    1.56 +const char* xliffExt = ".xlf";
    1.57 +
    1.58 +static int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString)
    1.59 +{
    1.60 +    UErrorCode status = U_ZERO_ERROR;
    1.61 +    int32_t len = 0;
    1.62 +
    1.63 +    // preflight to get the destination buffer size
    1.64 +    u_strToUTF8(NULL,
    1.65 +                0,
    1.66 +                &len,
    1.67 +                outString.getBuffer(),
    1.68 +                outString.length(),
    1.69 +                &status);
    1.70 +
    1.71 +    // allocate the buffer
    1.72 +    char* dest = (char*)uprv_malloc(len);
    1.73 +    status = U_ZERO_ERROR;
    1.74 +
    1.75 +    // convert the data
    1.76 +    u_strToUTF8(dest,
    1.77 +                len,
    1.78 +                &len,
    1.79 +                outString.getBuffer(),
    1.80 +                outString.length(),
    1.81 +                &status);
    1.82 +
    1.83 +    // write data to out file
    1.84 +    int32_t ret = T_FileStream_write(fileStream, dest, len);
    1.85 +    uprv_free(dest);
    1.86 +    return (ret);
    1.87 +}
    1.88 +
    1.89 +/*write indentation for formatting*/
    1.90 +static void write_tabs(FileStream* os){
    1.91 +    int i=0;
    1.92 +    for(;i<=tabCount;i++){
    1.93 +        write_utf8_file(os,UnicodeString("    "));
    1.94 +    }
    1.95 +}
    1.96 +
    1.97 +/*get ID for each element. ID is globally unique.*/
    1.98 +static char* getID(const char* id, const char* curKey, char* result) {
    1.99 +    if(curKey == NULL) {
   1.100 +        result = (char *)uprv_malloc(sizeof(char)*uprv_strlen(id) + 1);
   1.101 +        uprv_memset(result, 0, sizeof(char)*uprv_strlen(id) + 1);
   1.102 +        uprv_strcpy(result, id);
   1.103 +    } else {
   1.104 +        result = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(id) + 1 + uprv_strlen(curKey)) + 1);
   1.105 +        uprv_memset(result, 0, sizeof(char)*(uprv_strlen(id) + 1 + uprv_strlen(curKey)) + 1);
   1.106 +        if(id[0]!='\0'){
   1.107 +            uprv_strcpy(result, id);
   1.108 +            uprv_strcat(result, "_");
   1.109 +        }
   1.110 +        uprv_strcat(result, curKey);
   1.111 +    }
   1.112 +    return result;
   1.113 +}
   1.114 +
   1.115 +/*compute CRC for binary code*/
   1.116 +/* The code is from  http://www.theorem.com/java/CRC32.java
   1.117 + * Calculates the CRC32 - 32 bit Cyclical Redundancy Check
   1.118 + * <P> This check is used in numerous systems to verify the integrity
   1.119 + * of information.  It's also used as a hashing function.  Unlike a regular
   1.120 + * checksum, it's sensitive to the order of the characters.
   1.121 + * It produces a 32 bit
   1.122 + *
   1.123 + * @author Michael Lecuyer (mjl@theorem.com)
   1.124 + * @version 1.1 August 11, 1998
   1.125 + */
   1.126 +
   1.127 +/* ICU is not endian portable, because ICU data generated on big endian machines can be
   1.128 + * ported to big endian machines but not to little endian machines and vice versa. The
   1.129 + * conversion is not portable across platforms with different endianess.
   1.130 + */
   1.131 +
   1.132 +uint32_t computeCRC(char *ptr, uint32_t len, uint32_t lastcrc){
   1.133 +    int32_t crc;
   1.134 +    uint32_t temp1;
   1.135 +    uint32_t temp2;
   1.136 +
   1.137 +    int32_t crc_ta[256];
   1.138 +    int i = 0;
   1.139 +    int j = 0;
   1.140 +    uint32_t crc2 = 0;
   1.141 +
   1.142 +#define CRC32_POLYNOMIAL 0xEDB88320
   1.143 +
   1.144 +    /*build crc table*/
   1.145 +    for (i = 0; i <= 255; i++) {
   1.146 +        crc2 = i;
   1.147 +        for (j = 8; j > 0; j--) {
   1.148 +            if ((crc2 & 1) == 1) {
   1.149 +                crc2 = (crc2 >> 1) ^ CRC32_POLYNOMIAL;
   1.150 +            } else {
   1.151 +                crc2 >>= 1;
   1.152 +            }
   1.153 +        }
   1.154 +        crc_ta[i] = crc2;
   1.155 +    }
   1.156 +
   1.157 +    crc = lastcrc;
   1.158 +    while(len--!=0) {
   1.159 +        temp1 = (uint32_t)crc>>8;
   1.160 +        temp2 = crc_ta[(crc^*ptr) & 0xFF];
   1.161 +        crc = temp1^temp2;
   1.162 +        ptr++;
   1.163 +    }
   1.164 +    return(crc);
   1.165 +}
   1.166 +
   1.167 +static void strnrepchr(char* src, int32_t srcLen, char s, char r){
   1.168 +    int32_t i = 0;
   1.169 +    for(i=0;i<srcLen;i++){
   1.170 +        if(src[i]==s){
   1.171 +            src[i]=r;
   1.172 +        }
   1.173 +    }
   1.174 +}
   1.175 +/* Parse the filename, and get its language information.
   1.176 + * If it fails to get the language information from the filename,
   1.177 + * use "en" as the default value for language
   1.178 + */
   1.179 +static char* parseFilename(const char* id, char* /*lang*/) {
   1.180 +    int idLen = (int) uprv_strlen(id);
   1.181 +    char* localeID = (char*) uprv_malloc(idLen);
   1.182 +    int pos = 0;
   1.183 +    int canonCapacity = 0;
   1.184 +    char* canon = NULL;
   1.185 +    int canonLen = 0;
   1.186 +    /*int i;*/
   1.187 +    UErrorCode status = U_ZERO_ERROR;
   1.188 +    const char *ext = uprv_strchr(id, '.');
   1.189 +
   1.190 +    if(ext != NULL){
   1.191 +        pos = (int) (ext - id);
   1.192 +    } else {
   1.193 +        pos = idLen;
   1.194 +    }
   1.195 +    uprv_memcpy(localeID, id, pos);
   1.196 +    localeID[pos]=0; /* NUL terminate the string */
   1.197 +
   1.198 +    canonCapacity =pos*3;
   1.199 +    canon = (char*) uprv_malloc(canonCapacity);
   1.200 +    canonLen = uloc_canonicalize(localeID, canon, canonCapacity, &status);
   1.201 +
   1.202 +    if(U_FAILURE(status)){
   1.203 +        fprintf(stderr, "Could not canonicalize the locale ID: %s. Error: %s\n", localeID, u_errorName(status));
   1.204 +        exit(status);
   1.205 +    }
   1.206 +    strnrepchr(canon, canonLen, '_', '-');
   1.207 +    return canon;
   1.208 +}
   1.209 +
   1.210 +static const char* xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
   1.211 +#if 0
   1.212 +static const char* bundleStart = "<xliff version = \"1.2\" "
   1.213 +                                        "xmlns='urn:oasis:names:tc:xliff:document:1.2' "
   1.214 +                                        "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
   1.215 +                                        "xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd'>\n";
   1.216 +#else
   1.217 +static const char* bundleStart = "<xliff version = \"1.1\" "
   1.218 +                                        "xmlns='urn:oasis:names:tc:xliff:document:1.1' "
   1.219 +                                        "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
   1.220 +                                        "xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.1 http://www.oasis-open.org/committees/xliff/documents/xliff-core-1.1.xsd'>\n";
   1.221 +#endif
   1.222 +static const char* bundleEnd   = "</xliff>\n";
   1.223 +
   1.224 +void res_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status);
   1.225 +
   1.226 +static char* convertAndEscape(char** pDest, int32_t destCap, int32_t* destLength,
   1.227 +                              const UChar* src, int32_t srcLen, UErrorCode* status){
   1.228 +    int32_t srcIndex=0;
   1.229 +    char* dest=NULL;
   1.230 +    char* temp=NULL;
   1.231 +    int32_t destLen=0;
   1.232 +    UChar32 c = 0;
   1.233 +
   1.234 +    if(status==NULL || U_FAILURE(*status) || pDest==NULL  || srcLen==0 || src == NULL){
   1.235 +        return NULL;
   1.236 +    }
   1.237 +    dest =*pDest;
   1.238 +    if(dest==NULL || destCap <=0){
   1.239 +        destCap = srcLen * 8;
   1.240 +        dest = (char*) uprv_malloc(sizeof(char) * destCap);
   1.241 +        if(dest==NULL){
   1.242 +            *status=U_MEMORY_ALLOCATION_ERROR;
   1.243 +            return NULL;
   1.244 +        }
   1.245 +    }
   1.246 +
   1.247 +    dest[0]=0;
   1.248 +
   1.249 +    while(srcIndex<srcLen){
   1.250 +        U16_NEXT(src, srcIndex, srcLen, c);
   1.251 +
   1.252 +        if (U16_IS_LEAD(c) || U16_IS_TRAIL(c)) {
   1.253 +            *status = U_ILLEGAL_CHAR_FOUND;
   1.254 +            fprintf(stderr, "Illegal Surrogate! \n");
   1.255 +            uprv_free(dest);
   1.256 +            return NULL;
   1.257 +        }
   1.258 +
   1.259 +        if((destLen+U8_LENGTH(c)) < destCap){
   1.260 +
   1.261 +            /* ASCII Range */
   1.262 +            if(c <=0x007F){
   1.263 +                switch(c) {
   1.264 +                case '\x26':
   1.265 +                    uprv_strcpy(dest+( destLen),"\x26\x61\x6d\x70\x3b"); /* &amp;*/
   1.266 +                    destLen+=(int32_t)uprv_strlen("\x26\x61\x6d\x70\x3b");
   1.267 +                    break;
   1.268 +                case '\x3c':
   1.269 +                    uprv_strcpy(dest+(destLen),"\x26\x6c\x74\x3b"); /* &lt;*/
   1.270 +                    destLen+=(int32_t)uprv_strlen("\x26\x6c\x74\x3b");
   1.271 +                    break;
   1.272 +                case '\x3e':
   1.273 +                    uprv_strcpy(dest+(destLen),"\x26\x67\x74\x3b"); /* &gt;*/
   1.274 +                    destLen+=(int32_t)uprv_strlen("\x26\x67\x74\x3b");
   1.275 +                    break;
   1.276 +                case '\x22':
   1.277 +                    uprv_strcpy(dest+(destLen),"\x26\x71\x75\x6f\x74\x3b"); /* &quot;*/
   1.278 +                    destLen+=(int32_t)uprv_strlen("\x26\x71\x75\x6f\x74\x3b");
   1.279 +                    break;
   1.280 +                case '\x27':
   1.281 +                    uprv_strcpy(dest+(destLen),"\x26\x61\x70\x6f\x73\x3b"); /* &apos; */
   1.282 +                    destLen+=(int32_t)uprv_strlen("\x26\x61\x70\x6f\x73\x3b");
   1.283 +                    break;
   1.284 +
   1.285 +                 /* Disallow C0 controls except TAB, CR, LF*/
   1.286 +                case 0x00:
   1.287 +                case 0x01:
   1.288 +                case 0x02:
   1.289 +                case 0x03:
   1.290 +                case 0x04:
   1.291 +                case 0x05:
   1.292 +                case 0x06:
   1.293 +                case 0x07:
   1.294 +                case 0x08:
   1.295 +                /*case 0x09:*/
   1.296 +                /*case 0x0A: */
   1.297 +                case 0x0B:
   1.298 +                case 0x0C:
   1.299 +                /*case 0x0D:*/
   1.300 +                case 0x0E:
   1.301 +                case 0x0F:
   1.302 +                case 0x10:
   1.303 +                case 0x11:
   1.304 +                case 0x12:
   1.305 +                case 0x13:
   1.306 +                case 0x14:
   1.307 +                case 0x15:
   1.308 +                case 0x16:
   1.309 +                case 0x17:
   1.310 +                case 0x18:
   1.311 +                case 0x19:
   1.312 +                case 0x1A:
   1.313 +                case 0x1B:
   1.314 +                case 0x1C:
   1.315 +                case 0x1D:
   1.316 +                case 0x1E:
   1.317 +                case 0x1F:
   1.318 +                    *status = U_ILLEGAL_CHAR_FOUND;
   1.319 +                    fprintf(stderr, "Illegal Character \\u%04X!\n",(int)c);
   1.320 +                    uprv_free(dest);
   1.321 +                    return NULL;
   1.322 +                default:
   1.323 +                    dest[destLen++]=(char)c;
   1.324 +                }
   1.325 +            }else{
   1.326 +                UBool isError = FALSE;
   1.327 +                U8_APPEND((unsigned char*)dest,destLen,destCap,c,isError);
   1.328 +                if(isError){
   1.329 +                    *status = U_ILLEGAL_CHAR_FOUND;
   1.330 +                    fprintf(stderr, "Illegal Character \\U%08X!\n",(int)c);
   1.331 +                    uprv_free(dest);
   1.332 +                    return NULL;
   1.333 +                }
   1.334 +            }
   1.335 +        }else{
   1.336 +            destCap += destLen;
   1.337 +
   1.338 +            temp = (char*) uprv_malloc(sizeof(char)*destCap);
   1.339 +            if(temp==NULL){
   1.340 +                *status=U_MEMORY_ALLOCATION_ERROR;
   1.341 +                uprv_free(dest);
   1.342 +                return NULL;
   1.343 +            }
   1.344 +            uprv_memmove(temp,dest,destLen);
   1.345 +            destLen=0;
   1.346 +            uprv_free(dest);
   1.347 +            dest=temp;
   1.348 +            temp=NULL;
   1.349 +        }
   1.350 +
   1.351 +    }
   1.352 +    *destLength = destLen;
   1.353 +    return dest;
   1.354 +}
   1.355 +
   1.356 +#define ASTERISK 0x002A
   1.357 +#define SPACE    0x0020
   1.358 +#define CR       0x000A
   1.359 +#define LF       0x000D
   1.360 +#define AT_SIGN  0x0040
   1.361 +
   1.362 +static void
   1.363 +trim(char **src, int32_t *len){
   1.364 +
   1.365 +    char *s = NULL;
   1.366 +    int32_t i = 0;
   1.367 +    if(src == NULL || *src == NULL){
   1.368 +        return;
   1.369 +    }
   1.370 +    s = *src;
   1.371 +    /* trim from the end */
   1.372 +    for( i=(*len-1); i>= 0; i--){
   1.373 +        switch(s[i]){
   1.374 +        case ASTERISK:
   1.375 +        case SPACE:
   1.376 +        case CR:
   1.377 +        case LF:
   1.378 +            s[i] = 0;
   1.379 +            continue;
   1.380 +        default:
   1.381 +            break;
   1.382 +        }
   1.383 +        break;
   1.384 +
   1.385 +    }
   1.386 +    *len = i+1;
   1.387 +}
   1.388 +
   1.389 +static void
   1.390 +print(UChar* src, int32_t srcLen,const char *tagStart,const char *tagEnd,  UErrorCode *status){
   1.391 +    int32_t bufCapacity   = srcLen*4;
   1.392 +    char *buf       = NULL;
   1.393 +    int32_t bufLen = 0;
   1.394 +
   1.395 +    if(U_FAILURE(*status)){
   1.396 +        return;
   1.397 +    }
   1.398 +
   1.399 +    buf = (char*) (uprv_malloc(bufCapacity));
   1.400 +    if(buf==0){
   1.401 +        fprintf(stderr, "Could not allocate memory!!");
   1.402 +        exit(U_MEMORY_ALLOCATION_ERROR);
   1.403 +    }
   1.404 +    buf = convertAndEscape(&buf, bufCapacity, &bufLen, src, srcLen,status);
   1.405 +    if(U_SUCCESS(*status)){
   1.406 +        trim(&buf,&bufLen);
   1.407 +        write_utf8_file(out,UnicodeString(tagStart));
   1.408 +        write_utf8_file(out,UnicodeString(buf, bufLen, "UTF-8"));
   1.409 +        write_utf8_file(out,UnicodeString(tagEnd));
   1.410 +        write_utf8_file(out,UnicodeString("\n"));
   1.411 +
   1.412 +    }
   1.413 +}
   1.414 +static void
   1.415 +printNoteElements(struct UString *src, UErrorCode *status){
   1.416 +
   1.417 +#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */
   1.418 +
   1.419 +    int32_t capacity = 0;
   1.420 +    UChar* note = NULL;
   1.421 +    int32_t noteLen = 0;
   1.422 +    int32_t count = 0,i;
   1.423 +
   1.424 +    if(src == NULL){
   1.425 +        return;
   1.426 +    }
   1.427 +
   1.428 +    capacity = src->fLength;
   1.429 +    note  = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
   1.430 +
   1.431 +    count = getCount(src->fChars,src->fLength, UPC_NOTE, status);
   1.432 +    if(U_FAILURE(*status)){
   1.433 +        uprv_free(note);
   1.434 +        return;
   1.435 +    }
   1.436 +    for(i=0; i < count; i++){
   1.437 +        noteLen =  getAt(src->fChars,src->fLength, &note, capacity, i, UPC_NOTE, status);
   1.438 +        if(U_FAILURE(*status)){
   1.439 +            uprv_free(note);
   1.440 +            return;
   1.441 +        }
   1.442 +        if(noteLen > 0){
   1.443 +            write_tabs(out);
   1.444 +            print(note, noteLen,"<note>", "</note>", status);
   1.445 +        }
   1.446 +    }
   1.447 +    uprv_free(note);
   1.448 +#else
   1.449 +
   1.450 +    fprintf(stderr, "Warning: Could not output comments to XLIFF file. ICU has been built without RegularExpression support.\n");
   1.451 +
   1.452 +#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
   1.453 +
   1.454 +}
   1.455 +
   1.456 +static void printAttribute(const char *name, const char *value, int32_t /*len*/)
   1.457 +{
   1.458 +    write_utf8_file(out, UnicodeString(" "));
   1.459 +    write_utf8_file(out, UnicodeString(name));
   1.460 +    write_utf8_file(out, UnicodeString(" = \""));
   1.461 +    write_utf8_file(out, UnicodeString(value));
   1.462 +    write_utf8_file(out, UnicodeString("\""));
   1.463 +}
   1.464 +
   1.465 +static void printAttribute(const char *name, const UnicodeString value, int32_t /*len*/)
   1.466 +{
   1.467 +    write_utf8_file(out, UnicodeString(" "));
   1.468 +    write_utf8_file(out, UnicodeString(name));
   1.469 +    write_utf8_file(out, UnicodeString(" = \""));
   1.470 +    write_utf8_file(out, value);
   1.471 +    write_utf8_file(out, UnicodeString("\""));
   1.472 +}
   1.473 +
   1.474 +static void
   1.475 +printComments(struct UString *src, const char *resName, UBool printTranslate, UErrorCode *status){
   1.476 +
   1.477 +#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */
   1.478 +
   1.479 +    if(status==NULL || U_FAILURE(*status)){
   1.480 +        return;
   1.481 +    }
   1.482 +
   1.483 +    int32_t capacity = src->fLength + 1;
   1.484 +    char* buf = NULL;
   1.485 +    int32_t bufLen = 0;
   1.486 +    UChar* desc  = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
   1.487 +    UChar* trans = (UChar*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
   1.488 +
   1.489 +    int32_t descLen = 0, transLen=0;
   1.490 +    if(desc==NULL || trans==NULL){
   1.491 +        *status = U_MEMORY_ALLOCATION_ERROR;
   1.492 +        uprv_free(desc);
   1.493 +        uprv_free(trans);
   1.494 +        return;
   1.495 +    }
   1.496 +    src->fLength = removeCmtText(src->fChars, src->fLength, status);
   1.497 +    descLen  = getDescription(src->fChars,src->fLength, &desc, capacity, status);
   1.498 +    transLen = getTranslate(src->fChars,src->fLength, &trans, capacity, status);
   1.499 +
   1.500 +    /* first print translate attribute */
   1.501 +    if(transLen > 0){
   1.502 +        if(printTranslate){
   1.503 +            /* print translate attribute */
   1.504 +            buf = convertAndEscape(&buf, 0, &bufLen, trans, transLen, status);
   1.505 +            if(U_SUCCESS(*status)){
   1.506 +                printAttribute("translate", UnicodeString(buf, bufLen, "UTF-8"), bufLen);
   1.507 +                write_utf8_file(out,UnicodeString(">\n"));
   1.508 +            }
   1.509 +        }else if(getShowWarning()){
   1.510 +            fprintf(stderr, "Warning: Tranlate attribute for resource %s cannot be set. XLIFF prohibits it.\n", resName);
   1.511 +            /* no translate attribute .. just close the tag */
   1.512 +            write_utf8_file(out,UnicodeString(">\n"));
   1.513 +        }
   1.514 +    }else{
   1.515 +        /* no translate attribute .. just close the tag */
   1.516 +        write_utf8_file(out,UnicodeString(">\n"));
   1.517 +    }
   1.518 +
   1.519 +    if(descLen > 0){
   1.520 +        write_tabs(out);
   1.521 +        print(desc, descLen, "<!--", "-->", status);
   1.522 +    }
   1.523 +
   1.524 +    uprv_free(desc);
   1.525 +    uprv_free(trans);
   1.526 +#else
   1.527 +
   1.528 +    fprintf(stderr, "Warning: Could not output comments to XLIFF file. ICU has been built without RegularExpression support.\n");
   1.529 +
   1.530 +#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
   1.531 +
   1.532 +}
   1.533 +
   1.534 +/*
   1.535 + * Print out a containing element, like:
   1.536 + * <trans-unit id = "blah" resname = "blah" restype = "x-id-alias" translate = "no">
   1.537 + * <group id "calendar_gregorian" resname = "gregorian" restype = "x-icu-array">
   1.538 + */
   1.539 +static char *printContainer(struct SResource *res, const char *container, const char *restype, const char *mimetype, const char *id, UErrorCode *status)
   1.540 +{
   1.541 +    char resKeyBuffer[8];
   1.542 +    const char *resname = NULL;
   1.543 +    char *sid = NULL;
   1.544 +
   1.545 +    write_tabs(out);
   1.546 +
   1.547 +    resname = res_getKeyString(srBundle, res, resKeyBuffer);
   1.548 +    if (resname != NULL && *resname != 0) {
   1.549 +        sid = getID(id, resname, sid);
   1.550 +    } else {
   1.551 +        sid = getID(id, NULL, sid);
   1.552 +    }
   1.553 +
   1.554 +    write_utf8_file(out, UnicodeString("<"));
   1.555 +    write_utf8_file(out, UnicodeString(container));
   1.556 +    printAttribute("id", sid, (int32_t) uprv_strlen(sid));
   1.557 +
   1.558 +    if (resname != NULL) {
   1.559 +        printAttribute("resname", resname, (int32_t) uprv_strlen(resname));
   1.560 +    }
   1.561 +
   1.562 +    if (mimetype != NULL) {
   1.563 +        printAttribute("mime-type", mimetype, (int32_t) uprv_strlen(mimetype));
   1.564 +    }
   1.565 +
   1.566 +    if (restype != NULL) {
   1.567 +        printAttribute("restype", restype, (int32_t) uprv_strlen(restype));
   1.568 +    }
   1.569 +
   1.570 +    tabCount += 1;
   1.571 +    if (res->fComment.fLength > 0) {
   1.572 +        /* printComments will print the closing ">\n" */
   1.573 +        printComments(&res->fComment, resname, TRUE, status);
   1.574 +    } else {
   1.575 +        write_utf8_file(out, UnicodeString(">\n"));
   1.576 +    }
   1.577 +
   1.578 +    return sid;
   1.579 +}
   1.580 +
   1.581 +/* Writing Functions */
   1.582 +
   1.583 +static const char *trans_unit = "trans-unit";
   1.584 +static const char *close_trans_unit = "</trans-unit>\n";
   1.585 +static const char *source = "<source>";
   1.586 +static const char *close_source = "</source>\n";
   1.587 +static const char *group = "group";
   1.588 +static const char *close_group = "</group>\n";
   1.589 +
   1.590 +static const char *bin_unit = "bin-unit";
   1.591 +static const char *close_bin_unit = "</bin-unit>\n";
   1.592 +static const char *bin_source = "<bin-source>\n";
   1.593 +static const char *close_bin_source = "</bin-source>\n";
   1.594 +static const char *external_file = "<external-file";
   1.595 +/*static const char *close_external_file = "</external-file>\n";*/
   1.596 +static const char *internal_file = "<internal-file";
   1.597 +static const char *close_internal_file = "</internal-file>\n";
   1.598 +
   1.599 +static const char *application_mimetype = "application"; /* add "/octet-stream"? */
   1.600 +
   1.601 +static const char *alias_restype     = "x-icu-alias";
   1.602 +static const char *array_restype     = "x-icu-array";
   1.603 +static const char *binary_restype    = "x-icu-binary";
   1.604 +static const char *integer_restype   = "x-icu-integer";
   1.605 +static const char *intvector_restype = "x-icu-intvector";
   1.606 +static const char *table_restype     = "x-icu-table";
   1.607 +
   1.608 +static void
   1.609 +string_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
   1.610 +
   1.611 +    char *sid = NULL;
   1.612 +    char* buf = NULL;
   1.613 +    int32_t bufLen = 0;
   1.614 +
   1.615 +    if(status==NULL || U_FAILURE(*status)){
   1.616 +        return;
   1.617 +    }
   1.618 +
   1.619 +    sid = printContainer(res, trans_unit, NULL, NULL, id, status);
   1.620 +
   1.621 +    write_tabs(out);
   1.622 +
   1.623 +    write_utf8_file(out, UnicodeString(source));
   1.624 +
   1.625 +    buf = convertAndEscape(&buf, 0, &bufLen, res->u.fString.fChars, res->u.fString.fLength, status);
   1.626 +
   1.627 +    if (U_FAILURE(*status)) {
   1.628 +        return;
   1.629 +    }
   1.630 +
   1.631 +    write_utf8_file(out, UnicodeString(buf, bufLen, "UTF-8"));
   1.632 +    write_utf8_file(out, UnicodeString(close_source));
   1.633 +
   1.634 +    printNoteElements(&res->fComment, status);
   1.635 +
   1.636 +    tabCount -= 1;
   1.637 +    write_tabs(out);
   1.638 +
   1.639 +    write_utf8_file(out, UnicodeString(close_trans_unit));
   1.640 +
   1.641 +    uprv_free(buf);
   1.642 +    uprv_free(sid);
   1.643 +}
   1.644 +
   1.645 +static void
   1.646 +alias_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
   1.647 +    char *sid = NULL;
   1.648 +    char* buf = NULL;
   1.649 +    int32_t bufLen=0;
   1.650 +
   1.651 +    sid = printContainer(res, trans_unit, alias_restype, NULL, id, status);
   1.652 +
   1.653 +    write_tabs(out);
   1.654 +
   1.655 +    write_utf8_file(out, UnicodeString(source));
   1.656 +
   1.657 +    buf = convertAndEscape(&buf, 0, &bufLen, res->u.fString.fChars, res->u.fString.fLength, status);
   1.658 +
   1.659 +    if(U_FAILURE(*status)){
   1.660 +        return;
   1.661 +    }
   1.662 +    write_utf8_file(out, UnicodeString(buf, bufLen, "UTF-8"));
   1.663 +    write_utf8_file(out, UnicodeString(close_source));
   1.664 +
   1.665 +    printNoteElements(&res->fComment, status);
   1.666 +
   1.667 +    tabCount -= 1;
   1.668 +    write_tabs(out);
   1.669 +
   1.670 +    write_utf8_file(out, UnicodeString(close_trans_unit));
   1.671 +
   1.672 +    uprv_free(buf);
   1.673 +    uprv_free(sid);
   1.674 +}
   1.675 +
   1.676 +static void
   1.677 +array_write_xml(struct SResource *res, const char* id, const char* language, UErrorCode *status) {
   1.678 +    char* sid = NULL;
   1.679 +    int index = 0;
   1.680 +
   1.681 +    struct SResource *current = NULL;
   1.682 +
   1.683 +    sid = printContainer(res, group, array_restype, NULL, id, status);
   1.684 +
   1.685 +    current = res->u.fArray.fFirst;
   1.686 +
   1.687 +    while (current != NULL) {
   1.688 +        char c[256] = {0};
   1.689 +        char* subId = NULL;
   1.690 +
   1.691 +        itostr(c, index, 10, 0);
   1.692 +        index += 1;
   1.693 +        subId = getID(sid, c, subId);
   1.694 +
   1.695 +        res_write_xml(current, subId, language, FALSE, status);
   1.696 +        uprv_free(subId);
   1.697 +        subId = NULL;
   1.698 +
   1.699 +        if(U_FAILURE(*status)){
   1.700 +            return;
   1.701 +        }
   1.702 +
   1.703 +        current = current->fNext;
   1.704 +    }
   1.705 +
   1.706 +    tabCount -= 1;
   1.707 +    write_tabs(out);
   1.708 +    write_utf8_file(out, UnicodeString(close_group));
   1.709 +
   1.710 +    uprv_free(sid);
   1.711 +}
   1.712 +
   1.713 +static void
   1.714 +intvector_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
   1.715 +    char* sid = NULL;
   1.716 +    char* ivd = NULL;
   1.717 +    uint32_t i=0;
   1.718 +    uint32_t len=0;
   1.719 +    char buf[256] = {'0'};
   1.720 +
   1.721 +    sid = printContainer(res, group, intvector_restype, NULL, id, status);
   1.722 +
   1.723 +    for(i = 0; i < res->u.fIntVector.fCount; i += 1) {
   1.724 +        char c[256] = {0};
   1.725 +
   1.726 +        itostr(c, i, 10, 0);
   1.727 +        ivd = getID(sid, c, ivd);
   1.728 +        len = itostr(buf, res->u.fIntVector.fArray[i], 10, 0);
   1.729 +
   1.730 +        write_tabs(out);
   1.731 +        write_utf8_file(out, UnicodeString("<"));
   1.732 +        write_utf8_file(out, UnicodeString(trans_unit));
   1.733 +
   1.734 +        printAttribute("id", ivd, (int32_t)uprv_strlen(ivd));
   1.735 +        printAttribute("restype", integer_restype, (int32_t) strlen(integer_restype));
   1.736 +
   1.737 +        write_utf8_file(out, UnicodeString(">\n"));
   1.738 +
   1.739 +        tabCount += 1;
   1.740 +        write_tabs(out);
   1.741 +        write_utf8_file(out, UnicodeString(source));
   1.742 +
   1.743 +        write_utf8_file(out, UnicodeString(buf, len));
   1.744 +
   1.745 +        write_utf8_file(out, UnicodeString(close_source));
   1.746 +        tabCount -= 1;
   1.747 +        write_tabs(out);
   1.748 +        write_utf8_file(out, UnicodeString(close_trans_unit));
   1.749 +
   1.750 +        uprv_free(ivd);
   1.751 +        ivd = NULL;
   1.752 +    }
   1.753 +
   1.754 +    tabCount -= 1;
   1.755 +    write_tabs(out);
   1.756 +
   1.757 +    write_utf8_file(out, UnicodeString(close_group));
   1.758 +    uprv_free(sid);
   1.759 +    sid = NULL;
   1.760 +}
   1.761 +
   1.762 +static void
   1.763 +int_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
   1.764 +    char* sid = NULL;
   1.765 +    char buf[256] = {0};
   1.766 +    uint32_t len = 0;
   1.767 +
   1.768 +    sid = printContainer(res, trans_unit, integer_restype, NULL, id, status);
   1.769 +
   1.770 +    write_tabs(out);
   1.771 +
   1.772 +    write_utf8_file(out, UnicodeString(source));
   1.773 +
   1.774 +    len = itostr(buf, res->u.fIntValue.fValue, 10, 0);
   1.775 +    write_utf8_file(out, UnicodeString(buf, len));
   1.776 +
   1.777 +    write_utf8_file(out, UnicodeString(close_source));
   1.778 +
   1.779 +    printNoteElements(&res->fComment, status);
   1.780 +
   1.781 +    tabCount -= 1;
   1.782 +    write_tabs(out);
   1.783 +
   1.784 +    write_utf8_file(out, UnicodeString(close_trans_unit));
   1.785 +
   1.786 +    uprv_free(sid);
   1.787 +    sid = NULL;
   1.788 +}
   1.789 +
   1.790 +static void
   1.791 +bin_write_xml(struct SResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
   1.792 +    const char* m_type = application_mimetype;
   1.793 +    char* sid = NULL;
   1.794 +    uint32_t crc = 0xFFFFFFFF;
   1.795 +
   1.796 +    char fileName[1024] ={0};
   1.797 +    int32_t tLen = ( outDir == NULL) ? 0 :(int32_t)uprv_strlen(outDir);
   1.798 +    char* fn =  (char*) uprv_malloc(sizeof(char) * (tLen+1024 +
   1.799 +                                                    (res->u.fBinaryValue.fFileName !=NULL ?
   1.800 +                                                    uprv_strlen(res->u.fBinaryValue.fFileName) :0)));
   1.801 +    const char* ext = NULL;
   1.802 +
   1.803 +    char* f = NULL;
   1.804 +
   1.805 +    fn[0]=0;
   1.806 +
   1.807 +    if(res->u.fBinaryValue.fFileName != NULL){
   1.808 +        uprv_strcpy(fileName, res->u.fBinaryValue.fFileName);
   1.809 +        f = uprv_strrchr(fileName, '\\');
   1.810 +
   1.811 +        if (f != NULL) {
   1.812 +            f++;
   1.813 +        } else {
   1.814 +            f = fileName;
   1.815 +        }
   1.816 +
   1.817 +        ext = uprv_strrchr(fileName, '.');
   1.818 +
   1.819 +        if (ext == NULL) {
   1.820 +            fprintf(stderr, "Error: %s is an unknown binary filename type.\n", fileName);
   1.821 +            exit(U_ILLEGAL_ARGUMENT_ERROR);
   1.822 +        }
   1.823 +
   1.824 +        if(uprv_strcmp(ext, ".jpg")==0 || uprv_strcmp(ext, ".jpeg")==0 || uprv_strcmp(ext, ".gif")==0 ){
   1.825 +            m_type = "image";
   1.826 +        } else if(uprv_strcmp(ext, ".wav")==0 || uprv_strcmp(ext, ".au")==0 ){
   1.827 +            m_type = "audio";
   1.828 +        } else if(uprv_strcmp(ext, ".avi")==0 || uprv_strcmp(ext, ".mpg")==0 || uprv_strcmp(ext, ".mpeg")==0){
   1.829 +            m_type = "video";
   1.830 +        } else if(uprv_strcmp(ext, ".txt")==0 || uprv_strcmp(ext, ".text")==0){
   1.831 +            m_type = "text";
   1.832 +        }
   1.833 +
   1.834 +        sid = printContainer(res, bin_unit, binary_restype, m_type, id, status);
   1.835 +
   1.836 +        write_tabs(out);
   1.837 +
   1.838 +        write_utf8_file(out, UnicodeString(bin_source));
   1.839 +
   1.840 +        tabCount+= 1;
   1.841 +        write_tabs(out);
   1.842 +
   1.843 +        write_utf8_file(out, UnicodeString(external_file));
   1.844 +        printAttribute("href", f, (int32_t)uprv_strlen(f));
   1.845 +        write_utf8_file(out, UnicodeString("/>\n"));
   1.846 +        tabCount -= 1;
   1.847 +        write_tabs(out);
   1.848 +
   1.849 +        write_utf8_file(out, UnicodeString(close_bin_source));
   1.850 +
   1.851 +        printNoteElements(&res->fComment, status);
   1.852 +        tabCount -= 1;
   1.853 +        write_tabs(out);
   1.854 +        write_utf8_file(out, UnicodeString(close_bin_unit));
   1.855 +    } else {
   1.856 +        char temp[256] = {0};
   1.857 +        uint32_t i = 0;
   1.858 +        int32_t len=0;
   1.859 +
   1.860 +        sid = printContainer(res, bin_unit, binary_restype, m_type, id, status);
   1.861 +
   1.862 +        write_tabs(out);
   1.863 +        write_utf8_file(out, UnicodeString(bin_source));
   1.864 +
   1.865 +        tabCount += 1;
   1.866 +        write_tabs(out);
   1.867 +
   1.868 +        write_utf8_file(out, UnicodeString(internal_file));
   1.869 +        printAttribute("form", application_mimetype, (int32_t) uprv_strlen(application_mimetype));
   1.870 +
   1.871 +        while(i <res->u.fBinaryValue.fLength){
   1.872 +            len = itostr(temp, res->u.fBinaryValue.fData[i], 16, 2);
   1.873 +            crc = computeCRC(temp, len, crc);
   1.874 +            i++;
   1.875 +        }
   1.876 +
   1.877 +        len = itostr(temp, crc, 10, 0);
   1.878 +        printAttribute("crc", temp, len);
   1.879 +
   1.880 +        write_utf8_file(out, UnicodeString(">"));
   1.881 +
   1.882 +        i = 0;
   1.883 +        while(i <res->u.fBinaryValue.fLength){
   1.884 +            len = itostr(temp, res->u.fBinaryValue.fData[i], 16, 2);
   1.885 +            write_utf8_file(out, UnicodeString(temp));
   1.886 +            i += 1;
   1.887 +        }
   1.888 +
   1.889 +        write_utf8_file(out, UnicodeString(close_internal_file));
   1.890 +
   1.891 +        tabCount -= 2;
   1.892 +        write_tabs(out);
   1.893 +
   1.894 +        write_utf8_file(out, UnicodeString(close_bin_source));
   1.895 +        printNoteElements(&res->fComment, status);
   1.896 +
   1.897 +        tabCount -= 1;
   1.898 +        write_tabs(out);
   1.899 +        write_utf8_file(out, UnicodeString(close_bin_unit));
   1.900 +
   1.901 +        uprv_free(sid);
   1.902 +        sid = NULL;
   1.903 +    }
   1.904 +
   1.905 +    uprv_free(fn);
   1.906 +}
   1.907 +
   1.908 +
   1.909 +
   1.910 +static void
   1.911 +table_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status) {
   1.912 +
   1.913 +    uint32_t  i         = 0;
   1.914 +
   1.915 +    struct SResource *current = NULL;
   1.916 +    char* sid = NULL;
   1.917 +
   1.918 +    if (U_FAILURE(*status)) {
   1.919 +        return ;
   1.920 +    }
   1.921 +
   1.922 +    sid = printContainer(res, group, table_restype, NULL, id, status);
   1.923 +
   1.924 +    if(isTopLevel) {
   1.925 +        sid[0] = '\0';
   1.926 +    }
   1.927 +
   1.928 +    current = res->u.fTable.fFirst;
   1.929 +    i = 0;
   1.930 +
   1.931 +    while (current != NULL) {
   1.932 +        res_write_xml(current, sid, language, FALSE, status);
   1.933 +
   1.934 +        if(U_FAILURE(*status)){
   1.935 +            return;
   1.936 +        }
   1.937 +
   1.938 +        i += 1;
   1.939 +        current = current->fNext;
   1.940 +    }
   1.941 +
   1.942 +    tabCount -= 1;
   1.943 +    write_tabs(out);
   1.944 +
   1.945 +    write_utf8_file(out, UnicodeString(close_group));
   1.946 +
   1.947 +    uprv_free(sid);
   1.948 +    sid = NULL;
   1.949 +}
   1.950 +
   1.951 +void
   1.952 +res_write_xml(struct SResource *res, const char* id,  const char* language, UBool isTopLevel, UErrorCode *status) {
   1.953 +
   1.954 +    if (U_FAILURE(*status)) {
   1.955 +        return ;
   1.956 +    }
   1.957 +
   1.958 +    if (res != NULL) {
   1.959 +        switch (res->fType) {
   1.960 +        case URES_STRING:
   1.961 +             string_write_xml    (res, id, language, status);
   1.962 +             return;
   1.963 +
   1.964 +        case URES_ALIAS:
   1.965 +             alias_write_xml     (res, id, language, status);
   1.966 +             return;
   1.967 +
   1.968 +        case URES_INT_VECTOR:
   1.969 +             intvector_write_xml (res, id, language, status);
   1.970 +             return;
   1.971 +
   1.972 +        case URES_BINARY:
   1.973 +             bin_write_xml       (res, id, language, status);
   1.974 +             return;
   1.975 +
   1.976 +        case URES_INT:
   1.977 +             int_write_xml       (res, id, language, status);
   1.978 +             return;
   1.979 +
   1.980 +        case URES_ARRAY:
   1.981 +             array_write_xml     (res, id, language, status);
   1.982 +             return;
   1.983 +
   1.984 +        case URES_TABLE:
   1.985 +             table_write_xml     (res, id, language, isTopLevel, status);
   1.986 +             return;
   1.987 +
   1.988 +        default:
   1.989 +            break;
   1.990 +        }
   1.991 +    }
   1.992 +
   1.993 +    *status = U_INTERNAL_PROGRAM_ERROR;
   1.994 +}
   1.995 +
   1.996 +void
   1.997 +bundle_write_xml(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, const char* filename,
   1.998 +                  char *writtenFilename, int writtenFilenameLen,
   1.999 +                  const char* language, const char* outFileName, UErrorCode *status) {
  1.1000 +
  1.1001 +    char* xmlfileName = NULL;
  1.1002 +    char* outputFileName = NULL;
  1.1003 +    char* originalFileName = NULL;
  1.1004 +    const char* fileStart = "<file xml:space = \"preserve\" source-language = \"";
  1.1005 +    const char* file1 = "\" datatype = \"x-icu-resource-bundle\" ";
  1.1006 +    const char* file2 = "original = \"";
  1.1007 +    const char* file4 = "\" date = \"";
  1.1008 +    const char* fileEnd = "</file>\n";
  1.1009 +    const char* headerStart = "<header>\n";
  1.1010 +    const char* headerEnd = "</header>\n";
  1.1011 +    const char* bodyStart = "<body>\n";
  1.1012 +    const char* bodyEnd = "</body>\n";
  1.1013 +
  1.1014 +    const char *tool_start = "<tool";
  1.1015 +    const char *tool_id = "genrb-" GENRB_VERSION "-icu-" U_ICU_VERSION;
  1.1016 +    const char *tool_name = "genrb";
  1.1017 +
  1.1018 +    char* temp = NULL;
  1.1019 +    char* lang = NULL;
  1.1020 +    const char* pos = NULL;
  1.1021 +    int32_t first, index;
  1.1022 +    time_t currTime;
  1.1023 +    char timeBuf[128];
  1.1024 +
  1.1025 +    outDir = outputDir;
  1.1026 +
  1.1027 +    srBundle = bundle;
  1.1028 +
  1.1029 +    pos = uprv_strrchr(filename, '\\');
  1.1030 +    if(pos != NULL) {
  1.1031 +        first = (int32_t)(pos - filename + 1);
  1.1032 +    } else {
  1.1033 +        first = 0;
  1.1034 +    }
  1.1035 +    index = (int32_t)(uprv_strlen(filename) - uprv_strlen(textExt) - first);
  1.1036 +    originalFileName = (char *)uprv_malloc(sizeof(char)*index+1);
  1.1037 +    uprv_memset(originalFileName, 0, sizeof(char)*index+1);
  1.1038 +    uprv_strncpy(originalFileName, filename + first, index);
  1.1039 +
  1.1040 +    if(uprv_strcmp(originalFileName, srBundle->fLocale) != 0) {
  1.1041 +        fprintf(stdout, "Warning: The file name is not same as the resource name!\n");
  1.1042 +    }
  1.1043 +
  1.1044 +    temp = originalFileName;
  1.1045 +    originalFileName = (char *)uprv_malloc(sizeof(char)* (uprv_strlen(temp)+uprv_strlen(textExt)) + 1);
  1.1046 +    uprv_memset(originalFileName, 0, sizeof(char)* (uprv_strlen(temp)+uprv_strlen(textExt)) + 1);
  1.1047 +    uprv_strcat(originalFileName, temp);
  1.1048 +    uprv_strcat(originalFileName, textExt);
  1.1049 +    uprv_free(temp);
  1.1050 +    temp = NULL;
  1.1051 +
  1.1052 +
  1.1053 +    if (language == NULL) {
  1.1054 +/*        lang = parseFilename(filename, lang);
  1.1055 +        if (lang == NULL) {*/
  1.1056 +            /* now check if locale name is valid or not
  1.1057 +             * this is to cater for situation where
  1.1058 +             * pegasusServer.txt contains
  1.1059 +             *
  1.1060 +             * en{
  1.1061 +             *      ..
  1.1062 +             * }
  1.1063 +             */
  1.1064 +             lang = parseFilename(srBundle->fLocale, lang);
  1.1065 +             /*
  1.1066 +              * Neither  the file name nor the table name inside the
  1.1067 +              * txt file contain a valid country and language codes
  1.1068 +              * throw an error.
  1.1069 +              * pegasusServer.txt contains
  1.1070 +              *
  1.1071 +              * testelements{
  1.1072 +              *     ....
  1.1073 +              * }
  1.1074 +              */
  1.1075 +             if(lang==NULL){
  1.1076 +                 fprintf(stderr, "Error: The file name and table name do not contain a valid language code. Please use -l option to specify it.\n");
  1.1077 +                 exit(U_ILLEGAL_ARGUMENT_ERROR);
  1.1078 +             }
  1.1079 +       /* }*/
  1.1080 +    } else {
  1.1081 +        lang = (char *)uprv_malloc(sizeof(char)*uprv_strlen(language) +1);
  1.1082 +        uprv_memset(lang, 0, sizeof(char)*uprv_strlen(language) +1);
  1.1083 +        uprv_strcpy(lang, language);
  1.1084 +    }
  1.1085 +
  1.1086 +    if(outFileName) {
  1.1087 +        outputFileName = (char *)uprv_malloc(sizeof(char)*uprv_strlen(outFileName) + 1);
  1.1088 +        uprv_memset(outputFileName, 0, sizeof(char)*uprv_strlen(outFileName) + 1);
  1.1089 +        uprv_strcpy(outputFileName,outFileName);
  1.1090 +    } else {
  1.1091 +        outputFileName = (char *)uprv_malloc(sizeof(char)*uprv_strlen(srBundle->fLocale) + 1);
  1.1092 +        uprv_memset(outputFileName, 0, sizeof(char)*uprv_strlen(srBundle->fLocale) + 1);
  1.1093 +        uprv_strcpy(outputFileName,srBundle->fLocale);
  1.1094 +    }
  1.1095 +
  1.1096 +    if(outputDir) {
  1.1097 +        xmlfileName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(outputDir) + uprv_strlen(outputFileName) + uprv_strlen(xliffExt) + 1) +1);
  1.1098 +        uprv_memset(xmlfileName, 0, sizeof(char)*(uprv_strlen(outputDir)+ uprv_strlen(outputFileName) + uprv_strlen(xliffExt) + 1) +1);
  1.1099 +    } else {
  1.1100 +        xmlfileName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(outputFileName) + uprv_strlen(xliffExt)) +1);
  1.1101 +        uprv_memset(xmlfileName, 0, sizeof(char)*(uprv_strlen(outputFileName) + uprv_strlen(xliffExt)) +1);
  1.1102 +    }
  1.1103 +
  1.1104 +    if(outputDir){
  1.1105 +        uprv_strcpy(xmlfileName, outputDir);
  1.1106 +        if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){
  1.1107 +            uprv_strcat(xmlfileName,U_FILE_SEP_STRING);
  1.1108 +        }
  1.1109 +    }
  1.1110 +    uprv_strcat(xmlfileName,outputFileName);
  1.1111 +    uprv_strcat(xmlfileName,xliffExt);
  1.1112 +
  1.1113 +    if (writtenFilename) {
  1.1114 +        uprv_strncpy(writtenFilename, xmlfileName, writtenFilenameLen);
  1.1115 +    }
  1.1116 +
  1.1117 +    if (U_FAILURE(*status)) {
  1.1118 +        goto cleanup_bundle_write_xml;
  1.1119 +    }
  1.1120 +
  1.1121 +    out= T_FileStream_open(xmlfileName,"w");
  1.1122 +
  1.1123 +    if(out==NULL){
  1.1124 +        *status = U_FILE_ACCESS_ERROR;
  1.1125 +        goto cleanup_bundle_write_xml;
  1.1126 +    }
  1.1127 +    write_utf8_file(out, xmlHeader);
  1.1128 +
  1.1129 +    if(outputEnc && *outputEnc!='\0'){
  1.1130 +        /* store the output encoding */
  1.1131 +        enc = outputEnc;
  1.1132 +        conv=ucnv_open(enc,status);
  1.1133 +        if(U_FAILURE(*status)){
  1.1134 +            goto cleanup_bundle_write_xml;
  1.1135 +        }
  1.1136 +    }
  1.1137 +    write_utf8_file(out, bundleStart);
  1.1138 +    write_tabs(out);
  1.1139 +    write_utf8_file(out, fileStart);
  1.1140 +    /* check if lang and language are the same */
  1.1141 +    if(language != NULL && uprv_strcmp(lang, srBundle->fLocale)!=0){
  1.1142 +        fprintf(stderr,"Warning: The top level tag in the resource and language specified are not the same. Please check the input.\n");
  1.1143 +    }
  1.1144 +    write_utf8_file(out, UnicodeString(lang));
  1.1145 +    write_utf8_file(out, UnicodeString(file1));
  1.1146 +    write_utf8_file(out, UnicodeString(file2));
  1.1147 +    write_utf8_file(out, UnicodeString(originalFileName));
  1.1148 +    write_utf8_file(out, UnicodeString(file4));
  1.1149 +
  1.1150 +    time(&currTime);
  1.1151 +    strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&currTime));
  1.1152 +    write_utf8_file(out, UnicodeString(timeBuf));
  1.1153 +    write_utf8_file(out, UnicodeString("\">\n"));
  1.1154 +
  1.1155 +    tabCount += 1;
  1.1156 +    write_tabs(out);
  1.1157 +    write_utf8_file(out, headerStart);
  1.1158 +
  1.1159 +    tabCount += 1;
  1.1160 +    write_tabs(out);
  1.1161 +
  1.1162 +    write_utf8_file(out, tool_start);
  1.1163 +    printAttribute("tool-id", tool_id, (int32_t) uprv_strlen(tool_id));
  1.1164 +    printAttribute("tool-name", tool_name, (int32_t) uprv_strlen(tool_name));
  1.1165 +    write_utf8_file(out, UnicodeString("/>\n"));
  1.1166 +
  1.1167 +    tabCount -= 1;
  1.1168 +    write_tabs(out);
  1.1169 +
  1.1170 +    write_utf8_file(out, UnicodeString(headerEnd));
  1.1171 +
  1.1172 +    write_tabs(out);
  1.1173 +    tabCount += 1;
  1.1174 +
  1.1175 +    write_utf8_file(out, UnicodeString(bodyStart));
  1.1176 +
  1.1177 +
  1.1178 +    res_write_xml(bundle->fRoot, bundle->fLocale, lang, TRUE, status);
  1.1179 +
  1.1180 +    tabCount -= 1;
  1.1181 +    write_tabs(out);
  1.1182 +
  1.1183 +    write_utf8_file(out, UnicodeString(bodyEnd));
  1.1184 +    tabCount--;
  1.1185 +    write_tabs(out);
  1.1186 +    write_utf8_file(out, UnicodeString(fileEnd));
  1.1187 +    tabCount--;
  1.1188 +    write_tabs(out);
  1.1189 +    write_utf8_file(out, UnicodeString(bundleEnd));
  1.1190 +    T_FileStream_close(out);
  1.1191 +
  1.1192 +    ucnv_close(conv);
  1.1193 +
  1.1194 +cleanup_bundle_write_xml:
  1.1195 +    uprv_free(originalFileName);
  1.1196 +    uprv_free(lang);
  1.1197 +    if(xmlfileName != NULL) {
  1.1198 +        uprv_free(xmlfileName);
  1.1199 +    }
  1.1200 +    if(outputFileName != NULL){
  1.1201 +        uprv_free(outputFileName);
  1.1202 +    }
  1.1203 +}

mercurial