cinap_lenrekadd hg and python4bb9e2215 years agoHistory
788 lines · 19.8 KB · C
RawDownload
/***********************************************************Copyright (C) 1997, 2002, 2003 Martin von Loewis Permission to use, copy, modify, and distribute this software and itsdocumentation 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 "Python.h" #include <stdio.h>#include <locale.h>#include <string.h>#include <ctype.h> #ifdef HAVE_ERRNO_H#include <errno.h>#endif #ifdef HAVE_LANGINFO_H#include <langinfo.h>#endif #ifdef HAVE_LIBINTL_H#include <libintl.h>#endif #ifdef HAVE_WCHAR_H#include <wchar.h>#endif #if defined(__APPLE__)#include <CoreFoundation/CoreFoundation.h>#endif #if defined(MS_WINDOWS)#define WIN32_LEAN_AND_MEAN#include <windows.h>#endif #ifdef RISCOSchar *strdup(const char *);#endif PyDoc_STRVAR(locale__doc__, "Support for POSIX locales."); static PyObject *Error; /* support functions for formatting floating point numbers */ PyDoc_STRVAR(setlocale__doc__,"(integer,string=None) -> string. Activates/queries locale processing."); /* the grouping is terminated by either 0 or CHAR_MAX */static PyObject*copy_grouping(char* s){    int i;    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 */     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 = NULL;            break;        }    } while (s[i] != '\0' && s[i] != CHAR_MAX);     if (!val) {        Py_DECREF(result);        return NULL;    }     return result;} static voidfixup_ulcase(void){    PyObject *mods, *strop, *string, *ulo;    unsigned char ul[256];    int n, c;     /* find the string and strop modules */    mods = PyImport_GetModuleDict();    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 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 (string)        PyDict_SetItemString(string, "uppercase", ulo);    if (strop)        PyDict_SetItemString(strop, "uppercase", ulo);    Py_DECREF(ulo);     /* 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 (string)        PyDict_SetItemString(string, "lowercase", ulo);    if (strop)        PyDict_SetItemString(strop, "lowercase", ulo);    Py_DECREF(ulo);     /* 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 (string)        PyDict_SetItemString(string, "letters", ulo);    Py_DECREF(ulo);} static PyObject*PyLocale_setlocale(PyObject* self, PyObject* args){    int category;    char *locale = NULL, *result;    PyObject *result_object;     if (!PyArg_ParseTuple(args, "i|z:setlocale", &category, &locale))        return NULL;     if (locale) {        /* set locale */        result = setlocale(category, locale);        if (!result) {            /* operation failed, no setting was changed */            PyErr_SetString(Error, "unsupported locale setting");            return NULL;        }        result_object = PyString_FromString(result);        if (!result_object)            return NULL;        /* 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 */        result = setlocale(category, NULL);        if (!result) {            PyErr_SetString(Error, "locale query failed");            return NULL;        }        result_object = PyString_FromString(result);    }    return result_object;} PyDoc_STRVAR(localeconv__doc__,"() -> dict. Returns numeric and monetary locale-specific parameters."); static PyObject*PyLocale_localeconv(PyObject* self){    PyObject* result;    struct lconv *l;    PyObject *x;     result = PyDict_New();    if (!result)        return NULL;     /* 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 */    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;} PyDoc_STRVAR(strcoll__doc__,"string,string -> int. Compares two strings according to the locale."); static PyObject*PyLocale_strcoll(PyObject* self, PyObject* args){#if !defined(HAVE_WCSCOLL) || !defined(Py_USING_UNICODE)    char *s1,*s2;        if (!PyArg_ParseTuple(args, "ss:strcoll", &s1, &s2))        return NULL;    return PyInt_FromLong(strcoll(s1, s2));#else    PyObject *os1, *os2, *result = NULL;    wchar_t *ws1 = NULL, *ws2 = NULL;    int rel1 = 0, rel2 = 0, len1, len2;        if (!PyArg_UnpackTuple(args, "strcoll", 2, 2, &os1, &os2))        return NULL;    /* If both arguments are byte strings, use strcoll.  */    if (PyString_Check(os1) && PyString_Check(os2))        return PyInt_FromLong(strcoll(PyString_AS_STRING(os1),                                      PyString_AS_STRING(os2)));    /* If neither argument is unicode, it's an error.  */    if (!PyUnicode_Check(os1) && !PyUnicode_Check(os2)) {        PyErr_SetString(PyExc_ValueError, "strcoll arguments must be strings");    }    /* Convert the non-unicode argument to unicode. */    if (!PyUnicode_Check(os1)) {        os1 = PyUnicode_FromObject(os1);        if (!os1)            return NULL;        rel1 = 1;    }    if (!PyUnicode_Check(os2)) {        os2 = PyUnicode_FromObject(os2);        if (!os2) {            Py_DECREF(os1);            return NULL;        }         rel2 = 1;    }    /* Convert the unicode strings to wchar[]. */    len1 = PyUnicode_GET_SIZE(os1) + 1;    ws1 = PyMem_MALLOC(len1 * sizeof(wchar_t));    if (!ws1) {        PyErr_NoMemory();        goto done;    }    if (PyUnicode_AsWideChar((PyUnicodeObject*)os1, ws1, len1) == -1)        goto done;    ws1[len1 - 1] = 0;    len2 = PyUnicode_GET_SIZE(os2) + 1;    ws2 = PyMem_MALLOC(len2 * sizeof(wchar_t));    if (!ws2) {        PyErr_NoMemory();        goto done;    }    if (PyUnicode_AsWideChar((PyUnicodeObject*)os2, ws2, len2) == -1)        goto done;    ws2[len2 - 1] = 0;    /* Collate the strings. */    result = PyInt_FromLong(wcscoll(ws1, ws2));  done:    /* Deallocate everything. */    if (ws1) PyMem_FREE(ws1);    if (ws2) PyMem_FREE(ws2);    if (rel1) {        Py_DECREF(os1);    }    if (rel2) {        Py_DECREF(os2);    }    return result;#endif}  PyDoc_STRVAR(strxfrm__doc__,"string -> string. Returns a string that behaves for cmp locale-aware."); static PyObject*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();    n2 = strxfrm(buf, s, n1) + 1;    if (n2 > n1) {        /* more space needed */        buf = PyMem_Realloc(buf, n2);        if (!buf)            return PyErr_NoMemory();        strxfrm(buf, s, n2);    }    result = PyString_FromString(buf);    PyMem_Free(buf);    return result;} #if defined(PLAN9APE)static PyObject*PyLocale_getdefaultlocale(PyObject *self){    return Py_BuildValue("ss", Py_None, "UTF-8");}#endif #if defined(MS_WINDOWS)static PyObject*PyLocale_getdefaultlocale(PyObject* self){    char encoding[100];    char locale[100];     PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP());     if (GetLocaleInfo(LOCALE_USER_DEFAULT,                      LOCALE_SISO639LANGNAME,                      locale, sizeof(locale))) {        Py_ssize_t i = strlen(locale);        locale[i++] = '_';        if (GetLocaleInfo(LOCALE_USER_DEFAULT,                          LOCALE_SISO3166CTRYNAME,                          locale+i, (int)(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 #if defined(__APPLE__)/*** Find out what the current script is.** Donated by Fredrik Lundh.*/static char *mac_getscript(void){    CFStringEncoding enc = CFStringGetSystemEncoding();    static CFStringRef name = NULL;    /* Return the code name for the encodings for which we have codecs. */    switch(enc) {    case kCFStringEncodingMacRoman: return "mac-roman";    case kCFStringEncodingMacGreek: return "mac-greek";    case kCFStringEncodingMacCyrillic: return "mac-cyrillic";    case kCFStringEncodingMacTurkish: return "mac-turkish";    case kCFStringEncodingMacIcelandic: return "mac-icelandic";    /* XXX which one is mac-latin2? */    }    if (!name) {        /* This leaks an object. */        name = CFStringConvertEncodingToIANACharSetName(enc);    }    return (char *)CFStringGetCStringPtr(name, 0); } static PyObject*PyLocale_getdefaultlocale(PyObject* self){    return Py_BuildValue("Os", Py_None, mac_getscript());}#endif #ifdef HAVE_LANGINFO_H#define LANGINFO(X) {#X, X}struct langinfo_constant{	char* name;	int value;} langinfo_constants[] = {    /* These constants should exist on any langinfo implementation */    LANGINFO(DAY_1),    LANGINFO(DAY_2),    LANGINFO(DAY_3),    LANGINFO(DAY_4),    LANGINFO(DAY_5),    LANGINFO(DAY_6),    LANGINFO(DAY_7),     LANGINFO(ABDAY_1),    LANGINFO(ABDAY_2),    LANGINFO(ABDAY_3),    LANGINFO(ABDAY_4),    LANGINFO(ABDAY_5),    LANGINFO(ABDAY_6),    LANGINFO(ABDAY_7),     LANGINFO(MON_1),    LANGINFO(MON_2),    LANGINFO(MON_3),    LANGINFO(MON_4),    LANGINFO(MON_5),    LANGINFO(MON_6),    LANGINFO(MON_7),    LANGINFO(MON_8),    LANGINFO(MON_9),    LANGINFO(MON_10),    LANGINFO(MON_11),    LANGINFO(MON_12),     LANGINFO(ABMON_1),    LANGINFO(ABMON_2),    LANGINFO(ABMON_3),    LANGINFO(ABMON_4),    LANGINFO(ABMON_5),    LANGINFO(ABMON_6),    LANGINFO(ABMON_7),    LANGINFO(ABMON_8),    LANGINFO(ABMON_9),    LANGINFO(ABMON_10),    LANGINFO(ABMON_11),

Lines 1–500 of 788.