[Thuban-commits] r2799 - in branches/WIP-pyshapelib-Unicode/thuban: . libraries/pyshapelib libraries/shapelib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Jan 3 01:49:23 CET 2008


Author: bramz
Date: 2008-01-03 01:49:09 +0100 (Thu, 03 Jan 2008)
New Revision: 2799

Modified:
   branches/WIP-pyshapelib-Unicode/thuban/ChangeLog
   branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/dbflibmodule.c
   branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/pyshapelib_common.h
   branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/pytest.py
   branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/setup.py
   branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/shapelibmodule.c
   branches/WIP-pyshapelib-Unicode/thuban/libraries/shapelib/safileio.c
   branches/WIP-pyshapelib-Unicode/thuban/libraries/shapelib/shapefil.h
Log:
added support for UTF-8 filenames to shapelib (currently 
Thuban specific, but this should go upstream soon), and applied
it to pyshapelib.  Removed the HAVE_CODE_PAGE macro and 
assume it's always true. See libraries/pyshapelib/ChangeLog.

Modified: branches/WIP-pyshapelib-Unicode/thuban/ChangeLog
===================================================================
--- branches/WIP-pyshapelib-Unicode/thuban/ChangeLog	2007-12-18 18:51:33 UTC (rev 2798)
+++ branches/WIP-pyshapelib-Unicode/thuban/ChangeLog	2008-01-03 00:49:09 UTC (rev 2799)
@@ -1,3 +1,12 @@
+2008-01-03   Bram de Greve <bram.degreve at bramz.net>
+
+	* added support for UTF-8 filenames to shapelib (currently 
+	Thuban specific, but this should go upstream soon), and applied
+	it to pyshapelib.  See libraries/pyshapelib/ChangeLog
+	
+	* setup.py: removed the HAVE_CODE_PAGE macro from the pyshapelib
+	extension.
+	
 2007-12-15   Bram de Greve <bram.degreve at bramz.net>
 
 	* shapelib and pyshapelib Unicode support now read the .CPG

Modified: branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/dbflibmodule.c
===================================================================
--- branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/dbflibmodule.c	2007-12-18 18:51:33 UTC (rev 2798)
+++ branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/dbflibmodule.c	2008-01-03 00:49:09 UTC (rev 2799)
@@ -9,7 +9,6 @@
 
 #include "pyshapelib_common.h"
 
-
 static PyObject* default_codecs_map = NULL;
 
 /* --- DBFFile ------------------------------------------------------------------------------------------------------- */
@@ -51,7 +50,6 @@
 
 static int dbffile_init_codec(DBFFileObject* self, PyObject* codecs_map)
 {
-#if HAVE_CODE_PAGE
 	size_t n = 0;
 	PyObject* ocodec = NULL;
 	char* codec = NULL;
@@ -94,10 +92,6 @@
 		}
 		memcpy(self->codec, codec, n + 1);
 	}
-#else
-	PyMem_Free(self->codec);
-	self->codec = NULL;
-#endif
 
 	return 0;
 }
@@ -112,67 +106,24 @@
 	char* mode = "rb";
 	PyObject* return_unicode = 0;
 	PyObject* codecs_map = NULL;
-#if HAVE_CODE_PAGE
 	static char *kwlist[] = {"name", "mode", "return_unicode", "codecs_map", NULL};
-#else
-	static char *kwlist[] = {"name", "mode", "return_unicode", NULL};
-#endif
 
 	DBFClose(self->handle);
 	self->handle = NULL;
 
-#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES)
-	if (GetVersion() < 0x80000000) {    /* On NT, so wide API available */
-		PyObject *wfile;
-#if HAVE_CODE_PAGE
-		if (PyArg_ParseTupleAndKeywords(args, kwds, "U|sOO:DBFFile", kwlist, 
-				&wfile, &mode, &return_unicode, &codecs_map)) 
-#else
-		if (PyArg_ParseTupleAndKeywords(args, kwds, "U|sO:DBFFile", kwlist, 
-				&wfile, &mode, &return_unicode)) 
-#endif
-		{
-			PyObject *wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL);
-			if (!wmode) return -1;
-			self->handle = DBFOpenW(PyUnicode_AS_UNICODE(wfile), PyUnicode_AS_UNICODE(wmode));
-			Py_DECREF(wmode);
-			if (!self->handle)
-			{
-				PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile);
-				return -1;
-			}
-		} 
-		else 
-		{
-			/* Drop the argument parsing error as narrow
-			   strings are also valid. */
-			PyErr_Clear();
-		}
-	}
-#endif
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|sOO:DBFFile", kwlist, 
+		PYSHAPELIB_FILESYSTEMENCODING, &file, &mode, &return_unicode, &codecs_map)) return -1;	
 
