2001-09-19 23:43:16 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 1998-2000, International Business Machines Corporation
|
|
|
|
* and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
*
|
|
|
|
* File uwmsg.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 06/14/99 stephen Creation.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/umsg.h"
|
|
|
|
#include "unicode/uwmsg.h"
|
|
|
|
#include "unicode/ures.h"
|
2002-01-08 19:08:47 +00:00
|
|
|
#include "cstring.h"
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
#include <stdlib.h>
|
2001-09-19 23:43:16 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define BUF_SIZE 128
|
|
|
|
|
|
|
|
/* Print a ustring to the specified FILE* in the default codepage */
|
|
|
|
static void
|
|
|
|
uprint(const UChar *s,
|
|
|
|
int32_t sourceLen,
|
|
|
|
FILE *f,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
2001-10-10 16:45:32 +00:00
|
|
|
/* converter */
|
|
|
|
UConverter *converter;
|
|
|
|
char buf [BUF_SIZE];
|
|
|
|
const UChar *mySource;
|
|
|
|
const UChar *mySourceEnd;
|
|
|
|
char *myTarget;
|
|
|
|
int32_t arraySize;
|
|
|
|
|
|
|
|
if(s == 0) return;
|
|
|
|
|
|
|
|
/* set up the conversion parameters */
|
|
|
|
mySource = s;
|
|
|
|
mySourceEnd = mySource + sourceLen;
|
|
|
|
myTarget = buf;
|
|
|
|
arraySize = BUF_SIZE;
|
|
|
|
|
|
|
|
/* open a default converter */
|
|
|
|
converter = ucnv_open(0, status);
|
|
|
|
|
|
|
|
/* if we failed, clean up and exit */
|
|
|
|
if(U_FAILURE(*status)) goto finish;
|
2001-09-19 23:43:16 +00:00
|
|
|
|
|
|
|
/* perform the conversion */
|
2001-10-10 16:45:32 +00:00
|
|
|
do {
|
|
|
|
/* reset the error code */
|
|
|
|
*status = U_ZERO_ERROR;
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
/* perform the conversion */
|
|
|
|
ucnv_fromUnicode(converter, &myTarget, myTarget + arraySize,
|
|
|
|
&mySource, mySourceEnd, NULL,
|
|
|
|
TRUE, status);
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
/* Write the converted data to the FILE* */
|
|
|
|
fwrite(buf, sizeof(char), myTarget - buf, f);
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
/* update the conversion parameters*/
|
|
|
|
myTarget = buf;
|
|
|
|
arraySize = BUF_SIZE;
|
|
|
|
}
|
|
|
|
while(*status == U_BUFFER_OVERFLOW_ERROR);
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
|
|
|
/* close the converter */
|
|
|
|
ucnv_close(converter);
|
2001-09-19 23:43:16 +00:00
|
|
|
}
|
|
|
|
|
2002-01-05 01:20:45 +00:00
|
|
|
static const char *gPath = 0;
|
2001-09-19 23:43:16 +00:00
|
|
|
static UResourceBundle *gBundle = NULL;
|
|
|
|
|
2001-09-21 00:30:25 +00:00
|
|
|
U_CAPI UResourceBundle *u_wmsg_setPath(const char *path, UErrorCode *err)
|
2001-09-19 23:43:16 +00:00
|
|
|
{
|
|
|
|
if(U_FAILURE(*err))
|
|
|
|
{
|
2001-09-21 00:30:25 +00:00
|
|
|
return 0;
|
2001-09-19 23:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(gBundle != NULL)
|
|
|
|
{
|
|
|
|
*err = U_ILLEGAL_ARGUMENT_ERROR;
|
2001-09-21 00:30:25 +00:00
|
|
|
return 0;
|
2001-09-19 23:43:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UResourceBundle *b = NULL;
|
|
|
|
b = ures_open(path, NULL, err);
|
2002-03-15 02:12:01 +00:00
|
|
|
if(U_FAILURE(*err))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2002-01-05 01:20:45 +00:00
|
|
|
gPath = uprv_strdup(path);
|
2001-09-19 23:43:16 +00:00
|
|
|
gBundle = b;
|
|
|
|
}
|
|
|
|
|
2001-09-21 00:30:25 +00:00
|
|
|
return gBundle;
|
2001-09-19 23:43:16 +00:00
|
|
|
}
|
|
|
|
|
2002-03-19 17:14:10 +00:00
|
|
|
/* Format a message and print it's output to fp */
|
|
|
|
U_CAPI int u_wmsg(FILE *fp, const char *tag, ... )
|
2001-09-19 23:43:16 +00:00
|
|
|
{
|
2001-10-10 16:45:32 +00:00
|
|
|
const UChar *msg;
|
|
|
|
int32_t msgLen;
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
va_list ap;
|
|
|
|
UChar result[4096];
|
|
|
|
int32_t resultLength = 4096;
|
|
|
|
|
|
|
|
if(gBundle == NULL)
|
|
|
|
{
|
2002-03-19 17:14:10 +00:00
|
|
|
#if 0
|
2001-10-10 16:45:32 +00:00
|
|
|
fprintf(stderr, "u_wmsg: No path set!!\n"); /* FIXME: codepage?? */
|
2002-03-19 17:14:10 +00:00
|
|
|
#endif
|
|
|
|
return -1;
|
2001-10-10 16:45:32 +00:00
|
|
|
}
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
msg = ures_getStringByKey(gBundle, tag, &msgLen, &err);
|
|
|
|
|
|
|
|
if(U_FAILURE(err))
|
|
|
|
{
|
2002-03-19 17:14:10 +00:00
|
|
|
#if 0
|
2002-01-05 01:20:45 +00:00
|
|
|
fprintf(stderr, "u_wmsg: failed to load tag [%s] [%s] [%s]!!\n", tag, u_errorName(err), gPath);
|
2002-03-19 17:14:10 +00:00
|
|
|
#endif
|
|
|
|
return -1;
|
2001-10-10 16:45:32 +00:00
|
|
|
}
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
va_start(ap, tag);
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
resultLength = u_vformatMessage(uloc_getDefault(), msg, msgLen, result, resultLength, ap, &err);
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if(U_FAILURE(err))
|
|
|
|
{
|
2002-03-19 17:14:10 +00:00
|
|
|
#if 0
|
2001-10-10 16:45:32 +00:00
|
|
|
fprintf(stderr, "u_wmsg: failed to format %s:%s, err %s\n",
|
2001-09-19 23:43:16 +00:00
|
|
|
uloc_getDefault(),
|
|
|
|
tag,
|
|
|
|
u_errorName(err));
|
2002-03-19 17:14:10 +00:00
|
|
|
#endif
|
2001-10-10 16:45:32 +00:00
|
|
|
err = U_ZERO_ERROR;
|
2002-03-19 17:14:10 +00:00
|
|
|
uprint(msg,msgLen, fp, &err);
|
|
|
|
return -1;
|
2001-10-10 16:45:32 +00:00
|
|
|
}
|
|
|
|
|
2002-03-19 17:14:10 +00:00
|
|
|
uprint(result, resultLength, fp, &err);
|
2001-10-10 16:45:32 +00:00
|
|
|
|
|
|
|
if(U_FAILURE(err))
|
|
|
|
{
|
2002-03-19 17:14:10 +00:00
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "u_wmsg: failed to print %s: %s, err %s\n",
|
2001-09-19 23:43:16 +00:00
|
|
|
uloc_getDefault(),
|
|
|
|
tag,
|
|
|
|
u_errorName(err));
|
2002-03-19 17:14:10 +00:00
|
|
|
#endif
|
|
|
|
return -1;
|
2001-10-10 16:45:32 +00:00
|
|
|
}
|
2002-03-19 17:14:10 +00:00
|
|
|
|
2002-04-02 01:17:28 +00:00
|
|
|
return 0;
|
2001-09-19 23:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* these will break if the # of messages change. simply add or remove 0's .. */
|
|
|
|
UChar * gInfoMessages[U_ERROR_WARNING_LIMIT-U_ERROR_WARNING_START] =
|
2001-10-10 16:45:32 +00:00
|
|
|
{ 0,0 };
|
2001-09-19 23:43:16 +00:00
|
|
|
|
|
|
|
UChar * gErrMessages[U_ERROR_LIMIT] =
|
2001-10-10 16:45:32 +00:00
|
|
|
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
|
2001-09-19 23:43:16 +00:00
|
|
|
|
|
|
|
static const UChar *fetchErrorName(UErrorCode err)
|
|
|
|
{
|
2001-10-10 16:45:32 +00:00
|
|
|
if(err>=0)
|
|
|
|
return gErrMessages[err];
|
|
|
|
else
|
|
|
|
return gInfoMessages[err-U_ERROR_WARNING_START];
|
2001-09-19 23:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI const UChar *u_wmsg_errorName(UErrorCode err)
|
|
|
|
{
|
2001-10-10 16:45:32 +00:00
|
|
|
UChar *msg;
|
|
|
|
int32_t msgLen;
|
|
|
|
UErrorCode subErr = U_ZERO_ERROR;
|
|
|
|
const char *textMsg = NULL;
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
/* try the cache */
|
|
|
|
msg = (UChar*)fetchErrorName(err);
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
if(msg)
|
|
|
|
{
|
|
|
|
return msg;
|
|
|
|
}
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
if(gBundle == NULL)
|
2001-09-19 23:43:16 +00:00
|
|
|
{
|
2001-10-10 16:45:32 +00:00
|
|
|
msg = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-02-16 08:59:44 +00:00
|
|
|
const char *errname = u_errorName(err);
|
|
|
|
if (errname) {
|
|
|
|
msg = (UChar*)ures_getStringByKey(gBundle, errname, &msgLen, &subErr);
|
|
|
|
if(U_FAILURE(subErr))
|
|
|
|
{
|
|
|
|
msg = NULL;
|
|
|
|
}
|
2001-10-10 16:45:32 +00:00
|
|
|
}
|
2001-09-19 23:43:16 +00:00
|
|
|
}
|
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
if(msg == NULL) /* Couldn't find it anywhere.. */
|
|
|
|
{
|
2002-02-16 08:59:44 +00:00
|
|
|
char error[128];
|
2001-10-10 16:45:32 +00:00
|
|
|
textMsg = u_errorName(err);
|
2002-02-16 08:59:44 +00:00
|
|
|
if (!textMsg) {
|
|
|
|
sprintf(error, "UNDOCUMENTED ICU ERROR %d", err);
|
|
|
|
textMsg = error;
|
|
|
|
}
|
2001-10-10 16:45:32 +00:00
|
|
|
msg = (UChar*)malloc((strlen(textMsg)+1)*sizeof(msg[0]));
|
|
|
|
u_charsToUChars(textMsg, msg, strlen(textMsg)+1);
|
|
|
|
}
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
if(err>=0)
|
|
|
|
gErrMessages[err] = msg;
|
|
|
|
else
|
|
|
|
gInfoMessages[err-U_ERROR_WARNING_START] = msg;
|
2001-09-19 23:43:16 +00:00
|
|
|
|
2001-10-10 16:45:32 +00:00
|
|
|
return msg;
|
2001-09-19 23:43:16 +00:00
|
|
|
}
|