intl/icu/source/extra/uconv/uwmsg.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (C) 1998-2012, International Business Machines Corporation
michael@0 4 * and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 *
michael@0 7 * File uwmsg.c
michael@0 8 *
michael@0 9 * Modification History:
michael@0 10 *
michael@0 11 * Date Name Description
michael@0 12 * 06/14/99 stephen Creation.
michael@0 13 *******************************************************************************
michael@0 14 */
michael@0 15
michael@0 16 #include "unicode/ucnv.h"
michael@0 17 #include "unicode/ustring.h"
michael@0 18 #include "unicode/umsg.h"
michael@0 19 #include "unicode/uwmsg.h"
michael@0 20 #include "unicode/ures.h"
michael@0 21 #include "unicode/putil.h"
michael@0 22 #include "cstring.h"
michael@0 23
michael@0 24 #include <stdlib.h>
michael@0 25 #include <stdarg.h>
michael@0 26 #include <stdio.h>
michael@0 27 #include <string.h>
michael@0 28
michael@0 29 #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
michael@0 30
michael@0 31 #define BUF_SIZE 128
michael@0 32
michael@0 33 /* Print a ustring to the specified FILE* in the default codepage */
michael@0 34 static void
michael@0 35 uprint(const UChar *s,
michael@0 36 int32_t sourceLen,
michael@0 37 FILE *f,
michael@0 38 UErrorCode *status)
michael@0 39 {
michael@0 40 /* converter */
michael@0 41 UConverter *converter;
michael@0 42 char buf [BUF_SIZE];
michael@0 43 const UChar *mySource;
michael@0 44 const UChar *mySourceEnd;
michael@0 45 char *myTarget;
michael@0 46 int32_t arraySize;
michael@0 47
michael@0 48 if(s == 0) return;
michael@0 49
michael@0 50 /* set up the conversion parameters */
michael@0 51 mySource = s;
michael@0 52 mySourceEnd = mySource + sourceLen;
michael@0 53 myTarget = buf;
michael@0 54 arraySize = BUF_SIZE;
michael@0 55
michael@0 56 /* open a default converter */
michael@0 57 converter = ucnv_open(0, status);
michael@0 58
michael@0 59 /* if we failed, clean up and exit */
michael@0 60 if(U_FAILURE(*status)) goto finish;
michael@0 61
michael@0 62 /* perform the conversion */
michael@0 63 do {
michael@0 64 /* reset the error code */
michael@0 65 *status = U_ZERO_ERROR;
michael@0 66
michael@0 67 /* perform the conversion */
michael@0 68 ucnv_fromUnicode(converter, &myTarget, myTarget + arraySize,
michael@0 69 &mySource, mySourceEnd, NULL,
michael@0 70 TRUE, status);
michael@0 71
michael@0 72 /* Write the converted data to the FILE* */
michael@0 73 fwrite(buf, sizeof(char), myTarget - buf, f);
michael@0 74
michael@0 75 /* update the conversion parameters*/
michael@0 76 myTarget = buf;
michael@0 77 arraySize = BUF_SIZE;
michael@0 78 }
michael@0 79 while(*status == U_BUFFER_OVERFLOW_ERROR);
michael@0 80
michael@0 81 finish:
michael@0 82
michael@0 83 /* close the converter */
michael@0 84 ucnv_close(converter);
michael@0 85 }
michael@0 86
michael@0 87 static UResourceBundle *gBundle = NULL;
michael@0 88
michael@0 89 U_STRING_DECL(gNoFormatting, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38);
michael@0 90
michael@0 91 U_CFUNC UResourceBundle *u_wmsg_setPath(const char *path, UErrorCode *err)
michael@0 92 {
michael@0 93 if(U_FAILURE(*err))
michael@0 94 {
michael@0 95 return 0;
michael@0 96 }
michael@0 97
michael@0 98 if(gBundle != NULL)
michael@0 99 {
michael@0 100 *err = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 101 return 0;
michael@0 102 }
michael@0 103 else
michael@0 104 {
michael@0 105 UResourceBundle *b = NULL;
michael@0 106 b = ures_open(path, NULL, err);
michael@0 107 if(U_FAILURE(*err))
michael@0 108 {
michael@0 109 return 0;
michael@0 110 }
michael@0 111
michael@0 112 gBundle = b;
michael@0 113
michael@0 114 U_STRING_INIT(gNoFormatting, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38);
michael@0 115 }
michael@0 116
michael@0 117 return gBundle;
michael@0 118 }
michael@0 119
michael@0 120 /* Format a message and print it's output to fp */
michael@0 121 U_CFUNC int u_wmsg(FILE *fp, const char *tag, ... )
michael@0 122 {
michael@0 123 const UChar *msg;
michael@0 124 int32_t msgLen;
michael@0 125 UErrorCode err = U_ZERO_ERROR;
michael@0 126 #if !UCONFIG_NO_FORMATTING
michael@0 127 va_list ap;
michael@0 128 #endif
michael@0 129 UChar result[4096];
michael@0 130 int32_t resultLength = LENGTHOF(result);
michael@0 131
michael@0 132 if(gBundle == NULL)
michael@0 133 {
michael@0 134 #if 0
michael@0 135 fprintf(stderr, "u_wmsg: No path set!!\n"); /* FIXME: codepage?? */
michael@0 136 #endif
michael@0 137 return -1;
michael@0 138 }
michael@0 139
michael@0 140 msg = ures_getStringByKey(gBundle, tag, &msgLen, &err);
michael@0 141
michael@0 142 if(U_FAILURE(err))
michael@0 143 {
michael@0 144 return -1;
michael@0 145 }
michael@0 146
michael@0 147 #if UCONFIG_NO_FORMATTING
michael@0 148 resultLength = sizeof(gNoFormatting) / U_SIZEOF_UCHAR;
michael@0 149 if((msgLen + resultLength) <= LENGTHOF(result)) {
michael@0 150 memcpy(result, msg, msgLen * U_SIZEOF_UCHAR);
michael@0 151 memcpy(result + msgLen, gNoFormatting, resultLength);
michael@0 152 resultLength += msgLen;
michael@0 153 uprint(result, resultLength, fp, &err);
michael@0 154 } else {
michael@0 155 uprint(msg,msgLen, fp, &err);
michael@0 156 }
michael@0 157 #else
michael@0 158 va_start(ap, tag);
michael@0 159
michael@0 160 resultLength = u_vformatMessage(uloc_getDefault(), msg, msgLen, result, resultLength, ap, &err);
michael@0 161
michael@0 162 va_end(ap);
michael@0 163
michael@0 164 if(U_FAILURE(err))
michael@0 165 {
michael@0 166 #if 0
michael@0 167 fprintf(stderr, "u_wmsg: failed to format %s:%s, err %s\n",
michael@0 168 uloc_getDefault(),
michael@0 169 tag,
michael@0 170 u_errorName(err));
michael@0 171 #endif
michael@0 172 err = U_ZERO_ERROR;
michael@0 173 uprint(msg,msgLen, fp, &err);
michael@0 174 return -1;
michael@0 175 }
michael@0 176
michael@0 177 uprint(result, resultLength, fp, &err);
michael@0 178 #endif
michael@0 179
michael@0 180 if(U_FAILURE(err))
michael@0 181 {
michael@0 182 #if 0
michael@0 183 fprintf(stderr, "u_wmsg: failed to print %s: %s, err %s\n",
michael@0 184 uloc_getDefault(),
michael@0 185 tag,
michael@0 186 u_errorName(err));
michael@0 187 #endif
michael@0 188 return -1;
michael@0 189 }
michael@0 190
michael@0 191 return 0;
michael@0 192 }
michael@0 193
michael@0 194 /* these will break if the # of messages change. simply add or remove 0's .. */
michael@0 195 UChar **gInfoMessages = NULL;
michael@0 196
michael@0 197 UChar **gErrMessages = NULL;
michael@0 198
michael@0 199 static const UChar *fetchErrorName(UErrorCode err)
michael@0 200 {
michael@0 201 if (!gInfoMessages) {
michael@0 202 gInfoMessages = (UChar **)malloc((U_ERROR_WARNING_LIMIT-U_ERROR_WARNING_START)*sizeof(UChar*));
michael@0 203 memset(gInfoMessages, 0, (U_ERROR_WARNING_LIMIT-U_ERROR_WARNING_START)*sizeof(UChar*));
michael@0 204 }
michael@0 205 if (!gErrMessages) {
michael@0 206 gErrMessages = (UChar **)malloc(U_ERROR_LIMIT*sizeof(UChar*));
michael@0 207 memset(gErrMessages, 0, U_ERROR_LIMIT*sizeof(UChar*));
michael@0 208 }
michael@0 209 if(err>=0)
michael@0 210 return gErrMessages[err];
michael@0 211 else
michael@0 212 return gInfoMessages[err-U_ERROR_WARNING_START];
michael@0 213 }
michael@0 214
michael@0 215 U_CFUNC const UChar *u_wmsg_errorName(UErrorCode err)
michael@0 216 {
michael@0 217 UChar *msg;
michael@0 218 int32_t msgLen;
michael@0 219 UErrorCode subErr = U_ZERO_ERROR;
michael@0 220 const char *textMsg = NULL;
michael@0 221
michael@0 222 /* try the cache */
michael@0 223 msg = (UChar*)fetchErrorName(err);
michael@0 224
michael@0 225 if(msg)
michael@0 226 {
michael@0 227 return msg;
michael@0 228 }
michael@0 229
michael@0 230 if(gBundle == NULL)
michael@0 231 {
michael@0 232 msg = NULL;
michael@0 233 }
michael@0 234 else
michael@0 235 {
michael@0 236 const char *errname = u_errorName(err);
michael@0 237 if (errname) {
michael@0 238 msg = (UChar*)ures_getStringByKey(gBundle, errname, &msgLen, &subErr);
michael@0 239 if(U_FAILURE(subErr))
michael@0 240 {
michael@0 241 msg = NULL;
michael@0 242 }
michael@0 243 }
michael@0 244 }
michael@0 245
michael@0 246 if(msg == NULL) /* Couldn't find it anywhere.. */
michael@0 247 {
michael@0 248 char error[128];
michael@0 249 textMsg = u_errorName(err);
michael@0 250 if (!textMsg) {
michael@0 251 sprintf(error, "UNDOCUMENTED ICU ERROR %d", err);
michael@0 252 textMsg = error;
michael@0 253 }
michael@0 254 msg = (UChar*)malloc((strlen(textMsg)+1)*sizeof(msg[0]));
michael@0 255 u_charsToUChars(textMsg, msg, (int32_t)(strlen(textMsg)+1));
michael@0 256 }
michael@0 257
michael@0 258 if(err>=0)
michael@0 259 gErrMessages[err] = msg;
michael@0 260 else
michael@0 261 gInfoMessages[err-U_ERROR_WARNING_START] = msg;
michael@0 262
michael@0 263 return msg;
michael@0 264 }

mercurial