36 |
*/ |
*/ |
37 |
static int dbffile_init(DBFFileObject* self, PyObject* args, PyObject* kwds) |
static int dbffile_init(DBFFileObject* self, PyObject* args, PyObject* kwds) |
38 |
{ |
{ |
39 |
char* file; |
char* file = NULL; |
40 |
char* mode = "rb"; |
char* mode = "rb"; |
41 |
static char *kwlist[] = {"name", "mode", NULL}; |
static char *kwlist[] = {"name", "mode", NULL}; |
42 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:__init__", kwlist, |
|
43 |
Py_FileSystemDefaultEncoding, &file, &mode)) return -1; |
DBFClose(self->handle); |
44 |
|
self->handle = NULL; |
45 |
self->handle = DBFOpen(file, mode); |
|
46 |
|
#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES) |
47 |
|
if (GetVersion() < 0x80000000) { /* On NT, so wide API available */ |
48 |
|
PyObject *wfile; |
49 |
|
if (PyArg_ParseTupleAndKeywords(args, kwds, "U|s:DBFFile", kwlist, &wfile, &mode)) |
50 |
|
{ |
51 |
|
PyObject *wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL); |
52 |
|
if (!wmode) return -1; |
53 |
|
self->handle = DBFOpenW(PyUnicode_AS_UNICODE(wfile), PyUnicode_AS_UNICODE(wmode)); |
54 |
|
Py_DECREF(wmode); |
55 |
|
if (!self->handle) |
56 |
|
{ |
57 |
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile); |
58 |
|
return -1; |
59 |
|
} |
60 |
|
} |
61 |
|
else |
62 |
|
{ |
63 |
|
/* Drop the argument parsing error as narrow |
64 |
|
strings are also valid. */ |
65 |
|
PyErr_Clear(); |
66 |
|
} |
67 |
|
} |
68 |
|
#endif |
69 |
|
|
70 |
if (!self->handle) |
if (!self->handle) |
71 |
{ |
{ |
72 |
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|s:DBFFile", kwlist, |
73 |
|
Py_FileSystemDefaultEncoding, &file, &mode)) return -1; |
74 |
|
self->handle = DBFOpen(file, mode); |
75 |
|
|
76 |
|
if (!self->handle) |
77 |
|
{ |
78 |
|
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
79 |
|
PyMem_Free(file); |
80 |
|
return -1; |
81 |
|
} |
82 |
|
|
83 |
|
PyMem_Free(file); |
84 |
} |
} |
85 |
|
|
86 |
PyMem_Free(file); |
return 0; |
|
return self->handle ? 0 : -1; |
|
87 |
} |
} |
88 |
|
|
89 |
|
|
523 |
{ |
{ |
524 |
char* file; |
char* file; |
525 |
DBFFileObject* result; |
DBFFileObject* result; |
526 |
|
DBFHandle handle = NULL; |
527 |
|
int wideargument = 0; |
528 |
|
|
529 |
|
#if defined(SHPAPI_HAS_WIDE) && defined(Py_WIN_WIDE_FILENAMES) |
530 |
|
if (GetVersion() < 0x80000000) { /* On NT, so wide API available */ |
531 |
|
PyObject *wfile; |
532 |
|
if (PyArg_ParseTuple(args, "U:create", &wfile)) |
533 |
|
{ |
534 |
|
wideargument = 1; |
535 |
|
handle = DBFCreateW(PyUnicode_AS_UNICODE(wfile)); |
536 |
|
if (!handle) |
537 |
|
{ |
538 |
|
PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, wfile); |
539 |
|
return NULL; |
540 |
|
} |
541 |
|
} |
542 |
|
else |
543 |
|
{ |
544 |
|
/* Drop the argument parsing error as narrow |
545 |
|
strings are also valid. */ |
546 |
|
PyErr_Clear(); |
547 |
|
} |
548 |
|
} |
549 |
|
#endif |
550 |
|
|
551 |
if (!PyArg_ParseTuple(args, "et:create", Py_FileSystemDefaultEncoding, &file)) return NULL; |
if (!handle) |
552 |
|
{ |
553 |
|
if (!PyArg_ParseTuple(args, "et:create", Py_FileSystemDefaultEncoding, &file)) return NULL; |
554 |
|
handle = DBFCreate(file); |
555 |
|
if (!handle) |
556 |
|
{ |
557 |
|
PyErr_SetFromErrnoWithFilename(PyExc_IOError, file); |
558 |
|
PyMem_Free(file); |
559 |
|
return NULL; |
560 |
|
} |
561 |
|
PyMem_Free(file); |
562 |
|
} |
563 |
|
|
564 |
result = PyObject_New(DBFFileObject, &DBFFileType); |
result = PyObject_New(DBFFileObject, &DBFFileType); |
565 |
if (!result) |
if (!result) |
566 |
{ |
{ |
567 |
|
DBFClose(handle); |
568 |
return PyErr_NoMemory(); |
return PyErr_NoMemory(); |
569 |
} |
} |
570 |
|
|
571 |
result->handle = DBFCreate(file); |
result->handle = handle; |
|
if (!result->handle) |
|
|
{ |
|
|
PyObject_Del((PyObject*)result); |
|
|
PyErr_SetString(PyExc_RuntimeError, "Failed to create DBFFile"); |
|
|
return NULL; |
|
|
} |
|
|
|
|
572 |
return (PyObject*) result; |
return (PyObject*) result; |
573 |
} |
} |
574 |
|
|