]> git.saurik.com Git - apple/icu.git/blob - icuSources/tools/ctestfw/dbgutil.cpp
ICU-400.37.tar.gz
[apple/icu.git] / icuSources / tools / ctestfw / dbgutil.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2007, 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 for(int i=0;i<udbg_enumCount(type);i++) {
102 if(string == (strs[type][i])) {
103 return i;
104 }
105 }
106 return -1;
107 }
108
109 // from DataMap::utoi
110 T_CTEST_API int32_t
111 udbg_stoi(const UnicodeString &s)
112 {
113 char ch[256];
114 const UChar *u = s.getBuffer();
115 int32_t len = s.length();
116 u_UCharsToChars(u, ch, len);
117 ch[len] = 0; /* include terminating \0 */
118 return atoi(ch);
119 }
120
121 T_CTEST_API UnicodeString *
122 udbg_escape(const UnicodeString &src, UnicodeString *dst)
123 {
124 dst->remove();
125 for (int32_t i = 0; i < src.length(); ++i) {
126 UChar c = src[i];
127 if(ICU_Utility::isUnprintable(c)) {
128 *dst += UnicodeString("[");
129 ICU_Utility::escapeUnprintable(*dst, c);
130 *dst += UnicodeString("]");
131 }
132 else {
133 *dst += c;
134 }
135 }
136
137 return dst;
138 }
139
140
141
142 #endif