~ubuntu-branches/ubuntu/quantal/icu/quantal

« back to all changes in this revision

Viewing changes to source/extra/uconv/uwmsg.c

  • Committer: Package Import Robot
  • Author(s): Yves Arrouye
  • Date: 2002-03-03 15:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20020303153113-3ssceqlq45xbmbnc
Tags: upstream-2.0-2.1pre20020303
ImportĀ upstreamĀ versionĀ 2.0-2.1pre20020303

Show diffs side-by-side

added added

removed removed

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