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 * COPYRIGHT:
51cb0ef41Sopenharmony_ci * Copyright (c) 2007-2012, International Business Machines Corporation and
61cb0ef41Sopenharmony_ci * others. All Rights Reserved.
71cb0ef41Sopenharmony_ci ********************************************************************/
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "udbgutil.h"
101cb0ef41Sopenharmony_ci#include "dbgutil.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#if !UCONFIG_NO_FORMATTING
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci#include "unicode/unistr.h"
151cb0ef41Sopenharmony_ci#include "unicode/ustring.h"
161cb0ef41Sopenharmony_ci#include "util.h"
171cb0ef41Sopenharmony_ci#include "ucln.h"
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci#include <stdio.h>
201cb0ef41Sopenharmony_ci#include <string.h>
211cb0ef41Sopenharmony_ci#include <stdlib.h>
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciU_NAMESPACE_USE
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cistatic UnicodeString **strs = nullptr;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cistatic const UnicodeString&  _fieldString(UDebugEnumType type, int32_t field, UnicodeString& fillin) {
281cb0ef41Sopenharmony_ci    const char *str = udbg_enumName(type, field);
291cb0ef41Sopenharmony_ci    if(str == nullptr) {
301cb0ef41Sopenharmony_ci        return fillin.remove();
311cb0ef41Sopenharmony_ci    } else {
321cb0ef41Sopenharmony_ci        return fillin = UnicodeString(str, -1, US_INV);
331cb0ef41Sopenharmony_ci    }
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciU_CDECL_BEGIN
371cb0ef41Sopenharmony_cistatic void udbg_cleanup() {
381cb0ef41Sopenharmony_ci    if(strs != nullptr) {
391cb0ef41Sopenharmony_ci        for(int t=0;t<=UDBG_ENUM_COUNT;t++) {
401cb0ef41Sopenharmony_ci            delete [] strs[t];
411cb0ef41Sopenharmony_ci        }
421cb0ef41Sopenharmony_ci        delete[] strs;
431cb0ef41Sopenharmony_ci        strs = nullptr;
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cistatic UBool tu_cleanup()
481cb0ef41Sopenharmony_ci{
491cb0ef41Sopenharmony_ci    udbg_cleanup();
501cb0ef41Sopenharmony_ci    return true;
511cb0ef41Sopenharmony_ci}
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cistatic void udbg_register_cleanup() {
541cb0ef41Sopenharmony_ci   ucln_registerCleanup(UCLN_TOOLUTIL, tu_cleanup);
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ciU_CDECL_END
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cistatic void udbg_setup() {
591cb0ef41Sopenharmony_ci    if(strs == nullptr) {
601cb0ef41Sopenharmony_ci        udbg_register_cleanup();
611cb0ef41Sopenharmony_ci        //fprintf(stderr,"Initializing string cache..\n");
621cb0ef41Sopenharmony_ci        //fflush(stderr);
631cb0ef41Sopenharmony_ci        UnicodeString **newStrs = new UnicodeString*[UDBG_ENUM_COUNT+1];
641cb0ef41Sopenharmony_ci        for(int t=0;t<UDBG_ENUM_COUNT;t++) {
651cb0ef41Sopenharmony_ci            int32_t c = udbg_enumCount((UDebugEnumType)t);
661cb0ef41Sopenharmony_ci            newStrs[t] = new UnicodeString[c+1];
671cb0ef41Sopenharmony_ci            for(int f=0;f<=c;f++) {
681cb0ef41Sopenharmony_ci                _fieldString((UDebugEnumType)t, f, newStrs[t][f]);
691cb0ef41Sopenharmony_ci            }
701cb0ef41Sopenharmony_ci        }
711cb0ef41Sopenharmony_ci        newStrs[UDBG_ENUM_COUNT] = new UnicodeString[1]; // empty string
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci        strs = newStrs;
741cb0ef41Sopenharmony_ci    }
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciU_TOOLUTIL_API const UnicodeString& U_EXPORT2 udbg_enumString(UDebugEnumType type, int32_t field) {
801cb0ef41Sopenharmony_ci    if(strs == nullptr ) {
811cb0ef41Sopenharmony_ci        udbg_setup();
821cb0ef41Sopenharmony_ci    }
831cb0ef41Sopenharmony_ci    if(type<0||type>=UDBG_ENUM_COUNT) {
841cb0ef41Sopenharmony_ci        // use UDBG_ENUM_COUNT,0  to mean an empty string
851cb0ef41Sopenharmony_ci        //fprintf(stderr, "** returning out of range on %d\n",type);
861cb0ef41Sopenharmony_ci        //fflush(stderr);
871cb0ef41Sopenharmony_ci        return strs[UDBG_ENUM_COUNT][0];
881cb0ef41Sopenharmony_ci    }
891cb0ef41Sopenharmony_ci    int32_t count = udbg_enumCount(type);
901cb0ef41Sopenharmony_ci    //fprintf(stderr, "enumString [%d,%d]: typecount %d, fieldcount %d\n", type,field,UDBG_ENUM_COUNT,count);
911cb0ef41Sopenharmony_ci    //fflush(stderr);
921cb0ef41Sopenharmony_ci    if(field<0 || field > count) {
931cb0ef41Sopenharmony_ci        return strs[type][count];
941cb0ef41Sopenharmony_ci    } else {        return strs[type][field];
951cb0ef41Sopenharmony_ci    }
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ciU_CAPI int32_t  U_EXPORT2 udbg_enumByString(UDebugEnumType type, const UnicodeString& string) {
991cb0ef41Sopenharmony_ci    if(type<0||type>=UDBG_ENUM_COUNT) {
1001cb0ef41Sopenharmony_ci        return -1;
1011cb0ef41Sopenharmony_ci    }
1021cb0ef41Sopenharmony_ci    // initialize array
1031cb0ef41Sopenharmony_ci    udbg_enumString(type,0);
1041cb0ef41Sopenharmony_ci    // search
1051cb0ef41Sopenharmony_ci   /// printf("type=%d\n", type); fflush(stdout);
1061cb0ef41Sopenharmony_ci    for(int i=0;i<udbg_enumCount(type);i++) {
1071cb0ef41Sopenharmony_ci//    printf("i=%d/%d\n", i, udbg_enumCount(type)); fflush(stdout);
1081cb0ef41Sopenharmony_ci        if(string == (strs[type][i])) {
1091cb0ef41Sopenharmony_ci            return i;
1101cb0ef41Sopenharmony_ci        }
1111cb0ef41Sopenharmony_ci    }
1121cb0ef41Sopenharmony_ci    return -1;
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci// from DataMap::utoi
1161cb0ef41Sopenharmony_ciU_CAPI int32_t
1171cb0ef41Sopenharmony_ciudbg_stoi(const UnicodeString &s)
1181cb0ef41Sopenharmony_ci{
1191cb0ef41Sopenharmony_ci    char ch[256];
1201cb0ef41Sopenharmony_ci    const char16_t *u = toUCharPtr(s.getBuffer());
1211cb0ef41Sopenharmony_ci    int32_t len = s.length();
1221cb0ef41Sopenharmony_ci    u_UCharsToChars(u, ch, len);
1231cb0ef41Sopenharmony_ci    ch[len] = 0; /* include terminating \0 */
1241cb0ef41Sopenharmony_ci    return atoi(ch);
1251cb0ef41Sopenharmony_ci}
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciU_CAPI double
1291cb0ef41Sopenharmony_ciudbg_stod(const UnicodeString &s)
1301cb0ef41Sopenharmony_ci{
1311cb0ef41Sopenharmony_ci    char ch[256];
1321cb0ef41Sopenharmony_ci    const char16_t *u = toUCharPtr(s.getBuffer());
1331cb0ef41Sopenharmony_ci    int32_t len = s.length();
1341cb0ef41Sopenharmony_ci    u_UCharsToChars(u, ch, len);
1351cb0ef41Sopenharmony_ci    ch[len] = 0; /* include terminating \0 */
1361cb0ef41Sopenharmony_ci    return atof(ch);
1371cb0ef41Sopenharmony_ci}
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ciU_CAPI UnicodeString *
1401cb0ef41Sopenharmony_ciudbg_escape(const UnicodeString &src, UnicodeString *dst)
1411cb0ef41Sopenharmony_ci{
1421cb0ef41Sopenharmony_ci    dst->remove();
1431cb0ef41Sopenharmony_ci    for (int32_t i = 0; i < src.length(); ++i) {
1441cb0ef41Sopenharmony_ci        char16_t c = src[i];
1451cb0ef41Sopenharmony_ci        if(ICU_Utility::isUnprintable(c)) {
1461cb0ef41Sopenharmony_ci            *dst += UnicodeString("[");
1471cb0ef41Sopenharmony_ci            ICU_Utility::escapeUnprintable(*dst, c);
1481cb0ef41Sopenharmony_ci            *dst += UnicodeString("]");
1491cb0ef41Sopenharmony_ci        }
1501cb0ef41Sopenharmony_ci        else {
1511cb0ef41Sopenharmony_ci            *dst += c;
1521cb0ef41Sopenharmony_ci        }
1531cb0ef41Sopenharmony_ci    }
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci    return dst;
1561cb0ef41Sopenharmony_ci}
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci#endif
161