2007-09-01 07:22:23 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2012-08-15 17:46:17 +00:00
|
|
|
* Copyright (c) 2007-2012, International Business Machines Corporation and
|
2007-09-01 07:22:23 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
|
2010-05-12 17:56:02 +00:00
|
|
|
#include "udbgutil.h"
|
|
|
|
#include "dbgutil.h"
|
2007-09-01 07:22:23 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "ucln.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2010-06-15 21:19:16 +00:00
|
|
|
U_NAMESPACE_USE
|
|
|
|
|
2007-09-01 07:22:23 +00:00
|
|
|
static UnicodeString **strs = NULL;
|
|
|
|
|
|
|
|
static const UnicodeString& _fieldString(UDebugEnumType type, int32_t field, UnicodeString& fillin) {
|
|
|
|
const char *str = udbg_enumName(type, field);
|
|
|
|
if(str == NULL) {
|
|
|
|
return fillin.remove();
|
|
|
|
} else {
|
|
|
|
return fillin = UnicodeString(str, ""); // optimize?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CDECL_BEGIN
|
|
|
|
static void udbg_cleanup(void) {
|
|
|
|
if(strs != NULL) {
|
|
|
|
for(int t=0;t<=UDBG_ENUM_COUNT;t++) {
|
|
|
|
delete [] strs[t];
|
|
|
|
}
|
|
|
|
delete[] strs;
|
|
|
|
strs = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 17:56:02 +00:00
|
|
|
static UBool tu_cleanup(void)
|
2007-09-01 07:22:23 +00:00
|
|
|
{
|
|
|
|
udbg_cleanup();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void udbg_register_cleanup(void) {
|
2010-05-12 17:56:02 +00:00
|
|
|
ucln_registerCleanup(UCLN_TOOLUTIL, tu_cleanup);
|
2007-09-01 07:22:23 +00:00
|
|
|
}
|
|
|
|
U_CDECL_END
|
|
|
|
|
|
|
|
static void udbg_setup(void) {
|
|
|
|
if(strs == NULL) {
|
|
|
|
udbg_register_cleanup();
|
|
|
|
//fprintf(stderr,"Initializing string cache..\n");
|
|
|
|
//fflush(stderr);
|
|
|
|
UnicodeString **newStrs = new UnicodeString*[UDBG_ENUM_COUNT+1];
|
|
|
|
for(int t=0;t<UDBG_ENUM_COUNT;t++) {
|
|
|
|
int32_t c = udbg_enumCount((UDebugEnumType)t);
|
|
|
|
newStrs[t] = new UnicodeString[c+1];
|
|
|
|
for(int f=0;f<=c;f++) {
|
|
|
|
_fieldString((UDebugEnumType)t, f, newStrs[t][f]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newStrs[UDBG_ENUM_COUNT] = new UnicodeString[1]; // empty string
|
|
|
|
|
|
|
|
strs = newStrs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-15 17:46:17 +00:00
|
|
|
U_TOOLUTIL_API const UnicodeString& U_EXPORT2 udbg_enumString(UDebugEnumType type, int32_t field) {
|
2007-09-01 07:22:23 +00:00
|
|
|
if(strs == NULL ) {
|
|
|
|
udbg_setup();
|
|
|
|
}
|
|
|
|
if(type<0||type>=UDBG_ENUM_COUNT) {
|
|
|
|
// use UDBG_ENUM_COUNT,0 to mean an empty string
|
|
|
|
//fprintf(stderr, "** returning out of range on %d\n",type);
|
|
|
|
//fflush(stderr);
|
|
|
|
return strs[UDBG_ENUM_COUNT][0];
|
|
|
|
}
|
|
|
|
int32_t count = udbg_enumCount(type);
|
|
|
|
//fprintf(stderr, "enumString [%d,%d]: typecount %d, fieldcount %d\n", type,field,UDBG_ENUM_COUNT,count);
|
|
|
|
//fflush(stderr);
|
|
|
|
if(field<0 || field > count) {
|
|
|
|
return strs[type][count];
|
|
|
|
} else { return strs[type][field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-12 17:56:02 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2 udbg_enumByString(UDebugEnumType type, const UnicodeString& string) {
|
2007-09-01 07:22:23 +00:00
|
|
|
if(type<0||type>=UDBG_ENUM_COUNT) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// initialize array
|
|
|
|
udbg_enumString(type,0);
|
|
|
|
// search
|
2009-04-15 01:57:02 +00:00
|
|
|
/// printf("type=%d\n", type); fflush(stdout);
|
2007-09-01 07:22:23 +00:00
|
|
|
for(int i=0;i<udbg_enumCount(type);i++) {
|
2009-04-15 01:57:02 +00:00
|
|
|
// printf("i=%d/%d\n", i, udbg_enumCount(type)); fflush(stdout);
|
2007-09-01 07:22:23 +00:00
|
|
|
if(string == (strs[type][i])) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// from DataMap::utoi
|
2010-05-12 17:56:02 +00:00
|
|
|
U_CAPI int32_t
|
2007-09-01 07:22:23 +00:00
|
|
|
udbg_stoi(const UnicodeString &s)
|
|
|
|
{
|
|
|
|
char ch[256];
|
|
|
|
const UChar *u = s.getBuffer();
|
|
|
|
int32_t len = s.length();
|
|
|
|
u_UCharsToChars(u, ch, len);
|
|
|
|
ch[len] = 0; /* include terminating \0 */
|
|
|
|
return atoi(ch);
|
|
|
|
}
|
|
|
|
|
2008-07-02 21:43:39 +00:00
|
|
|
|
2010-05-12 17:56:02 +00:00
|
|
|
U_CAPI double
|
2008-07-02 21:43:39 +00:00
|
|
|
udbg_stod(const UnicodeString &s)
|
|
|
|
{
|
|
|
|
char ch[256];
|
|
|
|
const UChar *u = s.getBuffer();
|
|
|
|
int32_t len = s.length();
|
|
|
|
u_UCharsToChars(u, ch, len);
|
|
|
|
ch[len] = 0; /* include terminating \0 */
|
|
|
|
return atof(ch);
|
|
|
|
}
|
|
|
|
|
2010-05-12 17:56:02 +00:00
|
|
|
U_CAPI UnicodeString *
|
2007-09-01 07:22:23 +00:00
|
|
|
udbg_escape(const UnicodeString &src, UnicodeString *dst)
|
|
|
|
{
|
|
|
|
dst->remove();
|
|
|
|
for (int32_t i = 0; i < src.length(); ++i) {
|
|
|
|
UChar c = src[i];
|
|
|
|
if(ICU_Utility::isUnprintable(c)) {
|
|
|
|
*dst += UnicodeString("[");
|
|
|
|
ICU_Utility::escapeUnprintable(*dst, c);
|
|
|
|
*dst += UnicodeString("]");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*dst += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|