/[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 2744 by bramz, Thu Mar 15 13:48:58 2007 UTC revision 2750 by bramz, Thu Mar 22 20:35:08 2007 UTC
# Line 38  static int dbffile_init(DBFFileObject* s Line 38  static int dbffile_init(DBFFileObject* s
38  {  {
39          char* file;          char* file;
40          char* mode = "rb";          char* mode = "rb";
41          if (kwds != NULL && PyDict_Size(kwds) > 0)          static char *kwlist[] = {"name", "mode", NULL};
42            if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:__init__", kwlist,
43                    Py_FileSystemDefaultEncoding, &file, &mode)) return -1;
44            
45            self->handle = DBFOpen(file, mode);    
46            if (!self->handle)
47          {          {
48                  PyErr_Format(PyExc_TypeError, "dbflib.DBFFile.__init__ takes no keyword arguments");                  PyErr_SetFromErrnoWithFilename(PyExc_IOError, file);
                 return -1;  
49          }          }
50    
         if (!PyArg_ParseTuple(args, "et|s:__init__", Py_FileSystemDefaultEncoding, &file, &mode)) return -1;  
           
         self->handle = DBFOpen(file, mode);  
51          PyMem_Free(file);          PyMem_Free(file);
           
52          return self->handle ? 0 : -1;          return self->handle ? 0 : -1;
53  }  }
54    
# Line 142  static PyObject* do_read_attribute(DBFHa Line 142  static PyObject* do_read_attribute(DBFHa
142                  {                  {
143                  case FTString:                  case FTString:
144                          temp = DBFReadStringAttribute(handle, record, field);                          temp = DBFReadStringAttribute(handle, record, field);
145                          if (!temp)                          if (temp) return PyString_FromString(temp);
                         {  
                                 PyErr_Format(PyExc_IOError,  
                                                 "Can't read value for row %d column %d",  
                                                 record, field);  
                                 return NULL;  
                         }  
                         return PyString_FromString(temp);  
146    
147                  case FTInteger:                  case FTInteger:
148                          return PyInt_FromLong((long)DBFReadIntegerAttribute(handle, record, field));                          return PyInt_FromLong((long)DBFReadIntegerAttribute(handle, record, field));
149    
150                  case FTDouble:                  case FTDouble:
151                          return PyFloat_FromDouble(DBFReadDoubleAttribute(handle, record, field));                          return PyFloat_FromDouble(DBFReadDoubleAttribute(handle, record, field));
152                            
153                    case FTLogical:
154                            temp = DBFReadLogicalAttribute(handle, record, field);
155                            if (temp)
156                            {
157                                    switch (temp[0])
158                                    {
159                                    case 'F':
160                                    case 'N':
161                                            Py_RETURN_FALSE;
162                                    case 'T':
163                                    case 'Y':
164                                            Py_RETURN_TRUE;
165                                    }
166                            }
167                            break;
168    
169                  default:                  default:
170                          PyErr_Format(PyExc_TypeError, "Invalid field data type %d", type);                          PyErr_Format(PyExc_TypeError, "Invalid field data type %d", type);
171                          return NULL;                          return NULL;
172                  }                  }
173          }          }
174            
175            PyErr_Format(PyExc_IOError,     "Can't read value for row %d column %d", record, field);
176            return NULL;
177  }      }    
178    
179    
# Line 247  static int do_write_field(DBFHandle hand Line 259  static int do_write_field(DBFHandle hand
259          char * string_value;          char * string_value;
260          int int_value;          int int_value;
261          double double_value;          double double_value;
262            int logical_value;
263    
264          if (value == Py_None)          if (value == Py_None)
265          {          {
266                  if (!DBFWriteNULLAttribute(handle, record, field))                  if (DBFWriteNULLAttribute(handle, record, field)) return 1;
                 {  
                         PyErr_Format(PyExc_IOError,  
                                 "can't write NULL field %d of record %d",  
                                 field, record);  
                         return 0;  
                 }  
267          }          }
268          else          else
269          {          {
# Line 265  static int do_write_field(DBFHandle hand Line 272  static int do_write_field(DBFHandle hand
272                  case FTString:                  case FTString:
273                          string_value = PyString_AsString(value);                          string_value = PyString_AsString(value);
274                          if (!string_value) return 0;                          if (!string_value) return 0;
275                          if (!DBFWriteStringAttribute(handle, record, field, string_value))                          if (DBFWriteStringAttribute(handle, record, field, string_value)) return 1;
                         {  
                                 PyErr_Format(PyExc_IOError,  
                                                 "can't write field %d of record %d",  
                                                 field, record);  
                                 return 0;  
                         }  
276                          break;                          break;
277    
278                  case FTInteger:                  case FTInteger:
279                          int_value = PyInt_AsLong(value);                          int_value = PyInt_AsLong(value);
280                          if (int_value == -1 && PyErr_Occurred()) return 0;                          if (int_value == -1 && PyErr_Occurred()) return 0;
281                          if (!DBFWriteIntegerAttribute(handle, record, field, int_value))                          if (DBFWriteIntegerAttribute(handle, record, field, int_value)) return 1;
                         {  
                                 PyErr_Format(PyExc_IOError,  
                                                 "can't write field %d of record %d",  
                                                 field, record);  
                                 return 0;  
                         }  
282                          break;                          break;
283    
284                  case FTDouble:                  case FTDouble:
285                          double_value = PyFloat_AsDouble(value);                          double_value = PyFloat_AsDouble(value);
286                          if (double_value == -1 && PyErr_Occurred()) return 0;                          if (double_value == -1 && PyErr_Occurred()) return 0;
287                          if (!DBFWriteDoubleAttribute(handle, record, field, double_value))                          if (DBFWriteDoubleAttribute(handle, record, field, double_value)) return 1;
288                          {                          break;
289                                  PyErr_Format(PyExc_IOError,                          
290                                                  "can't write field %d of record %d",                  case FTLogical:
291                                                  field, record);                          logical_value = PyObject_IsTrue(value);
292                                  return 0;                          if (logical_value == -1) return 0;
293                          }                          if (DBFWriteLogicalAttribute(handle, record, field, logical_value ? 'T' : 'F')) return 1;
294                          break;                          break;
295    
296                  default:                  default:
# Line 304  static int do_write_field(DBFHandle hand Line 299  static int do_write_field(DBFHandle hand
299                  }                  }
300          }          }
301    
302          return 1;          PyErr_Format(PyExc_IOError,     "can't write field %d of record %d", field, record);
303            return 0;
304  }  }
305    
306    
# Line 422  static PyObject* dbffile_commit(DBFFileO Line 418  static PyObject* dbffile_commit(DBFFileO
418  static struct PyMethodDef dbffile_methods[] =  static struct PyMethodDef dbffile_methods[] =
419  {  {
420          {"close", (PyCFunction)dbffile_close, METH_NOARGS,          {"close", (PyCFunction)dbffile_close, METH_NOARGS,
421                  "close()\n"                  "close() -> None\n\n"
422                  "close DBFFile"},                  "closes DBFFile"},
423          {"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS,          {"field_count", (PyCFunction)dbffile_field_count, METH_NOARGS,
424                  "field_count()\n"                  "field_count() -> integer\n\n"
425                  "returns number of fields currently defined"},                  "returns number of fields currently defined"},
426          {"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS,          {"record_count", (PyCFunction)dbffile_record_count, METH_NOARGS,
427                  "record_count()\n"                  "record_count() -> integer\n\n"
428                  "returns number of records that currently exist"},                  "returns number of records that currently exist"},
429          {"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS,          {"field_info", (PyCFunction)dbffile_field_info, METH_VARARGS,
430                  "field_info(field_index)\n"                  "field_info(field_index) -> (type, name, width, decimals)\n\n"
431                  "returns info of a field as a tuple (type, name, width, decimals) with:\n"                  "returns info of a field as a tuple with:\n"
432                  "-type: the type of the field corresponding to the integer value of one of the constants FTString, FTInteger, ...\n"                  "- type: the type of the field corresponding to the integer value of one "
433                  "-name: the name of the field as a string\n"                  " of the constants FTString, FTInteger, ...\n"
434                  "-width: the width of the field as a number of characters\n"                  "- name: the name of the field as a string\n"
435                  "-decimals: the number of decimal digits" },                  "- width: the width of the field as a number of characters\n"
436                    "- decimals: the number of decimal digits" },
437          {"add_field", (PyCFunction)dbffile_add_field, METH_VARARGS,          {"add_field", (PyCFunction)dbffile_add_field, METH_VARARGS,
438                  "add_field(type, name, width, decimals)\n"                  "add_field(type, name, width, decimals) -> field_index\n\n"
439                  "adds a new field and returns field index if successful\n"                  "adds a new field and returns field index if successful\n"
440                  "-type: the type of the field corresponding to the integer value of one of the constants FTString, FTInteger, ...\n"                  "- type: the type of the field corresponding to the integer value of one "
441                  "-name: the name of the field as a string\n"                  " of the constants FTString, FTInteger, ...\n"
442                  "-width: the width of the field as a number of characters\n"                  "- name: the name of the field as a string\n"
443                  "-decimals: the number of decimal digits" },                  "- width: the width of the field as a number of characters\n"
444                    "- decimals: the number of decimal digits" },
445          {"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS,          {"read_attribute", (PyCFunction)dbffile_read_attribute, METH_VARARGS,
446                  "read_attribute(record_index, field_index)\n"                  "read_attribute(record_index, field_index) -> value\n\n"
447                  "return the value of one field of a record"},                  "returns the value of one field of a record"},
448          {"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS,          {"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS,
449                  "read_record(record_index)\n"                  "read_record(record_index) -> dict\n\n"
450                  "return an entire record as a dict of field names and values"},                  "returns an entire record as a dictionary of field names and values"},
451          {"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS,          {"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS,
452                  "write_field(record_index, field_index, new_value)\n"                  "write_field(record_index, field_index, new_value)\n"
453                  "write a single field of a record"},                  "writes a single field of a record"},
454          {"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS,          {"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS,
455                  "write_record(record_index, record)\n"                  "write_record(record_index, record) -> record_index\n\n"
456                  "write an entire record as a dict or a sequence\n"                  "Writes an entire record as a dict or a sequence, and return index of record\n"
457                  "record can either be a dictionary in which case the keys are used as field names, "                  "Record can either be a dictionary in which case the keys are used as field names, "
458                  "or a sequence that must have an item for every field (length = field_count())"},                  "or a sequence that must have an item for every field (length = field_count())"},
459  #if HAVE_UPDATE_HEADER  #if HAVE_UPDATE_HEADER
460          {"commit", (PyCFunction)dbffile_read_record, METH_NOARGS,          {"commit", (PyCFunction)dbffile_commit, METH_NOARGS,
461                  "commit()"},                  "commit() -> None"},
462  #endif  #endif
463          {NULL}          {NULL}
464  };  };
# Line 516  static PyObject* dbflib_create(PyObject* Line 514  static PyObject* dbflib_create(PyObject*
514  static struct PyMethodDef dbflib_methods[] =  static struct PyMethodDef dbflib_methods[] =
515  {  {
516          {"open", (PyCFunction)dbflib_open, METH_VARARGS,          {"open", (PyCFunction)dbflib_open, METH_VARARGS,
517                  "open(filename [, mode])\n"                  "open(name [, mode]) -> DBFFile\n\n"
518                  "open a DBFFile" },                  "opens a DBFFile" },
519          {"create", (PyCFunction)dbflib_create, METH_VARARGS,          {"create", (PyCFunction)dbflib_create, METH_VARARGS,
520                  "create(filename)\n"                  "create(name) -> DBFFile\n\n"
521                  "create a DBFFile" },                  "create a DBFFile" },
522          {NULL}          {NULL}
523  };  };
# Line 536  PyMODINIT_FUNC initdbflib(void) Line 534  PyMODINIT_FUNC initdbflib(void)
534          PYSHAPELIB_ADD_CONSTANT(FTString);          PYSHAPELIB_ADD_CONSTANT(FTString);
535          PYSHAPELIB_ADD_CONSTANT(FTInteger);          PYSHAPELIB_ADD_CONSTANT(FTInteger);
536          PYSHAPELIB_ADD_CONSTANT(FTDouble);          PYSHAPELIB_ADD_CONSTANT(FTDouble);
537            PYSHAPELIB_ADD_CONSTANT(FTLogical);
538          PYSHAPELIB_ADD_CONSTANT(FTInvalid);          PYSHAPELIB_ADD_CONSTANT(FTInvalid);
539          PyModule_AddIntConstant(module, "_have_commit", HAVE_UPDATE_HEADER);          PyModule_AddIntConstant(module, "_have_commit", HAVE_UPDATE_HEADER);
540  }  }

Legend:
Removed from v.2744  
changed lines
  Added in v.2750

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26