Welcome to mirror list, hosted at ThFree Co, Russian Federation.

locale.c « locale « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 66c93d740a253335877ba1218c6c7d4bb376c4a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
FUNCTION
<<setlocale>>, <<localeconv>>---select or query locale

INDEX
	setlocale
INDEX
	localeconv
INDEX
	_setlocale_r
INDEX
	_localeconv_r

ANSI_SYNOPSIS
	#include <locale.h>
	char *setlocale(int <[category]>, const char *<[locale]>);
	lconv *localeconv(void);

	char *_setlocale_r(void *<[reent]>,
                        int <[category]>, const char *<[locale]>);
	lconv *_localeconv_r(void *<[reent]>);

TRAD_SYNOPSIS
	#include <locale.h>
	char *setlocale(<[category]>, <[locale]>)
	int <[category]>;
	char *<[locale]>;

	lconv *localeconv();

	char *_setlocale_r(<[reent]>, <[category]>, <[locale]>)
	char *<[reent]>;
	int <[category]>;
	char *<[locale]>;

	lconv *_localeconv_r(<[reent]>);
	char *<[reent]>;

DESCRIPTION
<<setlocale>> is the facility defined by ANSI C to condition the
execution environment for international collating and formatting
information; <<localeconv>> reports on the settings of the current
locale.

This is a minimal implementation, supporting only the required <<"C">>
value for <[locale]>; strings representing other locales are not
honored unless MB_CAPABLE is defined in which case three new
extensions are allowed for LC_CTYPE or LC_MESSAGES only: <<"C-JIS">>, 
<<"C-EUCJP">>, <<"C-SJIS">>, or <<"C-ISO-8859-1">>.  (<<"">> is 
also accepted; it represents the default locale
for an implementation, here equivalent to <<"C">>.)

If you use <<NULL>> as the <[locale]> argument, <<setlocale>> returns
a pointer to the string representing the current locale (always
<<"C">> in this implementation).  The acceptable values for
<[category]> are defined in `<<locale.h>>' as macros beginning with
<<"LC_">>, but this implementation does not check the values you pass
in the <[category]> argument.

<<localeconv>> returns a pointer to a structure (also defined in
`<<locale.h>>') describing the locale-specific conventions currently
in effect.  

<<_localeconv_r>> and <<_setlocale_r>> are reentrant versions of
<<localeconv>> and <<setlocale>> respectively.  The extra argument
<[reent]> is a pointer to a reentrancy structure.

