OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-newlib/] [newlib-1.17.0/] [newlib/] [libc/] [locale/] [locale.c] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 jlechner
/*
2
FUNCTION
3
<<setlocale>>, <<localeconv>>---select or query locale
4
 
5
INDEX
6
        setlocale
7
INDEX
8
        localeconv
9
INDEX
10
        _setlocale_r
11
INDEX
12
        _localeconv_r
13
 
14
ANSI_SYNOPSIS
15
        #include <locale.h>
16
        char *setlocale(int <[category]>, const char *<[locale]>);
17
        lconv *localeconv(void);
18
 
19
        char *_setlocale_r(void *<[reent]>,
20
                        int <[category]>, const char *<[locale]>);
21
        lconv *_localeconv_r(void *<[reent]>);
22
 
23
TRAD_SYNOPSIS
24
        #include <locale.h>
25
        char *setlocale(<[category]>, <[locale]>)
26
        int <[category]>;
27
        char *<[locale]>;
28
 
29
        lconv *localeconv();
30
 
31
        char *_setlocale_r(<[reent]>, <[category]>, <[locale]>)
32
        char *<[reent]>;
33
        int <[category]>;
34
        char *<[locale]>;
35
 
36
        lconv *_localeconv_r(<[reent]>);
37
        char *<[reent]>;
38
 
39
DESCRIPTION
40
<<setlocale>> is the facility defined by ANSI C to condition the
41
execution environment for international collating and formatting
42
information; <<localeconv>> reports on the settings of the current
43
locale.
44
 
45
This is a minimal implementation, supporting only the required <<"C">>
46
value for <[locale]>; strings representing other locales are not
47
honored unless _MB_CAPABLE is defined in which case three new
48
extensions are allowed for LC_CTYPE or LC_MESSAGES only: <<"C-JIS">>,
49
<<"C-EUCJP">>, <<"C-SJIS">>, or <<"C-ISO-8859-1">>.  (<<"">> is
50
also accepted; it represents the default locale
51
for an implementation, here equivalent to <<"C">>.)
52
 
53
If you use <<NULL>> as the <[locale]> argument, <<setlocale>> returns
54
a pointer to the string representing the current locale (always
55
<<"C">> in this implementation).  The acceptable values for
56
<[category]> are defined in `<<locale.h>>' as macros beginning with
57
<<"LC_">>, but this implementation does not check the values you pass
58
in the <[category]> argument.
59
 
60
<<localeconv>> returns a pointer to a structure (also defined in
61
`<<locale.h>>') describing the locale-specific conventions currently
62
in effect.
63
 
64
<<_localeconv_r>> and <<_setlocale_r>> are reentrant versions of
65
<<localeconv>> and <<setlocale>> respectively.  The extra argument
66
<[reent]> is a pointer to a reentrancy structure.
67
 
68
RETURNS
69
<<setlocale>> returns either a pointer to a string naming the locale
70
currently in effect (always <<"C">> for this implementation, or, if
71
the locale request cannot be honored, <<NULL>>.
72
 
73
<<localeconv>> returns a pointer to a structure of type <<lconv>>,
74
which describes the formatting and collating conventions in effect (in
75
this implementation, always those of the C locale).
76
 
77
PORTABILITY
78
ANSI C requires <<setlocale>>, but the only locale required across all
79
implementations is the C locale.
80
 
81
No supporting OS subroutines are required.
82
*/
83
 
84
/*
85
 * setlocale, localeconv : internationalize your locale.
86
 *                         (Only "C" or null supported).
87
 */
88
 
89
#include <newlib.h>
90
#include <locale.h>
91
#include <string.h>
92
#include <limits.h>
93
#include <reent.h>
94
 
95
#ifdef __CYGWIN__
96
int __declspec(dllexport) __mb_cur_max = 1;
97
#else
98
int __mb_cur_max = 1;
99
#endif
100
 
101
int __nlocale_changed = 0;
102
int __mlocale_changed = 0;
103
char *_PathLocale = NULL;
104
 
105
static _CONST struct lconv lconv =
106
{
107
  ".", "", "", "", "", "", "", "", "", "",
108
  CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX,
109
  CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX,
110
};
111
 
112
 
113
char * _EXFUN(__locale_charset,(_VOID));
114
 
115
static char *charset = "ISO-8859-1";
116
char __lc_ctype[12] = "C";
117
 
118
char *
119
_DEFUN(_setlocale_r, (p, category, locale),
120
       struct _reent *p _AND
121
       int category _AND
122
       _CONST char *locale)
