- added _getdefaultlocale implementation for WIN32

- ansified, reindentified, spacified, nullified
This commit is contained in:
Fredrik Lundh 2000-07-08 19:57:37 +00:00
parent ddbc11893f
commit 8f017a01f8
1 changed files with 349 additions and 267 deletions

View File

@ -6,6 +6,7 @@ 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>
@ -14,9 +15,16 @@ This software comes with no warranty. Use at your own risk.
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include "Python.h"
#if defined(MS_WIN32)
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef macintosh
char *strdup Py_PROTO((char *));
char *strdup(char *);
#endif
static char locale__doc__[] = "Support for POSIX locales.";
@ -30,96 +38,110 @@ static char setlocale__doc__[]=
;
/* to record the LC_NUMERIC settings */
static PyObject* grouping=0;
static PyObject* thousands_sep=0;
static PyObject* decimal_point=0;
static PyObject* grouping = NULL;
static PyObject* thousands_sep = NULL;
static PyObject* decimal_point = NULL;
/* if non-null, indicates that LC_NUMERIC is different from "C" */
static char* saved_numeric=0;
static char* saved_numeric = NULL;
/* the grouping is terminated by either 0 or CHAR_MAX */
static PyObject*
copy_grouping(s)
char* s;
copy_grouping(char* s)
{
int i;
PyObject *result,*val=0;
PyObject *result, *val = NULL;
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 */;
; /* nothing */
result = PyList_New(i+1);
if(!result)return NULL;
if (!result)
return NULL;
i = -1;
do {
i++;
val = PyInt_FromLong(s[i]);
if(!val)break;
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()
fixup_ulcase(void)
{
PyObject *mods, *strop, *string, *ulo;
unsigned char ul[256];
int n, c;
/* finding sys.modules */
/* find the string and strop modules */
mods = PyImport_GetModuleDict();
if(!mods)return;
/* finding the module */
if (!mods)
return;
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 */
if (!string && !strop)
return;
/* create uppercase map string */
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(!ulo)
return;
if (string)
PyDict_SetItemString(string, "uppercase", ulo);
if (strop)
PyDict_SetItemString(strop, "uppercase", ulo);
Py_DECREF(ulo);
/* create lowercase */
/* create lowercase string */
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 (!ulo)
return;
if (string)
PyDict_SetItemString(string, "lowercase", ulo);
if (strop)
PyDict_SetItemString(strop, "lowercase", ulo);
Py_DECREF(ulo);
/* create letters */
/* create letters string */
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 (!ulo)
return;
if (string)
PyDict_SetItemString(string, "letters", ulo);
Py_DECREF(ulo);
@ -127,15 +149,16 @@ fixup_ulcase()
static PyObject*
PyLocale_setlocale(self,args)
PyObject *self;
PyObject *args;
PyLocale_setlocale(PyObject* self, PyObject* args)
{
int category;
char *locale=0,*result;
char *locale = NULL, *result;
PyObject *result_object;
struct lconv *lc;
if(!PyArg_ParseTuple(args,"i|z:setlocale",&category,&locale))return 0;
if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))
return 0;
if (locale) {
/* set locale */
result = setlocale(category, locale);
@ -145,13 +168,15 @@ PyLocale_setlocale(self,args)
return NULL;
}
result_object = PyString_FromString(result);
if(!result)return NULL;
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;
if (saved_numeric)
free(saved_numeric);
saved_numeric = NULL;
} else {
/* remember values */
lc = localeconv();
@ -162,7 +187,6 @@ PyLocale_setlocale(self,args)
Py_XDECREF(decimal_point);
decimal_point = PyString_FromString(lc->decimal_point);
saved_numeric = strdup(locale);
/* restore to "C" */
setlocale(LC_NUMERIC, "C");
}
@ -195,24 +219,36 @@ static char localeconv__doc__[]=
;
static PyObject*
PyLocale_localeconv(self,args)
PyObject *self;
PyObject *args;
PyLocale_localeconv(PyObject* self, PyObject* args)
{
PyObject* result;
struct lconv *l;
PyObject *x;
if(!PyArg_NoArgs(args))return 0;
if (!PyArg_NoArgs(args))
return 0;
result = PyDict_New();
if(!result)return 0;
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)
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)
x = PyInt_FromLong(l->i);\
if (!x) goto failed;\
PyDict_SetItemString(result, #i, x);\
Py_XDECREF(x)
/* Numeric information */
if (saved_numeric){
@ -224,7 +260,8 @@ PyLocale_localeconv(self,args)
RESULT_STRING(decimal_point);
RESULT_STRING(thousands_sep);
x = copy_grouping(l->grouping);
if(!x)goto failed;
if (!x)
goto failed;
PyDict_SetItemString(result, "grouping", x);
Py_XDECREF(x);
}
@ -235,7 +272,8 @@ PyLocale_localeconv(self,args)
RESULT_STRING(mon_decimal_point);
RESULT_STRING(mon_thousands_sep);
x = copy_grouping(l->mon_grouping);
if(!x)goto failed;
if (!x)
goto failed;
PyDict_SetItemString(result, "mon_grouping", x);
Py_XDECREF(x);
RESULT_STRING(positive_sign);
@ -248,8 +286,8 @@ PyLocale_localeconv(self,args)
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);
@ -261,11 +299,10 @@ static char strcoll__doc__[]=
;
static PyObject*
PyLocale_strcoll(self,args)
PyObject *self;
PyObject *args;
PyLocale_strcoll(PyObject* self, PyObject* args)
{
char *s1,*s2;
if (!PyArg_ParseTuple(args, "ss:strcoll", &s1, &s2))
return NULL;
return PyInt_FromLong(strcoll(s1, s2));
@ -276,24 +313,26 @@ static char strxfrm__doc__[]=
;
static PyObject*
PyLocale_strxfrm(self,args)
PyObject* self;
PyObject* args;
PyLocale_strxfrm(PyObject* self, PyObject* args)
{
char *s, *buf;
size_t n1, n2;
PyObject *result;
if(!PyArg_ParseTuple(args, "s:strxfrm", &s))
return NULL;
/* assume no change in size, first */
n1 = strlen(s) + 1;
buf = PyMem_Malloc(n1);
if(!buf)return PyErr_NoMemory();
if (!buf)
return PyErr_NoMemory();
n2 = strxfrm(buf, s, n1);
if (n2 > n1) {
/* more space needed */
buf = PyMem_Realloc(buf, n2);
if(!buf)return PyErr_NoMemory();
if (!buf)
return PyErr_NoMemory();
strxfrm(buf, s, n2);
}
result = PyString_FromString(buf);
@ -301,11 +340,51 @@ PyLocale_strxfrm(self,args)
return result;
}
#if defined(MS_WIN32)
static PyObject*
PyLocale_getdefaultlocale(PyObject* self, PyObject* args)
{
char encoding[100];
char locale[100];
sprintf(encoding, "cp%d", GetACP());
if (GetLocaleInfo(LOCALE_USER_DEFAULT,
LOCALE_SISO639LANGNAME,
locale, sizeof(locale))) {
int i = strlen(locale);
locale[i++] = '_';
if (GetLocaleInfo(LOCALE_USER_DEFAULT,
LOCALE_SISO3166CTRYNAME,
locale+i, sizeof(locale)-i))
return Py_BuildValue("ss", locale, encoding);
}
/* If we end up here, this windows version didn't know about
ISO639/ISO3166 names (it's probably Windows 95). Return the
Windows language identifier instead (a hexadecimal number) */
locale[0] = '0';
locale[1] = 'x';
if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE,
locale+2, sizeof(locale)-2)) {
return Py_BuildValue("ss", locale, encoding);
}
/* cannot determine the language code (very unlikely) */
Py_INCREF(Py_None);
return Py_BuildValue("Os", Py_None, encoding);
}
#endif
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__},
#if defined(MS_WIN32)
{"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, 1},
#endif
{NULL, NULL}
};
@ -313,8 +392,11 @@ DL_EXPORT(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);