RETURNS
<<setlocale>> returns either a pointer to a string naming the locale
currently in effect (always <<"C">> for this implementation, or, if
the locale request cannot be honored, <<NULL>>.

<<localeconv>> returns a pointer to a structure of type <<lconv>>,
which describes the formatting and collating conventions in effect (in
this implementation, always those of the C locale).

PORTABILITY
ANSI C requires <<setlocale>>, but the only locale required across all
implementations is the C locale.

No supporting OS subroutines are required.
*/

/*
 * setlocale, localeconv : internationalize your locale.
 *                         (Only "C" or null supported).
 */

#include <locale.h>
#include <string.h>
#include <limits.h>
#include <reent.h>

#ifdef __CYGWIN__
int __declspec(dllexport) __mb_cur_max = 1;
#else
int __mb_cur_max = 1;
#endif

int __nlocale_changed = 0;
int __mlocale_changed = 0;
char *_PathLocale = NULL;

static _CONST struct lconv lconv = 
{
  ".", "", "", "", "", "", "", "", "", "",
  CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX,
  CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX,
};


char * _EXFUN(__locale_charset,(_VOID));

static char *charset = "ISO-8859-1";
char __lc_ctype[12] = "C";

char *
_DEFUN(_setlocale_r, (p, category, locale),
       struct _reent *p _AND
       int category _AND
       _CONST char *locale)
{
#ifndef MB_CAPABLE
  if (locale)
    { 
      if (strcmp (locale, "C") && strcmp (locale, ""))
        return 0;
      p->_current_category = category;  
      p->_current_locale = locale;
    }
  return "C";
#else
  static char last_lc_ctype[12] = "C";
  static char lc_messages[12] = "C";
  static char last_lc_messages[12] = "C";

  if (locale)
    {
      char *locale_name = (char *)locale;
      if (category != LC_CTYPE && category != LC_MESSAGES) 
        { 
          if (strcmp (locale, "C") && strcmp (locale, ""))
            return 0;
          if (category == LC_ALL)
            {
              strcpy (last_lc_ctype, __lc_ctype);
              strcpy (__lc_ctype, "C");
              strcpy (last_lc_messages, lc_messages);
              strcpy (lc_messages, "C");
              __mb_cur_max = 1;
            }
        }
      else
        {
          if (locale[0] == 'C' && locale[1] == '-')
            {
              switch (locale[2])
                {
                case 'U':
                  if (strcmp (locale, "C-UTF-8"))
                    return 0;
                break;
                case 'J':
                  if (strcmp (locale, "C-JIS"))
                    return 0;
                break;
                case 'E':
                  if (strcmp (locale, "C-EUCJP"))
                    return 0;
                break;
                case 'S':
                  if (strcmp (locale, "C-SJIS"))
                    return 0;
                break;
                case 'I':
                  if (strcmp (locale, "C-ISO-8859-1"))
                    return 0;
                break;
                default:
                  return 0;
                }
            }
          else 
            {
              if (strcmp (locale, "C") && strcmp (locale, ""))
                return 0;
              locale_name = "C"; /* C is always the default locale */
            }

          if (category == LC_CTYPE)
            {
              strcpy (last_lc_ctype, __lc_ctype);
              strcpy (__lc_ctype, locale_name);

              __mb_cur_max = 1;
              if (locale[1] == '-')
                {
                  switch (locale[2])
                    {
                    case 'U':
                      __mb_cur_max = 6;
                    break;
                    case 'J':
                      __mb_cur_max = 8;
                    break;
                    case 'E':
                      __mb_cur_max = 2;
                    break;
                    case 'S':
                      __mb_cur_max = 2;
                    break;
                    case 'I':
                    default:
                      __mb_cur_max = 1;
                    }
                }
            }
          else
            {
              strcpy (last_lc_messages, lc_messages);
              strcpy (lc_messages, locale_name);

              charset = "ISO-8859-1";
              if (locale[1] == '-')
                {
                  switch (locale[2])
                    {
                    case 'U':
                      charset = "UTF-8";
                    break;
                    case 'J':
                      charset = "JIS";
                    break;
                    case 'E':
                      charset = "EUCJP";
                    break;
                    case 'S':
                      charset = "SJIS";
                    break;
                    case 'I':
                      charset = "ISO-8859-1";
                    break;
                    default:
                      return 0;
                    }
                }
            }
        }
      p->_current_category = category;  
      p->_current_locale = locale;

      if (category == LC_CTYPE)
        return last_lc_ctype;
      else if (category == LC_MESSAGES)
        return last_lc_messages;
    }
  else
    {
      if (category == LC_CTYPE)
        return __lc_ctype;
      else if (category == LC_MESSAGES)
        return lc_messages;
    }
 
  return "C";
#endif
  
}

char *
_DEFUN_VOID(__locale_charset)
{
  return charset;
}

struct lconv *
_DEFUN(_localeconv_r, (data), 
      struct _reent *data)
{
  return (struct lconv *) &lconv;
}

#ifndef _REENT_ONLY

char *
_DEFUN(setlocale, (category, locale),
       int category _AND
       _CONST char *locale)
{
  return _setlocale_r (_REENT, category, locale);
}


struct lconv *
_DEFUN_VOID(localeconv)
{
  return _localeconv_r (_REENT);
}

#endif