]>
git.saurik.com Git - apple/icu.git/blob - icuSources/extra/uconv/uwmsg.c
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1998-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 **********************************************************************
11 * Modification History:
13 * Date Name Description
14 * 06/14/99 stephen Creation.
15 *******************************************************************************
18 #include "unicode/ucnv.h"
19 #include "unicode/ustring.h"
20 #include "unicode/umsg.h"
21 #include "unicode/uwmsg.h"
22 #include "unicode/ures.h"
23 #include "unicode/putil.h"
34 /* Print a ustring to the specified FILE* in the default codepage */
36 uprint(const UChar
*s
,
42 UConverter
*converter
;
44 const UChar
*mySource
;
45 const UChar
*mySourceEnd
;
51 /* set up the conversion parameters */
53 mySourceEnd
= mySource
+ sourceLen
;
57 /* open a default converter */
58 converter
= ucnv_open(0, status
);
60 /* if we failed, clean up and exit */
61 if(U_FAILURE(*status
)) goto finish
;
63 /* perform the conversion */
65 /* reset the error code */
66 *status
= U_ZERO_ERROR
;
68 /* perform the conversion */
69 ucnv_fromUnicode(converter
, &myTarget
, myTarget
+ arraySize
,
70 &mySource
, mySourceEnd
, NULL
,
73 /* Write the converted data to the FILE* */
74 fwrite(buf
, sizeof(char), myTarget
- buf
, f
);
76 /* update the conversion parameters*/
80 while(*status
== U_BUFFER_OVERFLOW_ERROR
);
84 /* close the converter */
85 ucnv_close(converter
);
88 static UResourceBundle
*gBundle
= NULL
;
90 U_STRING_DECL(gNoFormatting
, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38);
92 U_CFUNC UResourceBundle
*u_wmsg_setPath(const char *path
, UErrorCode
*err
)
101 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
106 UResourceBundle
*b
= NULL
;
107 b
= ures_open(path
, NULL
, err
);
115 U_STRING_INIT(gNoFormatting
, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38);
121 /* Format a message and print it's output to fp */
122 U_CFUNC
int u_wmsg(FILE *fp
, const char *tag
, ... )
126 UErrorCode err
= U_ZERO_ERROR
;
127 #if !UCONFIG_NO_FORMATTING
131 int32_t resultLength
= UPRV_LENGTHOF(result
);
136 fprintf(stderr
, "u_wmsg: No path set!!\n"); /* FIXME: codepage?? */
141 msg
= ures_getStringByKey(gBundle
, tag
, &msgLen
, &err
);
148 #if UCONFIG_NO_FORMATTING
149 resultLength
= UPRV_LENGTHOF(gNoFormatting
);
150 if((msgLen
+ resultLength
) <= UPRV_LENGTHOF(result
)) {
151 memcpy(result
, msg
, msgLen
* U_SIZEOF_UCHAR
);
152 memcpy(result
+ msgLen
, gNoFormatting
, resultLength
);
153 resultLength
+= msgLen
;
154 uprint(result
, resultLength
, fp
, &err
);
156 uprint(msg
,msgLen
, fp
, &err
);
159 (void)gNoFormatting
; // suppress -Wunused-variable
162 resultLength
= u_vformatMessage(uloc_getDefault(), msg
, msgLen
, result
, resultLength
, ap
, &err
);
169 fprintf(stderr
, "u_wmsg: failed to format %s:%s, err %s\n",
175 uprint(msg
,msgLen
, fp
, &err
);
179 uprint(result
, resultLength
, fp
, &err
);
185 fprintf(stderr
, "u_wmsg: failed to print %s: %s, err %s\n",
196 /* these will break if the # of messages change. simply add or remove 0's .. */
197 UChar
**gInfoMessages
= NULL
;
199 UChar
**gErrMessages
= NULL
;
201 static const UChar
*fetchErrorName(UErrorCode err
)
203 if (!gInfoMessages
) {
204 gInfoMessages
= (UChar
**)malloc((U_ERROR_WARNING_LIMIT
-U_ERROR_WARNING_START
)*sizeof(UChar
*));
205 memset(gInfoMessages
, 0, (U_ERROR_WARNING_LIMIT
-U_ERROR_WARNING_START
)*sizeof(UChar
*));
208 gErrMessages
= (UChar
**)malloc(U_ERROR_LIMIT
*sizeof(UChar
*));
209 memset(gErrMessages
, 0, U_ERROR_LIMIT
*sizeof(UChar
*));
212 return gErrMessages
[err
];
214 return gInfoMessages
[err
-U_ERROR_WARNING_START
];
217 U_CFUNC
const UChar
*u_wmsg_errorName(UErrorCode err
)
221 UErrorCode subErr
= U_ZERO_ERROR
;
222 const char *textMsg
= NULL
;
225 msg
= (UChar
*)fetchErrorName(err
);
238 const char *errname
= u_errorName(err
);
240 msg
= (UChar
*)ures_getStringByKey(gBundle
, errname
, &msgLen
, &subErr
);
241 if(U_FAILURE(subErr
))
248 if(msg
== NULL
) /* Couldn't find it anywhere.. */
251 textMsg
= u_errorName(err
);
253 sprintf(error
, "UNDOCUMENTED ICU ERROR %d", err
);
256 msg
= (UChar
*)malloc((strlen(textMsg
)+1)*sizeof(msg
[0]));
257 u_charsToUChars(textMsg
, msg
, (int32_t)(strlen(textMsg
)+1));
261 gErrMessages
[err
] = msg
;
263 gInfoMessages
[err
-U_ERROR_WARNING_START
] = msg
;