Home | History | Annotate | Download | only in toolutil
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 2007-2010, International Business Machines Corporation and
      4  * others. All Rights Reserved.
      5  ********************************************************************/
      6 
      7 #include "udbgutil.h"
      8 #include "dbgutil.h"
      9 
     10 #if !UCONFIG_NO_FORMATTING
     11 
     12 #include "unicode/unistr.h"
     13 #include "unicode/ustring.h"
     14 #include "util.h"
     15 #include "ucln.h"
     16 
     17 #include <stdio.h>
     18 #include <string.h>
     19 #include <stdlib.h>
     20 
     21 U_NAMESPACE_USE
     22 
     23 static UnicodeString **strs = NULL;
     24 
     25 static const UnicodeString&  _fieldString(UDebugEnumType type, int32_t field, UnicodeString& fillin) {
     26     const char *str = udbg_enumName(type, field);
     27     if(str == NULL) {
     28         return fillin.remove();
     29     } else {
     30         return fillin = UnicodeString(str, ""); // optimize?
     31     }
     32 }
     33 
     34 U_CDECL_BEGIN
     35 static void udbg_cleanup(void) {
     36     if(strs != NULL) {
     37         for(int t=0;t<=UDBG_ENUM_COUNT;t++) {
     38             delete [] strs[t];
     39         }
     40         delete[] strs;
     41         strs = NULL;
     42     }
     43 }
     44 
     45 static UBool tu_cleanup(void)
     46 {
     47     udbg_cleanup();
     48     return TRUE;
     49 }
     50 
     51 static void udbg_register_cleanup(void) {
     52    ucln_registerCleanup(UCLN_TOOLUTIL, tu_cleanup);
     53 }
     54 U_CDECL_END
     55 
     56 static void udbg_setup(void) {
     57     if(strs == NULL) {
     58         udbg_register_cleanup();
     59         //fprintf(stderr,"Initializing string cache..\n");
     60         //fflush(stderr);
     61         UnicodeString **newStrs = new UnicodeString*[UDBG_ENUM_COUNT+1];
     62         for(int t=0;t<UDBG_ENUM_COUNT;t++) {
     63             int32_t c = udbg_enumCount((UDebugEnumType)t);
     64             newStrs[t] = new UnicodeString[c+1];
     65             for(int f=0;f<=c;f++) {
     66                 _fieldString((UDebugEnumType)t, f, newStrs[t][f]);
     67             }
     68         }
     69         newStrs[UDBG_ENUM_COUNT] = new UnicodeString[1]; // empty string
     70 
     71         strs = newStrs;
     72     }
     73 }
     74 
     75 
     76 
     77 U_CAPI const UnicodeString& U_EXPORT2 udbg_enumString(UDebugEnumType type, int32_t field) {
     78     if(strs == NULL ) {
     79         udbg_setup();
     80     }
     81     if(type<0||type>=UDBG_ENUM_COUNT) {
     82         // use UDBG_ENUM_COUNT,0  to mean an empty string
     83         //fprintf(stderr, "** returning out of range on %d\n",type);
     84         //fflush(stderr);
     85         return strs[UDBG_ENUM_COUNT][0];
     86     }
     87     int32_t count = udbg_enumCount(type);
     88     //fprintf(stderr, "enumString [%d,%d]: typecount %d, fieldcount %d\n", type,field,UDBG_ENUM_COUNT,count);
     89     //fflush(stderr);
     90     if(field<0 || field > count) {
     91         return strs[type][count];
     92     } else {        return strs[type][field];
     93     }
     94 }
     95 
     96 U_CAPI int32_t  U_EXPORT2 udbg_enumByString(UDebugEnumType type, const UnicodeString& string) {
     97     if(type<0||type>=UDBG_ENUM_COUNT) {
     98         return -1;
     99     }
    100     // initialize array
    101     udbg_enumString(type,0);
    102     // search
    103    /// printf("type=%d\n", type); fflush(stdout);
    104     for(int i=0;i<udbg_enumCount(type);i++) {
    105 //    printf("i=%d/%d\n", i, udbg_enumCount(type)); fflush(stdout);
    106         if(string == (strs[type][i])) {
    107             return i;
    108         }
    109     }
    110     return -1;
    111 }
    112 
    113 // from DataMap::utoi
    114 U_CAPI int32_t
    115 udbg_stoi(const UnicodeString &s)
    116 {
    117     char ch[256];
    118     const UChar *u = s.getBuffer();
    119     int32_t len = s.length();
    120     u_UCharsToChars(u, ch, len);
    121     ch[len] = 0; /* include terminating \0 */
    122     return atoi(ch);
    123 }
    124 
    125 
    126 U_CAPI double
    127 udbg_stod(const UnicodeString &s)
    128 {
    129     char ch[256];
    130     const UChar *u = s.getBuffer();
    131     int32_t len = s.length();
    132     u_UCharsToChars(u, ch, len);
    133     ch[len] = 0; /* include terminating \0 */
    134     return atof(ch);
    135 }
    136 
    137 U_CAPI UnicodeString *
    138 udbg_escape(const UnicodeString &src, UnicodeString *dst)
    139 {
    140     dst->remove();
    141     for (int32_t i = 0; i < src.length(); ++i) {
    142         UChar c = src[i];
    143         if(ICU_Utility::isUnprintable(c)) {
    144             *dst += UnicodeString("[");
    145             ICU_Utility::escapeUnprintable(*dst, c);
    146             *dst += UnicodeString("]");
    147         }
    148         else {
    149             *dst += c;
    150         }
    151     }
    152 
    153     return dst;
    154 }
    155 
    156 
    157 
    158 #endif
    159