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 |
|
&& !PyArg_ParseTuple(args, "Siii:add_field", &oname, &type, &width, &decimals)) return NULL; |
227 |
|
|
228 |
field = DBFAddField(self->handle, name, (DBFFieldType)type, width, decimals); |
name = encode_string(self->handle, oname); |
229 |
|
if (!name) return NULL; |
230 |
|
|
231 |
|
field = DBFAddField(self->handle, PyString_AsString(name), (DBFFieldType)type, width, decimals); |
232 |
|
Py_DECREF(name); |
233 |
|
|
234 |
if (field < 0) |
if (field < 0) |
235 |
{ |
{ |
383 |
|
|
384 |
|
|
385 |
/* write a single field of a record. */ |
/* write a single field of a record. */ |
386 |
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) |
387 |
{ |
{ |
388 |
PyObject* encoded_string = NULL; |
PyObject* string_value = NULL; |
|
char * string_value; |
|
389 |
int int_value; |
int int_value; |
390 |
double double_value; |
double double_value; |
391 |
int logical_value; |
int logical_value; |
399 |
switch (type) |
switch (type) |
400 |
{ |
{ |
401 |
case FTString: |
case FTString: |
402 |
encoded_string = encode_string(handle, value); |
string_value = encode_string(handle, value); |
403 |
if (!encoded_string) return 0; |
if (!string_value) return 0; |
404 |
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)) |
|
405 |
{ |
{ |
406 |
Py_DECREF(encoded_string); |
Py_DECREF(string_value); |
407 |
return 1; |
return 1; |
408 |
} |
} |
409 |
Py_DECREF(encoded_string); |
Py_DECREF(string_value); |
410 |
break; |
break; |
411 |
|
|
412 |
case FTInteger: |
case FTInteger: |
439 |
|
|
440 |
|
|
441 |
|
|
442 |
static PyObject* dbffile_write_field(DBFFileObject* self, PyObject* args) |
static PyObject* dbffile_write_attribute(DBFFileObject* self, PyObject* args) |
443 |
{ |
{ |
444 |
int record, field; |
int record, field; |
445 |
PyObject* value; |
PyObject* value; |
446 |
int type; |
int type; |
447 |
|
|
448 |
if (!PyArg_ParseTuple(args, "iiO:write_field", &record, &field, &value)) return NULL; |
if (!PyArg_ParseTuple(args, "iiO:write_attribute", &record, &field, &value)) return NULL; |
449 |
|
|
450 |
if (field < 0 || field >= DBFGetFieldCount(self->handle)) |
if (field < 0 || field >= DBFGetFieldCount(self->handle)) |
451 |
{ |
{ |
456 |
} |
} |
457 |
|
|
458 |
type = DBFGetFieldInfo(self->handle, field, NULL, NULL, NULL); |
type = DBFGetFieldInfo(self->handle, field, NULL, NULL, NULL); |
459 |
if (!do_write_field(self->handle, record, field, type, value)) return NULL; |
if (!do_write_attribute(self->handle, record, field, type, value)) return NULL; |
460 |
Py_RETURN_NONE; |
Py_RETURN_NONE; |
461 |
} |
} |
462 |
|
|
497 |
type = DBFGetFieldInfo(self->handle, i, NULL, NULL, NULL); |
type = DBFGetFieldInfo(self->handle, i, NULL, NULL, NULL); |
498 |
value = PySequence_GetItem(record_object, i); |
value = PySequence_GetItem(record_object, i); |
499 |
if (!value) return NULL; |
if (!value) return NULL; |
500 |
if (!do_write_field(self->handle, record, i, type, value)) |
if (!do_write_attribute(self->handle, record, i, type, value)) |
501 |
{ |
{ |
502 |
Py_DECREF(value); |
Py_DECREF(value); |
503 |
return NULL; |
return NULL; |
515 |
name[0] = '\0'; |
name[0] = '\0'; |
516 |
type = DBFGetFieldInfo(self->handle, i, name, NULL, NULL); |
type = DBFGetFieldInfo(self->handle, i, name, NULL, NULL); |
517 |
value = PyDict_GetItemString(record_object, name); |
value = PyDict_GetItemString(record_object, name); |
518 |
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; |
519 |
} |
} |
520 |
} |
} |
521 |
|
|
591 |
{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, |
{"read_record", (PyCFunction)dbffile_read_record, METH_VARARGS, |
592 |
"read_record(record_index) -> dict\n\n" |
"read_record(record_index) -> dict\n\n" |
593 |
"returns an entire record as a dictionary of field names and values"}, |
"returns an entire record as a dictionary of field names and values"}, |
594 |
{"write_field", (PyCFunction)dbffile_write_field, METH_VARARGS, |
{"write_attribute", (PyCFunction)dbffile_write_attribute, METH_VARARGS, |
595 |
"write_field(record_index, field_index, new_value)\n" |
"write_attribute(record_index, field_index, new_value)\n" |
596 |
"writes a single field of a record"}, |
"writes a single field of a record"}, |
597 |
{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, |
{"write_record", (PyCFunction)dbffile_write_record, METH_VARARGS, |
598 |
"write_record(record_index, record) -> record_index\n\n" |
"write_record(record_index, record) -> record_index\n\n" |