summaryrefslogtreecommitdiff
path: root/Modules/_localemodule.c
blob: 2f7a42b7b373ccf41f4103b746b87ee676d3f058 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/***********************************************************
Copyright (C) 1997 Martin von Loewis

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies.

This software comes with no warranty. Use at your own risk.
******************************************************************/

#include <stdio.h>
#include <errno.h>
#include <locale.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include "Python.h"
#ifdef macintosh
char *strdup Py_PROTO((char *));
#endif

static char locale__doc__[]="Support for POSIX locales.";

static PyObject *Error;

/* support functions for formatting floating point numbers */

static char setlocale__doc__[]=
"(integer,string=None) -> string. Activates/queries locale processing."
;

/* to record the LC_NUMERIC settings */
static PyObject* grouping=0;
static PyObject* thousands_sep=0;
static PyObject* decimal_point=0;
/* if non-null, indicates that LC_NUMERIC is different from "C" */
static char* saved_numeric=0;

/* the grouping is terminated by either 0 or CHAR_MAX */
static PyObject*
copy_grouping(s)
  char* s;
{
  int i;
  PyObject *result,*val=0;
  if(s[0]=='\0')
     /* empty string: no grouping at all */
     return PyList_New(0);
  for(i=0;s[i]!='\0' && s[i]!=CHAR_MAX;i++)
     /* nothing */;
  result = PyList_New(i+1);
  if(!result)return NULL;
  i=-1;
  do{
    i++;
    val=PyInt_FromLong(s[i]);
    if(!val)break;
    if(PyList_SetItem(result,i,val)){
      Py_DECREF(val);
      val=0;
      break;
    }
  }while(s[i]!='\0' && s[i]!=CHAR_MAX);
  if(!val){
    Py_DECREF(result);
    return NULL;
  }
  return result;
}

static void
fixup_ulcase()
{
  PyObject *mods,*strop,*string,*ulo;
  unsigned char ul[256];
  int n,c;

  /* finding sys.modules */
  mods=PyImport_GetModuleDict();
  if(!mods)return;
  /* finding the module */
  string=PyDict_GetItemString(mods,"string");
  if(string)
     string=PyModule_GetDict(string);
  strop=PyDict_GetItemString(mods,"strop");
  if(strop)
     strop=PyModule_GetDict(strop);
  if(!string && !strop)return;
  /* create uppercase */
  n = 0;
  for (c = 0; c < 256; c++) {
    if (isupper(c))
       ul[n++] = c;
  }
  ulo=PyString_FromStringAndSize((const char *)ul,n);
  if(!ulo)return;
  if(string)
     PyDict_SetItemString(string,"uppercase",ulo);
  if(strop)
     PyDict_SetItemString(strop,"uppercase",ulo);
  Py_DECREF(ulo);
  /* create lowercase */
  n = 0;
  for (c = 0; c < 256; c++) {
    if (islower(c))
       ul[n++] = c;
  }
  ulo=PyString_FromStringAndSize((const char *)ul,n);
  if(!ulo)return;
  if(string)
     PyDict_SetItemString(string,"lowercase",ulo);
  if(strop)
     PyDict_SetItemString(strop,"lowercase",ulo);
  Py_DECREF(ulo);
  /* create letters */
  n = 0;
  for (c = 0; c < 256; c++) {
    if (isalpha(c))
       ul[n++] = c;
  }
  ulo=PyString_FromStringAndSize((const char *)ul,n);
  if(!ulo)return;
  if(string)
     PyDict_SetItemString(string,"letters",ulo);
  Py_DECREF(ulo);
}
  

