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