1 /********************************************************************
3 * Copyright (c) 2007, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/udbgutil.h"
8 #include "unicode/dbgutil.h"
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/unistr.h"
13 #include "unicode/ustring.h"
21 static UnicodeString
**strs
= NULL
;
23 static const UnicodeString
& _fieldString(UDebugEnumType type
, int32_t field
, UnicodeString
& fillin
) {
24 const char *str
= udbg_enumName(type
, field
);
26 return fillin
.remove();
28 return fillin
= UnicodeString(str
, ""); // optimize?
33 static void udbg_cleanup(void) {
35 for(int t
=0;t
<=UDBG_ENUM_COUNT
;t
++) {
43 static UBool
ctestfw_cleanup(void)
49 static void udbg_register_cleanup(void) {
50 ucln_registerCleanup(UCLN_CTESTFW
, ctestfw_cleanup
);
54 static void udbg_setup(void) {
56 udbg_register_cleanup();
57 //fprintf(stderr,"Initializing string cache..\n");
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
]);
67 newStrs
[UDBG_ENUM_COUNT
] = new UnicodeString
[1]; // empty string
75 T_CTEST_API
const UnicodeString
& T_CTEST_EXPORT2
udbg_enumString(UDebugEnumType type
, int32_t field
) {
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);
83 return strs
[UDBG_ENUM_COUNT
][0];
85 int32_t count
= udbg_enumCount(type
);
86 //fprintf(stderr, "enumString [%d,%d]: typecount %d, fieldcount %d\n", type,field,UDBG_ENUM_COUNT,count);
88 if(field
<0 || field
> count
) {
89 return strs
[type
][count
];
90 } else { return strs
[type
][field
];
94 T_CTEST_API
int32_t T_CTEST_EXPORT2
udbg_enumByString(UDebugEnumType type
, const UnicodeString
& string
) {
95 if(type
<0||type
>=UDBG_ENUM_COUNT
) {
99 udbg_enumString(type
,0);
101 for(int i
=0;i
<udbg_enumCount(type
);i
++) {
102 if(string
== (strs
[type
][i
])) {
109 // from DataMap::utoi
111 udbg_stoi(const UnicodeString
&s
)
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 */
121 T_CTEST_API UnicodeString
*
122 udbg_escape(const UnicodeString
&src
, UnicodeString
*dst
)
125 for (int32_t i
= 0; i
< src
.length(); ++i
) {
127 if(ICU_Utility::isUnprintable(c
)) {
128 *dst
+= UnicodeString("[");
129 ICU_Utility::escapeUnprintable(*dst
, c
);
130 *dst
+= UnicodeString("]");