11cb0ef41Sopenharmony_ci// © 2016 and later: Unicode, Inc. and others.
21cb0ef41Sopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html
31cb0ef41Sopenharmony_ci/*
41cb0ef41Sopenharmony_ci*******************************************************************************
51cb0ef41Sopenharmony_ci*
61cb0ef41Sopenharmony_ci*   Copyright (C) 2002-2015, International Business Machines
71cb0ef41Sopenharmony_ci*   Corporation and others.  All Rights Reserved.
81cb0ef41Sopenharmony_ci*
91cb0ef41Sopenharmony_ci*******************************************************************************
101cb0ef41Sopenharmony_ci*
111cb0ef41Sopenharmony_ci* File wrtxml.cpp
121cb0ef41Sopenharmony_ci*
131cb0ef41Sopenharmony_ci* Modification History:
141cb0ef41Sopenharmony_ci*
151cb0ef41Sopenharmony_ci*   Date        Name        Description
161cb0ef41Sopenharmony_ci*   10/01/02    Ram         Creation.
171cb0ef41Sopenharmony_ci*   02/07/08    Spieth      Correct XLIFF generation on EBCDIC platform
181cb0ef41Sopenharmony_ci*
191cb0ef41Sopenharmony_ci*******************************************************************************
201cb0ef41Sopenharmony_ci*/
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Safer use of UnicodeString.
231cb0ef41Sopenharmony_ci#ifndef UNISTR_FROM_CHAR_EXPLICIT
241cb0ef41Sopenharmony_ci#   define UNISTR_FROM_CHAR_EXPLICIT explicit
251cb0ef41Sopenharmony_ci#endif
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci// Less important, but still a good idea.
281cb0ef41Sopenharmony_ci#ifndef UNISTR_FROM_STRING_EXPLICIT
291cb0ef41Sopenharmony_ci#   define UNISTR_FROM_STRING_EXPLICIT explicit
301cb0ef41Sopenharmony_ci#endif
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci#include "reslist.h"
331cb0ef41Sopenharmony_ci#include "unewdata.h"
341cb0ef41Sopenharmony_ci#include "unicode/ures.h"
351cb0ef41Sopenharmony_ci#include "errmsg.h"
361cb0ef41Sopenharmony_ci#include "filestrm.h"
371cb0ef41Sopenharmony_ci#include "cstring.h"
381cb0ef41Sopenharmony_ci#include "unicode/ucnv.h"
391cb0ef41Sopenharmony_ci#include "genrb.h"
401cb0ef41Sopenharmony_ci#include "rle.h"
411cb0ef41Sopenharmony_ci#include "uhash.h"
421cb0ef41Sopenharmony_ci#include "uresimp.h"
431cb0ef41Sopenharmony_ci#include "unicode/ustring.h"
441cb0ef41Sopenharmony_ci#include "unicode/uchar.h"
451cb0ef41Sopenharmony_ci#include "ustr.h"
461cb0ef41Sopenharmony_ci#include "prscmnts.h"
471cb0ef41Sopenharmony_ci#include "unicode/unistr.h"
481cb0ef41Sopenharmony_ci#include "unicode/utf8.h"
491cb0ef41Sopenharmony_ci#include "unicode/utf16.h"
501cb0ef41Sopenharmony_ci#include <time.h>
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciU_NAMESPACE_USE
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cistatic int tabCount = 0;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cistatic FileStream* out=nullptr;
571cb0ef41Sopenharmony_cistatic struct SRBRoot* srBundle ;
581cb0ef41Sopenharmony_cistatic const char* outDir = nullptr;
591cb0ef41Sopenharmony_cistatic const char* enc ="";
601cb0ef41Sopenharmony_cistatic UConverter* conv = nullptr;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciconst char* const* ISOLanguages;
631cb0ef41Sopenharmony_ciconst char* const* ISOCountries;
641cb0ef41Sopenharmony_ciconst char* textExt = ".txt";
651cb0ef41Sopenharmony_ciconst char* xliffExt = ".xlf";
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_cistatic int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString)
681cb0ef41Sopenharmony_ci{
691cb0ef41Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
701cb0ef41Sopenharmony_ci    int32_t len = 0;
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    // preflight to get the destination buffer size
731cb0ef41Sopenharmony_ci    u_strToUTF8(nullptr,
741cb0ef41Sopenharmony_ci                0,
751cb0ef41Sopenharmony_ci                &len,
761cb0ef41Sopenharmony_ci                toUCharPtr(outString.getBuffer()),
771cb0ef41Sopenharmony_ci                outString.length(),
781cb0ef41Sopenharmony_ci                &status);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    // allocate the buffer
811cb0ef41Sopenharmony_ci    char* dest = (char*)uprv_malloc(len);
821cb0ef41Sopenharmony_ci    status = U_ZERO_ERROR;
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    // convert the data
851cb0ef41Sopenharmony_ci    u_strToUTF8(dest,
861cb0ef41Sopenharmony_ci                len,
871cb0ef41Sopenharmony_ci                &len,
881cb0ef41Sopenharmony_ci                toUCharPtr(outString.getBuffer()),
891cb0ef41Sopenharmony_ci                outString.length(),
901cb0ef41Sopenharmony_ci                &status);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci    // write data to out file
931cb0ef41Sopenharmony_ci    int32_t ret = T_FileStream_write(fileStream, dest, len);
941cb0ef41Sopenharmony_ci    uprv_free(dest);
951cb0ef41Sopenharmony_ci    return (ret);
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci/*write indentation for formatting*/
991cb0ef41Sopenharmony_cistatic void write_tabs(FileStream* os){
1001cb0ef41Sopenharmony_ci    int i=0;
1011cb0ef41Sopenharmony_ci    for(;i<=tabCount;i++){
1021cb0ef41Sopenharmony_ci        write_utf8_file(os,UnicodeString("    "));
1031cb0ef41Sopenharmony_ci    }
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci/*get ID for each element. ID is globally unique.*/
1071cb0ef41Sopenharmony_cistatic char* getID(const char* id, const char* curKey, char* result) {
1081cb0ef41Sopenharmony_ci    if(curKey == nullptr) {
1091cb0ef41Sopenharmony_ci        result = (char *)uprv_malloc(sizeof(char)*uprv_strlen(id) + 1);
1101cb0ef41Sopenharmony_ci        uprv_memset(result, 0, sizeof(char)*uprv_strlen(id) + 1);
1111cb0ef41Sopenharmony_ci        uprv_strcpy(result, id);
1121cb0ef41Sopenharmony_ci    } else {
1131cb0ef41Sopenharmony_ci        result = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(id) + 1 + uprv_strlen(curKey)) + 1);
1141cb0ef41Sopenharmony_ci        uprv_memset(result, 0, sizeof(char)*(uprv_strlen(id) + 1 + uprv_strlen(curKey)) + 1);
1151cb0ef41Sopenharmony_ci        if(id[0]!='\0'){
1161cb0ef41Sopenharmony_ci            uprv_strcpy(result, id);
1171cb0ef41Sopenharmony_ci            uprv_strcat(result, "_");
1181cb0ef41Sopenharmony_ci        }
1191cb0ef41Sopenharmony_ci        uprv_strcat(result, curKey);
1201cb0ef41Sopenharmony_ci    }
1211cb0ef41Sopenharmony_ci    return result;
1221cb0ef41Sopenharmony_ci}
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci/*compute CRC for binary code*/
1251cb0ef41Sopenharmony_ci/* The code is from  http://www.theorem.com/java/CRC32.java
1261cb0ef41Sopenharmony_ci * Calculates the CRC32 - 32 bit Cyclical Redundancy Check
1271cb0ef41Sopenharmony_ci * <P> This check is used in numerous systems to verify the integrity
1281cb0ef41Sopenharmony_ci * of information.  It's also used as a hashing function.  Unlike a regular
1291cb0ef41Sopenharmony_ci * checksum, it's sensitive to the order of the characters.
1301cb0ef41Sopenharmony_ci * It produces a 32 bit
1311cb0ef41Sopenharmony_ci *
1321cb0ef41Sopenharmony_ci * @author Michael Lecuyer (mjl@theorem.com)
1331cb0ef41Sopenharmony_ci * @version 1.1 August 11, 1998
1341cb0ef41Sopenharmony_ci */
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci/* ICU is not endian portable, because ICU data generated on big endian machines can be
1371cb0ef41Sopenharmony_ci * ported to big endian machines but not to little endian machines and vice versa. The
1381cb0ef41Sopenharmony_ci * conversion is not portable across platforms with different endianness.
1391cb0ef41Sopenharmony_ci */
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ciuint32_t computeCRC(const char *ptr, uint32_t len, uint32_t lastcrc){
1421cb0ef41Sopenharmony_ci    int32_t crc;
1431cb0ef41Sopenharmony_ci    uint32_t temp1;
1441cb0ef41Sopenharmony_ci    uint32_t temp2;
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci    int32_t crc_ta[256];
1471cb0ef41Sopenharmony_ci    int i = 0;
1481cb0ef41Sopenharmony_ci    int j = 0;
1491cb0ef41Sopenharmony_ci    uint32_t crc2 = 0;
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci#define CRC32_POLYNOMIAL 0xEDB88320
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci    /*build crc table*/
1541cb0ef41Sopenharmony_ci    for (i = 0; i <= 255; i++) {
1551cb0ef41Sopenharmony_ci        crc2 = i;
1561cb0ef41Sopenharmony_ci        for (j = 8; j > 0; j--) {
1571cb0ef41Sopenharmony_ci            if ((crc2 & 1) == 1) {
1581cb0ef41Sopenharmony_ci                crc2 = (crc2 >> 1) ^ CRC32_POLYNOMIAL;
1591cb0ef41Sopenharmony_ci            } else {
1601cb0ef41Sopenharmony_ci                crc2 >>= 1;
1611cb0ef41Sopenharmony_ci            }
1621cb0ef41Sopenharmony_ci        }
1631cb0ef41Sopenharmony_ci        crc_ta[i] = crc2;
1641cb0ef41Sopenharmony_ci    }
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci    crc = lastcrc;
1671cb0ef41Sopenharmony_ci    while(len--!=0) {
1681cb0ef41Sopenharmony_ci        temp1 = (uint32_t)crc>>8;
1691cb0ef41Sopenharmony_ci        temp2 = crc_ta[(crc^*ptr) & 0xFF];
1701cb0ef41Sopenharmony_ci        crc = temp1^temp2;
1711cb0ef41Sopenharmony_ci        ptr++;
1721cb0ef41Sopenharmony_ci    }
1731cb0ef41Sopenharmony_ci    return(crc);
1741cb0ef41Sopenharmony_ci}
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_cistatic void strnrepchr(char* src, int32_t srcLen, char s, char r){
1771cb0ef41Sopenharmony_ci    int32_t i = 0;
1781cb0ef41Sopenharmony_ci    for(i=0;i<srcLen;i++){
1791cb0ef41Sopenharmony_ci        if(src[i]==s){
1801cb0ef41Sopenharmony_ci            src[i]=r;
1811cb0ef41Sopenharmony_ci        }
1821cb0ef41Sopenharmony_ci    }
1831cb0ef41Sopenharmony_ci}
1841cb0ef41Sopenharmony_ci/* Parse the filename, and get its language information.
1851cb0ef41Sopenharmony_ci * If it fails to get the language information from the filename,
1861cb0ef41Sopenharmony_ci * use "en" as the default value for language
1871cb0ef41Sopenharmony_ci */
1881cb0ef41Sopenharmony_cistatic char* parseFilename(const char* id, char* /*lang*/) {
1891cb0ef41Sopenharmony_ci    int idLen = (int) uprv_strlen(id);
1901cb0ef41Sopenharmony_ci    char* localeID = (char*) uprv_malloc(idLen);
1911cb0ef41Sopenharmony_ci    int pos = 0;
1921cb0ef41Sopenharmony_ci    int canonCapacity = 0;
1931cb0ef41Sopenharmony_ci    char* canon = nullptr;
1941cb0ef41Sopenharmony_ci    int canonLen = 0;
1951cb0ef41Sopenharmony_ci    /*int i;*/
1961cb0ef41Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
1971cb0ef41Sopenharmony_ci    const char *ext = uprv_strchr(id, '.');
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci    if(ext != nullptr){
2001cb0ef41Sopenharmony_ci        pos = (int) (ext - id);
2011cb0ef41Sopenharmony_ci    } else {
2021cb0ef41Sopenharmony_ci        pos = idLen;
2031cb0ef41Sopenharmony_ci    }
2041cb0ef41Sopenharmony_ci    uprv_memcpy(localeID, id, pos);
2051cb0ef41Sopenharmony_ci    localeID[pos]=0; /* NUL terminate the string */
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci    canonCapacity =pos*3;
2081cb0ef41Sopenharmony_ci    canon = (char*) uprv_malloc(canonCapacity);
2091cb0ef41Sopenharmony_ci    canonLen = uloc_canonicalize(localeID, canon, canonCapacity, &status);
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci    if(U_FAILURE(status)){
2121cb0ef41Sopenharmony_ci        fprintf(stderr, "Could not canonicalize the locale ID: %s. Error: %s\n", localeID, u_errorName(status));
2131cb0ef41Sopenharmony_ci        exit(status);
2141cb0ef41Sopenharmony_ci    }
2151cb0ef41Sopenharmony_ci    strnrepchr(canon, canonLen, '_', '-');
2161cb0ef41Sopenharmony_ci    return canon;
2171cb0ef41Sopenharmony_ci}
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_cistatic const char* xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
2201cb0ef41Sopenharmony_ci#if 0
2211cb0ef41Sopenharmony_cistatic const char* bundleStart = "<xliff version = \"1.2\" "
2221cb0ef41Sopenharmony_ci                                        "xmlns='urn:oasis:names:tc:xliff:document:1.2' "
2231cb0ef41Sopenharmony_ci                                        "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
2241cb0ef41Sopenharmony_ci                                        "xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd'>\n";
2251cb0ef41Sopenharmony_ci#else
2261cb0ef41Sopenharmony_cistatic const char* bundleStart = "<xliff version = \"1.1\" "
2271cb0ef41Sopenharmony_ci                                        "xmlns='urn:oasis:names:tc:xliff:document:1.1' "
2281cb0ef41Sopenharmony_ci                                        "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
2291cb0ef41Sopenharmony_ci                                        "xsi:schemaLocation='urn:oasis:names:tc:xliff:document:1.1 http://www.oasis-open.org/committees/xliff/documents/xliff-core-1.1.xsd'>\n";
2301cb0ef41Sopenharmony_ci#endif
2311cb0ef41Sopenharmony_cistatic const char* bundleEnd   = "</xliff>\n";
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_civoid res_write_xml(struct SResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status);
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_cistatic char* convertAndEscape(char** pDest, int32_t destCap, int32_t* destLength,
2361cb0ef41Sopenharmony_ci                              const char16_t* src, int32_t srcLen, UErrorCode* status){
2371cb0ef41Sopenharmony_ci    int32_t srcIndex=0;
2381cb0ef41Sopenharmony_ci    char* dest=nullptr;
2391cb0ef41Sopenharmony_ci    char* temp=nullptr;
2401cb0ef41Sopenharmony_ci    int32_t destLen=0;
2411cb0ef41Sopenharmony_ci    UChar32 c = 0;
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci    if(status==nullptr || U_FAILURE(*status) || pDest==nullptr  || srcLen==0 || src == nullptr){
2441cb0ef41Sopenharmony_ci        return nullptr;
2451cb0ef41Sopenharmony_ci    }
2461cb0ef41Sopenharmony_ci    dest =*pDest;
2471cb0ef41Sopenharmony_ci    if(dest==nullptr || destCap <=0){
2481cb0ef41Sopenharmony_ci        destCap = srcLen * 8;
2491cb0ef41Sopenharmony_ci        dest = (char*) uprv_malloc(sizeof(char) * destCap);
2501cb0ef41Sopenharmony_ci        if(dest==nullptr){
2511cb0ef41Sopenharmony_ci            *status=U_MEMORY_ALLOCATION_ERROR;
2521cb0ef41Sopenharmony_ci            return nullptr;
2531cb0ef41Sopenharmony_ci        }
2541cb0ef41Sopenharmony_ci    }
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci    dest[0]=0;
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci    while(srcIndex<srcLen){
2591cb0ef41Sopenharmony_ci        U16_NEXT(src, srcIndex, srcLen, c);
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci        if (U16_IS_LEAD(c) || U16_IS_TRAIL(c)) {
2621cb0ef41Sopenharmony_ci            *status = U_ILLEGAL_CHAR_FOUND;
2631cb0ef41Sopenharmony_ci            fprintf(stderr, "Illegal Surrogate! \n");
2641cb0ef41Sopenharmony_ci            uprv_free(dest);
2651cb0ef41Sopenharmony_ci            return nullptr;
2661cb0ef41Sopenharmony_ci        }
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ci        if((destLen+U8_LENGTH(c)) < destCap){
2691cb0ef41Sopenharmony_ci
2701cb0ef41Sopenharmony_ci            /* ASCII Range */
2711cb0ef41Sopenharmony_ci            if(c <=0x007F){
2721cb0ef41Sopenharmony_ci                switch(c) {
2731cb0ef41Sopenharmony_ci                case '\x26':
2741cb0ef41Sopenharmony_ci                    uprv_strcpy(dest+( destLen),"\x26\x61\x6d\x70\x3b"); /* &amp;*/
2751cb0ef41Sopenharmony_ci                    destLen+=(int32_t)uprv_strlen("\x26\x61\x6d\x70\x3b");
2761cb0ef41Sopenharmony_ci                    break;
2771cb0ef41Sopenharmony_ci                case '\x3c':
2781cb0ef41Sopenharmony_ci                    uprv_strcpy(dest+(destLen),"\x26\x6c\x74\x3b"); /* &lt;*/
2791cb0ef41Sopenharmony_ci                    destLen+=(int32_t)uprv_strlen("\x26\x6c\x74\x3b");
2801cb0ef41Sopenharmony_ci                    break;
2811cb0ef41Sopenharmony_ci                case '\x3e':
2821cb0ef41Sopenharmony_ci                    uprv_strcpy(dest+(destLen),"\x26\x67\x74\x3b"); /* &gt;*/
2831cb0ef41Sopenharmony_ci                    destLen+=(int32_t)uprv_strlen("\x26\x67\x74\x3b");
2841cb0ef41Sopenharmony_ci                    break;
2851cb0ef41Sopenharmony_ci                case '\x22':
2861cb0ef41Sopenharmony_ci                    uprv_strcpy(dest+(destLen),"\x26\x71\x75\x6f\x74\x3b"); /* &quot;*/
2871cb0ef41Sopenharmony_ci                    destLen+=(int32_t)uprv_strlen("\x26\x71\x75\x6f\x74\x3b");
2881cb0ef41Sopenharmony_ci                    break;
2891cb0ef41Sopenharmony_ci                case '\x27':
2901cb0ef41Sopenharmony_ci                    uprv_strcpy(dest+(destLen),"\x26\x61\x70\x6f\x73\x3b"); /* &apos; */
2911cb0ef41Sopenharmony_ci                    destLen+=(int32_t)uprv_strlen("\x26\x61\x70\x6f\x73\x3b");
2921cb0ef41Sopenharmony_ci                    break;
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci                 /* Disallow C0 controls except TAB, CR, LF*/
2951cb0ef41Sopenharmony_ci                case 0x00:
2961cb0ef41Sopenharmony_ci                case 0x01:
2971cb0ef41Sopenharmony_ci                case 0x02:
2981cb0ef41Sopenharmony_ci                case 0x03:
2991cb0ef41Sopenharmony_ci                case 0x04:
3001cb0ef41Sopenharmony_ci                case 0x05:
3011cb0ef41Sopenharmony_ci                case 0x06:
3021cb0ef41Sopenharmony_ci                case 0x07:
3031cb0ef41Sopenharmony_ci                case 0x08:
3041cb0ef41Sopenharmony_ci                /*case 0x09:*/
3051cb0ef41Sopenharmony_ci                /*case 0x0A: */
3061cb0ef41Sopenharmony_ci                case 0x0B:
3071cb0ef41Sopenharmony_ci                case 0x0C:
3081cb0ef41Sopenharmony_ci                /*case 0x0D:*/
3091cb0ef41Sopenharmony_ci                case 0x0E:
3101cb0ef41Sopenharmony_ci                case 0x0F:
3111cb0ef41Sopenharmony_ci                case 0x10:
3121cb0ef41Sopenharmony_ci                case 0x11:
3131cb0ef41Sopenharmony_ci                case 0x12:
3141cb0ef41Sopenharmony_ci                case 0x13:
3151cb0ef41Sopenharmony_ci                case 0x14:
3161cb0ef41Sopenharmony_ci                case 0x15:
3171cb0ef41Sopenharmony_ci                case 0x16:
3181cb0ef41Sopenharmony_ci                case 0x17:
3191cb0ef41Sopenharmony_ci                case 0x18:
3201cb0ef41Sopenharmony_ci                case 0x19:
3211cb0ef41Sopenharmony_ci                case 0x1A:
3221cb0ef41Sopenharmony_ci                case 0x1B:
3231cb0ef41Sopenharmony_ci                case 0x1C:
3241cb0ef41Sopenharmony_ci                case 0x1D:
3251cb0ef41Sopenharmony_ci                case 0x1E:
3261cb0ef41Sopenharmony_ci                case 0x1F:
3271cb0ef41Sopenharmony_ci                    *status = U_ILLEGAL_CHAR_FOUND;
3281cb0ef41Sopenharmony_ci                    fprintf(stderr, "Illegal Character \\u%04X!\n",(int)c);
3291cb0ef41Sopenharmony_ci                    uprv_free(dest);
3301cb0ef41Sopenharmony_ci                    return nullptr;
3311cb0ef41Sopenharmony_ci                default:
3321cb0ef41Sopenharmony_ci                    dest[destLen++]=(char)c;
3331cb0ef41Sopenharmony_ci                }
3341cb0ef41Sopenharmony_ci            }else{
3351cb0ef41Sopenharmony_ci                UBool isError = false;
3361cb0ef41Sopenharmony_ci                U8_APPEND((unsigned char*)dest,destLen,destCap,c,isError);
3371cb0ef41Sopenharmony_ci                if(isError){
3381cb0ef41Sopenharmony_ci                    *status = U_ILLEGAL_CHAR_FOUND;
3391cb0ef41Sopenharmony_ci                    fprintf(stderr, "Illegal Character \\U%08X!\n",(int)c);
3401cb0ef41Sopenharmony_ci                    uprv_free(dest);
3411cb0ef41Sopenharmony_ci                    return nullptr;
3421cb0ef41Sopenharmony_ci                }
3431cb0ef41Sopenharmony_ci            }
3441cb0ef41Sopenharmony_ci        }else{
3451cb0ef41Sopenharmony_ci            destCap += destLen;
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_ci            temp = (char*) uprv_malloc(sizeof(char)*destCap);
3481cb0ef41Sopenharmony_ci            if(temp==nullptr){
3491cb0ef41Sopenharmony_ci                *status=U_MEMORY_ALLOCATION_ERROR;
3501cb0ef41Sopenharmony_ci                uprv_free(dest);
3511cb0ef41Sopenharmony_ci                return nullptr;
3521cb0ef41Sopenharmony_ci            }
3531cb0ef41Sopenharmony_ci            uprv_memmove(temp,dest,destLen);
3541cb0ef41Sopenharmony_ci            destLen=0;
3551cb0ef41Sopenharmony_ci            uprv_free(dest);
3561cb0ef41Sopenharmony_ci            dest=temp;
3571cb0ef41Sopenharmony_ci            temp=nullptr;
3581cb0ef41Sopenharmony_ci        }
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ci    }
3611cb0ef41Sopenharmony_ci    *destLength = destLen;
3621cb0ef41Sopenharmony_ci    return dest;
3631cb0ef41Sopenharmony_ci}
3641cb0ef41Sopenharmony_ci
3651cb0ef41Sopenharmony_ci#define ASTERISK 0x002A
3661cb0ef41Sopenharmony_ci#define SPACE    0x0020
3671cb0ef41Sopenharmony_ci#define CR       0x000A
3681cb0ef41Sopenharmony_ci#define LF       0x000D
3691cb0ef41Sopenharmony_ci#define AT_SIGN  0x0040
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci#if UCONFIG_NO_REGULAR_EXPRESSIONS==0
3721cb0ef41Sopenharmony_cistatic void
3731cb0ef41Sopenharmony_citrim(char **src, int32_t *len){
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci    char *s = nullptr;
3761cb0ef41Sopenharmony_ci    int32_t i = 0;
3771cb0ef41Sopenharmony_ci    if(src == nullptr || *src == nullptr){
3781cb0ef41Sopenharmony_ci        return;
3791cb0ef41Sopenharmony_ci    }
3801cb0ef41Sopenharmony_ci    s = *src;
3811cb0ef41Sopenharmony_ci    /* trim from the end */
3821cb0ef41Sopenharmony_ci    for( i=(*len-1); i>= 0; i--){
3831cb0ef41Sopenharmony_ci        switch(s[i]){
3841cb0ef41Sopenharmony_ci        case ASTERISK:
3851cb0ef41Sopenharmony_ci        case SPACE:
3861cb0ef41Sopenharmony_ci        case CR:
3871cb0ef41Sopenharmony_ci        case LF:
3881cb0ef41Sopenharmony_ci            s[i] = 0;
3891cb0ef41Sopenharmony_ci            continue;
3901cb0ef41Sopenharmony_ci        default:
3911cb0ef41Sopenharmony_ci            break;
3921cb0ef41Sopenharmony_ci        }
3931cb0ef41Sopenharmony_ci        break;
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci    }
3961cb0ef41Sopenharmony_ci    *len = i+1;
3971cb0ef41Sopenharmony_ci}
3981cb0ef41Sopenharmony_ci
3991cb0ef41Sopenharmony_cistatic void
4001cb0ef41Sopenharmony_ciprint(char16_t* src, int32_t srcLen,const char *tagStart,const char *tagEnd,  UErrorCode *status){
4011cb0ef41Sopenharmony_ci    int32_t bufCapacity   = srcLen*4;
4021cb0ef41Sopenharmony_ci    char *buf       = nullptr;
4031cb0ef41Sopenharmony_ci    int32_t bufLen = 0;
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_ci    if(U_FAILURE(*status)){
4061cb0ef41Sopenharmony_ci        return;
4071cb0ef41Sopenharmony_ci    }
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ci    buf = (char*) (uprv_malloc(bufCapacity));
4101cb0ef41Sopenharmony_ci    if(buf==0){
4111cb0ef41Sopenharmony_ci        fprintf(stderr, "Could not allocate memory!!");
4121cb0ef41Sopenharmony_ci        exit(U_MEMORY_ALLOCATION_ERROR);
4131cb0ef41Sopenharmony_ci    }
4141cb0ef41Sopenharmony_ci    buf = convertAndEscape(&buf, bufCapacity, &bufLen, src, srcLen,status);
4151cb0ef41Sopenharmony_ci    if(U_SUCCESS(*status)){
4161cb0ef41Sopenharmony_ci        trim(&buf,&bufLen);
4171cb0ef41Sopenharmony_ci        write_utf8_file(out,UnicodeString(tagStart));
4181cb0ef41Sopenharmony_ci        write_utf8_file(out,UnicodeString(buf, bufLen, "UTF-8"));
4191cb0ef41Sopenharmony_ci        write_utf8_file(out,UnicodeString(tagEnd));
4201cb0ef41Sopenharmony_ci        write_utf8_file(out,UnicodeString("\n"));
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci    }
4231cb0ef41Sopenharmony_ci}
4241cb0ef41Sopenharmony_ci#endif
4251cb0ef41Sopenharmony_ci
4261cb0ef41Sopenharmony_cistatic void
4271cb0ef41Sopenharmony_ciprintNoteElements(const UString *src, UErrorCode *status){
4281cb0ef41Sopenharmony_ci
4291cb0ef41Sopenharmony_ci#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_ci    int32_t capacity = 0;
4321cb0ef41Sopenharmony_ci    char16_t* note = nullptr;
4331cb0ef41Sopenharmony_ci    int32_t noteLen = 0;
4341cb0ef41Sopenharmony_ci    int32_t count = 0,i;
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci    if(src == nullptr){
4371cb0ef41Sopenharmony_ci        return;
4381cb0ef41Sopenharmony_ci    }
4391cb0ef41Sopenharmony_ci
4401cb0ef41Sopenharmony_ci    capacity = src->fLength;
4411cb0ef41Sopenharmony_ci    note  = (char16_t*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
4421cb0ef41Sopenharmony_ci
4431cb0ef41Sopenharmony_ci    count = getCount(src->fChars,src->fLength, UPC_NOTE, status);
4441cb0ef41Sopenharmony_ci    if(U_FAILURE(*status)){
4451cb0ef41Sopenharmony_ci        uprv_free(note);
4461cb0ef41Sopenharmony_ci        return;
4471cb0ef41Sopenharmony_ci    }
4481cb0ef41Sopenharmony_ci    for(i=0; i < count; i++){
4491cb0ef41Sopenharmony_ci        noteLen =  getAt(src->fChars,src->fLength, &note, capacity, i, UPC_NOTE, status);
4501cb0ef41Sopenharmony_ci        if(U_FAILURE(*status)){
4511cb0ef41Sopenharmony_ci            uprv_free(note);
4521cb0ef41Sopenharmony_ci            return;
4531cb0ef41Sopenharmony_ci        }
4541cb0ef41Sopenharmony_ci        if(noteLen > 0){
4551cb0ef41Sopenharmony_ci            write_tabs(out);
4561cb0ef41Sopenharmony_ci            print(note, noteLen,"<note>", "</note>", status);
4571cb0ef41Sopenharmony_ci        }
4581cb0ef41Sopenharmony_ci    }
4591cb0ef41Sopenharmony_ci    uprv_free(note);
4601cb0ef41Sopenharmony_ci#else
4611cb0ef41Sopenharmony_ci
4621cb0ef41Sopenharmony_ci    fprintf(stderr, "Warning: Could not output comments to XLIFF file. ICU has been built without RegularExpression support.\n");
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_ci#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
4651cb0ef41Sopenharmony_ci
4661cb0ef41Sopenharmony_ci}
4671cb0ef41Sopenharmony_ci
4681cb0ef41Sopenharmony_cistatic void printAttribute(const char *name, const char *value, int32_t /*len*/)
4691cb0ef41Sopenharmony_ci{
4701cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(" "));
4711cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(name));
4721cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(" = \""));
4731cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(value));
4741cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString("\""));
4751cb0ef41Sopenharmony_ci}
4761cb0ef41Sopenharmony_ci
4771cb0ef41Sopenharmony_ci#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */
4781cb0ef41Sopenharmony_cistatic void printAttribute(const char *name, const UnicodeString value, int32_t /*len*/)
4791cb0ef41Sopenharmony_ci{
4801cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(" "));
4811cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(name));
4821cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(" = \""));
4831cb0ef41Sopenharmony_ci    write_utf8_file(out, value);
4841cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString("\""));
4851cb0ef41Sopenharmony_ci}
4861cb0ef41Sopenharmony_ci#endif
4871cb0ef41Sopenharmony_ci
4881cb0ef41Sopenharmony_cistatic void
4891cb0ef41Sopenharmony_ciprintComments(struct UString *src, const char *resName, UBool printTranslate, UErrorCode *status){
4901cb0ef41Sopenharmony_ci
4911cb0ef41Sopenharmony_ci#if UCONFIG_NO_REGULAR_EXPRESSIONS==0 /* donot compile when no RegularExpressions are available */
4921cb0ef41Sopenharmony_ci
4931cb0ef41Sopenharmony_ci    if(status==nullptr || U_FAILURE(*status)){
4941cb0ef41Sopenharmony_ci        return;
4951cb0ef41Sopenharmony_ci    }
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_ci    int32_t capacity = src->fLength + 1;
4981cb0ef41Sopenharmony_ci    char* buf = nullptr;
4991cb0ef41Sopenharmony_ci    int32_t bufLen = 0;
5001cb0ef41Sopenharmony_ci    char16_t* desc  = (char16_t*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
5011cb0ef41Sopenharmony_ci    char16_t* trans = (char16_t*) uprv_malloc(U_SIZEOF_UCHAR * capacity);
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_ci    int32_t descLen = 0, transLen=0;
5041cb0ef41Sopenharmony_ci    if(desc==nullptr || trans==nullptr){
5051cb0ef41Sopenharmony_ci        *status = U_MEMORY_ALLOCATION_ERROR;
5061cb0ef41Sopenharmony_ci        uprv_free(desc);
5071cb0ef41Sopenharmony_ci        uprv_free(trans);
5081cb0ef41Sopenharmony_ci        return;
5091cb0ef41Sopenharmony_ci    }
5101cb0ef41Sopenharmony_ci    // TODO: make src const, stop modifying it in-place, make printContainer() take const resource, etc.
5111cb0ef41Sopenharmony_ci    src->fLength = removeCmtText(src->fChars, src->fLength, status);
5121cb0ef41Sopenharmony_ci    descLen  = getDescription(src->fChars,src->fLength, &desc, capacity, status);
5131cb0ef41Sopenharmony_ci    transLen = getTranslate(src->fChars,src->fLength, &trans, capacity, status);
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_ci    /* first print translate attribute */
5161cb0ef41Sopenharmony_ci    if(transLen > 0){
5171cb0ef41Sopenharmony_ci        if(printTranslate){
5181cb0ef41Sopenharmony_ci            /* print translate attribute */
5191cb0ef41Sopenharmony_ci            buf = convertAndEscape(&buf, 0, &bufLen, trans, transLen, status);
5201cb0ef41Sopenharmony_ci            if(U_SUCCESS(*status)){
5211cb0ef41Sopenharmony_ci                printAttribute("translate", UnicodeString(buf, bufLen, "UTF-8"), bufLen);
5221cb0ef41Sopenharmony_ci                write_utf8_file(out,UnicodeString(">\n"));
5231cb0ef41Sopenharmony_ci            }
5241cb0ef41Sopenharmony_ci        }else if(getShowWarning()){
5251cb0ef41Sopenharmony_ci            fprintf(stderr, "Warning: Translate attribute for resource %s cannot be set. XLIFF prohibits it.\n", resName);
5261cb0ef41Sopenharmony_ci            /* no translate attribute .. just close the tag */
5271cb0ef41Sopenharmony_ci            write_utf8_file(out,UnicodeString(">\n"));
5281cb0ef41Sopenharmony_ci        }
5291cb0ef41Sopenharmony_ci    }else{
5301cb0ef41Sopenharmony_ci        /* no translate attribute .. just close the tag */
5311cb0ef41Sopenharmony_ci        write_utf8_file(out,UnicodeString(">\n"));
5321cb0ef41Sopenharmony_ci    }
5331cb0ef41Sopenharmony_ci
5341cb0ef41Sopenharmony_ci    if(descLen > 0){
5351cb0ef41Sopenharmony_ci        write_tabs(out);
5361cb0ef41Sopenharmony_ci        print(desc, descLen, "<!--", "-->", status);
5371cb0ef41Sopenharmony_ci    }
5381cb0ef41Sopenharmony_ci
5391cb0ef41Sopenharmony_ci    uprv_free(desc);
5401cb0ef41Sopenharmony_ci    uprv_free(trans);
5411cb0ef41Sopenharmony_ci#else
5421cb0ef41Sopenharmony_ci
5431cb0ef41Sopenharmony_ci    fprintf(stderr, "Warning: Could not output comments to XLIFF file. ICU has been built without RegularExpression support.\n");
5441cb0ef41Sopenharmony_ci
5451cb0ef41Sopenharmony_ci#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ci}
5481cb0ef41Sopenharmony_ci
5491cb0ef41Sopenharmony_ci/*
5501cb0ef41Sopenharmony_ci * Print out a containing element, like:
5511cb0ef41Sopenharmony_ci * <trans-unit id = "blah" resname = "blah" restype = "x-id-alias" translate = "no">
5521cb0ef41Sopenharmony_ci * <group id "calendar_gregorian" resname = "gregorian" restype = "x-icu-array">
5531cb0ef41Sopenharmony_ci */
5541cb0ef41Sopenharmony_cistatic char *printContainer(SResource *res, const char *container, const char *restype, const char *mimetype, const char *id, UErrorCode *status)
5551cb0ef41Sopenharmony_ci{
5561cb0ef41Sopenharmony_ci    const char *resname = nullptr;
5571cb0ef41Sopenharmony_ci    char *sid = nullptr;
5581cb0ef41Sopenharmony_ci
5591cb0ef41Sopenharmony_ci    write_tabs(out);
5601cb0ef41Sopenharmony_ci
5611cb0ef41Sopenharmony_ci    resname = res->getKeyString(srBundle);
5621cb0ef41Sopenharmony_ci    if (resname != nullptr && *resname != 0) {
5631cb0ef41Sopenharmony_ci        sid = getID(id, resname, sid);
5641cb0ef41Sopenharmony_ci    } else {
5651cb0ef41Sopenharmony_ci        sid = getID(id, nullptr, sid);
5661cb0ef41Sopenharmony_ci    }
5671cb0ef41Sopenharmony_ci
5681cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString("<"));
5691cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(container));
5701cb0ef41Sopenharmony_ci    printAttribute("id", sid, (int32_t) uprv_strlen(sid));
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_ci    if (resname != nullptr) {
5731cb0ef41Sopenharmony_ci        printAttribute("resname", resname, (int32_t) uprv_strlen(resname));
5741cb0ef41Sopenharmony_ci    }
5751cb0ef41Sopenharmony_ci
5761cb0ef41Sopenharmony_ci    if (mimetype != nullptr) {
5771cb0ef41Sopenharmony_ci        printAttribute("mime-type", mimetype, (int32_t) uprv_strlen(mimetype));
5781cb0ef41Sopenharmony_ci    }
5791cb0ef41Sopenharmony_ci
5801cb0ef41Sopenharmony_ci    if (restype != nullptr) {
5811cb0ef41Sopenharmony_ci        printAttribute("restype", restype, (int32_t) uprv_strlen(restype));
5821cb0ef41Sopenharmony_ci    }
5831cb0ef41Sopenharmony_ci
5841cb0ef41Sopenharmony_ci    tabCount += 1;
5851cb0ef41Sopenharmony_ci    if (res->fComment.fLength > 0) {
5861cb0ef41Sopenharmony_ci        /* printComments will print the closing ">\n" */
5871cb0ef41Sopenharmony_ci        printComments(&res->fComment, resname, true, status);
5881cb0ef41Sopenharmony_ci    } else {
5891cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(">\n"));
5901cb0ef41Sopenharmony_ci    }
5911cb0ef41Sopenharmony_ci
5921cb0ef41Sopenharmony_ci    return sid;
5931cb0ef41Sopenharmony_ci}
5941cb0ef41Sopenharmony_ci
5951cb0ef41Sopenharmony_ci/* Writing Functions */
5961cb0ef41Sopenharmony_ci
5971cb0ef41Sopenharmony_cistatic const char *trans_unit = "trans-unit";
5981cb0ef41Sopenharmony_cistatic const char *close_trans_unit = "</trans-unit>\n";
5991cb0ef41Sopenharmony_cistatic const char *source = "<source>";
6001cb0ef41Sopenharmony_cistatic const char *close_source = "</source>\n";
6011cb0ef41Sopenharmony_cistatic const char *group = "group";
6021cb0ef41Sopenharmony_cistatic const char *close_group = "</group>\n";
6031cb0ef41Sopenharmony_ci
6041cb0ef41Sopenharmony_cistatic const char *bin_unit = "bin-unit";
6051cb0ef41Sopenharmony_cistatic const char *close_bin_unit = "</bin-unit>\n";
6061cb0ef41Sopenharmony_cistatic const char *bin_source = "<bin-source>\n";
6071cb0ef41Sopenharmony_cistatic const char *close_bin_source = "</bin-source>\n";
6081cb0ef41Sopenharmony_cistatic const char *external_file = "<external-file";
6091cb0ef41Sopenharmony_ci/*static const char *close_external_file = "</external-file>\n";*/
6101cb0ef41Sopenharmony_cistatic const char *internal_file = "<internal-file";
6111cb0ef41Sopenharmony_cistatic const char *close_internal_file = "</internal-file>\n";
6121cb0ef41Sopenharmony_ci
6131cb0ef41Sopenharmony_cistatic const char *application_mimetype = "application"; /* add "/octet-stream"? */
6141cb0ef41Sopenharmony_ci
6151cb0ef41Sopenharmony_cistatic const char *alias_restype     = "x-icu-alias";
6161cb0ef41Sopenharmony_cistatic const char *array_restype     = "x-icu-array";
6171cb0ef41Sopenharmony_cistatic const char *binary_restype    = "x-icu-binary";
6181cb0ef41Sopenharmony_cistatic const char *integer_restype   = "x-icu-integer";
6191cb0ef41Sopenharmony_cistatic const char *intvector_restype = "x-icu-intvector";
6201cb0ef41Sopenharmony_cistatic const char *table_restype     = "x-icu-table";
6211cb0ef41Sopenharmony_ci
6221cb0ef41Sopenharmony_cistatic void
6231cb0ef41Sopenharmony_cistring_write_xml(StringResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
6241cb0ef41Sopenharmony_ci
6251cb0ef41Sopenharmony_ci    char *sid = nullptr;
6261cb0ef41Sopenharmony_ci    char* buf = nullptr;
6271cb0ef41Sopenharmony_ci    int32_t bufLen = 0;
6281cb0ef41Sopenharmony_ci
6291cb0ef41Sopenharmony_ci    if(status==nullptr || U_FAILURE(*status)){
6301cb0ef41Sopenharmony_ci        return;
6311cb0ef41Sopenharmony_ci    }
6321cb0ef41Sopenharmony_ci
6331cb0ef41Sopenharmony_ci    sid = printContainer(res, trans_unit, nullptr, nullptr, id, status);
6341cb0ef41Sopenharmony_ci
6351cb0ef41Sopenharmony_ci    write_tabs(out);
6361cb0ef41Sopenharmony_ci
6371cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(source));
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_ci    buf = convertAndEscape(&buf, 0, &bufLen, res->getBuffer(), res->length(), status);
6401cb0ef41Sopenharmony_ci
6411cb0ef41Sopenharmony_ci    if (U_FAILURE(*status)) {
6421cb0ef41Sopenharmony_ci        return;
6431cb0ef41Sopenharmony_ci    }
6441cb0ef41Sopenharmony_ci
6451cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(buf, bufLen, "UTF-8"));
6461cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_source));
6471cb0ef41Sopenharmony_ci
6481cb0ef41Sopenharmony_ci    printNoteElements(&res->fComment, status);
6491cb0ef41Sopenharmony_ci
6501cb0ef41Sopenharmony_ci    tabCount -= 1;
6511cb0ef41Sopenharmony_ci    write_tabs(out);
6521cb0ef41Sopenharmony_ci
6531cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_trans_unit));
6541cb0ef41Sopenharmony_ci
6551cb0ef41Sopenharmony_ci    uprv_free(buf);
6561cb0ef41Sopenharmony_ci    uprv_free(sid);
6571cb0ef41Sopenharmony_ci}
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_cistatic void
6601cb0ef41Sopenharmony_cialias_write_xml(AliasResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
6611cb0ef41Sopenharmony_ci    char *sid = nullptr;
6621cb0ef41Sopenharmony_ci    char* buf = nullptr;
6631cb0ef41Sopenharmony_ci    int32_t bufLen=0;
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ci    sid = printContainer(res, trans_unit, alias_restype, nullptr, id, status);
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_ci    write_tabs(out);
6681cb0ef41Sopenharmony_ci
6691cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(source));
6701cb0ef41Sopenharmony_ci
6711cb0ef41Sopenharmony_ci    buf = convertAndEscape(&buf, 0, &bufLen, res->getBuffer(), res->length(), status);
6721cb0ef41Sopenharmony_ci
6731cb0ef41Sopenharmony_ci    if(U_FAILURE(*status)){
6741cb0ef41Sopenharmony_ci        return;
6751cb0ef41Sopenharmony_ci    }
6761cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(buf, bufLen, "UTF-8"));
6771cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_source));
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_ci    printNoteElements(&res->fComment, status);
6801cb0ef41Sopenharmony_ci
6811cb0ef41Sopenharmony_ci    tabCount -= 1;
6821cb0ef41Sopenharmony_ci    write_tabs(out);
6831cb0ef41Sopenharmony_ci
6841cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_trans_unit));
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ci    uprv_free(buf);
6871cb0ef41Sopenharmony_ci    uprv_free(sid);
6881cb0ef41Sopenharmony_ci}
6891cb0ef41Sopenharmony_ci
6901cb0ef41Sopenharmony_cistatic void
6911cb0ef41Sopenharmony_ciarray_write_xml(ArrayResource *res, const char* id, const char* language, UErrorCode *status) {
6921cb0ef41Sopenharmony_ci    char* sid = nullptr;
6931cb0ef41Sopenharmony_ci    int index = 0;
6941cb0ef41Sopenharmony_ci
6951cb0ef41Sopenharmony_ci    struct SResource *current = nullptr;
6961cb0ef41Sopenharmony_ci
6971cb0ef41Sopenharmony_ci    sid = printContainer(res, group, array_restype, nullptr, id, status);
6981cb0ef41Sopenharmony_ci
6991cb0ef41Sopenharmony_ci    current = res->fFirst;
7001cb0ef41Sopenharmony_ci
7011cb0ef41Sopenharmony_ci    while (current != nullptr) {
7021cb0ef41Sopenharmony_ci        char c[256] = {0};
7031cb0ef41Sopenharmony_ci        char* subId = nullptr;
7041cb0ef41Sopenharmony_ci
7051cb0ef41Sopenharmony_ci        itostr(c, index, 10, 0);
7061cb0ef41Sopenharmony_ci        index += 1;
7071cb0ef41Sopenharmony_ci        subId = getID(sid, c, subId);
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ci        res_write_xml(current, subId, language, false, status);
7101cb0ef41Sopenharmony_ci        uprv_free(subId);
7111cb0ef41Sopenharmony_ci        subId = nullptr;
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_ci        if(U_FAILURE(*status)){
7141cb0ef41Sopenharmony_ci            return;
7151cb0ef41Sopenharmony_ci        }
7161cb0ef41Sopenharmony_ci
7171cb0ef41Sopenharmony_ci        current = current->fNext;
7181cb0ef41Sopenharmony_ci    }
7191cb0ef41Sopenharmony_ci
7201cb0ef41Sopenharmony_ci    tabCount -= 1;
7211cb0ef41Sopenharmony_ci    write_tabs(out);
7221cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_group));
7231cb0ef41Sopenharmony_ci
7241cb0ef41Sopenharmony_ci    uprv_free(sid);
7251cb0ef41Sopenharmony_ci}
7261cb0ef41Sopenharmony_ci
7271cb0ef41Sopenharmony_cistatic void
7281cb0ef41Sopenharmony_ciintvector_write_xml(IntVectorResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
7291cb0ef41Sopenharmony_ci    char* sid = nullptr;
7301cb0ef41Sopenharmony_ci    char* ivd = nullptr;
7311cb0ef41Sopenharmony_ci    uint32_t i=0;
7321cb0ef41Sopenharmony_ci    uint32_t len=0;
7331cb0ef41Sopenharmony_ci    char buf[256] = {'0'};
7341cb0ef41Sopenharmony_ci
7351cb0ef41Sopenharmony_ci    sid = printContainer(res, group, intvector_restype, nullptr, id, status);
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ci    for(i = 0; i < res->fCount; i += 1) {
7381cb0ef41Sopenharmony_ci        char c[256] = {0};
7391cb0ef41Sopenharmony_ci
7401cb0ef41Sopenharmony_ci        itostr(c, i, 10, 0);
7411cb0ef41Sopenharmony_ci        ivd = getID(sid, c, ivd);
7421cb0ef41Sopenharmony_ci        len = itostr(buf, res->fArray[i], 10, 0);
7431cb0ef41Sopenharmony_ci
7441cb0ef41Sopenharmony_ci        write_tabs(out);
7451cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString("<"));
7461cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(trans_unit));
7471cb0ef41Sopenharmony_ci
7481cb0ef41Sopenharmony_ci        printAttribute("id", ivd, (int32_t)uprv_strlen(ivd));
7491cb0ef41Sopenharmony_ci        printAttribute("restype", integer_restype, (int32_t) strlen(integer_restype));
7501cb0ef41Sopenharmony_ci
7511cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(">\n"));
7521cb0ef41Sopenharmony_ci
7531cb0ef41Sopenharmony_ci        tabCount += 1;
7541cb0ef41Sopenharmony_ci        write_tabs(out);
7551cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(source));
7561cb0ef41Sopenharmony_ci
7571cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(buf, len));
7581cb0ef41Sopenharmony_ci
7591cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(close_source));
7601cb0ef41Sopenharmony_ci        tabCount -= 1;
7611cb0ef41Sopenharmony_ci        write_tabs(out);
7621cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(close_trans_unit));
7631cb0ef41Sopenharmony_ci
7641cb0ef41Sopenharmony_ci        uprv_free(ivd);
7651cb0ef41Sopenharmony_ci        ivd = nullptr;
7661cb0ef41Sopenharmony_ci    }
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ci    tabCount -= 1;
7691cb0ef41Sopenharmony_ci    write_tabs(out);
7701cb0ef41Sopenharmony_ci
7711cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_group));
7721cb0ef41Sopenharmony_ci    uprv_free(sid);
7731cb0ef41Sopenharmony_ci    sid = nullptr;
7741cb0ef41Sopenharmony_ci}
7751cb0ef41Sopenharmony_ci
7761cb0ef41Sopenharmony_cistatic void
7771cb0ef41Sopenharmony_ciint_write_xml(IntResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
7781cb0ef41Sopenharmony_ci    char* sid = nullptr;
7791cb0ef41Sopenharmony_ci    char buf[256] = {0};
7801cb0ef41Sopenharmony_ci    uint32_t len = 0;
7811cb0ef41Sopenharmony_ci
7821cb0ef41Sopenharmony_ci    sid = printContainer(res, trans_unit, integer_restype, nullptr, id, status);
7831cb0ef41Sopenharmony_ci
7841cb0ef41Sopenharmony_ci    write_tabs(out);
7851cb0ef41Sopenharmony_ci
7861cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(source));
7871cb0ef41Sopenharmony_ci
7881cb0ef41Sopenharmony_ci    len = itostr(buf, res->fValue, 10, 0);
7891cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(buf, len));
7901cb0ef41Sopenharmony_ci
7911cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_source));
7921cb0ef41Sopenharmony_ci
7931cb0ef41Sopenharmony_ci    printNoteElements(&res->fComment, status);
7941cb0ef41Sopenharmony_ci
7951cb0ef41Sopenharmony_ci    tabCount -= 1;
7961cb0ef41Sopenharmony_ci    write_tabs(out);
7971cb0ef41Sopenharmony_ci
7981cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_trans_unit));
7991cb0ef41Sopenharmony_ci
8001cb0ef41Sopenharmony_ci    uprv_free(sid);
8011cb0ef41Sopenharmony_ci    sid = nullptr;
8021cb0ef41Sopenharmony_ci}
8031cb0ef41Sopenharmony_ci
8041cb0ef41Sopenharmony_cistatic void
8051cb0ef41Sopenharmony_cibin_write_xml(BinaryResource *res, const char* id, const char* /*language*/, UErrorCode *status) {
8061cb0ef41Sopenharmony_ci    const char* m_type = application_mimetype;
8071cb0ef41Sopenharmony_ci    char* sid = nullptr;
8081cb0ef41Sopenharmony_ci    uint32_t crc = 0xFFFFFFFF;
8091cb0ef41Sopenharmony_ci
8101cb0ef41Sopenharmony_ci    char fileName[1024] ={0};
8111cb0ef41Sopenharmony_ci    int32_t tLen = ( outDir == nullptr) ? 0 :(int32_t)uprv_strlen(outDir);
8121cb0ef41Sopenharmony_ci    char* fn =  (char*) uprv_malloc(sizeof(char) * (tLen+1024 +
8131cb0ef41Sopenharmony_ci                                                    (res->fFileName !=nullptr ?
8141cb0ef41Sopenharmony_ci                                                    uprv_strlen(res->fFileName) :0)));
8151cb0ef41Sopenharmony_ci    const char* ext = nullptr;
8161cb0ef41Sopenharmony_ci
8171cb0ef41Sopenharmony_ci    char* f = nullptr;
8181cb0ef41Sopenharmony_ci
8191cb0ef41Sopenharmony_ci    fn[0]=0;
8201cb0ef41Sopenharmony_ci
8211cb0ef41Sopenharmony_ci    if(res->fFileName != nullptr){
8221cb0ef41Sopenharmony_ci        uprv_strcpy(fileName, res->fFileName);
8231cb0ef41Sopenharmony_ci        f = uprv_strrchr(fileName, '\\');
8241cb0ef41Sopenharmony_ci
8251cb0ef41Sopenharmony_ci        if (f != nullptr) {
8261cb0ef41Sopenharmony_ci            f++;
8271cb0ef41Sopenharmony_ci        } else {
8281cb0ef41Sopenharmony_ci            f = fileName;
8291cb0ef41Sopenharmony_ci        }
8301cb0ef41Sopenharmony_ci
8311cb0ef41Sopenharmony_ci        ext = uprv_strrchr(fileName, '.');
8321cb0ef41Sopenharmony_ci
8331cb0ef41Sopenharmony_ci        if (ext == nullptr) {
8341cb0ef41Sopenharmony_ci            fprintf(stderr, "Error: %s is an unknown binary filename type.\n", fileName);
8351cb0ef41Sopenharmony_ci            exit(U_ILLEGAL_ARGUMENT_ERROR);
8361cb0ef41Sopenharmony_ci        }
8371cb0ef41Sopenharmony_ci
8381cb0ef41Sopenharmony_ci        if(uprv_strcmp(ext, ".jpg")==0 || uprv_strcmp(ext, ".jpeg")==0 || uprv_strcmp(ext, ".gif")==0 ){
8391cb0ef41Sopenharmony_ci            m_type = "image";
8401cb0ef41Sopenharmony_ci        } else if(uprv_strcmp(ext, ".wav")==0 || uprv_strcmp(ext, ".au")==0 ){
8411cb0ef41Sopenharmony_ci            m_type = "audio";
8421cb0ef41Sopenharmony_ci        } else if(uprv_strcmp(ext, ".avi")==0 || uprv_strcmp(ext, ".mpg")==0 || uprv_strcmp(ext, ".mpeg")==0){
8431cb0ef41Sopenharmony_ci            m_type = "video";
8441cb0ef41Sopenharmony_ci        } else if(uprv_strcmp(ext, ".txt")==0 || uprv_strcmp(ext, ".text")==0){
8451cb0ef41Sopenharmony_ci            m_type = "text";
8461cb0ef41Sopenharmony_ci        }
8471cb0ef41Sopenharmony_ci
8481cb0ef41Sopenharmony_ci        sid = printContainer(res, bin_unit, binary_restype, m_type, id, status);
8491cb0ef41Sopenharmony_ci
8501cb0ef41Sopenharmony_ci        write_tabs(out);
8511cb0ef41Sopenharmony_ci
8521cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(bin_source));
8531cb0ef41Sopenharmony_ci
8541cb0ef41Sopenharmony_ci        tabCount+= 1;
8551cb0ef41Sopenharmony_ci        write_tabs(out);
8561cb0ef41Sopenharmony_ci
8571cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(external_file));
8581cb0ef41Sopenharmony_ci        printAttribute("href", f, (int32_t)uprv_strlen(f));
8591cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString("/>\n"));
8601cb0ef41Sopenharmony_ci        tabCount -= 1;
8611cb0ef41Sopenharmony_ci        write_tabs(out);
8621cb0ef41Sopenharmony_ci
8631cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(close_bin_source));
8641cb0ef41Sopenharmony_ci
8651cb0ef41Sopenharmony_ci        printNoteElements(&res->fComment, status);
8661cb0ef41Sopenharmony_ci        tabCount -= 1;
8671cb0ef41Sopenharmony_ci        write_tabs(out);
8681cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(close_bin_unit));
8691cb0ef41Sopenharmony_ci    } else {
8701cb0ef41Sopenharmony_ci        char temp[256] = {0};
8711cb0ef41Sopenharmony_ci        uint32_t i = 0;
8721cb0ef41Sopenharmony_ci        int32_t len=0;
8731cb0ef41Sopenharmony_ci
8741cb0ef41Sopenharmony_ci        sid = printContainer(res, bin_unit, binary_restype, m_type, id, status);
8751cb0ef41Sopenharmony_ci
8761cb0ef41Sopenharmony_ci        write_tabs(out);
8771cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(bin_source));
8781cb0ef41Sopenharmony_ci
8791cb0ef41Sopenharmony_ci        tabCount += 1;
8801cb0ef41Sopenharmony_ci        write_tabs(out);
8811cb0ef41Sopenharmony_ci
8821cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(internal_file));
8831cb0ef41Sopenharmony_ci        printAttribute("form", application_mimetype, (int32_t) uprv_strlen(application_mimetype));
8841cb0ef41Sopenharmony_ci
8851cb0ef41Sopenharmony_ci        while(i <res->fLength){
8861cb0ef41Sopenharmony_ci            len = itostr(temp, res->fData[i], 16, 2);
8871cb0ef41Sopenharmony_ci            crc = computeCRC(temp, len, crc);
8881cb0ef41Sopenharmony_ci            i++;
8891cb0ef41Sopenharmony_ci        }
8901cb0ef41Sopenharmony_ci
8911cb0ef41Sopenharmony_ci        len = itostr(temp, crc, 10, 0);
8921cb0ef41Sopenharmony_ci        printAttribute("crc", temp, len);
8931cb0ef41Sopenharmony_ci
8941cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(">"));
8951cb0ef41Sopenharmony_ci
8961cb0ef41Sopenharmony_ci        i = 0;
8971cb0ef41Sopenharmony_ci        while(i <res->fLength){
8981cb0ef41Sopenharmony_ci            len = itostr(temp, res->fData[i], 16, 2);
8991cb0ef41Sopenharmony_ci            write_utf8_file(out, UnicodeString(temp));
9001cb0ef41Sopenharmony_ci            i += 1;
9011cb0ef41Sopenharmony_ci        }
9021cb0ef41Sopenharmony_ci
9031cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(close_internal_file));
9041cb0ef41Sopenharmony_ci
9051cb0ef41Sopenharmony_ci        tabCount -= 2;
9061cb0ef41Sopenharmony_ci        write_tabs(out);
9071cb0ef41Sopenharmony_ci
9081cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(close_bin_source));
9091cb0ef41Sopenharmony_ci        printNoteElements(&res->fComment, status);
9101cb0ef41Sopenharmony_ci
9111cb0ef41Sopenharmony_ci        tabCount -= 1;
9121cb0ef41Sopenharmony_ci        write_tabs(out);
9131cb0ef41Sopenharmony_ci        write_utf8_file(out, UnicodeString(close_bin_unit));
9141cb0ef41Sopenharmony_ci
9151cb0ef41Sopenharmony_ci        uprv_free(sid);
9161cb0ef41Sopenharmony_ci        sid = nullptr;
9171cb0ef41Sopenharmony_ci    }
9181cb0ef41Sopenharmony_ci
9191cb0ef41Sopenharmony_ci    uprv_free(fn);
9201cb0ef41Sopenharmony_ci}
9211cb0ef41Sopenharmony_ci
9221cb0ef41Sopenharmony_ci
9231cb0ef41Sopenharmony_ci
9241cb0ef41Sopenharmony_cistatic void
9251cb0ef41Sopenharmony_citable_write_xml(TableResource *res, const char* id, const char* language, UBool isTopLevel, UErrorCode *status) {
9261cb0ef41Sopenharmony_ci
9271cb0ef41Sopenharmony_ci    struct SResource *current = nullptr;
9281cb0ef41Sopenharmony_ci    char* sid = nullptr;
9291cb0ef41Sopenharmony_ci
9301cb0ef41Sopenharmony_ci    if (U_FAILURE(*status)) {
9311cb0ef41Sopenharmony_ci        return ;
9321cb0ef41Sopenharmony_ci    }
9331cb0ef41Sopenharmony_ci
9341cb0ef41Sopenharmony_ci    sid = printContainer(res, group, table_restype, nullptr, id, status);
9351cb0ef41Sopenharmony_ci
9361cb0ef41Sopenharmony_ci    if(isTopLevel) {
9371cb0ef41Sopenharmony_ci        sid[0] = '\0';
9381cb0ef41Sopenharmony_ci    }
9391cb0ef41Sopenharmony_ci
9401cb0ef41Sopenharmony_ci    current = res->fFirst;
9411cb0ef41Sopenharmony_ci
9421cb0ef41Sopenharmony_ci    while (current != nullptr) {
9431cb0ef41Sopenharmony_ci        res_write_xml(current, sid, language, false, status);
9441cb0ef41Sopenharmony_ci
9451cb0ef41Sopenharmony_ci        if(U_FAILURE(*status)){
9461cb0ef41Sopenharmony_ci            return;
9471cb0ef41Sopenharmony_ci        }
9481cb0ef41Sopenharmony_ci
9491cb0ef41Sopenharmony_ci        current = current->fNext;
9501cb0ef41Sopenharmony_ci    }
9511cb0ef41Sopenharmony_ci
9521cb0ef41Sopenharmony_ci    tabCount -= 1;
9531cb0ef41Sopenharmony_ci    write_tabs(out);
9541cb0ef41Sopenharmony_ci
9551cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(close_group));
9561cb0ef41Sopenharmony_ci
9571cb0ef41Sopenharmony_ci    uprv_free(sid);
9581cb0ef41Sopenharmony_ci    sid = nullptr;
9591cb0ef41Sopenharmony_ci}
9601cb0ef41Sopenharmony_ci
9611cb0ef41Sopenharmony_civoid
9621cb0ef41Sopenharmony_cires_write_xml(struct SResource *res, const char* id,  const char* language, UBool isTopLevel, UErrorCode *status) {
9631cb0ef41Sopenharmony_ci
9641cb0ef41Sopenharmony_ci    if (U_FAILURE(*status)) {
9651cb0ef41Sopenharmony_ci        return ;
9661cb0ef41Sopenharmony_ci    }
9671cb0ef41Sopenharmony_ci
9681cb0ef41Sopenharmony_ci    if (res != nullptr) {
9691cb0ef41Sopenharmony_ci        switch (res->fType) {
9701cb0ef41Sopenharmony_ci        case URES_STRING:
9711cb0ef41Sopenharmony_ci             string_write_xml    (static_cast<StringResource *>(res), id, language, status);
9721cb0ef41Sopenharmony_ci             return;
9731cb0ef41Sopenharmony_ci
9741cb0ef41Sopenharmony_ci        case URES_ALIAS:
9751cb0ef41Sopenharmony_ci             alias_write_xml     (static_cast<AliasResource *>(res), id, language, status);
9761cb0ef41Sopenharmony_ci             return;
9771cb0ef41Sopenharmony_ci
9781cb0ef41Sopenharmony_ci        case URES_INT_VECTOR:
9791cb0ef41Sopenharmony_ci             intvector_write_xml (static_cast<IntVectorResource *>(res), id, language, status);
9801cb0ef41Sopenharmony_ci             return;
9811cb0ef41Sopenharmony_ci
9821cb0ef41Sopenharmony_ci        case URES_BINARY:
9831cb0ef41Sopenharmony_ci             bin_write_xml       (static_cast<BinaryResource *>(res), id, language, status);
9841cb0ef41Sopenharmony_ci             return;
9851cb0ef41Sopenharmony_ci
9861cb0ef41Sopenharmony_ci        case URES_INT:
9871cb0ef41Sopenharmony_ci             int_write_xml       (static_cast<IntResource *>(res), id, language, status);
9881cb0ef41Sopenharmony_ci             return;
9891cb0ef41Sopenharmony_ci
9901cb0ef41Sopenharmony_ci        case URES_ARRAY:
9911cb0ef41Sopenharmony_ci             array_write_xml     (static_cast<ArrayResource *>(res), id, language, status);
9921cb0ef41Sopenharmony_ci             return;
9931cb0ef41Sopenharmony_ci
9941cb0ef41Sopenharmony_ci        case URES_TABLE:
9951cb0ef41Sopenharmony_ci             table_write_xml     (static_cast<TableResource *>(res), id, language, isTopLevel, status);
9961cb0ef41Sopenharmony_ci             return;
9971cb0ef41Sopenharmony_ci
9981cb0ef41Sopenharmony_ci        default:
9991cb0ef41Sopenharmony_ci            break;
10001cb0ef41Sopenharmony_ci        }
10011cb0ef41Sopenharmony_ci    }
10021cb0ef41Sopenharmony_ci
10031cb0ef41Sopenharmony_ci    *status = U_INTERNAL_PROGRAM_ERROR;
10041cb0ef41Sopenharmony_ci}
10051cb0ef41Sopenharmony_ci
10061cb0ef41Sopenharmony_civoid
10071cb0ef41Sopenharmony_cibundle_write_xml(struct SRBRoot *bundle, const char *outputDir,const char* outputEnc, const char* filename,
10081cb0ef41Sopenharmony_ci                  char *writtenFilename, int writtenFilenameLen,
10091cb0ef41Sopenharmony_ci                  const char* language, const char* outFileName, UErrorCode *status) {
10101cb0ef41Sopenharmony_ci
10111cb0ef41Sopenharmony_ci    char* xmlfileName = nullptr;
10121cb0ef41Sopenharmony_ci    char* outputFileName = nullptr;
10131cb0ef41Sopenharmony_ci    char* originalFileName = nullptr;
10141cb0ef41Sopenharmony_ci    const char* fileStart = "<file xml:space = \"preserve\" source-language = \"";
10151cb0ef41Sopenharmony_ci    const char* file1 = "\" datatype = \"x-icu-resource-bundle\" ";
10161cb0ef41Sopenharmony_ci    const char* file2 = "original = \"";
10171cb0ef41Sopenharmony_ci    const char* file4 = "\" date = \"";
10181cb0ef41Sopenharmony_ci    const char* fileEnd = "</file>\n";
10191cb0ef41Sopenharmony_ci    const char* headerStart = "<header>\n";
10201cb0ef41Sopenharmony_ci    const char* headerEnd = "</header>\n";
10211cb0ef41Sopenharmony_ci    const char* bodyStart = "<body>\n";
10221cb0ef41Sopenharmony_ci    const char* bodyEnd = "</body>\n";
10231cb0ef41Sopenharmony_ci
10241cb0ef41Sopenharmony_ci    const char *tool_start = "<tool";
10251cb0ef41Sopenharmony_ci    const char *tool_id = "genrb-" GENRB_VERSION "-icu-" U_ICU_VERSION;
10261cb0ef41Sopenharmony_ci    const char *tool_name = "genrb";
10271cb0ef41Sopenharmony_ci
10281cb0ef41Sopenharmony_ci    char* temp = nullptr;
10291cb0ef41Sopenharmony_ci    char* lang = nullptr;
10301cb0ef41Sopenharmony_ci    const char* pos = nullptr;
10311cb0ef41Sopenharmony_ci    int32_t first, index;
10321cb0ef41Sopenharmony_ci    time_t currTime;
10331cb0ef41Sopenharmony_ci    char timeBuf[128];
10341cb0ef41Sopenharmony_ci
10351cb0ef41Sopenharmony_ci    outDir = outputDir;
10361cb0ef41Sopenharmony_ci
10371cb0ef41Sopenharmony_ci    srBundle = bundle;
10381cb0ef41Sopenharmony_ci
10391cb0ef41Sopenharmony_ci    pos = uprv_strrchr(filename, '\\');
10401cb0ef41Sopenharmony_ci    if(pos != nullptr) {
10411cb0ef41Sopenharmony_ci        first = (int32_t)(pos - filename + 1);
10421cb0ef41Sopenharmony_ci    } else {
10431cb0ef41Sopenharmony_ci        first = 0;
10441cb0ef41Sopenharmony_ci    }
10451cb0ef41Sopenharmony_ci    index = (int32_t)(uprv_strlen(filename) - uprv_strlen(textExt) - first);
10461cb0ef41Sopenharmony_ci    originalFileName = (char *)uprv_malloc(sizeof(char)*index+1);
10471cb0ef41Sopenharmony_ci    uprv_memset(originalFileName, 0, sizeof(char)*index+1);
10481cb0ef41Sopenharmony_ci    uprv_strncpy(originalFileName, filename + first, index);
10491cb0ef41Sopenharmony_ci
10501cb0ef41Sopenharmony_ci    if(uprv_strcmp(originalFileName, srBundle->fLocale) != 0) {
10511cb0ef41Sopenharmony_ci        fprintf(stdout, "Warning: The file name is not same as the resource name!\n");
10521cb0ef41Sopenharmony_ci    }
10531cb0ef41Sopenharmony_ci
10541cb0ef41Sopenharmony_ci    temp = originalFileName;
10551cb0ef41Sopenharmony_ci    originalFileName = (char *)uprv_malloc(sizeof(char)* (uprv_strlen(temp)+uprv_strlen(textExt)) + 1);
10561cb0ef41Sopenharmony_ci    uprv_memset(originalFileName, 0, sizeof(char)* (uprv_strlen(temp)+uprv_strlen(textExt)) + 1);
10571cb0ef41Sopenharmony_ci    uprv_strcat(originalFileName, temp);
10581cb0ef41Sopenharmony_ci    uprv_strcat(originalFileName, textExt);
10591cb0ef41Sopenharmony_ci    uprv_free(temp);
10601cb0ef41Sopenharmony_ci    temp = nullptr;
10611cb0ef41Sopenharmony_ci
10621cb0ef41Sopenharmony_ci
10631cb0ef41Sopenharmony_ci    if (language == nullptr) {
10641cb0ef41Sopenharmony_ci/*        lang = parseFilename(filename, lang);
10651cb0ef41Sopenharmony_ci        if (lang == nullptr) {*/
10661cb0ef41Sopenharmony_ci            /* now check if locale name is valid or not
10671cb0ef41Sopenharmony_ci             * this is to cater for situation where
10681cb0ef41Sopenharmony_ci             * pegasusServer.txt contains
10691cb0ef41Sopenharmony_ci             *
10701cb0ef41Sopenharmony_ci             * en{
10711cb0ef41Sopenharmony_ci             *      ..
10721cb0ef41Sopenharmony_ci             * }
10731cb0ef41Sopenharmony_ci             */
10741cb0ef41Sopenharmony_ci             lang = parseFilename(srBundle->fLocale, lang);
10751cb0ef41Sopenharmony_ci             /*
10761cb0ef41Sopenharmony_ci              * Neither  the file name nor the table name inside the
10771cb0ef41Sopenharmony_ci              * txt file contain a valid country and language codes
10781cb0ef41Sopenharmony_ci              * throw an error.
10791cb0ef41Sopenharmony_ci              * pegasusServer.txt contains
10801cb0ef41Sopenharmony_ci              *
10811cb0ef41Sopenharmony_ci              * testelements{
10821cb0ef41Sopenharmony_ci              *     ....
10831cb0ef41Sopenharmony_ci              * }
10841cb0ef41Sopenharmony_ci              */
10851cb0ef41Sopenharmony_ci             if(lang==nullptr){
10861cb0ef41Sopenharmony_ci                 fprintf(stderr, "Error: The file name and table name do not contain a valid language code. Please use -l option to specify it.\n");
10871cb0ef41Sopenharmony_ci                 exit(U_ILLEGAL_ARGUMENT_ERROR);
10881cb0ef41Sopenharmony_ci             }
10891cb0ef41Sopenharmony_ci       /* }*/
10901cb0ef41Sopenharmony_ci    } else {
10911cb0ef41Sopenharmony_ci        lang = (char *)uprv_malloc(sizeof(char)*uprv_strlen(language) +1);
10921cb0ef41Sopenharmony_ci        uprv_memset(lang, 0, sizeof(char)*uprv_strlen(language) +1);
10931cb0ef41Sopenharmony_ci        uprv_strcpy(lang, language);
10941cb0ef41Sopenharmony_ci    }
10951cb0ef41Sopenharmony_ci
10961cb0ef41Sopenharmony_ci    if(outFileName) {
10971cb0ef41Sopenharmony_ci        outputFileName = (char *)uprv_malloc(sizeof(char)*uprv_strlen(outFileName) + 1);
10981cb0ef41Sopenharmony_ci        uprv_memset(outputFileName, 0, sizeof(char)*uprv_strlen(outFileName) + 1);
10991cb0ef41Sopenharmony_ci        uprv_strcpy(outputFileName,outFileName);
11001cb0ef41Sopenharmony_ci    } else {
11011cb0ef41Sopenharmony_ci        outputFileName = (char *)uprv_malloc(sizeof(char)*uprv_strlen(srBundle->fLocale) + 1);
11021cb0ef41Sopenharmony_ci        uprv_memset(outputFileName, 0, sizeof(char)*uprv_strlen(srBundle->fLocale) + 1);
11031cb0ef41Sopenharmony_ci        uprv_strcpy(outputFileName,srBundle->fLocale);
11041cb0ef41Sopenharmony_ci    }
11051cb0ef41Sopenharmony_ci
11061cb0ef41Sopenharmony_ci    if(outputDir) {
11071cb0ef41Sopenharmony_ci        xmlfileName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(outputDir) + uprv_strlen(outputFileName) + uprv_strlen(xliffExt) + 1) +1);
11081cb0ef41Sopenharmony_ci        uprv_memset(xmlfileName, 0, sizeof(char)*(uprv_strlen(outputDir)+ uprv_strlen(outputFileName) + uprv_strlen(xliffExt) + 1) +1);
11091cb0ef41Sopenharmony_ci    } else {
11101cb0ef41Sopenharmony_ci        xmlfileName = (char *)uprv_malloc(sizeof(char)*(uprv_strlen(outputFileName) + uprv_strlen(xliffExt)) +1);
11111cb0ef41Sopenharmony_ci        uprv_memset(xmlfileName, 0, sizeof(char)*(uprv_strlen(outputFileName) + uprv_strlen(xliffExt)) +1);
11121cb0ef41Sopenharmony_ci    }
11131cb0ef41Sopenharmony_ci
11141cb0ef41Sopenharmony_ci    if(outputDir){
11151cb0ef41Sopenharmony_ci        uprv_strcpy(xmlfileName, outputDir);
11161cb0ef41Sopenharmony_ci        if(outputDir[uprv_strlen(outputDir)-1] !=U_FILE_SEP_CHAR){
11171cb0ef41Sopenharmony_ci            uprv_strcat(xmlfileName,U_FILE_SEP_STRING);
11181cb0ef41Sopenharmony_ci        }
11191cb0ef41Sopenharmony_ci    }
11201cb0ef41Sopenharmony_ci    uprv_strcat(xmlfileName,outputFileName);
11211cb0ef41Sopenharmony_ci    uprv_strcat(xmlfileName,xliffExt);
11221cb0ef41Sopenharmony_ci
11231cb0ef41Sopenharmony_ci    if (writtenFilename) {
11241cb0ef41Sopenharmony_ci        uprv_strncpy(writtenFilename, xmlfileName, writtenFilenameLen);
11251cb0ef41Sopenharmony_ci    }
11261cb0ef41Sopenharmony_ci
11271cb0ef41Sopenharmony_ci    if (U_FAILURE(*status)) {
11281cb0ef41Sopenharmony_ci        goto cleanup_bundle_write_xml;
11291cb0ef41Sopenharmony_ci    }
11301cb0ef41Sopenharmony_ci
11311cb0ef41Sopenharmony_ci    out= T_FileStream_open(xmlfileName,"w");
11321cb0ef41Sopenharmony_ci
11331cb0ef41Sopenharmony_ci    if(out==nullptr){
11341cb0ef41Sopenharmony_ci        *status = U_FILE_ACCESS_ERROR;
11351cb0ef41Sopenharmony_ci        goto cleanup_bundle_write_xml;
11361cb0ef41Sopenharmony_ci    }
11371cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(xmlHeader));
11381cb0ef41Sopenharmony_ci
11391cb0ef41Sopenharmony_ci    if(outputEnc && *outputEnc!='\0'){
11401cb0ef41Sopenharmony_ci        /* store the output encoding */
11411cb0ef41Sopenharmony_ci        enc = outputEnc;
11421cb0ef41Sopenharmony_ci        conv=ucnv_open(enc,status);
11431cb0ef41Sopenharmony_ci        if(U_FAILURE(*status)){
11441cb0ef41Sopenharmony_ci            goto cleanup_bundle_write_xml;
11451cb0ef41Sopenharmony_ci        }
11461cb0ef41Sopenharmony_ci    }
11471cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(bundleStart));
11481cb0ef41Sopenharmony_ci    write_tabs(out);
11491cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(fileStart));
11501cb0ef41Sopenharmony_ci    /* check if lang and language are the same */
11511cb0ef41Sopenharmony_ci    if(language != nullptr && uprv_strcmp(lang, srBundle->fLocale)!=0){
11521cb0ef41Sopenharmony_ci        fprintf(stderr,"Warning: The top level tag in the resource and language specified are not the same. Please check the input.\n");
11531cb0ef41Sopenharmony_ci    }
11541cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(lang));
11551cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(file1));
11561cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(file2));
11571cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(originalFileName));
11581cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(file4));
11591cb0ef41Sopenharmony_ci
11601cb0ef41Sopenharmony_ci    time(&currTime);
11611cb0ef41Sopenharmony_ci    strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&currTime));
11621cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(timeBuf));
11631cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString("\">\n"));
11641cb0ef41Sopenharmony_ci
11651cb0ef41Sopenharmony_ci    tabCount += 1;
11661cb0ef41Sopenharmony_ci    write_tabs(out);
11671cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(headerStart));
11681cb0ef41Sopenharmony_ci
11691cb0ef41Sopenharmony_ci    tabCount += 1;
11701cb0ef41Sopenharmony_ci    write_tabs(out);
11711cb0ef41Sopenharmony_ci
11721cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(tool_start));
11731cb0ef41Sopenharmony_ci    printAttribute("tool-id", tool_id, (int32_t) uprv_strlen(tool_id));
11741cb0ef41Sopenharmony_ci    printAttribute("tool-name", tool_name, (int32_t) uprv_strlen(tool_name));
11751cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString("/>\n"));
11761cb0ef41Sopenharmony_ci
11771cb0ef41Sopenharmony_ci    tabCount -= 1;
11781cb0ef41Sopenharmony_ci    write_tabs(out);
11791cb0ef41Sopenharmony_ci
11801cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(headerEnd));
11811cb0ef41Sopenharmony_ci
11821cb0ef41Sopenharmony_ci    write_tabs(out);
11831cb0ef41Sopenharmony_ci    tabCount += 1;
11841cb0ef41Sopenharmony_ci
11851cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(bodyStart));
11861cb0ef41Sopenharmony_ci
11871cb0ef41Sopenharmony_ci
11881cb0ef41Sopenharmony_ci    res_write_xml(bundle->fRoot, bundle->fLocale, lang, true, status);
11891cb0ef41Sopenharmony_ci
11901cb0ef41Sopenharmony_ci    tabCount -= 1;
11911cb0ef41Sopenharmony_ci    write_tabs(out);
11921cb0ef41Sopenharmony_ci
11931cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(bodyEnd));
11941cb0ef41Sopenharmony_ci    tabCount--;
11951cb0ef41Sopenharmony_ci    write_tabs(out);
11961cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(fileEnd));
11971cb0ef41Sopenharmony_ci    tabCount--;
11981cb0ef41Sopenharmony_ci    write_tabs(out);
11991cb0ef41Sopenharmony_ci    write_utf8_file(out, UnicodeString(bundleEnd));
12001cb0ef41Sopenharmony_ci    T_FileStream_close(out);
12011cb0ef41Sopenharmony_ci
12021cb0ef41Sopenharmony_ci    ucnv_close(conv);
12031cb0ef41Sopenharmony_ci
12041cb0ef41Sopenharmony_cicleanup_bundle_write_xml:
12051cb0ef41Sopenharmony_ci    uprv_free(originalFileName);
12061cb0ef41Sopenharmony_ci    uprv_free(lang);
12071cb0ef41Sopenharmony_ci    if(xmlfileName != nullptr) {
12081cb0ef41Sopenharmony_ci        uprv_free(xmlfileName);
12091cb0ef41Sopenharmony_ci    }
12101cb0ef41Sopenharmony_ci    if(outputFileName != nullptr){
12111cb0ef41Sopenharmony_ci        uprv_free(outputFileName);
12121cb0ef41Sopenharmony_ci    }
12131cb0ef41Sopenharmony_ci}
1214