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