+	PYSHAPELIB_SETUPHOOKS(&hooks);
+	self->handle = DBFOpenLL(file, mode, &hooks);
+
 	if (!self->handle)
 	{
-#if HAVE_CODE_PAGE
-		if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|sOO:DBFFile", kwlist, 
-			Py_FileSystemDefaultEncoding, &file, &mode, &return_unicode, &codecs_map)) return -1;	
-#else
-		if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|sO:DBFFile", kwlist, 
-			Py_FileSystemDefaultEncoding, &file, &mode, &return_unicode)) return -1;	
-#endif
-		SASetupDefaultHooks(&hooks);
-		hooks.Atof = PyOS_ascii_atof;
-		self->handle = DBFOpenLL(file, mode, &hooks);
-
-		if (!self->handle)
-		{
-			PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
-			PyMem_Free(file);
-			return -1;
-		}
-
+		PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
 		PyMem_Free(file);
+		return -1;
 	}
+	PyMem_Free(file);
 
 	self->return_unicode = return_unicode && PyObject_IsTrue(return_unicode);
 
@@ -596,8 +547,6 @@
 #endif
 
 
-#if HAVE_CODE_PAGE
-
 static PyObject* dbffile_code_page(DBFFileObject* self, void* closure)
 {
 	const char* code_page = DBFGetCodePage(self->handle);
@@ -608,8 +557,6 @@
 	return PyString_FromString(code_page);
 }
 
-#endif
-
 static PyObject* dbffile_codec(DBFFileObject* self, void* closure)
 {
 	if (!self->codec)
@@ -673,9 +620,7 @@
 static struct PyGetSetDef dbffile_getsetters[] = 
 {
 	{"codec", (getter)dbffile_codec, NULL, "Python codec name used to encode or decode Unicode strings (read-only)" },
-#if HAVE_CODE_PAGE
 	{"code_page", (getter)dbffile_code_page, NULL, "DBF Code Page from LDID or .CPG file (read-only)" },
-#endif
 	{NULL}
 };
 
@@ -696,6 +641,7 @@
 
 static PyObject* dbflib_create(PyObject* module, PyObject* args, PyObject* kwds)
 {
+	SAHooks hooks;
 	char* file;
 	DBFFileObject* result;
 	DBFHandle handle = NULL;
@@ -704,62 +650,20 @@
 	PyObject* codecs_map = NULL;
 	char* code_page = NULL;
 
-#if HAVE_CODE_PAGE
 	static char *kwlist[] = {"name", "code_page", "return_unicode", "codecs_map", NULL};
-#else
-	static char *kwlist[] = {"name", "return_unicode", NULL};
-#endif
 
-#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES)
-	if (GetVersion() < 0x80000000) {    /* On NT, so wide API available */
-		PyObject *wfile;
-#if HAVE_CODE_PAGE
-		if (PyArg_ParseTupleAndKeywords(args, kwds, "U|sOO:create", kwlist, 
-				&wfile, &code_page, &return_unicode, &codecs_map))
-#else
-		if (PyArg_ParseTupleAndKeywords(args, kwds, "U|O:create", kwlist, 
-				&wfile, &return_unicode))
-#endif
-		{
-			wideargument = 1;
-			handle = DBFCreateW(PyUnicode_AS_UNICODE(wfile));
-			if (!handle)
-			{
-				PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile);
-				return NULL;
-			}
-		} 
-		else 
-		{
-			/* Drop the argument parsing error as narrow
-			   strings are also valid. */
-			PyErr_Clear();
-		}
-	}
-#endif
-	
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|sOO:create", kwlist, PYSHAPELIB_FILESYSTEMENCODING, 
+			&file, &code_page, &return_unicode, &codecs_map)) return NULL;
+
+	PYSHAPELIB_SETUPHOOKS(&hooks);
+	handle = DBFCreateLL(file, code_page, &hooks);
 	if (!handle)
 	{
-		SAHooks hooks;
-		SASetupDefaultHooks(&hooks);
-		hooks.Atof = PyOS_ascii_atof;
-#if HAVE_CODE_PAGE
-		if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|sOO:create", kwlist, Py_FileSystemDefaultEncoding, 
-				&file, &code_page, &return_unicode, &codecs_map)) return NULL;
-		handle = DBFCreateLL(file, code_page, &hooks);
-#else
-		if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|O:create", kwlist, Py_FileSystemDefaultEncoding, 
-				&file, &return_unicode)) return NULL;
-		handle = DBFCreateLL(file, &hooks);
-#endif
-		if (!handle)
-		{
-				PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
-				PyMem_Free(file);
-				return NULL;
-		}
-		PyMem_Free(file);
+			PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
+			PyMem_Free(file);
+			return NULL;
 	}