static PyObject*
PyLocale_setlocale(self,args)
  PyObject *self;
  PyObject *args;
{
  int category;
  char *locale=0,*result;
  PyObject *result_object;
  struct lconv *lc;
  if(!PyArg_ParseTuple(args,"i|z",&category,&locale))return 0;
  if(locale){
    /* set locale */
    result=setlocale(category,locale);
    if(!result){
      /* operation failed, no setting was changed */
      PyErr_SetString(Error,"locale setting not supported");
      return NULL;
    }
    result_object=PyString_FromString(result);
    if(!result)return NULL;
    /* record changes to LC_NUMERIC */
    if(category==LC_NUMERIC || category==LC_ALL){
      if(strcmp(locale,"C")==0 || strcmp(locale,"POSIX")==0){
        /* user just asked for default numeric locale */
        if(saved_numeric)free(saved_numeric);
        saved_numeric=0;
      }else{
        /* remember values */
        lc=localeconv();
        Py_XDECREF(grouping);
        grouping=copy_grouping(lc->grouping);
        Py_XDECREF(thousands_sep);
        thousands_sep=PyString_FromString(lc->thousands_sep);
        Py_XDECREF(decimal_point);
        decimal_point=PyString_FromString(lc->decimal_point);
        saved_numeric = strdup(locale);

        /* restore to "C" */
        setlocale(LC_NUMERIC,"C");
      }
    }
    /* record changes to LC_CTYPE */
    if(category==LC_CTYPE || category==LC_ALL)
       fixup_ulcase();
    /* things that got wrong up to here are ignored */
    PyErr_Clear();
  }else{
    /* get locale */
    /* restore LC_NUMERIC first, if appropriate */
    if(saved_numeric)
       setlocale(LC_NUMERIC,saved_numeric);
    result=setlocale(category,NULL);
    if(!result){
      PyErr_SetString(Error,"locale query failed");
      return NULL;
    }
    result_object=PyString_FromString(result);
    /* restore back to "C" */
    if(saved_numeric)
       setlocale(LC_NUMERIC,"C");
  }
  return result_object;
}

static char localeconv__doc__[]=
"() -> dict. Returns numeric and monetary locale-specific parameters."
;

