intl/icu/source/tools/toolutil/dbgutil.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial