/[thuban]/branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/libraries/pyshapelib/dbflibmodule.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2752 by bramz, Tue Apr 10 23:45:00 2007 UTC revision 2754 by bramz, Wed Apr 11 19:04:12 2007 UTC
# Line 203  static PyObject* dbffile_field_info(DBFF Line 203  static PyObject* dbffile_field_info(DBFF
203  {  {
204          char field_name[12];          char field_name[12];
205          int field, width = 0, decimals = 0, field_type;          int field, width = 0, decimals = 0, field_type;
206            PyObject* name_object = NULL;
207                    
208          if (!PyArg_ParseTuple(args, "i:field_info", &field)) return NULL;          if (!PyArg_ParseTuple(args, "i:field_info", &field)) return NULL;
209                    
210          field_name[0] = '\0';          field_name[0] = '\0';
211          field_type = DBFGetFieldInfo(self->handle, field, field_name, &width, &decimals);          field_type = DBFGetFieldInfo(self->handle, field, field_name, &width, &decimals);
212            name_object = decode_string(self->handle, field_name);
213                    
214          return Py_BuildValue("isii", field_type, field_name, width, decimals);          return Py_BuildValue("iOii", field_type, name_object, width, decimals);
215  }  }
216    
217    
218    
219  static PyObject* dbffile_add_field(DBFFileObject* self, PyObject* args)  static PyObject* dbffile_add_field(DBFFileObject* self, PyObject* args)
220  {  {
221          char* name;          PyObject *oname = NULL, *name = NULL;
222          int type, width, decimals;          int type, width, decimals;
223          int field;          int field;
224                    
225          if (!PyArg_ParseTuple(args, "siii:add_field", &name, &type, &width, &decimals)) return NULL;          if (!PyArg_ParseTuple(args, "Uiii:add_field", &oname, &type, &width, &decimals))
226            {
227                    PyErr_Clear();
228                    if (!PyArg_ParseTuple(args, "Siii:add_field", &oname, &type, &width, &decimals)) return NULL;
229            }
230                    
231          field = DBFAddField(self->handle, name, (DBFFieldType)type, width, decimals);          name = encode_string(self->handle, oname);
232            if (!name) return NULL;
233    
234            field = DBFAddField(self->handle, PyString_AsString(name), (DBFFieldType)type, width, decimals);
235            Py_DECREF(name);
236                    
237          if (field < 0)          if (field < 0)
238          {          {
# Line 376  fail: Line 386  fail:
386    
387    
388  /* write a single field of a record. */  /* write a single field of a record. */
389  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)
390  {  {
391          PyObject* encoded_string = NULL;          PyObject* string_value = NULL;
         char * string_value;  
392          int int_value;          int int_value;
393          double double_value;          double double_value;
394          int logical_value;          int logical_value;
# Line 393  static int do_write_field(DBFHandle hand Line 402  static int do_write_field(DBFHandle hand
402                  switch (type)                  switch (type)
403                  {                  {
404                  case FTString:                  case FTString:
405                          encoded_string = encode_string(handle, value);                          string_value = encode_string(handle, value);
406                          if (!encoded_string) return 0;                          if (!string_value) return 0;
407                          string_value = PyString_AsString(encoded_string);                          if (DBFWriteStringAttribute(handle, record, field, PyString_AsString(string_value)))
                         if (!string_value)  
                         {  
                                 Py_DECREF(encoded_string);  
                                 return 0;  
                         }  
                         if (DBFWriteStringAttribute(handle, record, field, string_value))  
408                          {                          {
409                                  Py_DECREF(encoded_string);                                  Py_DECREF(string_value);
410                                  return 1;                                  return 1;
411                          }                          }
412                          Py_DECREF(encoded_string);                          Py_DECREF(string_value);
413                          break;                          break;
414    
415                  case FTInteger:                  case FTInteger:
# Line 439  static int do_write_field(DBFHandle hand Line 442  static int do_write_field(DBFHandle hand
442    
443    
444    
445  static PyObject* dbffile_write_field(DBFFileObject* self, PyObject* args)  static PyObject* dbffile_write_attribute(DBFFileObject* self, PyObject* args)
446  {  {
447          int record, field;          int record, field;
448          PyObject* value;          PyObject* value;
449          int type;          int type;
450    
451          if (!PyArg_ParseTuple(args, "iiO:write_field", &record, &field, &value)) return NULL;          if (!PyArg_ParseTuple(args, "iiO:write_attribute", &record, &field, &value)) return NULL;
452                    
453          if (field < 0 || field >= DBFGetFieldCount(self->handle))          if (field < 0 || field >= DBFGetFieldCount(self->handle))
454          {          {
# Line 456  static PyObject* dbffile_write_field(DBF Line 459  static PyObject* dbffile_write_field(DBF
459          }          }
460    
461          type = DBFGetFieldInfo(self->handle, field, NULL, NULL, NULL);          type = DBFGetFieldInfo(self->handle, field, NULL, NULL, NULL);
462          if (!do_write_field(self->handle, record, field, type, value)) return NULL;          if (!do_write_attribute(self->handle, record, field, type, value)) return NULL;
463          Py_RETURN_NONE;          Py_RETURN_NONE;
464  }  }
465    
# Line 497  static PyObject* dbffile_write_record(DB Line 500  static PyObject* dbffile_write_record(DB
500                          type = DBFGetFieldInfo(self->handle, i, NULL, NULL, NULL);                          type = DBFGetFieldInfo(self->handle, i, NULL, NULL, NULL);
501                          value = PySequence_GetItem(record_object, i);                          value = PySequence_GetItem(record_object, i);
502                          if (!value) return NULL;                          if (!value) return NULL;
503                          if (!do_write_field(self->handle, record, i, type, value))                          if (!do_write_attribute(self->handle, record, i, type, value))
504                          {                          {
505                                  Py_DECREF(value);                                  Py_DECREF(value);
506                                  return NULL;                                  return NULL;
# Line 515  static PyObject* dbffile_write_record(DB Line 518  static PyObject* dbffile_write_record(DB
518                          name[0] = '\0';                          name[0] = '\0';
519                          type = DBFGetFieldInfo(self->handle, i, name, NULL, NULL);                          type = DBFGetFieldInfo(self->handle, i, name, NULL, NULL);
520                          value = PyDict_GetItemString(record_object, name);                          value = PyDict_GetItemString(record_object, name);
521                          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;
522                  }                  }
523          }          }
524                    
# Line 591  static struct PyMethodDef dbffile_method Line 594  static struct PyMethodDef dbffile_method
594          {"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS,          {"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS,
595                  "read_record(record_index) -> dict\n\n"                  "read_record(record_index) -> dict\n\n"
596                  "returns an entire record as a dictionary of field names and values"},                  "returns an entire record as a dictionary of field names and values"},
597          {"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS,          {"write_attribute", (PyCFunction)dbffile_write_attribute, METH_VARARGS,
598                  "write_field(record_index, field_index, new_value)\n"                  "write_attribute(record_index, field_index, new_value)\n"
599                  "writes a single field of a record"},                  "writes a single field of a record"},
600          {"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS,          {"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS,
601                  "write_record(record_index, record) -> record_index\n\n"                  "write_record(record_index, record) -> record_index\n\n"

Legend:
Removed from v.2752  
changed lines
  Added in v.2754

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26