[Python-checkins] CVS: python/dist/src/Objects unicodeobject.c,2.3,2.4
Guido van Rossum
python-dev@python.org
Mon, 27 Mar 2000 21:01:55 -0500 (EST)
Update of /projects/cvsroot/python/dist/src/Objects
In directory eric:/home/guido/hp/mal/py-patched/Objects
Modified Files:
unicodeobject.c
Log Message:
MBCS codecs. (Win32 only.) By Mark Hammond.
Index: unicodeobject.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.3
retrieving revision 2.4
diff -C2 -r2.3 -r2.4
*** unicodeobject.c 2000/03/20 16:36:44 2.3
--- unicodeobject.c 2000/03/28 02:01:52 2.4
***************
*** 74,77 ****
--- 74,80 ----
#endif
+ #ifdef MS_WIN32
+ #include <windows.h>
+ #endif
/* Limit for the Unicode object free list */
***************
*** 1479,1482 ****
--- 1482,1541 ----
NULL);
}
+
+ #ifdef MS_WIN32
+ /* --- MBCS codecs for Windows -------------------------------------------- */
+ PyObject *PyUnicode_DecodeMBCS(const char *s,
+ int size,
+ const char *errors)
+ {
+ PyUnicodeObject *v;
+ Py_UNICODE *p;
+
+ /* First get the size of the result */
+ DWORD usize = MultiByteToWideChar(CP_ACP, 0, s, size, NULL, 0);
+ if (usize==0)
+ return PyErr_SetFromWindowsErrWithFilename(0, NULL);
+
+ v = _PyUnicode_New(usize);
+ if (v == NULL)
+ return NULL;
+ if (usize == 0)
+ return (PyObject *)v;
+ p = PyUnicode_AS_UNICODE(v);
+ if (0 == MultiByteToWideChar(CP_ACP, 0, s, size, p, usize)) {
+ Py_DECREF(v);
+ return PyErr_SetFromWindowsErrWithFilename(0, NULL);
+ }
+
+ return (PyObject *)v;
+ }
+
+ PyObject *PyUnicode_EncodeMBCS(const Py_UNICODE *p,
+ int size,
+ const char *errors)
+ {
+ PyObject *repr;
+ char *s;
+
+ /* First get the size of the result */
+ DWORD mbcssize = WideCharToMultiByte(CP_ACP, 0, p, size, NULL, 0, NULL, NULL);
+ if (mbcssize==0)
+ return PyErr_SetFromWindowsErrWithFilename(0, NULL);
+
+ repr = PyString_FromStringAndSize(NULL, mbcssize);
+ if (repr == NULL)
+ return NULL;
+ if (mbcssize==0)
+ return repr;
+
+ /* Do the conversion */
+ s = PyString_AS_STRING(repr);
+ if (0 == WideCharToMultiByte(CP_ACP, 0, p, size, s, mbcssize, NULL, NULL)) {
+ Py_DECREF(repr);
+ return PyErr_SetFromWindowsErrWithFilename(0, NULL);
+ }
+ return repr;
+ }
+ #endif /* MS_WIN32 */
/* --- Character Mapping Codec -------------------------------------------- */