]>
git.saurik.com Git - apple/icu.git/blob - icuSources/extra/uconv/uwmsg.c
2 **********************************************************************
3 * Copyright (C) 1998-2012, 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"
29 #define LENGTHOF(array) (sizeof(array)/sizeof((array)[0]))
33 /* Print a ustring to the specified FILE* in the default codepage */
35 uprint(const UChar
*s
,
41 UConverter
*converter
;
43 const UChar
*mySource
;
44 const UChar
*mySourceEnd
;
50 /* set up the conversion parameters */
52 mySourceEnd
= mySource
+ sourceLen
;
56 /* open a default converter */
57 converter
= ucnv_open(0, status
);
59 /* if we failed, clean up and exit */
60 if(U_FAILURE(*status
)) goto finish
;
62 /* perform the conversion */
64 /* reset the error code */
65 *status
= U_ZERO_ERROR
;
67 /* perform the conversion */
68 ucnv_fromUnicode(converter
, &myTarget
, myTarget
+ arraySize
,
69 &mySource
, mySourceEnd
, NULL
,
72 /* Write the converted data to the FILE* */
73 fwrite(buf
, sizeof(char), myTarget
- buf
, f
);
75 /* update the conversion parameters*/
79 while(*status
== U_BUFFER_OVERFLOW_ERROR
);
83 /* close the converter */
84 ucnv_close(converter
);
87 static UResourceBundle
*gBundle
= NULL
;
89 U_STRING_DECL(gNoFormatting
, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38);
91 U_CFUNC UResourceBundle
*u_wmsg_setPath(const char *path
, UErrorCode
*err
)
100 *err
= U_ILLEGAL_ARGUMENT_ERROR
;
105 UResourceBundle
*b
= NULL
;
106 b
= ures_open(path
, NULL
, err
);
114 U_STRING_INIT(gNoFormatting
, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38);
120 /* Format a message and print it's output to fp */
121 U_CFUNC
int u_wmsg(FILE *fp
, const char *tag
, ... )
125 UErrorCode err
= U_ZERO_ERROR
;
126 #if !UCONFIG_NO_FORMATTING
130 int32_t resultLength
= LENGTHOF(result
);
135 fprintf(stderr
, "u_wmsg: No path set!!\n"); /* FIXME: codepage?? */
140 msg
= ures_getStringByKey(gBundle
, tag
, &msgLen
, &err
);
147 #if UCONFIG_NO_FORMATTING
148 resultLength
= sizeof(gNoFormatting
) / U_SIZEOF_UCHAR
;
149 if((msgLen
+ resultLength
) <= LENGTHOF(result
)) {
150 memcpy(result
, msg
, msgLen
* U_SIZEOF_UCHAR
);
151 memcpy(result
+ msgLen
, gNoFormatting
, resultLength
);
152 resultLength
+= msgLen
;
153 uprint(result
, resultLength
, fp
, &err
);
155 uprint(msg
,msgLen
, fp
, &err
);
160 resultLength
= u_vformatMessage(uloc_getDefault(), msg
, msgLen
, result
, resultLength
, ap
, &err
);
167 fprintf(stderr
, "u_wmsg: failed to format %s:%s, err %s\n",
173 uprint(msg
,msgLen
, fp
, &err
);
177 uprint(result
, resultLength
, fp
, &err
);
183 fprintf(stderr
, "u_wmsg: failed to print %s: %s, err %s\n",
194 /* these will break if the # of messages change. simply add or remove 0's .. */
195 UChar
**gInfoMessages
= NULL
;
197 UChar
**gErrMessages
= NULL
;
199 static const UChar
*fetchErrorName(UErrorCode err
)
201 if (!gInfoMessages
) {
202 gInfoMessages
= (UChar
**)malloc((U_ERROR_WARNING_LIMIT
-U_ERROR_WARNING_START
)*sizeof(UChar
*));
203 memset(gInfoMessages
, 0, (U_ERROR_WARNING_LIMIT
-U_ERROR_WARNING_START
)*sizeof(UChar
*));
206 gErrMessages
= (UChar
**)malloc(U_ERROR_LIMIT
*sizeof(UChar
*));
207 memset(gErrMessages
, 0, U_ERROR_LIMIT
*sizeof(UChar
*));
210 return gErrMessages
[err
];
212 return gInfoMessages
[err
-U_ERROR_WARNING_START
];
215 U_CFUNC
const UChar
*u_wmsg_errorName(UErrorCode err
)
219 UErrorCode subErr
= U_ZERO_ERROR
;
220 const char *textMsg
= NULL
;
223 msg
= (UChar
*)fetchErrorName(err
);
236 const char *errname
= u_errorName(err
);
238 msg
= (UChar
*)ures_getStringByKey(gBundle
, errname
, &msgLen
, &subErr
);
239 if(U_FAILURE(subErr
))
246 if(msg
== NULL
) /* Couldn't find it anywhere.. */
249 textMsg
= u_errorName(err
);
251 sprintf(error
, "UNDOCUMENTED ICU ERROR %d", err
);
254 msg
= (UChar
*)malloc((strlen(textMsg
)+1)*sizeof(msg
[0]));
255 u_charsToUChars(textMsg
, msg
, (int32_t)(strlen(textMsg
)+1));
259 gErrMessages
[err
] = msg
;
261 gInfoMessages
[err
-U_ERROR_WARNING_START
] = msg
;