]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/dbgutil.cpp
1 /********************************************************************
3 * Copyright (c) 2007-2010, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/unistr.h"
13 #include "unicode/ustring.h"
23 static UnicodeString
**strs
= NULL
;
25 static const UnicodeString
& _fieldString(UDebugEnumType type
, int32_t field
, UnicodeString
& fillin
) {
26 const char *str
= udbg_enumName(type
, field
);
28 return fillin
.remove();
30 return fillin
= UnicodeString(str
, ""); // optimize?
35 static void udbg_cleanup(void) {
37 for(int t
=0;t
<=UDBG_ENUM_COUNT
;t
++) {
45 static UBool
tu_cleanup(void)
51 static void udbg_register_cleanup(void) {
52 ucln_registerCleanup(UCLN_TOOLUTIL
, tu_cleanup
);
56 static void udbg_setup(void) {
58 udbg_register_cleanup();
59 //fprintf(stderr,"Initializing string cache..\n");
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
]);
69 newStrs
[UDBG_ENUM_COUNT
] = new UnicodeString
[1]; // empty string
77 U_CAPI
const UnicodeString
& U_EXPORT2
udbg_enumString(UDebugEnumType type
, int32_t field
) {
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);
85 return strs
[UDBG_ENUM_COUNT
][0];
87 int32_t count
= udbg_enumCount(type
);
88 //fprintf(stderr, "enumString [%d,%d]: typecount %d, fieldcount %d\n", type,field,UDBG_ENUM_COUNT,count);
90 if(field
<0 || field
> count
) {
91 return strs
[type
][count
];
92 } else { return strs
[type
][field
];
96 U_CAPI
int32_t U_EXPORT2
udbg_enumByString(UDebugEnumType type
, const UnicodeString
& string
) {
97 if(type
<0||type
>=UDBG_ENUM_COUNT
) {
101 udbg_enumString(type
,0);
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
])) {
113 // from DataMap::utoi
115 udbg_stoi(const UnicodeString
&s
)
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 */
127 udbg_stod(const UnicodeString
&s
)
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 */
137 U_CAPI UnicodeString
*
138 udbg_escape(const UnicodeString
&src
, UnicodeString
*dst
)
141 for (int32_t i
= 0; i
< src
.length(); ++i
) {
143 if(ICU_Utility::isUnprintable(c
)) {
144 *dst
+= UnicodeString("[");
145 ICU_Utility::escapeUnprintable(*dst
, c
);
146 *dst
+= UnicodeString("]");