+	PyMem_Free(file);
 
 	result = PyObject_New(DBFFileObject, &DBFFileType);
 	if (!result)
@@ -786,25 +690,15 @@
 static struct PyMethodDef dbflib_methods[] = 
 {
 	{"open", (PyCFunction)dbflib_open, METH_VARARGS | METH_KEYWORDS,
-#if HAVE_CODE_PAGE
 		"open(name [, mode [, return_unicode [, codecs_map]]]) -> DBFFile\n\n"
-#else
-		"open(name [, mode [, return_unicode]]) -> DBFFile\n\n"
-#endif
 		"opens a DBFFile" },
 	{"create", (PyCFunction)dbflib_create, METH_VARARGS | METH_KEYWORDS, 
-#if HAVE_CODE_PAGE
 		"create(name [, code_page [, return_unicode [, codecs_map]]]) -> DBFFile\n\n"
-#else
-		"create(name [, return_unicode]) -> DBFFile\n\n"
-#endif
 		"create a DBFFile " },
 	{NULL}
 };
 
 
-#if HAVE_CODE_PAGE
-
 void add_ldid(PyObject* module, int ldid, const char* codec, const char* name)
 {
 	char code_page[64];
@@ -827,8 +721,6 @@
 	PyModule_AddStringConstant(module, constant, code_page);
 }
 
