127f654740f2a26ad62a5c155af9199af9e69b889claireho/********************************************************************
227f654740f2a26ad62a5c155af9199af9e69b889claireho * COPYRIGHT:
354dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius * Copyright (c) 2007-2012, International Business Machines Corporation and
427f654740f2a26ad62a5c155af9199af9e69b889claireho * others. All Rights Reserved.
527f654740f2a26ad62a5c155af9199af9e69b889claireho ********************************************************************/
627f654740f2a26ad62a5c155af9199af9e69b889claireho
727f654740f2a26ad62a5c155af9199af9e69b889claireho#include "udbgutil.h"
827f654740f2a26ad62a5c155af9199af9e69b889claireho#include "dbgutil.h"
927f654740f2a26ad62a5c155af9199af9e69b889claireho
1027f654740f2a26ad62a5c155af9199af9e69b889claireho#if !UCONFIG_NO_FORMATTING
1127f654740f2a26ad62a5c155af9199af9e69b889claireho
1227f654740f2a26ad62a5c155af9199af9e69b889claireho#include "unicode/unistr.h"
1327f654740f2a26ad62a5c155af9199af9e69b889claireho#include "unicode/ustring.h"
1427f654740f2a26ad62a5c155af9199af9e69b889claireho#include "util.h"
1527f654740f2a26ad62a5c155af9199af9e69b889claireho#include "ucln.h"
1627f654740f2a26ad62a5c155af9199af9e69b889claireho
1727f654740f2a26ad62a5c155af9199af9e69b889claireho#include <stdio.h>
1827f654740f2a26ad62a5c155af9199af9e69b889claireho#include <string.h>
1927f654740f2a26ad62a5c155af9199af9e69b889claireho#include <stdlib.h>
2027f654740f2a26ad62a5c155af9199af9e69b889claireho
2127f654740f2a26ad62a5c155af9199af9e69b889clairehoU_NAMESPACE_USE
2227f654740f2a26ad62a5c155af9199af9e69b889claireho
2327f654740f2a26ad62a5c155af9199af9e69b889clairehostatic UnicodeString **strs = NULL;
2427f654740f2a26ad62a5c155af9199af9e69b889claireho
2527f654740f2a26ad62a5c155af9199af9e69b889clairehostatic const UnicodeString&  _fieldString(UDebugEnumType type, int32_t field, UnicodeString& fillin) {
2627f654740f2a26ad62a5c155af9199af9e69b889claireho    const char *str = udbg_enumName(type, field);
2727f654740f2a26ad62a5c155af9199af9e69b889claireho    if(str == NULL) {
2827f654740f2a26ad62a5c155af9199af9e69b889claireho        return fillin.remove();
2927f654740f2a26ad62a5c155af9199af9e69b889claireho    } else {
3027f654740f2a26ad62a5c155af9199af9e69b889claireho        return fillin = UnicodeString(str, ""); // optimize?
3127f654740f2a26ad62a5c155af9199af9e69b889claireho    }
3227f654740f2a26ad62a5c155af9199af9e69b889claireho}
3327f654740f2a26ad62a5c155af9199af9e69b889claireho
3427f654740f2a26ad62a5c155af9199af9e69b889clairehoU_CDECL_BEGIN
3527f654740f2a26ad62a5c155af9199af9e69b889clairehostatic void udbg_cleanup(void) {
3627f654740f2a26ad62a5c155af9199af9e69b889claireho    if(strs != NULL) {
3727f654740f2a26ad62a5c155af9199af9e69b889claireho        for(int t=0;t<=UDBG_ENUM_COUNT;t++) {
3827f654740f2a26ad62a5c155af9199af9e69b889claireho            delete [] strs[t];
3927f654740f2a26ad62a5c155af9199af9e69b889claireho        }
4027f654740f2a26ad62a5c155af9199af9e69b889claireho        delete[] strs;
4127f654740f2a26ad62a5c155af9199af9e69b889claireho        strs = NULL;
4227f654740f2a26ad62a5c155af9199af9e69b889claireho    }
4327f654740f2a26ad62a5c155af9199af9e69b889claireho}
4427f654740f2a26ad62a5c155af9199af9e69b889claireho
4527f654740f2a26ad62a5c155af9199af9e69b889clairehostatic UBool tu_cleanup(void)
4627f654740f2a26ad62a5c155af9199af9e69b889claireho{
4727f654740f2a26ad62a5c155af9199af9e69b889claireho    udbg_cleanup();
4827f654740f2a26ad62a5c155af9199af9e69b889claireho    return TRUE;
4927f654740f2a26ad62a5c155af9199af9e69b889claireho}
5027f654740f2a26ad62a5c155af9199af9e69b889claireho
5127f654740f2a26ad62a5c155af9199af9e69b889clairehostatic void udbg_register_cleanup(void) {
5227f654740f2a26ad62a5c155af9199af9e69b889claireho   ucln_registerCleanup(UCLN_TOOLUTIL, tu_cleanup);
5327f654740f2a26ad62a5c155af9199af9e69b889claireho}
5427f654740f2a26ad62a5c155af9199af9e69b889clairehoU_CDECL_END
5527f654740f2a26ad62a5c155af9199af9e69b889claireho
5627f654740f2a26ad62a5c155af9199af9e69b889clairehostatic void udbg_setup(void) {
5727f654740f2a26ad62a5c155af9199af9e69b889claireho    if(strs == NULL) {
5827f654740f2a26ad62a5c155af9199af9e69b889claireho        udbg_register_cleanup();
5927f654740f2a26ad62a5c155af9199af9e69b889claireho        //fprintf(stderr,"Initializing string cache..\n");
6027f654740f2a26ad62a5c155af9199af9e69b889claireho        //fflush(stderr);
6127f654740f2a26ad62a5c155af9199af9e69b889claireho        UnicodeString **newStrs = new UnicodeString*[UDBG_ENUM_COUNT+1];
6227f654740f2a26ad62a5c155af9199af9e69b889claireho        for(int t=0;t<UDBG_ENUM_COUNT;t++) {
6327f654740f2a26ad62a5c155af9199af9e69b889claireho            int32_t c = udbg_enumCount((UDebugEnumType)t);
6427f654740f2a26ad62a5c155af9199af9e69b889claireho            newStrs[t] = new UnicodeString[c+1];
6527f654740f2a26ad62a5c155af9199af9e69b889claireho            for(int f=0;f<=c;f++) {
6627f654740f2a26ad62a5c155af9199af9e69b889claireho                _fieldString((UDebugEnumType)t, f, newStrs[t][f]);
6727f654740f2a26ad62a5c155af9199af9e69b889claireho            }
6827f654740f2a26ad62a5c155af9199af9e69b889claireho        }
6927f654740f2a26ad62a5c155af9199af9e69b889claireho        newStrs[UDBG_ENUM_COUNT] = new UnicodeString[1]; // empty string
7027f654740f2a26ad62a5c155af9199af9e69b889claireho
7127f654740f2a26ad62a5c155af9199af9e69b889claireho        strs = newStrs;
7227f654740f2a26ad62a5c155af9199af9e69b889claireho    }
7327f654740f2a26ad62a5c155af9199af9e69b889claireho}
7427f654740f2a26ad62a5c155af9199af9e69b889claireho
7527f654740f2a26ad62a5c155af9199af9e69b889claireho
7627f654740f2a26ad62a5c155af9199af9e69b889claireho
7754dcd9b6a06071f647dac967e9e267abb9410720Craig CorneliusU_TOOLUTIL_API const UnicodeString& U_EXPORT2 udbg_enumString(UDebugEnumType type, int32_t field) {
7827f654740f2a26ad62a5c155af9199af9e69b889claireho    if(strs == NULL ) {
7927f654740f2a26ad62a5c155af9199af9e69b889claireho        udbg_setup();
8027f654740f2a26ad62a5c155af9199af9e69b889claireho    }
8127f654740f2a26ad62a5c155af9199af9e69b889claireho    if(type<0||type>=UDBG_ENUM_COUNT) {
8227f654740f2a26ad62a5c155af9199af9e69b889claireho        // use UDBG_ENUM_COUNT,0  to mean an empty string
8327f654740f2a26ad62a5c155af9199af9e69b889claireho        //fprintf(stderr, "** returning out of range on %d\n",type);
8427f654740f2a26ad62a5c155af9199af9e69b889claireho        //fflush(stderr);
8527f654740f2a26ad62a5c155af9199af9e69b889claireho        return strs[UDBG_ENUM_COUNT][0];
8627f654740f2a26ad62a5c155af9199af9e69b889claireho    }
8727f654740f2a26ad62a5c155af9199af9e69b889claireho    int32_t count = udbg_enumCount(type);
8827f654740f2a26ad62a5c155af9199af9e69b889claireho    //fprintf(stderr, "enumString [%d,%d]: typecount %d, fieldcount %d\n", type,field,UDBG_ENUM_COUNT,count);
8927f654740f2a26ad62a5c155af9199af9e69b889claireho    //fflush(stderr);
9027f654740f2a26ad62a5c155af9199af9e69b889claireho    if(field<0 || field > count) {
9127f654740f2a26ad62a5c155af9199af9e69b889claireho        return strs[type][count];
9227f654740f2a26ad62a5c155af9199af9e69b889claireho    } else {        return strs[type][field];
9327f654740f2a26ad62a5c155af9199af9e69b889claireho    }
9427f654740f2a26ad62a5c155af9199af9e69b889claireho}
9527f654740f2a26ad62a5c155af9199af9e69b889claireho
9627f654740f2a26ad62a5c155af9199af9e69b889clairehoU_CAPI int32_t  U_EXPORT2 udbg_enumByString(UDebugEnumType type, const UnicodeString& string) {
9727f654740f2a26ad62a5c155af9199af9e69b889claireho    if(type<0||type>=UDBG_ENUM_COUNT) {
9827f654740f2a26ad62a5c155af9199af9e69b889claireho        return -1;
9927f654740f2a26ad62a5c155af9199af9e69b889claireho    }
10027f654740f2a26ad62a5c155af9199af9e69b889claireho    // initialize array
10127f654740f2a26ad62a5c155af9199af9e69b889claireho    udbg_enumString(type,0);
10227f654740f2a26ad62a5c155af9199af9e69b889claireho    // search
10327f654740f2a26ad62a5c155af9199af9e69b889claireho   /// printf("type=%d\n", type); fflush(stdout);
10427f654740f2a26ad62a5c155af9199af9e69b889claireho    for(int i=0;i<udbg_enumCount(type);i++) {
10527f654740f2a26ad62a5c155af9199af9e69b889claireho//    printf("i=%d/%d\n", i, udbg_enumCount(type)); fflush(stdout);
10627f654740f2a26ad62a5c155af9199af9e69b889claireho        if(string == (strs[type][i])) {
10727f654740f2a26ad62a5c155af9199af9e69b889claireho            return i;
10827f654740f2a26ad62a5c155af9199af9e69b889claireho        }
10927f654740f2a26ad62a5c155af9199af9e69b889claireho    }
11027f654740f2a26ad62a5c155af9199af9e69b889claireho    return -1;
11127f654740f2a26ad62a5c155af9199af9e69b889claireho}
11227f654740f2a26ad62a5c155af9199af9e69b889claireho
11327f654740f2a26ad62a5c155af9199af9e69b889claireho// from DataMap::utoi
11427f654740f2a26ad62a5c155af9199af9e69b889clairehoU_CAPI int32_t
11527f654740f2a26ad62a5c155af9199af9e69b889clairehoudbg_stoi(const UnicodeString &s)
11627f654740f2a26ad62a5c155af9199af9e69b889claireho{
11727f654740f2a26ad62a5c155af9199af9e69b889claireho    char ch[256];
11827f654740f2a26ad62a5c155af9199af9e69b889claireho    const UChar *u = s.getBuffer();
11927f654740f2a26ad62a5c155af9199af9e69b889claireho    int32_t len = s.length();
12027f654740f2a26ad62a5c155af9199af9e69b889claireho    u_UCharsToChars(u, ch, len);
12127f654740f2a26ad62a5c155af9199af9e69b889claireho    ch[len] = 0; /* include terminating \0 */
12227f654740f2a26ad62a5c155af9199af9e69b889claireho    return atoi(ch);
12327f654740f2a26ad62a5c155af9199af9e69b889claireho}
12427f654740f2a26ad62a5c155af9199af9e69b889claireho
12527f654740f2a26ad62a5c155af9199af9e69b889claireho
12627f654740f2a26ad62a5c155af9199af9e69b889clairehoU_CAPI double
12727f654740f2a26ad62a5c155af9199af9e69b889clairehoudbg_stod(const UnicodeString &s)
12827f654740f2a26ad62a5c155af9199af9e69b889claireho{
12927f654740f2a26ad62a5c155af9199af9e69b889claireho    char ch[256];
13027f654740f2a26ad62a5c155af9199af9e69b889claireho    const UChar *u = s.getBuffer();
13127f654740f2a26ad62a5c155af9199af9e69b889claireho    int32_t len = s.length();
13227f654740f2a26ad62a5c155af9199af9e69b889claireho    u_UCharsToChars(u, ch, len);
13327f654740f2a26ad62a5c155af9199af9e69b889claireho    ch[len] = 0; /* include terminating \0 */
13427f654740f2a26ad62a5c155af9199af9e69b889claireho    return atof(ch);
13527f654740f2a26ad62a5c155af9199af9e69b889claireho}
13627f654740f2a26ad62a5c155af9199af9e69b889claireho
13727f654740f2a26ad62a5c155af9199af9e69b889clairehoU_CAPI UnicodeString *
13827f654740f2a26ad62a5c155af9199af9e69b889clairehoudbg_escape(const UnicodeString &src, UnicodeString *dst)
13927f654740f2a26ad62a5c155af9199af9e69b889claireho{
14027f654740f2a26ad62a5c155af9199af9e69b889claireho    dst->remove();
14127f654740f2a26ad62a5c155af9199af9e69b889claireho    for (int32_t i = 0; i < src.length(); ++i) {
14227f654740f2a26ad62a5c155af9199af9e69b889claireho        UChar c = src[i];
14327f654740f2a26ad62a5c155af9199af9e69b889claireho        if(ICU_Utility::isUnprintable(c)) {
14427f654740f2a26ad62a5c155af9199af9e69b889claireho            *dst += UnicodeString("[");
14527f654740f2a26ad62a5c155af9199af9e69b889claireho            ICU_Utility::escapeUnprintable(*dst, c);
14627f654740f2a26ad62a5c155af9199af9e69b889claireho            *dst += UnicodeString("]");
14727f654740f2a26ad62a5c155af9199af9e69b889claireho        }
14827f654740f2a26ad62a5c155af9199af9e69b889claireho        else {
14927f654740f2a26ad62a5c155af9199af9e69b889claireho            *dst += c;
15027f654740f2a26ad62a5c155af9199af9e69b889claireho        }
15127f654740f2a26ad62a5c155af9199af9e69b889claireho    }
15227f654740f2a26ad62a5c155af9199af9e69b889claireho
15327f654740f2a26ad62a5c155af9199af9e69b889claireho    return dst;
15427f654740f2a26ad62a5c155af9199af9e69b889claireho}
15527f654740f2a26ad62a5c155af9199af9e69b889claireho
15627f654740f2a26ad62a5c155af9199af9e69b889claireho
15727f654740f2a26ad62a5c155af9199af9e69b889claireho
15827f654740f2a26ad62a5c155af9199af9e69b889claireho#endif
159