]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | /***********************************************************\r |
2 | Copyright (C) 1997, 2002, 2003 Martin von Loewis\r | |
3 | \r | |
4 | Permission to use, copy, modify, and distribute this software and its\r | |
5 | documentation for any purpose and without fee is hereby granted,\r | |
6 | provided that the above copyright notice appear in all copies.\r | |
7 | \r | |
8 | This software comes with no warranty. Use at your own risk.\r | |
9 | \r | |
10 | ******************************************************************/\r | |
11 | \r | |
12 | #include "Python.h"\r | |
13 | \r | |
14 | #include <stdio.h>\r | |
15 | #include <locale.h>\r | |
16 | #include <string.h>\r | |
17 | #include <ctype.h>\r | |
18 | \r | |
19 | #ifdef HAVE_ERRNO_H\r | |
20 | #include <errno.h>\r | |
21 | #endif\r | |
22 | \r | |
23 | #ifdef HAVE_LANGINFO_H\r | |
24 | #include <langinfo.h>\r | |
25 | #endif\r | |
26 | \r | |
27 | #ifdef HAVE_LIBINTL_H\r | |
28 | #include <libintl.h>\r | |
29 | #endif\r | |
30 | \r | |
31 | #ifdef HAVE_WCHAR_H\r | |
32 | #include <wchar.h>\r | |
33 | #endif\r | |
34 | \r | |
35 | #if defined(MS_WINDOWS)\r | |
36 | #define WIN32_LEAN_AND_MEAN\r | |
37 | #include <windows.h>\r | |
38 | #endif\r | |
39 | \r | |
40 | #ifdef RISCOS\r | |
41 | char *strdup(const char *);\r | |
42 | #endif\r | |
43 | \r | |
44 | PyDoc_STRVAR(locale__doc__, "Support for POSIX locales.");\r | |
45 | \r | |
46 | static PyObject *Error;\r | |
47 | \r | |
48 | /* support functions for formatting floating point numbers */\r | |
49 | \r | |
50 | PyDoc_STRVAR(setlocale__doc__,\r | |
51 | "(integer,string=None) -> string. Activates/queries locale processing.");\r | |
52 | \r | |
53 | /* the grouping is terminated by either 0 or CHAR_MAX */\r | |
54 | static PyObject*\r | |
55 | copy_grouping(char* s)\r | |
56 | {\r | |
57 | int i;\r | |
58 | PyObject *result, *val = NULL;\r | |
59 | \r | |
60 | if (s[0] == '\0')\r | |
61 | /* empty string: no grouping at all */\r | |
62 | return PyList_New(0);\r | |
63 | \r | |
64 | for (i = 0; s[i] != '\0' && s[i] != CHAR_MAX; i++)\r | |
65 | ; /* nothing */\r | |
66 | \r | |
67 | result = PyList_New(i+1);\r | |
68 | if (!result)\r | |
69 | return NULL;\r | |
70 | \r | |
71 | i = -1;\r | |
72 | do {\r | |
73 | i++;\r | |
74 | val = PyInt_FromLong(s[i]);\r | |
75 | if (!val)\r | |
76 | break;\r | |
77 | if (PyList_SetItem(result, i, val)) {\r | |
78 | Py_DECREF(val);\r | |
79 | val = NULL;\r | |
80 | break;\r | |
81 | }\r | |
82 | } while (s[i] != '\0' && s[i] != CHAR_MAX);\r | |
83 | \r | |
84 | if (!val) {\r | |
85 | Py_DECREF(result);\r | |
86 | return NULL;\r | |
87 | }\r | |
88 | \r | |
89 | return result;\r | |
90 | }\r | |
91 | \r | |
92 | static void\r | |
93 | fixup_ulcase(void)\r | |
94 | {\r | |
95 | PyObject *mods, *strop, *string, *ulo;\r | |
96 | unsigned char ul[256];\r | |
97 | int n, c;\r | |
98 | \r | |
99 | /* find the string and strop modules */\r | |
100 | mods = PyImport_GetModuleDict();\r | |
101 | if (!mods)\r | |
102 | return;\r | |
103 | string = PyDict_GetItemString(mods, "string");\r | |
104 | if (string)\r | |
105 | string = PyModule_GetDict(string);\r | |
106 | strop=PyDict_GetItemString(mods, "strop");\r | |
107 | if (strop)\r | |
108 | strop = PyModule_GetDict(strop);\r | |
109 | if (!string && !strop)\r | |
110 | return;\r | |
111 | \r | |
112 | /* create uppercase map string */\r | |
113 | n = 0;\r | |
114 | for (c = 0; c < 256; c++) {\r | |
115 | if (isupper(c))\r | |
116 | ul[n++] = c;\r | |
117 | }\r | |
118 | ulo = PyString_FromStringAndSize((const char *)ul, n);\r | |
119 | if (!ulo)\r | |
120 | return;\r | |
121 | if (string)\r | |
122 | PyDict_SetItemString(string, "uppercase", ulo);\r | |
123 | if (strop)\r | |
124 | PyDict_SetItemString(strop, "uppercase", ulo);\r | |
125 | Py_DECREF(ulo);\r | |
126 | \r | |
127 | /* create lowercase string */\r | |
128 | n = 0;\r | |
129 | for (c = 0; c < 256; c++) {\r | |
130 | if (islower(c))\r | |
131 | ul[n++] = c;\r | |
132 | }\r | |
133 | ulo = PyString_FromStringAndSize((const char *)ul, n);\r | |
134 | if (!ulo)\r | |
135 | return;\r | |
136 | if (string)\r | |
137 | PyDict_SetItemString(string, "lowercase", ulo);\r | |
138 | if (strop)\r | |
139 | PyDict_SetItemString(strop, "lowercase", ulo);\r | |
140 | Py_DECREF(ulo);\r | |
141 | \r | |
142 | /* create letters string */\r | |
143 | n = 0;\r | |
144 | for (c = 0; c < 256; c++) {\r | |
145 | if (isalpha(c))\r | |
146 | ul[n++] = c;\r | |
147 | }\r | |
148 | ulo = PyString_FromStringAndSize((const char *)ul, n);\r | |
149 | if (!ulo)\r | |
150 | return;\r | |
151 | if (string)\r | |
152 | PyDict_SetItemString(string, "letters", ulo);\r | |
153 | Py_DECREF(ulo);\r | |
154 | }\r | |
155 | \r | |
156 | static PyObject*\r | |
157 | PyLocale_setlocale(PyObject* self, PyObject* args)\r | |
158 | {\r | |
159 | int category;\r | |
160 | char *locale = NULL, *result;\r | |
161 | PyObject *result_object;\r | |
162 | \r | |
163 | if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))\r | |
164 | return NULL;\r | |
165 | \r | |
166 | #if defined(MS_WINDOWS)\r | |
167 | if (category < LC_MIN || category > LC_MAX)\r | |
168 | {\r | |
169 | PyErr_SetString(Error, "invalid locale category");\r | |
170 | return NULL;\r | |
171 | }\r | |
172 | #endif\r | |
173 | \r | |
174 | if (locale) {\r | |
175 | /* set locale */\r | |
176 | result = setlocale(category, locale);\r | |
177 | if (!result) {\r | |
178 | /* operation failed, no setting was changed */\r | |
179 | PyErr_SetString(Error, "unsupported locale setting");\r | |
180 | return NULL;\r | |
181 | }\r | |
182 | result_object = PyString_FromString(result);\r | |
183 | if (!result_object)\r | |
184 | return NULL;\r | |
185 | /* record changes to LC_CTYPE */\r | |
186 | if (category == LC_CTYPE || category == LC_ALL)\r | |
187 | fixup_ulcase();\r | |
188 | /* things that got wrong up to here are ignored */\r | |
189 | PyErr_Clear();\r | |
190 | } else {\r | |
191 | /* get locale */\r | |
192 | result = setlocale(category, NULL);\r | |
193 | if (!result) {\r | |
194 | PyErr_SetString(Error, "locale query failed");\r | |
195 | return NULL;\r | |
196 | }\r | |
197 | result_object = PyString_FromString(result);\r | |
198 | }\r | |
199 | return result_object;\r | |
200 | }\r | |
201 | \r | |
202 | PyDoc_STRVAR(localeconv__doc__,\r | |
203 | "() -> dict. Returns numeric and monetary locale-specific parameters.");\r | |
204 | \r | |
205 | static PyObject*\r | |
206 | PyLocale_localeconv(PyObject* self)\r | |
207 | {\r | |
208 | PyObject* result;\r | |
209 | struct lconv *l;\r | |
210 | PyObject *x;\r | |
211 | \r | |
212 | result = PyDict_New();\r | |
213 | if (!result)\r | |
214 | return NULL;\r | |
215 | \r | |
216 | /* if LC_NUMERIC is different in the C library, use saved value */\r | |
217 | l = localeconv();\r | |
218 | \r | |
219 | /* hopefully, the localeconv result survives the C library calls\r | |
220 | involved herein */\r | |
221 | \r | |
222 | #define RESULT_STRING(s)\\r | |
223 | x = PyString_FromString(l->s);\\r | |
224 | if (!x) goto failed;\\r | |
225 | PyDict_SetItemString(result, #s, x);\\r | |
226 | Py_XDECREF(x)\r | |
227 | \r | |
228 | #define RESULT_INT(i)\\r | |
229 | x = PyInt_FromLong(l->i);\\r | |
230 | if (!x) goto failed;\\r | |
231 | PyDict_SetItemString(result, #i, x);\\r | |
232 | Py_XDECREF(x)\r | |
233 | \r | |
234 | /* Numeric information */\r | |
235 | RESULT_STRING(decimal_point);\r | |
236 | RESULT_STRING(thousands_sep);\r | |
237 | x = copy_grouping(l->grouping);\r | |
238 | if (!x)\r | |
239 | goto failed;\r | |
240 | PyDict_SetItemString(result, "grouping", x);\r | |
241 | Py_XDECREF(x);\r | |
242 | \r | |
243 | /* Monetary information */\r | |
244 | RESULT_STRING(int_curr_symbol);\r | |
245 | RESULT_STRING(currency_symbol);\r | |
246 | RESULT_STRING(mon_decimal_point);\r | |
247 | RESULT_STRING(mon_thousands_sep);\r | |
248 | x = copy_grouping(l->mon_grouping);\r | |
249 | if (!x)\r | |
250 | goto failed;\r | |
251 | PyDict_SetItemString(result, "mon_grouping", x);\r | |
252 | Py_XDECREF(x);\r | |
253 | RESULT_STRING(positive_sign);\r | |
254 | RESULT_STRING(negative_sign);\r | |
255 | RESULT_INT(int_frac_digits);\r | |
256 | RESULT_INT(frac_digits);\r | |
257 | RESULT_INT(p_cs_precedes);\r | |
258 | RESULT_INT(p_sep_by_space);\r | |
259 | RESULT_INT(n_cs_precedes);\r | |
260 | RESULT_INT(n_sep_by_space);\r | |
261 | RESULT_INT(p_sign_posn);\r | |
262 | RESULT_INT(n_sign_posn);\r | |
263 | return result;\r | |
264 | \r | |
265 | failed:\r | |
266 | Py_XDECREF(result);\r | |
267 | Py_XDECREF(x);\r | |
268 | return NULL;\r | |
269 | }\r | |
270 | \r | |
271 | PyDoc_STRVAR(strcoll__doc__,\r | |
272 | "string,string -> int. Compares two strings according to the locale.");\r | |
273 | \r | |
274 | static PyObject*\r | |
275 | PyLocale_strcoll(PyObject* self, PyObject* args)\r | |
276 | {\r | |
277 | #if !defined(HAVE_WCSCOLL) || !defined(Py_USING_UNICODE)\r | |
278 | char *s1,*s2;\r | |
279 | \r | |
280 | if (!PyArg_ParseTuple(args, "ss:strcoll", &s1, &s2))\r | |
281 | return NULL;\r | |
282 | return PyInt_FromLong(strcoll(s1, s2));\r | |
283 | #else\r | |
284 | PyObject *os1, *os2, *result = NULL;\r | |
285 | wchar_t *ws1 = NULL, *ws2 = NULL;\r | |
286 | int rel1 = 0, rel2 = 0, len1, len2;\r | |
287 | \r | |
288 | if (!PyArg_UnpackTuple(args, "strcoll", 2, 2, &os1, &os2))\r | |
289 | return NULL;\r | |
290 | /* If both arguments are byte strings, use strcoll. */\r | |
291 | if (PyString_Check(os1) && PyString_Check(os2))\r | |
292 | return PyInt_FromLong(strcoll(PyString_AS_STRING(os1),\r | |
293 | PyString_AS_STRING(os2)));\r | |
294 | /* If neither argument is unicode, it's an error. */\r | |
295 | if (!PyUnicode_Check(os1) && !PyUnicode_Check(os2)) {\r | |
296 | PyErr_SetString(PyExc_ValueError, "strcoll arguments must be strings");\r | |
297 | }\r | |
298 | /* Convert the non-unicode argument to unicode. */\r | |
299 | if (!PyUnicode_Check(os1)) {\r | |
300 | os1 = PyUnicode_FromObject(os1);\r | |
301 | if (!os1)\r | |
302 | return NULL;\r | |
303 | rel1 = 1;\r | |
304 | }\r | |
305 | if (!PyUnicode_Check(os2)) {\r | |
306 | os2 = PyUnicode_FromObject(os2);\r | |
307 | if (!os2) {\r | |
308 | if (rel1) {\r | |
309 | Py_DECREF(os1);\r | |
310 | }\r | |
311 | return NULL;\r | |
312 | }\r | |
313 | rel2 = 1;\r | |
314 | }\r | |
315 | /* Convert the unicode strings to wchar[]. */\r | |
316 | len1 = PyUnicode_GET_SIZE(os1) + 1;\r | |
317 | ws1 = PyMem_MALLOC(len1 * sizeof(wchar_t));\r | |
318 | if (!ws1) {\r | |
319 | PyErr_NoMemory();\r | |
320 | goto done;\r | |
321 | }\r | |
322 | if (PyUnicode_AsWideChar((PyUnicodeObject*)os1, ws1, len1) == -1)\r | |
323 | goto done;\r | |
324 | ws1[len1 - 1] = 0;\r | |
325 | len2 = PyUnicode_GET_SIZE(os2) + 1;\r | |
326 | ws2 = PyMem_MALLOC(len2 * sizeof(wchar_t));\r | |
327 | if (!ws2) {\r | |
328 | PyErr_NoMemory();\r | |
329 | goto done;\r | |
330 | }\r | |
331 | if (PyUnicode_AsWideChar((PyUnicodeObject*)os2, ws2, len2) == -1)\r | |
332 | goto done;\r | |
333 | ws2[len2 - 1] = 0;\r | |
334 | /* Collate the strings. */\r | |
335 | result = PyInt_FromLong(wcscoll(ws1, ws2));\r | |
336 | done:\r | |
337 | /* Deallocate everything. */\r | |
338 | if (ws1) PyMem_FREE(ws1);\r | |
339 | if (ws2) PyMem_FREE(ws2);\r | |
340 | if (rel1) {\r | |
341 | Py_DECREF(os1);\r | |
342 | }\r | |
343 | if (rel2) {\r | |
344 | Py_DECREF(os2);\r | |
345 | }\r | |
346 | return result;\r | |
347 | #endif\r | |
348 | }\r | |
349 | \r | |
350 | \r | |
351 | PyDoc_STRVAR(strxfrm__doc__,\r | |
352 | "string -> string. Returns a string that behaves for cmp locale-aware.");\r | |
353 | \r | |
354 | static PyObject*\r | |
355 | PyLocale_strxfrm(PyObject* self, PyObject* args)\r | |
356 | {\r | |
357 | char *s, *buf;\r | |
358 | size_t n1, n2;\r | |
359 | PyObject *result;\r | |
360 | \r | |
361 | if (!PyArg_ParseTuple(args, "s:strxfrm", &s))\r | |
362 | return NULL;\r | |
363 | \r | |
364 | /* assume no change in size, first */\r | |
365 | n1 = strlen(s) + 1;\r | |
366 | buf = PyMem_Malloc(n1);\r | |
367 | if (!buf)\r | |
368 | return PyErr_NoMemory();\r | |
369 | n2 = strxfrm(buf, s, n1) + 1;\r | |
370 | if (n2 > n1) {\r | |
371 | /* more space needed */\r | |
372 | buf = PyMem_Realloc(buf, n2);\r | |
373 | if (!buf)\r | |
374 | return PyErr_NoMemory();\r | |
375 | strxfrm(buf, s, n2);\r | |
376 | }\r | |
377 | result = PyString_FromString(buf);\r | |
378 | PyMem_Free(buf);\r | |
379 | return result;\r | |
380 | }\r | |
381 | \r | |
382 | #if defined(MS_WINDOWS)\r | |
383 | static PyObject*\r | |
384 | PyLocale_getdefaultlocale(PyObject* self)\r | |
385 | {\r | |
386 | char encoding[100];\r | |
387 | char locale[100];\r | |
388 | \r | |
389 | PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());\r | |
390 | \r | |
391 | if (GetLocaleInfo(LOCALE_USER_DEFAULT,\r | |
392 | LOCALE_SISO639LANGNAME,\r | |
393 | locale, sizeof(locale))) {\r | |
394 | Py_ssize_t i = strlen(locale);\r | |
395 | locale[i++] = '_';\r | |
396 | if (GetLocaleInfo(LOCALE_USER_DEFAULT,\r | |
397 | LOCALE_SISO3166CTRYNAME,\r | |
398 | locale+i, (int)(sizeof(locale)-i)))\r | |
399 | return Py_BuildValue("ss", locale, encoding);\r | |
400 | }\r | |
401 | \r | |
402 | /* If we end up here, this windows version didn't know about\r | |
403 | ISO639/ISO3166 names (it's probably Windows 95). Return the\r | |
404 | Windows language identifier instead (a hexadecimal number) */\r | |
405 | \r | |
406 | locale[0] = '0';\r | |
407 | locale[1] = 'x';\r | |
408 | if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,\r | |
409 | locale+2, sizeof(locale)-2)) {\r | |
410 | return Py_BuildValue("ss", locale, encoding);\r | |
411 | }\r | |
412 | \r | |
413 | /* cannot determine the language code (very unlikely) */\r | |
414 | Py_INCREF(Py_None);\r | |
415 | return Py_BuildValue("Os", Py_None, encoding);\r | |
416 | }\r | |
417 | #endif\r | |
418 | \r | |
419 | #ifdef HAVE_LANGINFO_H\r | |
420 | #define LANGINFO(X) {#X, X}\r | |
421 | static struct langinfo_constant{\r | |
422 | char* name;\r | |
423 | int value;\r | |
424 | } langinfo_constants[] =\r | |
425 | {\r | |
426 | /* These constants should exist on any langinfo implementation */\r | |
427 | LANGINFO(DAY_1),\r | |
428 | LANGINFO(DAY_2),\r | |
429 | LANGINFO(DAY_3),\r | |
430 | LANGINFO(DAY_4),\r | |
431 | LANGINFO(DAY_5),\r | |
432 | LANGINFO(DAY_6),\r | |
433 | LANGINFO(DAY_7),\r | |
434 | \r | |
435 | LANGINFO(ABDAY_1),\r | |
436 | LANGINFO(ABDAY_2),\r | |
437 | LANGINFO(ABDAY_3),\r | |
438 | LANGINFO(ABDAY_4),\r | |
439 | LANGINFO(ABDAY_5),\r | |
440 | LANGINFO(ABDAY_6),\r | |
441 | LANGINFO(ABDAY_7),\r | |
442 | \r | |
443 | LANGINFO(MON_1),\r | |
444 | LANGINFO(MON_2),\r | |
445 | LANGINFO(MON_3),\r | |
446 | LANGINFO(MON_4),\r | |
447 | LANGINFO(MON_5),\r | |
448 | LANGINFO(MON_6),\r | |
449 | LANGINFO(MON_7),\r | |
450 | LANGINFO(MON_8),\r | |
451 | LANGINFO(MON_9),\r | |
452 | LANGINFO(MON_10),\r | |
453 | LANGINFO(MON_11),\r | |
454 | LANGINFO(MON_12),\r | |
455 | \r | |
456 | LANGINFO(ABMON_1),\r | |
457 | LANGINFO(ABMON_2),\r | |
458 | LANGINFO(ABMON_3),\r | |
459 | LANGINFO(ABMON_4),\r | |
460 | LANGINFO(ABMON_5),\r | |
461 | LANGINFO(ABMON_6),\r | |
462 | LANGINFO(ABMON_7),\r | |
463 | LANGINFO(ABMON_8),\r | |
464 | LANGINFO(ABMON_9),\r | |
465 | LANGINFO(ABMON_10),\r | |
466 | LANGINFO(ABMON_11),\r | |
467 | LANGINFO(ABMON_12),\r | |
468 | \r | |
469 | #ifdef RADIXCHAR\r | |
470 | /* The following are not available with glibc 2.0 */\r | |
471 | LANGINFO(RADIXCHAR),\r | |
472 | LANGINFO(THOUSEP),\r | |
473 | /* YESSTR and NOSTR are deprecated in glibc, since they are\r | |
474 | a special case of message translation, which should be rather\r | |
475 | done using gettext. So we don't expose it to Python in the\r | |
476 | first place.\r | |
477 | LANGINFO(YESSTR),\r | |
478 | LANGINFO(NOSTR),\r | |
479 | */\r | |
480 | LANGINFO(CRNCYSTR),\r | |
481 | #endif\r | |
482 | \r | |
483 | LANGINFO(D_T_FMT),\r | |
484 | LANGINFO(D_FMT),\r | |
485 | LANGINFO(T_FMT),\r | |
486 | LANGINFO(AM_STR),\r | |
487 | LANGINFO(PM_STR),\r | |
488 | \r | |
489 | /* The following constants are available only with XPG4, but...\r | |
490 | AIX 3.2. only has CODESET.\r | |
491 | OpenBSD doesn't have CODESET but has T_FMT_AMPM, and doesn't have\r | |
492 | a few of the others.\r | |
493 | Solution: ifdef-test them all. */\r | |
494 | #ifdef CODESET\r | |
495 | LANGINFO(CODESET),\r | |
496 | #endif\r | |
497 | #ifdef T_FMT_AMPM\r | |
498 | LANGINFO(T_FMT_AMPM),\r | |
499 | #endif\r | |
500 | #ifdef ERA\r | |
501 | LANGINFO(ERA),\r | |
502 | #endif\r | |
503 | #ifdef ERA_D_FMT\r | |
504 | LANGINFO(ERA_D_FMT),\r | |
505 | #endif\r | |
506 | #ifdef ERA_D_T_FMT\r | |
507 | LANGINFO(ERA_D_T_FMT),\r | |
508 | #endif\r | |
509 | #ifdef ERA_T_FMT\r | |
510 | LANGINFO(ERA_T_FMT),\r | |
511 | #endif\r | |
512 | #ifdef ALT_DIGITS\r | |
513 | LANGINFO(ALT_DIGITS),\r | |
514 | #endif\r | |
515 | #ifdef YESEXPR\r | |
516 | LANGINFO(YESEXPR),\r | |
517 | #endif\r | |
518 | #ifdef NOEXPR\r | |
519 | LANGINFO(NOEXPR),\r | |
520 | #endif\r | |
521 | #ifdef _DATE_FMT\r | |
522 | /* This is not available in all glibc versions that have CODESET. */\r | |
523 | LANGINFO(_DATE_FMT),\r | |
524 | #endif\r | |
525 | {0, 0}\r | |
526 | };\r | |
527 | \r | |
528 | PyDoc_STRVAR(nl_langinfo__doc__,\r | |
529 | "nl_langinfo(key) -> string\n"\r | |
530 | "Return the value for the locale information associated with key.");\r | |
531 | \r | |
532 | static PyObject*\r | |
533 | PyLocale_nl_langinfo(PyObject* self, PyObject* args)\r | |
534 | {\r | |
535 | int item, i;\r | |
536 | if (!PyArg_ParseTuple(args, "i:nl_langinfo", &item))\r | |
537 | return NULL;\r | |
538 | /* Check whether this is a supported constant. GNU libc sometimes\r | |
539 | returns numeric values in the char* return value, which would\r | |
540 | crash PyString_FromString. */\r | |
541 | for (i = 0; langinfo_constants[i].name; i++)\r | |
542 | if (langinfo_constants[i].value == item) {\r | |
543 | /* Check NULL as a workaround for GNU libc's returning NULL\r | |
544 | instead of an empty string for nl_langinfo(ERA). */\r | |
545 | const char *result = nl_langinfo(item);\r | |
546 | return PyString_FromString(result != NULL ? result : "");\r | |
547 | }\r | |
548 | PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");\r | |
549 | return NULL;\r | |
550 | }\r | |
551 | #endif /* HAVE_LANGINFO_H */\r | |
552 | \r | |
553 | #ifdef HAVE_LIBINTL_H\r | |
554 | \r | |
555 | PyDoc_STRVAR(gettext__doc__,\r | |
556 | "gettext(msg) -> string\n"\r | |
557 | "Return translation of msg.");\r | |
558 | \r | |
559 | static PyObject*\r | |
560 | PyIntl_gettext(PyObject* self, PyObject *args)\r | |
561 | {\r | |
562 | char *in;\r | |
563 | if (!PyArg_ParseTuple(args, "s", &in))\r | |
564 | return 0;\r | |
565 | return PyString_FromString(gettext(in));\r | |
566 | }\r | |
567 | \r | |
568 | PyDoc_STRVAR(dgettext__doc__,\r | |
569 | "dgettext(domain, msg) -> string\n"\r | |
570 | "Return translation of msg in domain.");\r | |
571 | \r | |
572 | static PyObject*\r | |
573 | PyIntl_dgettext(PyObject* self, PyObject *args)\r | |
574 | {\r | |
575 | char *domain, *in;\r | |
576 | if (!PyArg_ParseTuple(args, "zs", &domain, &in))\r | |
577 | return 0;\r | |
578 | return PyString_FromString(dgettext(domain, in));\r | |
579 | }\r | |
580 | \r | |
581 | PyDoc_STRVAR(dcgettext__doc__,\r | |
582 | "dcgettext(domain, msg, category) -> string\n"\r | |
583 | "Return translation of msg in domain and category.");\r | |
584 | \r | |
585 | static PyObject*\r | |
586 | PyIntl_dcgettext(PyObject *self, PyObject *args)\r | |
587 | {\r | |
588 | char *domain, *msgid;\r | |
589 | int category;\r | |
590 | if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))\r | |
591 | return 0;\r | |
592 | return PyString_FromString(dcgettext(domain,msgid,category));\r | |
593 | }\r | |
594 | \r | |
595 | PyDoc_STRVAR(textdomain__doc__,\r | |
596 | "textdomain(domain) -> string\n"\r | |
597 | "Set the C library's textdmain to domain, returning the new domain.");\r | |
598 | \r | |
599 | static PyObject*\r | |
600 | PyIntl_textdomain(PyObject* self, PyObject* args)\r | |
601 | {\r | |
602 | char *domain;\r | |
603 | if (!PyArg_ParseTuple(args, "z", &domain))\r | |
604 | return 0;\r | |
605 | domain = textdomain(domain);\r | |
606 | if (!domain) {\r | |
607 | PyErr_SetFromErrno(PyExc_OSError);\r | |
608 | return NULL;\r | |
609 | }\r | |
610 | return PyString_FromString(domain);\r | |
611 | }\r | |
612 | \r | |
613 | PyDoc_STRVAR(bindtextdomain__doc__,\r | |
614 | "bindtextdomain(domain, dir) -> string\n"\r | |
615 | "Bind the C library's domain to dir.");\r | |
616 | \r | |
617 | static PyObject*\r | |
618 | PyIntl_bindtextdomain(PyObject* self,PyObject*args)\r | |
619 | {\r | |
620 | char *domain, *dirname;\r | |
621 | if (!PyArg_ParseTuple(args, "sz", &domain, &dirname))\r | |
622 | return 0;\r | |
623 | if (!strlen(domain)) {\r | |
624 | PyErr_SetString(Error, "domain must be a non-empty string");\r | |
625 | return 0;\r | |
626 | }\r | |
627 | dirname = bindtextdomain(domain, dirname);\r | |
628 | if (!dirname) {\r | |
629 | PyErr_SetFromErrno(PyExc_OSError);\r | |
630 | return NULL;\r | |
631 | }\r | |
632 | return PyString_FromString(dirname);\r | |
633 | }\r | |
634 | \r | |
635 | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET\r | |
636 | PyDoc_STRVAR(bind_textdomain_codeset__doc__,\r | |
637 | "bind_textdomain_codeset(domain, codeset) -> string\n"\r | |
638 | "Bind the C library's domain to codeset.");\r | |
639 | \r | |
640 | static PyObject*\r | |
641 | PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)\r | |
642 | {\r | |
643 | char *domain,*codeset;\r | |
644 | if (!PyArg_ParseTuple(args, "sz", &domain, &codeset))\r | |
645 | return NULL;\r | |
646 | codeset = bind_textdomain_codeset(domain, codeset);\r | |
647 | if (codeset)\r | |
648 | return PyString_FromString(codeset);\r | |
649 | Py_RETURN_NONE;\r | |
650 | }\r | |
651 | #endif\r | |
652 | \r | |
653 | #endif\r | |
654 | \r | |
655 | static struct PyMethodDef PyLocale_Methods[] = {\r | |
656 | {"setlocale", (PyCFunction) PyLocale_setlocale,\r | |
657 | METH_VARARGS, setlocale__doc__},\r | |
658 | {"localeconv", (PyCFunction) PyLocale_localeconv,\r | |
659 | METH_NOARGS, localeconv__doc__},\r | |
660 | {"strcoll", (PyCFunction) PyLocale_strcoll,\r | |
661 | METH_VARARGS, strcoll__doc__},\r | |
662 | {"strxfrm", (PyCFunction) PyLocale_strxfrm,\r | |
663 | METH_VARARGS, strxfrm__doc__},\r | |
664 | #if defined(MS_WINDOWS)\r | |
665 | {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS},\r | |
666 | #endif\r | |
667 | #ifdef HAVE_LANGINFO_H\r | |
668 | {"nl_langinfo", (PyCFunction) PyLocale_nl_langinfo,\r | |
669 | METH_VARARGS, nl_langinfo__doc__},\r | |
670 | #endif\r | |
671 | #ifdef HAVE_LIBINTL_H\r | |
672 | {"gettext",(PyCFunction)PyIntl_gettext,METH_VARARGS,\r | |
673 | gettext__doc__},\r | |
674 | {"dgettext",(PyCFunction)PyIntl_dgettext,METH_VARARGS,\r | |
675 | dgettext__doc__},\r | |
676 | {"dcgettext",(PyCFunction)PyIntl_dcgettext,METH_VARARGS,\r | |
677 | dcgettext__doc__},\r | |
678 | {"textdomain",(PyCFunction)PyIntl_textdomain,METH_VARARGS,\r | |
679 | textdomain__doc__},\r | |
680 | {"bindtextdomain",(PyCFunction)PyIntl_bindtextdomain,METH_VARARGS,\r | |
681 | bindtextdomain__doc__},\r | |
682 | #ifdef HAVE_BIND_TEXTDOMAIN_CODESET\r | |
683 | {"bind_textdomain_codeset",(PyCFunction)PyIntl_bind_textdomain_codeset,\r | |
684 | METH_VARARGS, bind_textdomain_codeset__doc__},\r | |
685 | #endif\r | |
686 | #endif\r | |
687 | {NULL, NULL}\r | |
688 | };\r | |
689 | \r | |
690 | PyMODINIT_FUNC\r | |
691 | init_locale(void)\r | |
692 | {\r | |
693 | PyObject *m, *d, *x;\r | |
694 | #ifdef HAVE_LANGINFO_H\r | |
695 | int i;\r | |
696 | #endif\r | |
697 | \r | |
698 | m = Py_InitModule("_locale", PyLocale_Methods);\r | |
699 | if (m == NULL)\r | |
700 | return;\r | |
701 | \r | |
702 | d = PyModule_GetDict(m);\r | |
703 | \r | |
704 | x = PyInt_FromLong(LC_CTYPE);\r | |
705 | PyDict_SetItemString(d, "LC_CTYPE", x);\r | |
706 | Py_XDECREF(x);\r | |
707 | \r | |
708 | x = PyInt_FromLong(LC_TIME);\r | |
709 | PyDict_SetItemString(d, "LC_TIME", x);\r | |
710 | Py_XDECREF(x);\r | |
711 | \r | |
712 | x = PyInt_FromLong(LC_COLLATE);\r | |
713 | PyDict_SetItemString(d, "LC_COLLATE", x);\r | |
714 | Py_XDECREF(x);\r | |
715 | \r | |
716 | x = PyInt_FromLong(LC_MONETARY);\r | |
717 | PyDict_SetItemString(d, "LC_MONETARY", x);\r | |
718 | Py_XDECREF(x);\r | |
719 | \r | |
720 | #ifdef LC_MESSAGES\r | |
721 | x = PyInt_FromLong(LC_MESSAGES);\r | |
722 | PyDict_SetItemString(d, "LC_MESSAGES", x);\r | |
723 | Py_XDECREF(x);\r | |
724 | #endif /* LC_MESSAGES */\r | |
725 | \r | |
726 | x = PyInt_FromLong(LC_NUMERIC);\r | |
727 | PyDict_SetItemString(d, "LC_NUMERIC", x);\r | |
728 | Py_XDECREF(x);\r | |
729 | \r | |
730 | x = PyInt_FromLong(LC_ALL);\r | |
731 | PyDict_SetItemString(d, "LC_ALL", x);\r | |
732 | Py_XDECREF(x);\r | |
733 | \r | |
734 | x = PyInt_FromLong(CHAR_MAX);\r | |
735 | PyDict_SetItemString(d, "CHAR_MAX", x);\r | |
736 | Py_XDECREF(x);\r | |
737 | \r | |
738 | Error = PyErr_NewException("locale.Error", NULL, NULL);\r | |
739 | PyDict_SetItemString(d, "Error", Error);\r | |
740 | \r | |
741 | x = PyString_FromString(locale__doc__);\r | |
742 | PyDict_SetItemString(d, "__doc__", x);\r | |
743 | Py_XDECREF(x);\r | |
744 | \r | |
745 | #ifdef HAVE_LANGINFO_H\r | |
746 | for (i = 0; langinfo_constants[i].name; i++) {\r | |
747 | PyModule_AddIntConstant(m, langinfo_constants[i].name,\r | |
748 | langinfo_constants[i].value);\r | |
749 | }\r | |
750 | #endif\r | |
751 | }\r | |
752 | \r | |
753 | /*\r | |
754 | Local variables:\r | |
755 | c-basic-offset: 4\r | |
756 | indent-tabs-mode: nil\r | |
757 | End:\r | |
758 | */\r |