-#endif
-
 PyMODINIT_FUNC initdbflib(void)
 {
 	PyObject* module = Py_InitModule("dbflib", dbflib_methods);
@@ -842,9 +734,7 @@
 	PYSHAPELIB_ADD_CONSTANT(FTLogical);
 	PYSHAPELIB_ADD_CONSTANT(FTInvalid);
 	PyModule_AddIntConstant(module, "_have_commit", HAVE_UPDATE_HEADER);
-	PyModule_AddIntConstant(module, "_have_code_page", HAVE_CODE_PAGE);
 
-#if HAVE_CODE_PAGE
 	default_codecs_map = PyDict_New();
 
 	/* table compiled from these resources:
@@ -948,8 +838,4 @@
 	add_cpg(module, "ISO 885910", "iso-8859-10", "ISO_8859_10");
 	add_cpg(module, "ISO 885913", "iso-8859-13", "ISO_8859_13");
 	add_cpg(module, "ISO 885915", "iso-8859-15", "ISO_8859_15");
-
-
-#endif
-
 }

Modified: branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/pyshapelib_common.h
===================================================================
--- branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/pyshapelib_common.h	2007-12-18 18:51:33 UTC (rev 2798)
+++ branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/pyshapelib_common.h	2008-01-03 00:49:09 UTC (rev 2799)
@@ -5,102 +5,97 @@
  * This program is free software under the GPL (>=v2)
  * Read the file COPYING coming with Thuban for details.
  */
-
-#ifndef PYSHAPELIB_H
-#define PYSHAPELIB_H
-
-#include <Python.h>
-#include <structmember.h>
-#include "shapefil.h"
-#include "pyshapelib_api.h"
-
-/* WIDE-CHARACTER FILENAME SUPPORT FOR WINDOWS
- * 
- * pyshapelib can support full Unicode strings as filename on the Windows platform
- * trough the wide-character version of the Win API.  At this moment, however, this
- * needs unofficial modifications to the shapelib library to be able to use this API.
- * 
- * A ticket has been made in the maptools Bugzilla with patches for the 1.2.10 release
- * of shapelib: http://bugzilla.maptools.org/show_bug.cgi?id=1692
- *
- * The version of shapelib that is contained in the thuban source tree already contains
- * these modifications.
- */
-#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES)
-/* Need GetVersion to see if on NT so safe to use _wfopen */
-#	define WIN32_LEAN_AND_MEAN
-#	include <windows.h>
-#endif
-
-/* ---------------------------------------------------------------------------------------------- */
-
-/* helper to export constants (macros) to Python.
- * The constant in Python will have the same name as in C
- */
-#define PYSHAPELIB_ADD_CONSTANT(constant) PyModule_AddIntConstant(module, #constant, constant)
-
-/* helper to define the type object.
- *
- * This assumes quite a few things about different things being available and their name.
- * For example, if prefix = foo, then there should be a deallocation function called foo_dealloc.
- * See the macro itself for other examples.
- */
-#define PYSHAPELIB_DEFINE_TYPE(object, prefix, name, doc) \
-{ \
-		PyObject_HEAD_INIT(NULL) \
-		0,									/*ob_size*/ \
-		name,								/*tp_name*/ \
-		sizeof(object),						/*tp_basicsize*/ \
-		0,									/*tp_itemsize*/ \
-		(destructor) prefix ## _dealloc,	/*tp_dealloc*/ \
-		0,									/*tp_print*/ \
-		0,									/*tp_getattr*/ \
-		0,									/*tp_setattr*/ \
-		0,									/*tp_compare*/ \
-		(reprfunc) prefix ## _repr,			/*tp_repr*/ \
-		0,									/*tp_as_number*/ \
-		0,									/*tp_as_sequence*/ \
-		0,									/*tp_as_mapping*/ \
-		0,									/*tp_hash */ \
-		0,									/*tp_call*/ \
-		0,									/*tp_str*/ \
-		0,									/*tp_getattro*/ \
-		0,									/*tp_setattro*/ \
-		0,									/*tp_as_buffer*/ \
-		Py_TPFLAGS_DEFAULT,					/*tp_flags*/ \
-		doc,								/* tp_doc */ \
-		0,									/* tp_traverse */ \
-		0,									/* tp_clear */ \
-		0,									/* tp_richcompare */ \
-		0,									/* tp_weaklistoffset */ \
-		0,									/* tp_iter */ \
-		0,									/* tp_iternext */ \
-		prefix ## _methods,					/* tp_methods */ \
-		0,									/* tp_members */ \
-		prefix ## _getsetters,				/* tp_getset */ \
-		0,									/* tp_base */ \
-		0,									/* tp_dict */ \
-		0,									/* tp_descr_get */ \
-		0,									/* tp_descr_set */ \
-		0,									/* tp_dictoffset */ \
-		(initproc) prefix ## _init,			/* tp_init */ \
-		0,									/* tp_alloc */ \
-		prefix ## _new,						/* tp_new */ \
-	} \
-	/**/
-
-/* helper to add type to module.
- * Does a bit of the tedious bookkeeping for us
- */
-#define PYSHAPELIB_ADD_TYPE(type, name) \
-	type.ob_type = &PyType_Type; \
-	if (PyType_Ready(&type) >= 0) \
-	{ \
-		Py_INCREF(&type); \
-		PyModule_AddObject(module, name, (PyObject*)&type); \
-	}
-
-#define PYSHAPELIB_NO_DATA_LIMIT 1e-38
-#define PYSHAPELIB_NO_DATA 0
-
+
+#ifndef PYSHAPELIB_H
+#define PYSHAPELIB_H
+
+#include <Python.h>
+#include <structmember.h>
+#include "shapefil.h"
+#include "pyshapelib_api.h"
+
+
+/* helper to export constants (macros) to Python.
+ * The constant in Python will have the same name as in C
+ */
+#define PYSHAPELIB_ADD_CONSTANT(constant) PyModule_AddIntConstant(module, #constant, constant)
+
+/* helper to define the type object.
+ *
+ * This assumes quite a few things about different things being available and their name.
+ * For example, if prefix = foo, then there should be a deallocation function called foo_dealloc.
+ * See the macro itself for other examples.
+ */
+#define PYSHAPELIB_DEFINE_TYPE(object, prefix, name, doc) \
+{ \
+		PyObject_HEAD_INIT(NULL) \
+		0,									/*ob_size*/ \
+		name,								/*tp_name*/ \
+		sizeof(object),						/*tp_basicsize*/ \
+		0,									/*tp_itemsize*/ \
+		(destructor) prefix ## _dealloc,	/*tp_dealloc*/ \
+		0,									/*tp_print*/ \
+		0,									/*tp_getattr*/ \
+		0,									/*tp_setattr*/ \
+		0,									/*tp_compare*/ \
+		(reprfunc) prefix ## _repr,			/*tp_repr*/ \
+		0,									/*tp_as_number*/ \
+		0,									/*tp_as_sequence*/ \
+		0,									/*tp_as_mapping*/ \
+		0,									/*tp_hash */ \
+		0,									/*tp_call*/ \
+		0,									/*tp_str*/ \
+		0,									/*tp_getattro*/ \
+		0,									/*tp_setattro*/ \
+		0,									/*tp_as_buffer*/ \
+		Py_TPFLAGS_DEFAULT,					/*tp_flags*/ \
+		doc,								/* tp_doc */ \
+		0,									/* tp_traverse */ \
+		0,									/* tp_clear */ \
+		0,									/* tp_richcompare */ \
+		0,									/* tp_weaklistoffset */ \
+		0,									/* tp_iter */ \
+		0,									/* tp_iternext */ \
+		prefix ## _methods,					/* tp_methods */ \
+		0,									/* tp_members */ \
+		prefix ## _getsetters,				/* tp_getset */ \
+		0,									/* tp_base */ \
+		0,									/* tp_dict */ \
+		0,									/* tp_descr_get */ \
+		0,									/* tp_descr_set */ \
+		0,									/* tp_dictoffset */ \
+		(initproc) prefix ## _init,			/* tp_init */ \
+		0,									/* tp_alloc */ \
+		prefix ## _new,						/* tp_new */ \
+	} \
+	/**/
+
+/* helper to add type to module.
+ * Does a bit of the tedious bookkeeping for us
+ */
+#define PYSHAPELIB_ADD_TYPE(type, name) \
+	type.ob_type = &PyType_Type; \
+	if (PyType_Ready(&type) >= 0) \
+	{ \
+		Py_INCREF(&type); \
+		PyModule_AddObject(module, name, (PyObject*)&type); \
+	}
+
+#define PYSHAPELIB_NO_DATA_LIMIT 1e-38
+#define PYSHAPELIB_NO_DATA 0
+
+
+
+#if HAVE_UTF8_HOOKS && defined(MS_WINDOWS)
+#	define PYSHAPELIB_FILESYSTEMENCODING "utf-8"
+#	define PYSHAPELIB_SETUPHOOKS(pHooks)\
+		SASetupUtf8Hooks(pHooks);\
+		(pHooks)->Atof = PyOS_ascii_atof
+#else
+#	define PYSHAPELIB_FILESYSTEMENCODING Py_FileSystemDefaultEncoding
+#	define PYSHAPELIB_SETUPHOOKS(pHooks)\
+		SASetupDefaultHooks(pHooks);\
+		(pHooks)->Atof = PyOS_ascii_atof
+#endif
+
 #endif
\ No newline at end of file

Modified: branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/pytest.py
===================================================================
--- branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/pytest.py	2007-12-18 18:51:33 UTC (rev 2798)
+++ branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/pytest.py	2008-01-03 00:49:09 UTC (rev 2799)
@@ -1,5 +1,8 @@
 import shapelib, dbflib, shptree
 
+filename = "testfile"
+# filename = u"x\u03C0\u03C1\u03C2" # test a unicode filename
+
 #
 #       The the shapefile module
 #
@@ -112,8 +115,8 @@
 
 print "--- testing shapelib ---"
 
-make_shapefile("testfile")
-read_shapefile("testfile")
+make_shapefile(filename)
+read_shapefile(filename)
 
 #
 #		Test MultiPatch shapefiles
@@ -212,6 +215,6 @@
         print format % dbf.read_record(i)
 
 
-make_dbf("testfile")
-add_dbf_records("testfile")
-list_dbf("testfile")
+make_dbf(filename)
+add_dbf_records(filename)
+list_dbf(filename)

Modified: branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/setup.py
===================================================================
--- branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/setup.py	2007-12-18 18:51:33 UTC (rev 2798)
+++ branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/setup.py	2008-01-03 00:49:09 UTC (rev 2799)
@@ -53,8 +53,7 @@
 		return "0"
 	
 	return [
-		("HAVE_UPDATE_HEADER", have("DBFUpdateHeader")),
-		("HAVE_CODE_PAGE", have("DBFGetCodePage"))]
+		("HAVE_UPDATE_HEADER", have("DBFUpdateHeader"))]
 
 extensions = [Extension("shapelib",
 						["shapelibmodule.c",

Modified: branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/shapelibmodule.c
===================================================================
--- branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/shapelibmodule.c	2007-12-18 18:51:33 UTC (rev 2798)
+++ branches/WIP-pyshapelib-Unicode/thuban/libraries/pyshapelib/shapelibmodule.c	2008-01-03 00:49:09 UTC (rev 2799)
@@ -547,6 +547,7 @@
  */
 static int shapefile_init(ShapeFileObject* self, PyObject* args, PyObject* kwds)
 {
+	SAHooks hooks;
 	char* file = NULL;
 	char* mode = "rb";
 	static char *kwlist[] = {"name", "mode", NULL};
@@ -554,45 +555,18 @@
 	SHPClose(self->handle);
 	self->handle = NULL;
 
-#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES)
-	if (GetVersion() < 0x80000000) {    /* On NT, so wide API available */
-		PyObject *wfile;
-		if (PyArg_ParseTupleAndKeywords(args, kwds, "U|s:ShapeFile", kwlist, &wfile, &mode)) 
-		{
-			PyObject *wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL);
-			if (!wmode) return -1;
-			self->handle = SHPOpenW(PyUnicode_AS_UNICODE(wfile), PyUnicode_AS_UNICODE(wmode));
-			Py_DECREF(wmode);
-			if (!self->handle)
-			{
-				PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile);
-				return -1;
-			}
-		} 
-		else 
-		{
-			/* Drop the argument parsing error as narrow
-			   strings are also valid. */
-			PyErr_Clear();
-		}
-	}
-#endif
-
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:ShapeFile", kwlist, 
+		PYSHAPELIB_FILESYSTEMENCODING, &file, &mode)) return -1;	
+		
+	PYSHAPELIB_SETUPHOOKS(&hooks);
+	self->handle = SHPOpenLL(file, mode, &hooks);
 	if (!self->handle)
 	{
-		if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:ShapeFile", kwlist, 
-			Py_FileSystemDefaultEncoding, &file, &mode)) return -1;	
-		self->handle = SHPOpen(file, mode);
-
-		if (!self->handle)
-		{
-			PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
-			PyMem_Free(file);
-			return -1;
-		}
-
+		PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
 		PyMem_Free(file);