123
{
124
#ifndef _MB_CAPABLE
125
  if (locale)
126
    {
127
      if (strcmp (locale, "C") && strcmp (locale, ""))
128
        return 0;
129
      p->_current_category = category;
130
      p->_current_locale = locale;
131
    }
132
  return "C";
133
#else
134
  static char last_lc_ctype[12] = "C";
135
  static char lc_messages[12] = "C";
136
  static char last_lc_messages[12] = "C";
137
 
138
  if (locale)
139
    {
140
      char *locale_name = (char *)locale;
141
      if (category != LC_CTYPE && category != LC_MESSAGES)
142
        {
143
          if (strcmp (locale, "C") && strcmp (locale, ""))
144
            return 0;
145
          if (category == LC_ALL)
146
            {
147
              strcpy (last_lc_ctype, __lc_ctype);
148
              strcpy (__lc_ctype, "C");
149
              strcpy (last_lc_messages, lc_messages);
150
              strcpy (lc_messages, "C");
151
              __mb_cur_max = 1;
152
            }
153
        }
154
      else
155
        {
156
          if (locale[0] == 'C' && locale[1] == '-')
157
            {
158
              switch (locale[2])
159
                {
160
                case 'U':
161
                  if (strcmp (locale, "C-UTF-8"))
162
                    return 0;
163
                break;
164
                case 'J':
165
                  if (strcmp (locale, "C-JIS"))
166
                    return 0;
167
                break;
168
                case 'E':
169
                  if (strcmp (locale, "C-EUCJP"))
170
                    return 0;
171
                break;
172
                case 'S':
173
                  if (strcmp (locale, "C-SJIS"))
174
                    return 0;
175
                break;
176
                case 'I':
177
                  if (strcmp (locale, "C-ISO-8859-1"))
178
                    return 0;
179
                break;
180
                default:
181
                  return 0;
182
                }
183
            }
184
          else
185
            {
186
              if (strcmp (locale, "C") && strcmp (locale, ""))
187
                return 0;
188
              locale_name = "C"; /* C is always the default locale */
189
            }
190
 
191
          if (category == LC_CTYPE)
192
            {
193
              strcpy (last_lc_ctype, __lc_ctype);
194
              strcpy (__lc_ctype, locale_name);
195
 
196
              __mb_cur_max = 1;
197
              if (locale[1] == '-')
198
                {
199
                  switch (locale[2])
200
                    {
201
                    case 'U':
202
                      __mb_cur_max = 6;
203
                    break;
204
                    case 'J':
205
                      __mb_cur_max = 8;
206
                    break;
207
                    case 'E':
208
                      __mb_cur_max = 2;
209
                    break;
210
                    case 'S':
211
                      __mb_cur_max = 2;
212
                    break;
213
                    case 'I':
214
                    default:
215
                      __mb_cur_max = 1;
216
                    }
217
                }
218
            }
219
          else
220
            {
221
              strcpy (last_lc_messages, lc_messages);
222
              strcpy (lc_messages, locale_name);
223
 
224
              charset = "ISO-8859-1";
225
              if (locale[1] == '-')
226
                {
227
                  switch (locale[2])
228
                    {
229
                    case 'U':
230
                      charset = "UTF-8";
231
                    break;
232
                    case 'J':
233
                      charset = "JIS";
234
                    break;
235
                    case 'E':
236
                      charset = "EUCJP";
237
                    break;
238
                    case 'S':
239
                      charset = "SJIS";
240
                    break;
241
                    case 'I':
242
                      charset = "ISO-8859-1";
243
                    break;
244
                    default:
245
                      return 0;
246
                    }
247
                }
248
            }
249
        }
250
      p->_current_category = category;
251
      p->_current_locale = locale;
252
 
253
      if (category == LC_CTYPE)
254
        return last_lc_ctype;
255
      else if (category == LC_MESSAGES)
256
        return last_lc_messages;
257
    }
258
  else
259
    {
260
      if (category == LC_CTYPE)
261
        return __lc_ctype;
262
      else if (category == LC_MESSAGES)
263
        return lc_messages;
264
    }
265
 
266
  return "C";
267
#endif
268
 
269
}
270
 
271
char *
272
_DEFUN_VOID(__locale_charset)
273
{
274
  return charset;
275
}
276
 
277
struct lconv *
278
_DEFUN(_localeconv_r, (data),
279
      struct _reent *data)
280
{
281
  return (struct lconv *) &lconv;
282
}
283
 
284
#ifndef _REENT_ONLY
285
 
286
char *
287
_DEFUN(setlocale, (category, locale),
288
       int category _AND
289
       _CONST char *locale)
290
{
291
  return _setlocale_r (_REENT, category, locale);
292
}
293
 
294
 
295
struct lconv *
296
_DEFUN_VOID(localeconv)
297
{
298
  return _localeconv_r (_REENT);
299
}
300
 
301
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.