intl/icu/source/tools/toolutil/dbgutil.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/toolutil/dbgutil.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,158 @@
     1.4 +/********************************************************************
     1.5 + * COPYRIGHT:
     1.6 + * Copyright (c) 2007-2012, International Business Machines Corporation and
     1.7 + * others. All Rights Reserved.
     1.8 + ********************************************************************/
     1.9 +
    1.10 +#include "udbgutil.h"
    1.11 +#include "dbgutil.h"
    1.12 +
    1.13 +#if !UCONFIG_NO_FORMATTING
    1.14 +
    1.15 +#include "unicode/unistr.h"
    1.16 +#include "unicode/ustring.h"
    1.17 +#include "util.h"
    1.18 +#include "ucln.h"
    1.19 +
    1.20 +#include <stdio.h>
    1.21 +#include <string.h>
    1.22 +#include <stdlib.h>
    1.23 +
    1.24 +U_NAMESPACE_USE
    1.25 +
    1.26 +static UnicodeString **strs = NULL;
    1.27 +
    1.28 +static const UnicodeString&  _fieldString(UDebugEnumType type, int32_t field, UnicodeString& fillin) {
    1.29 +    const char *str = udbg_enumName(type, field);
    1.30 +    if(str == NULL) {
    1.31 +        return fillin.remove();
    1.32 +    } else {
    1.33 +        return fillin = UnicodeString(str, ""); // optimize?
    1.34 +    }
    1.35 +}
    1.36 +
    1.37 +U_CDECL_BEGIN
    1.38 +static void udbg_cleanup(void) {
    1.39 +    if(strs != NULL) {
    1.40 +        for(int t=0;t<=UDBG_ENUM_COUNT;t++) {
    1.41 +            delete [] strs[t];
    1.42 +        }
    1.43 +        delete[] strs;
    1.44 +        strs = NULL;
    1.45 +    }
    1.46 +}
    1.47 +
    1.48 +static UBool tu_cleanup(void)
    1.49 +{
    1.50 +    udbg_cleanup();
    1.51 +    return TRUE;
    1.52 +}
    1.53 +
    1.54 +static void udbg_register_cleanup(void) {
    1.55 +   ucln_registerCleanup(UCLN_TOOLUTIL, tu_cleanup);
    1.56 +}
    1.57 +U_CDECL_END
    1.58 +
    1.59 +static void udbg_setup(void) {
    1.60 +    if(strs == NULL) {
    1.61 +        udbg_register_cleanup();
    1.62 +        //fprintf(stderr,"Initializing string cache..\n");
    1.63 +        //fflush(stderr);
    1.64 +        UnicodeString **newStrs = new UnicodeString*[UDBG_ENUM_COUNT+1];
    1.65 +        for(int t=0;t<UDBG_ENUM_COUNT;t++) {
    1.66 +            int32_t c = udbg_enumCount((UDebugEnumType)t);
    1.67 +            newStrs[t] = new UnicodeString[c+1];
    1.68 +            for(int f=0;f<=c;f++) {
    1.69 +                _fieldString((UDebugEnumType)t, f, newStrs[t][f]);
    1.70 +            }
    1.71 +        }
    1.72 +        newStrs[UDBG_ENUM_COUNT] = new UnicodeString[1]; // empty string
    1.73 +
    1.74 +        strs = newStrs;
    1.75 +    }
    1.76 +}
    1.77 +
    1.78 +
    1.79 +
    1.80 +U_TOOLUTIL_API const UnicodeString& U_EXPORT2 udbg_enumString(UDebugEnumType type, int32_t field) {
    1.81 +    if(strs == NULL ) {
    1.82 +        udbg_setup();
    1.83 +    }
    1.84 +    if(type<0||type>=UDBG_ENUM_COUNT) {
    1.85 +        // use UDBG_ENUM_COUNT,0  to mean an empty string
    1.86 +        //fprintf(stderr, "** returning out of range on %d\n",type);
    1.87 +        //fflush(stderr);
    1.88 +        return strs[UDBG_ENUM_COUNT][0];
    1.89 +    }
    1.90 +    int32_t count = udbg_enumCount(type);
    1.91 +    //fprintf(stderr, "enumString [%d,%d]: typecount %d, fieldcount %d\n", type,field,UDBG_ENUM_COUNT,count);
    1.92 +    //fflush(stderr);
    1.93 +    if(field<0 || field > count) {
    1.94 +        return strs[type][count];
    1.95 +    } else {        return strs[type][field];
    1.96 +    }
    1.97 +}
    1.98 +
    1.99 +U_CAPI int32_t  U_EXPORT2 udbg_enumByString(UDebugEnumType type, const UnicodeString& string) {
   1.100 +    if(type<0||type>=UDBG_ENUM_COUNT) {
   1.101 +        return -1;
   1.102 +    }
   1.103 +    // initialize array
   1.104 +    udbg_enumString(type,0);
   1.105 +    // search
   1.106 +   /// printf("type=%d\n", type); fflush(stdout);
   1.107 +    for(int i=0;i<udbg_enumCount(type);i++) {
   1.108 +//    printf("i=%d/%d\n", i, udbg_enumCount(type)); fflush(stdout);
   1.109 +        if(string == (strs[type][i])) {
   1.110 +            return i;
   1.111 +        }
   1.112 +    }
   1.113 +    return -1;
   1.114 +}
   1.115 +
   1.116 +// from DataMap::utoi
   1.117 +U_CAPI int32_t 
   1.118 +udbg_stoi(const UnicodeString &s)
   1.119 +{
   1.120 +    char ch[256];
   1.121 +    const UChar *u = s.getBuffer();
   1.122 +    int32_t len = s.length();
   1.123 +    u_UCharsToChars(u, ch, len);
   1.124 +    ch[len] = 0; /* include terminating \0 */
   1.125 +    return atoi(ch);
   1.126 +}
   1.127 +
   1.128 +
   1.129 +U_CAPI double 
   1.130 +udbg_stod(const UnicodeString &s)
   1.131 +{
   1.132 +    char ch[256];
   1.133 +    const UChar *u = s.getBuffer();
   1.134 +    int32_t len = s.length();
   1.135 +    u_UCharsToChars(u, ch, len);
   1.136 +    ch[len] = 0; /* include terminating \0 */
   1.137 +    return atof(ch);
   1.138 +}
   1.139 +
   1.140 +U_CAPI UnicodeString *
   1.141 +udbg_escape(const UnicodeString &src, UnicodeString *dst)
   1.142 +{
   1.143 +    dst->remove();
   1.144 +    for (int32_t i = 0; i < src.length(); ++i) {
   1.145 +        UChar c = src[i];
   1.146 +        if(ICU_Utility::isUnprintable(c)) {
   1.147 +            *dst += UnicodeString("[");
   1.148 +            ICU_Utility::escapeUnprintable(*dst, c);
   1.149 +            *dst += UnicodeString("]");
   1.150 +        }
   1.151 +        else {
   1.152 +            *dst += c;
   1.153 +        }
   1.154 +    }
   1.155 +
   1.156 +    return dst;
   1.157 +}
   1.158 +
   1.159 +
   1.160 +
   1.161 +#endif

mercurial