+		return -1;
 	}
+	PyMem_Free(file);
 
 	return 0;
 }
@@ -719,46 +693,24 @@
 
 static PyObject* shapelib_create(PyObject* module, PyObject* args)
 {
+	SAHooks hooks;
 	char* file;
 	int type;
 	ShapeFileObject* result;
 	SHPHandle handle = NULL;
 	int wideargument = 0;
 
-#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES)
-	if (GetVersion() < 0x80000000) {    /* On NT, so wide API available */
-		PyObject *wfile;
-		if (PyArg_ParseTuple(args, "Ui:create", &wfile, &type)) 
-		{
-			wideargument = 1;
-			handle = SHPCreateW(PyUnicode_AS_UNICODE(wfile), type);
-			if (!handle)
-			{
-				PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile);
-				return NULL;
-			}
-		} 
-		else 
-		{
-			/* Drop the argument parsing error as narrow
-			   strings are also valid. */
-			PyErr_Clear();
-		}
-	}
-#endif
+	if (!PyArg_ParseTuple(args, "eti:create", PYSHAPELIB_FILESYSTEMENCODING, &file, &type)) return NULL;
 	
+	PYSHAPELIB_SETUPHOOKS(&hooks);
+	handle = SHPCreateLL(file, type, &hooks);
 	if (!handle)
 	{
-		if (!PyArg_ParseTuple(args, "eti:create", Py_FileSystemDefaultEncoding, &file, &type)) return NULL;
-		handle = SHPCreate(file, type);
-		if (!handle)
-		{
-				PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
-				PyMem_Free(file);
-				return NULL;
-		}
-		PyMem_Free(file);
+			PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
+			PyMem_Free(file);
+			return NULL;
 	}
