[Thuban-commits] r2753 - branches/WIP-pyshapelib-bramz/libraries/pyshapelib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Apr 11 20:57:04 CEST 2007


Author: bramz
Date: 2007-04-11 20:57:04 +0200 (Wed, 11 Apr 2007)
New Revision: 2753

Modified:
   branches/WIP-pyshapelib-bramz/libraries/pyshapelib/ChangeLog
   branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c
Log:
Expanded Unicode support to field names (formely it was only available for string values in the records.  
Renamed the write_field function to write_attribute to be symmetric with the read_attribute function that already existed.

Modified: branches/WIP-pyshapelib-bramz/libraries/pyshapelib/ChangeLog
===================================================================
--- branches/WIP-pyshapelib-bramz/libraries/pyshapelib/ChangeLog	2007-04-10 23:45:00 UTC (rev 2752)
+++ branches/WIP-pyshapelib-bramz/libraries/pyshapelib/ChangeLog	2007-04-11 18:57:04 UTC (rev 2753)
@@ -1,3 +1,10 @@
+2007-04-12	Bram de Greve <bram.degreve at intec.ugent.be>
+
+	* dbflibmodule.c: Expanded Unicode support to field names (formely it was
+	only available for string values in the records.  Renamed the write_field 
+	function to write_attribute to be symmetric with the read_attribute 
+	function that already existed.
+
 2007-04-11	Bram de Greve <bram.degreve at intec.ugent.be>
 
 	* dbflibmodule.c, pyshapelib_common.h, setup.py: attempt to add support for

Modified: branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c
===================================================================
--- branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c	2007-04-10 23:45:00 UTC (rev 2752)
+++ branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c	2007-04-11 18:57:04 UTC (rev 2753)
@@ -203,26 +203,33 @@
 {
 	char field_name[12];
 	int field, width = 0, decimals = 0, field_type;
+	PyObject* name_object = NULL;
 	
 	if (!PyArg_ParseTuple(args, "i:field_info", &field)) return NULL;
 	
 	field_name[0] = '\0';
 	field_type = DBFGetFieldInfo(self->handle, field, field_name, &width, &decimals);
+	name_object = decode_string(self->handle, field_name);
 	
-	return Py_BuildValue("isii", field_type, field_name, width, decimals);
+	return Py_BuildValue("iOii", field_type, name_object, width, decimals);
 }
 
 
 
 static PyObject* dbffile_add_field(DBFFileObject* self, PyObject* args)
 {
-	char* name;
+	PyObject *oname = NULL, *name = NULL;
 	int type, width, decimals;
 	int field;
 	
-	if (!PyArg_ParseTuple(args, "siii:add_field", &name, &type, &width, &decimals)) return NULL;
+	if (!PyArg_ParseTuple(args, "Uiii:add_field", &oname, &type, &width, &decimals)
+		&& !PyArg_ParseTuple(args, "Siii:add_field", &oname, &type, &width, &decimals)) return NULL;
 	
-	field = DBFAddField(self->handle, name, (DBFFieldType)type, width, decimals);
+	name = encode_string(self->handle, oname);
+	if (!name) return NULL;
+
+	field = DBFAddField(self->handle, PyString_AsString(name), (DBFFieldType)type, width, decimals);
+	Py_DECREF(name);
 	
 	if (field < 0)
 	{
@@ -376,10 +383,9 @@
 
 
 /* write a single field of a record. */
-static int do_write_field(DBFHandle handle, int record, int field, int type, PyObject* value)
+static int do_write_attribute(DBFHandle handle, int record, int field, int type, PyObject* value)
 {
-	PyObject* encoded_string = NULL;
-	char * string_value;
+	PyObject* string_value = NULL;
 	int int_value;
 	double double_value;
 	int logical_value;
@@ -393,20 +399,14 @@
 		switch (type)
 		{
 		case FTString:
-			encoded_string = encode_string(handle, value);
-			if (!encoded_string) return 0;
-			string_value = PyString_AsString(encoded_string);
-			if (!string_value) 
+			string_value = encode_string(handle, value);
+			if (!string_value) return 0;
+			if (DBFWriteStringAttribute(handle, record, field, PyString_AsString(string_value)))
 			{
-				Py_DECREF(encoded_string);
-				return 0;
-			}
-			if (DBFWriteStringAttribute(handle, record, field, string_value))
-			{
-				Py_DECREF(encoded_string);
+				Py_DECREF(string_value);
 				return 1;
 			}
-			Py_DECREF(encoded_string);
+			Py_DECREF(string_value);
 			break;
 
 		case FTInteger:
@@ -439,13 +439,13 @@
 
 
 
-static PyObject* dbffile_write_field(DBFFileObject* self, PyObject* args)
+static PyObject* dbffile_write_attribute(DBFFileObject* self, PyObject* args)
 {
 	int record, field;
 	PyObject* value;
 	int type;
 
-	if (!PyArg_ParseTuple(args, "iiO:write_field", &record, &field, &value)) return NULL;
+	if (!PyArg_ParseTuple(args, "iiO:write_attribute", &record, &field, &value)) return NULL;
 	
 	if (field < 0 || field >= DBFGetFieldCount(self->handle))
 	{
@@ -456,7 +456,7 @@
 	}
 
 	type = DBFGetFieldInfo(self->handle, field, NULL, NULL, NULL);
-	if (!do_write_field(self->handle, record, field, type, value)) return NULL;
+	if (!do_write_attribute(self->handle, record, field, type, value)) return NULL;
 	Py_RETURN_NONE;
 }
 
@@ -497,7 +497,7 @@
 			type = DBFGetFieldInfo(self->handle, i, NULL, NULL, NULL); 
 			value = PySequence_GetItem(record_object, i);
 			if (!value) return NULL;
-			if (!do_write_field(self->handle, record, i, type, value)) 
+			if (!do_write_attribute(self->handle, record, i, type, value)) 
 			{
 				Py_DECREF(value);
 				return NULL;
@@ -515,7 +515,7 @@
 			name[0] = '\0';
 			type = DBFGetFieldInfo(self->handle, i, name, NULL, NULL);
 			value = PyDict_GetItemString(record_object, name);
-			if (value && !do_write_field(self->handle, record, i, type, value)) return NULL;
+			if (value && !do_write_attribute(self->handle, record, i, type, value)) return NULL;
 		}
 	}
 	
@@ -591,8 +591,8 @@
 	{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, 
 		"read_record(record_index) -> dict\n\n"
 		"returns an entire record as a dictionary of field names and values"},
-	{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, 
-		"write_field(record_index, field_index, new_value)\n"
+	{"write_attribute", (PyCFunction)dbffile_write_attribute, METH_VARARGS, 
+		"write_attribute(record_index, field_index, new_value)\n"
 		"writes a single field of a record"},
 	{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, 
 		"write_record(record_index, record) -> record_index\n\n"



More information about the Thuban-commits mailing list