static PyObject*
PyLocale_localeconv(self,args)
  PyObject *self;
  PyObject *args;
{
  PyObject* result;
  struct lconv *l;
  PyObject *x;
  if(!PyArg_NoArgs(args))return 0;
  result = PyDict_New();
  if(!result)return 0;
  /* if LC_NUMERIC is different in the C library, use saved value */
  l = localeconv();
  /* hopefully, the localeconv result survives the C library calls
     involved herein */
#define RESULT_STRING(s) \
  x=PyString_FromString(l->s);if(!x)goto failed;PyDict_SetItemString(result,#s,x);Py_XDECREF(x)
#define RESULT_INT(i) \
  x=PyInt_FromLong(l->i);if(!x)goto failed;PyDict_SetItemString(result,#i,x);Py_XDECREF(x)

    /* Numeric information */
  if(saved_numeric){
    /* cannot use localeconv results */
    PyDict_SetItemString(result,"decimal_point",decimal_point);
    PyDict_SetItemString(result,"grouping",grouping);
    PyDict_SetItemString(result,"thousands_sep",thousands_sep);
  }else{
    RESULT_STRING(decimal_point);
    RESULT_STRING(thousands_sep);
    x=copy_grouping(l->grouping);
    if(!x)goto failed;
    PyDict_SetItemString(result,"grouping",x);
    Py_XDECREF(x);
  }

  /* Monetary information */
  RESULT_STRING(int_curr_symbol);
  RESULT_STRING(currency_symbol);
  RESULT_STRING(mon_decimal_point);
  RESULT_STRING(mon_thousands_sep);
  x=copy_grouping(l->mon_grouping);
  if(!x)goto failed;
  PyDict_SetItemString(result,"mon_grouping",x);
  Py_XDECREF(x);
  RESULT_STRING(positive_sign);
  RESULT_STRING(negative_sign);
  RESULT_INT(int_frac_digits);
  RESULT_INT(frac_digits);
  RESULT_INT(p_cs_precedes);
  RESULT_INT(p_sep_by_space);
  RESULT_INT(n_cs_precedes);
  RESULT_INT(n_sep_by_space);
  RESULT_INT(p_sign_posn);
  RESULT_INT(n_sign_posn);

  return result;
 failed:
  Py_XDECREF(result);
  Py_XDECREF(x);
  return NULL;
}

static char strcoll__doc__[]=
"string,string -> int. Compares two strings according to the locale."
;

static PyObject*
PyLocale_strcoll(self,args)
  PyObject *self;
  PyObject *args;
{
  char *s1,*s2;
  if(!PyArg_ParseTuple(args,"ss",&s1,&s2))
     return NULL;
  return PyInt_FromLong(strcoll(s1,s2));
}

static char strxfrm__doc__[]=
"string -> string. Returns a string that behaves for cmp locale-aware."
;

static PyObject*
PyLocale_strxfrm(self,args)
  PyObject* self;
  PyObject* args;
{
  char *s,*buf;
  int n1,n2;
  PyObject *result;
  if(!PyArg_ParseTuple(args,"s",&s))
     return NULL;
  /* assume no change in size, first */
  n1=strlen(s)+1;
  buf=Py_Malloc(n1);
  if(!buf)return NULL;
  n2=strxfrm(buf,s,n1);
  if(n2>n1){
    /* more space needed */
    buf=Py_Realloc(buf,n2);
    if(!buf)return NULL;
    strxfrm(buf,s,n2);
  }
  result=PyString_FromString(buf);
  Py_Free(buf);
  return result;
}

static struct PyMethodDef PyLocale_Methods[] = {
  {"setlocale",(PyCFunction)PyLocale_setlocale,1,setlocale__doc__},
  {"localeconv",(PyCFunction)PyLocale_localeconv,0,localeconv__doc__},
  {"strcoll",(PyCFunction)PyLocale_strcoll,1,strcoll__doc__},
  {"strxfrm",(PyCFunction)PyLocale_strxfrm,1,strxfrm__doc__},
  {NULL, NULL}
};

void
init_locale()
{
  PyObject *m,*d,*x;
  m=Py_InitModule("_locale",PyLocale_Methods);
  d = PyModule_GetDict(m);
  x=PyInt_FromLong(LC_CTYPE);
  PyDict_SetItemString(d,"LC_CTYPE",x);
  Py_XDECREF(x);

  x=PyInt_FromLong(LC_TIME);
  PyDict_SetItemString(d,"LC_TIME",x);
  Py_XDECREF(x);

  x=PyInt_FromLong(LC_COLLATE);
  PyDict_SetItemString(d,"LC_COLLATE",x);
  Py_XDECREF(x);

  x=PyInt_FromLong(LC_MONETARY);
  PyDict_SetItemString(d,"LC_MONETARY",x);
  Py_XDECREF(x);

#ifdef LC_MESSAGES
  x=PyInt_FromLong(LC_MESSAGES);
  PyDict_SetItemString(d,"LC_MESSAGES",x);
  Py_XDECREF(x);
#endif /* LC_MESSAGES */

  x=PyInt_FromLong(LC_NUMERIC);
  PyDict_SetItemString(d,"LC_NUMERIC",x);
  Py_XDECREF(x);

  x=PyInt_FromLong(LC_ALL);
  PyDict_SetItemString(d,"LC_ALL",x);
  Py_XDECREF(x);

  x=PyInt_FromLong(CHAR_MAX);
  PyDict_SetItemString(d,"CHAR_MAX",x);
  Py_XDECREF(x);

  Error = PyErr_NewException("locale.Error", NULL, NULL);
  PyDict_SetItemString(d, "Error", Error);

  x=PyString_FromString(locale__doc__);
  PyDict_SetItemString(d,"__doc__",x);
  Py_XDECREF(x);

  if(PyErr_Occurred())
    Py_FatalError("Can't initialize module locale");
}