+	PyMem_Free(file);
 
 	result = PyObject_New(ShapeFileObject, &ShapeFileType);
 	if (!result)

Modified: branches/WIP-pyshapelib-Unicode/thuban/libraries/shapelib/safileio.c
===================================================================
--- branches/WIP-pyshapelib-Unicode/thuban/libraries/shapelib/safileio.c	2007-12-18 18:51:33 UTC (rev 2798)
+++ branches/WIP-pyshapelib-Unicode/thuban/libraries/shapelib/safileio.c	2008-01-03 00:49:09 UTC (rev 2799)
@@ -61,6 +61,14 @@
 
 SHP_CVSID("$Id: safileio.c,v 1.3 2007/12/18 18:28:11 bram Exp $");
 
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
+#	define SA_UTF8_WINDOWS
+#   define WIN32_LEAN_AND_MEAN
+#   define NOMINMAX
+#   include <windows.h>
+#   pragma comment(lib, "kernel32.lib")
+#endif
+
 /************************************************************************/
 /*                              SADFOpen()                              */
 /************************************************************************/
@@ -172,3 +180,95 @@
     psHooks->Error   = SADError;
     psHooks->Atof    = atof;
 }
+
+
+
+
+#ifdef SA_UTF8_WINDOWS
+
+/************************************************************************/
+/*                          Utf8ToWideChar                              */
+/************************************************************************/
+
+const wchar_t* Utf8ToWideChar( const char *pszFilename )
+{
+    int nMulti, nWide;
+    wchar_t *pwszFileName;
+    
+    nMulti = strlen(pszFilename) + 1;
+    nWide = MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, 0, 0);
+    if( nWide == 0 )
+    {
+        return NULL;
+    }
+    pwszFileName = (wchar_t*) malloc(nWide * sizeof(wchar_t));
+    if ( pwszFileName == NULL )
+    {
+        return NULL;
+    }
+    if( MultiByteToWideChar( CP_UTF8, 0, pszFilename, nMulti, pwszFileName, nWide ) == 0 )
+    {
+        free( pwszFileName );
+        return NULL;
+    }
+    return pwszFileName;
+}
+
+/************************************************************************/
+/*                           SAUtf8WFOpen                               */
+/************************************************************************/
+
+SAFile SAUtf8WFOpen( const char *pszFilename, const char *pszAccess )
+{
+    SAFile file = NULL;
+    const wchar_t *pwszFileName, *pwszAccess;
+    pwszFileName = Utf8ToWideChar( pszFilename );
+    pwszAccess = Utf8ToWideChar( pszAccess );
+    if( pwszFileName != NULL && pwszFileName != NULL)
+    {
+        file = (SAFile) _wfopen( pwszFileName, pwszAccess );
+    }
+    free (pwszFileName);
+    free (pwszAccess);
+    return file;
+}
+
+/************************************************************************/
+/*                             SAUtf8WRemove()                          */
+/************************************************************************/
+
+int SAUtf8WRemove( const char *pszFilename )
+{
+    const wchar_t *pwszFileName = Utf8ToWideChar( pszFilename );
+    if( pwszFileName == NULL )
+    {
+        return -1;
+    }    
+    return _wremove( pwszFileName );
+}
+
+#endif
+
+/************************************************************************/
+/*                          SASetupUtf8Hooks()                          */
+/************************************************************************/
+
+void SASetupUtf8Hooks( SAHooks *psHooks )
+{
+#ifdef SA_UTF8_WINDOWS    
+    psHooks->FOpen   = SAUtf8WFOpen;
+    psHooks->Remove  = SAUtf8WRemove;
+#else
+    psHooks->FOpen   = SADFOpen;
+    psHooks->Remove  = SADRemove;
+#endif
+    psHooks->FRead   = SADFRead;
+    psHooks->FWrite  = SADFWrite;
+    psHooks->FSeek   = SADFSeek;
+    psHooks->FTell   = SADFTell;
+    psHooks->FFlush  = SADFFlush;
+    psHooks->FClose  = SADFClose;
+
+    psHooks->Error   = SADError;
+    psHooks->Atof    = atof;
+}

Modified: branches/WIP-pyshapelib-Unicode/thuban/libraries/shapelib/shapefil.h
===================================================================
--- branches/WIP-pyshapelib-Unicode/thuban/libraries/shapelib/shapefil.h	2007-12-18 18:51:33 UTC (rev 2798)
+++ branches/WIP-pyshapelib-Unicode/thuban/libraries/shapelib/shapefil.h	2008-01-03 00:49:09 UTC (rev 2799)
@@ -220,6 +220,7 @@
 } SAHooks;
 
 void SHPAPI_CALL SASetupDefaultHooks( SAHooks *psHooks );
+void SHPAPI_CALL SASetupUtf8Hooks( SAHooks *psHooks );
 
 /************************************************************************/
 /*                             SHP Support.                             */



More information about the Thuban-commits mailing list