]>
git.saurik.com Git - apple/icu.git/blob - icuSources/extra/uconv/uwmsg.c
2 **********************************************************************
3 * Copyright (C) 1998-2016, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 **********************************************************************
9 * Modification History:
11 * Date Name Description
12 * 06/14/99 stephen Creation.
13 *******************************************************************************
16 #include "unicode/ucnv.h"
17 #include "unicode/ustring.h"
18 #include "unicode/umsg.h"
19 #include "unicode/uwmsg.h"
20 #include "unicode/ures.h"
21 #include "unicode/putil.h"
32 /* Print a ustring to the specified FILE* in the default codepage */
34 uprint(const UChar
*s
,
40 UConverter
*converter
;
42 const UChar
*mySource
;
43 const UChar
*mySourceEnd
;
49 /* set up the conversion parameters */
51 mySourceEnd
= mySource
+ sourceLen
;
55 /* open a default converter */
56 converter
= ucnv_open(0, status
);
58 /* if we failed, clean up and exit */
59 if(U_FAILURE(*status
)) goto finish
;
61 /* perform the conversion */
63 /* reset the error code */
64 *status
= U_ZERO_ERROR
;
66 /* perform the conversion */
67 ucnv_fromUnicode(converter
, &myTarget
, myTarget
+ arraySize
,
68 &mySource
, mySourceEnd
, NULL
,
71 /* Write the converted data to the FILE* */
72 fwrite(buf
, sizeof(char), myTarget
- buf
, f
);
74 /* update the conversion parameters*/
78 while(*status
== U_BUFFER_OVERFLOW_ERROR
);
82 /* close the converter */
83 ucnv_close(converter
);
86 static UResourceBundle
*gBundle
= NULL
;
88 U_STRING_DECL(gNoFormatting
, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38);
90 U_CFUNC UResourceBundle
*u_wmsg_setPath(const char *path
, UErrorCode
*err
)
99 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
104 UResourceBundle
*b
= NULL
;
105 b
= ures_open(path
, NULL
, err
);
113 U_STRING_INIT(gNoFormatting
, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38);
119 /* Format a message and print it's output to fp */
120 U_CFUNC
int u_wmsg(FILE *fp
, const char *tag
, ... )
124 UErrorCode err
= U_ZERO_ERROR
;
125 #if !UCONFIG_NO_FORMATTING
129 int32_t resultLength
= UPRV_LENGTHOF(result
);
134 fprintf(stderr
, "u_wmsg: No path set!!\n"); /* FIXME: codepage?? */
139 msg
= ures_getStringByKey(gBundle
, tag
, &msgLen
, &err
);
146 #if UCONFIG_NO_FORMATTING
147 resultLength
= UPRV_LENGTHOF(gNoFormatting
);
148 if((msgLen
+ resultLength
) <= UPRV_LENGTHOF(result
)) {
149 memcpy(result
, msg
, msgLen
* U_SIZEOF_UCHAR
);
150 memcpy(result
+ msgLen
, gNoFormatting
, resultLength
);
151 resultLength
+= msgLen
;
152 uprint(result
, resultLength
, fp
, &err
);
154 uprint(msg
,msgLen
, fp
, &err
);
159 resultLength
= u_vformatMessage(uloc_getDefault(), msg
, msgLen
, result
, resultLength
, ap
, &err
);
166 fprintf(stderr
, "u_wmsg: failed to format %s:%s, err %s\n",
172 uprint(msg
,msgLen
, fp
, &err
);
176 uprint(result
, resultLength
, fp
, &err
);
182 fprintf(stderr
, "u_wmsg: failed to print %s: %s, err %s\n",
193 /* these will break if the # of messages change. simply add or remove 0's .. */
194 UChar
**gInfoMessages
= NULL
;
196 UChar
**gErrMessages
= NULL
;
198 static const UChar
*fetchErrorName(UErrorCode err
)
200 if (!gInfoMessages
) {
201 gInfoMessages
= (UChar
**)malloc((U_ERROR_WARNING_LIMIT
-U_ERROR_WARNING_START
)*sizeof(UChar
*));
202 memset(gInfoMessages
, 0, (U_ERROR_WARNING_LIMIT
-U_ERROR_WARNING_START
)*sizeof(UChar
*));
205 gErrMessages
= (UChar
**)malloc(U_ERROR_LIMIT
*sizeof(UChar
*));
206 memset(gErrMessages
, 0, U_ERROR_LIMIT
*sizeof(UChar
*));
209 return gErrMessages
[err
];
211 return gInfoMessages
[err
-U_ERROR_WARNING_START
];
214 U_CFUNC
const UChar
*u_wmsg_errorName(UErrorCode err
)
218 UErrorCode subErr
= U_ZERO_ERROR
;
219 const char *textMsg
= NULL
;
222 msg
= (UChar
*)fetchErrorName(err
);
235 const char *errname
= u_errorName(err
);
237 msg
= (UChar
*)ures_getStringByKey(gBundle
, errname
, &msgLen
, &subErr
);
238 if(U_FAILURE(subErr
))
245 if(msg
== NULL
) /* Couldn't find it anywhere.. */
248 textMsg
= u_errorName(err
);
250 sprintf(error
, "UNDOCUMENTED ICU ERROR %d", err
);
253 msg
= (UChar
*)malloc((strlen(textMsg
)+1)*sizeof(msg
[0]));
254 u_charsToUChars(textMsg
, msg
, (int32_t)(strlen(textMsg
)+1));
258 gErrMessages
[err
] = msg
;
260 gInfoMessages
[err
-U_ERROR_WARNING_START
] = msg
;