static PyObject *t_resourcebundle_get(t_resourcebundle *self, PyObject *arg) { UErrorCode status = U_ZERO_ERROR; charsArg key; int i; if (!parseArg(arg, "i", &i)) { ResourceBundle rb = self->object->get(i, status); if (U_FAILURE(status)) return ICUException(status).reportError(); return wrap_ResourceBundle(rb); } if (!parseArg(arg, "n", &key)) { ResourceBundle rb = self->object->get(key, status); if (U_FAILURE(status)) return ICUException(status).reportError(); return wrap_ResourceBundle(rb); } return PyErr_SetArgsError((PyObject *) self, "get", arg); }
static PyObject *t_regexpattern_matcher(t_regexpattern *self, PyObject *args) { UnicodeString *u; RegexMatcher *matcher; PyObject *input = NULL; switch (PyTuple_Size(args)) { case 0: STATUS_CALL(matcher = self->object->matcher(status)); return wrap_RegexMatcher(matcher, (PyObject *) self, input); case 1: if (!parseArgs(args, "W", &u, &input)) { UErrorCode status = U_ZERO_ERROR; matcher = self->object->matcher(*u, status); if (U_FAILURE(status)) { Py_XDECREF(input); return ICUException(status).reportError(); } return wrap_RegexMatcher(matcher, (PyObject *) self, input); } break; } return PyErr_SetArgsError((PyObject *) self, "matcher", args); }
static PyObject *t_resourcebundle_setAppData(PyTypeObject *type, PyObject *args) { charsArg packageName, path; if (!parseArgs(args, "nf", &packageName, &path)) { HANDLE fd = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); UErrorCode status = U_ZERO_ERROR; DWORD dwSize; HANDLE hMap; LPVOID data; if (fd == INVALID_HANDLE_VALUE) return PyErr_SetFromWindowsErrWithFilename(0, path); dwSize = GetFileSize(fd, NULL); if (dwSize == INVALID_FILE_SIZE) { PyErr_SetFromWindowsErrWithFilename(0, path); CloseHandle(fd); return NULL; } hMap = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, dwSize, NULL); if (!hMap) { PyErr_SetFromWindowsErrWithFilename(0, path); CloseHandle(fd); return NULL; } CloseHandle(fd); data = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0); if (!data) { PyErr_SetFromWindowsErrWithFilename(0, path); CloseHandle(hMap); return NULL; } CloseHandle(hMap); udata_setAppData(packageName, data, &status); if (U_FAILURE(status)) { UnmapViewOfFile(data); return ICUException(status).reportError(); } Py_RETURN_NONE; } return PyErr_SetArgsError(type, "setAppData", args); }
static PyObject *t_resourcebundle_getNext(t_resourcebundle *self) { UErrorCode status = U_ZERO_ERROR; ResourceBundle rb = self->object->getNext(status); if (U_FAILURE(status)) return ICUException(status).reportError(); return wrap_ResourceBundle(rb); }
static PyObject *t_regexpattern_compile(PyTypeObject *type, PyObject *args) { UnicodeString *u; uint32_t flags; RegexPattern *pattern; PyObject *re = NULL; switch (PyTuple_Size(args)) { case 1: if (!parseArgs(args, "W", &u, &re)) { UErrorCode status = U_ZERO_ERROR; UParseError parseError; pattern = RegexPattern::compile(*u, parseError, status); if (U_FAILURE(status)) { Py_XDECREF(re); return ICUException(parseError, status).reportError(); } return wrap_RegexPattern(pattern, re); } break; case 2: if (!parseArgs(args, "Wi", &u, &re, &flags)) { UErrorCode status = U_ZERO_ERROR; UParseError parseError; pattern = RegexPattern::compile(*u, flags, parseError, status); if (U_FAILURE(status)) { Py_XDECREF(re); return ICUException(parseError, status).reportError(); } return wrap_RegexPattern(pattern, re); } break; } return PyErr_SetArgsError(type, "compile", args); }
EXPORT UnicodeString &PyObject_AsUnicodeString(PyObject *object, const char *encoding, const char *mode, UnicodeString &string) { if (PyUnicode_Check(object)) { if (sizeof(Py_UNICODE) == sizeof(UChar)) string.setTo((const UChar *) PyUnicode_AS_UNICODE(object), (int32_t) PyUnicode_GET_SIZE(object)); else { int32_t len = (int32_t) PyUnicode_GET_SIZE(object); Py_UNICODE *pchars = PyUnicode_AS_UNICODE(object); UChar *chars = new UChar[len * 3]; UErrorCode status = U_ZERO_ERROR; int32_t dstLen; u_strFromUTF32(chars, len * 3, &dstLen, (const UChar32 *) pchars, len, &status); if (U_FAILURE(status)) { delete[] chars; throw ICUException(status); } string.setTo((const UChar *) chars, (int32_t) dstLen); delete[] chars; } } else if (PyBytes_Check(object)) PyBytes_AsUnicodeString(object, encoding, mode, string); else { PyErr_SetObject(PyExc_TypeError, object); throw ICUException(); } return string; }
static PyObject *t_resourcebundle_next(t_resourcebundle *self) { if (self->object->hasNext()) { UErrorCode status = U_ZERO_ERROR; ResourceBundle rb = self->object->getNext(status); if (U_FAILURE(status)) return ICUException(status).reportError(); return wrap_ResourceBundle(rb); } PyErr_SetNone(PyExc_StopIteration); return NULL; }
static PyObject *t_resourcebundle_setAppData(PyTypeObject *type, PyObject *args) { charsArg packageName, path; if (!parseArgs(args, "nf", &packageName, &path)) { int fd = open(path, O_RDONLY); UErrorCode status = U_ZERO_ERROR; struct stat buf; void *data; if (fd < 0) return PyErr_SetFromErrnoWithFilename(PyExc_ValueError, path); if (fstat(fd, &buf) < 0) { PyErr_SetFromErrnoWithFilename(PyExc_ValueError, path); close(fd); return NULL; } data = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0); if (data == MAP_FAILED) { PyErr_SetFromErrnoWithFilename(PyExc_ValueError, path); close(fd); return NULL; } close(fd); udata_setAppData(packageName, data, &status); if (U_FAILURE(status)) { munmap(data, buf.st_size); return ICUException(status).reportError(); } Py_RETURN_NONE; } return PyErr_SetArgsError(type, "setAppData", args); }
static int t_rulebasedbreakiterator_init(t_rulebasedbreakiterator *self, PyObject *args, PyObject *kwds) { UnicodeString *u, _u; charsArg path, name; switch (PyTuple_Size(args)) { case 0: self->object = new RuleBasedBreakIterator(); self->flags = T_OWNED; break; case 1: if (!parseArgs(args, "S", &u, &_u)) { RuleBasedBreakIterator *iterator; INT_STATUS_PARSER_CALL(iterator = new RuleBasedBreakIterator(*u, parseError, status)); self->object = iterator; self->flags = T_OWNED; break; } PyErr_SetArgsError((PyObject *) self, "__init__", args); return -1; case 2: if (!parseArg(args, "fn", &path, &name)) { RuleBasedBreakIterator *iterator; UDataMemory *data; UErrorCode status; status = U_ZERO_ERROR; data = udata_open(path, NULL, name, &status); if (U_FAILURE(status)) { ICUException(status).reportError(); return -1; } status = U_ZERO_ERROR; iterator = new RuleBasedBreakIterator(data, status); if (U_FAILURE(status)) { udata_close(data); ICUException(status).reportError(); return -1; } self->object = iterator; self->flags = T_OWNED; break; } PyErr_SetArgsError((PyObject *) self, "__init__", args); return -1; default: PyErr_SetArgsError((PyObject *) self, "__init__", args); return -1; } if (self->object) return 0; return -1; }
EXPORT UDate PyObject_AsUDate(PyObject *object) { if (PyFloat_CheckExact(object)) return (UDate) (PyFloat_AsDouble(object) * 1000.0); else { if (PyDateTime_CheckExact(object)) { PyObject *tzinfo = PyObject_GetAttrString(object, "tzinfo"); PyObject *utcoffset, *ordinal; if (tzinfo == Py_None) { PyObject *m = PyImport_ImportModule("icu"); PyObject *cls = PyObject_GetAttrString(m, "ICUtzinfo"); tzinfo = PyObject_CallMethodObjArgs(cls, getDefault_NAME, NULL); Py_DECREF(cls); Py_DECREF(m); utcoffset = PyObject_CallMethodObjArgs(tzinfo, utcoffset_NAME, object, NULL); Py_DECREF(tzinfo); } else { utcoffset = PyObject_CallMethodObjArgs(object, utcoffset_NAME, NULL); Py_DECREF(tzinfo); } ordinal = PyObject_CallMethodObjArgs(object, toordinal_NAME, NULL); if (utcoffset != NULL && PyDelta_CheckExact(utcoffset) && ordinal != NULL && PyInt_CheckExact(ordinal)) { #if PY_MAJOR_VERSION >= 3 double ordinalValue = PyLong_AsDouble(ordinal); #else long ordinalValue = PyInt_AsLong(ordinal); #endif double timestamp = (ordinalValue - 719163) * 86400.0 + PyDateTime_DATE_GET_HOUR(object) * 3600.0 + PyDateTime_DATE_GET_MINUTE(object) * 60.0 + (double) PyDateTime_DATE_GET_SECOND(object) + PyDateTime_DATE_GET_MICROSECOND(object) / 1e6 - #ifndef PYPY_VERSION (((PyDateTime_Delta *) utcoffset)->days * 86400.0 + (double) ((PyDateTime_Delta *) utcoffset)->seconds); #else (PyDateTime_DELTA_GET_DAYS( (PyDateTime_Delta *) utcoffset) * 86400.0 + (double) PyDateTime_DELTA_GET_SECONDS( (PyDateTime_Delta *) utcoffset)); #endif Py_DECREF(utcoffset); Py_DECREF(ordinal); return (UDate) (timestamp * 1000.0); } Py_XDECREF(utcoffset); Py_XDECREF(ordinal); } } PyErr_SetObject(PyExc_TypeError, object); throw ICUException(); }
EXPORT UnicodeString &PyBytes_AsUnicodeString(PyObject *object, const char *encoding, const char *mode, UnicodeString &string) { UErrorCode status = U_ZERO_ERROR; UConverter *conv = ucnv_open(encoding, &status); if (U_FAILURE(status)) throw ICUException(status); _STOPReason stop; char *src; Py_ssize_t len; UChar *buffer, *target; memset(&stop, 0, sizeof(stop)); if (!strcmp(mode, "strict")) { ucnv_setToUCallBack(conv, _stopDecode, &stop, NULL, NULL, &status); if (U_FAILURE(status)) { ucnv_close(conv); throw ICUException(status); } } PyBytes_AsStringAndSize(object, &src, &len); stop.src = src; stop.src_length = len; buffer = target = new UChar[len]; if (buffer == NULL) { ucnv_close(conv); PyErr_NoMemory(); throw ICUException(); } ucnv_toUnicode(conv, &target, target + len, (const char **) &src, src + len, NULL, true, &status); if (U_FAILURE(status)) { const char *reasonName; switch (stop.reason) { case UCNV_UNASSIGNED: reasonName = "the code point is unassigned"; break; case UCNV_ILLEGAL: reasonName = "the code point is illegal"; break; case UCNV_IRREGULAR: reasonName = "the code point is not a regular sequence in the encoding"; break; default: reasonName = "unexpected reason code"; break; } status = U_ZERO_ERROR; PyErr_Format(PyExc_ValueError, "'%s' codec can't decode byte 0x%x in position %d: reason code %d (%s)", ucnv_getName(conv, &status), (int) (unsigned char) stop.chars[0], stop.error_position, stop.reason, reasonName); delete[] buffer; ucnv_close(conv); throw ICUException(); } string.setTo(buffer, target - buffer); delete[] buffer; ucnv_close(conv); return string; }