-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_wvcsv.cc
More file actions
172 lines (142 loc) · 3.64 KB
/
Copy path_wvcsv.cc
File metadata and controls
172 lines (142 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <Python.h>
#include "wvcsv.h"
#include "wvstring.h"
#include <vector>
#include "wvbufstream.h"
#include "wvfdstream.h"
static PyObject *pywvcsv_dequote(PyObject *self, PyObject *args)
{
char *quoted;
if (!PyArg_ParseTuple(args, "et", "utf-8", "ed))
return NULL;
PyObject *r = Py_BuildValue("s", wvcsv_dequote(quoted, quoted));
PyMem_Free(quoted);
return r;
}
static PyObject *pywvcsv_quote(PyObject *self, PyObject *args)
{
const char *unquoted;
// Can take a 'None' type, which should be interpreted like any other.
// 'z' allows nulls, but doesn't decode unicode properly.
if (!PyArg_ParseTuple(args, "z", &unquoted) || unquoted)
{
PyErr_Clear();
if (!PyArg_ParseTuple(args, "et", "utf-8", &unquoted))
return NULL;
}
WvString s = wvcsv_quote(unquoted);
if (unquoted)
PyMem_Free((void *)unquoted);
return Py_BuildValue("s", s.cstr());
}
static PyObject *pywvcsv_splitline(PyObject *self, PyObject *args)
{
char *line;
std::vector<char *> v;
std::vector<size_t> lengths;
if (!PyArg_ParseTuple(args, "et", "utf-8", &line))
return NULL;
wvcsv_splitline(v, lengths, line, strlen(line), true);
Py_ssize_t l = v.size();
PyObject *r = PyTuple_New(l);
for (Py_ssize_t i = 0; i < l; ++i)
PyTuple_SET_ITEM(r, i, Py_BuildValue("s", v[i]));
PyMem_Free(line);
return r;
}
struct CsvContents
{
WvStream *contents;
WvDynBuf remainder;
PyObject *ref;
CsvContents(WvStream *c, PyObject *r = NULL)
{
contents = c;
if (r)
Py_INCREF(r);
ref = r;
}
~CsvContents()
{
delete contents;
if (ref)
{
Py_DECREF(ref);
}
}
};
static PyObject *pywvcsv_setup(PyObject *self, PyObject *args)
{
CsvContents *c;
char *line;
if (!PyArg_ParseTuple(args, "et", "utf-8", &line))
{
PyObject *f;
PyErr_Clear();
if (!PyArg_ParseTuple(args, "O", &f))
return NULL;
if (!PyFile_Check(f))
{
PyErr_SetString(PyExc_TypeError, "csvsetup needs a string/unicode "
"or file object");
return NULL;
}
//Handle case of file
c = new CsvContents(new WvFdStream(dup(fileno(PyFile_AsFile(f)))), f);
}
else
{
// Case of string
WvBufStream *s = new WvBufStream();
s->write(line, strlen(line)); // \0
s->seteof();
c = new CsvContents(s);
PyMem_Free(line);
}
PyObject *r = PyLong_FromLong((long)c);
Py_INCREF(r);
return r;
}
static PyObject *pywvcsv_takedown(PyObject *self, PyObject *args)
{
long idx = 0;
if (!PyArg_ParseTuple(args, "l", &idx))
return NULL;
CsvContents *c = (CsvContents *)idx;
delete c;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *pywvcsv_readline(PyObject *self, PyObject *args)
{
long idx = 0;
if (!PyArg_ParseTuple(args, "l", &idx))
return NULL;
CsvContents *c = (CsvContents *)idx;
char *r = wvcsv_readline(*c->contents, c->remainder);
if (!r)
{
Py_INCREF(Py_None);
return Py_None;
}
return Py_BuildValue("s", r);
}
static PyMethodDef wvcsv_methods[] = {
{ "dequote", pywvcsv_dequote, METH_VARARGS,
"Dequote one CSV cell." },
{ "quote", pywvcsv_quote, METH_VARARGS,
"Quote one CSV cell." },
{ "splitline", pywvcsv_splitline, METH_VARARGS,
"Take one CSV-encoded line and split it, decoding each cell." },
{ "readline", pywvcsv_readline, METH_VARARGS,
"Read one CSV-encoded line from preset buffer." },
{ "setup", pywvcsv_setup, METH_VARARGS,
"Set up CSV reading stuff." },
{ "takedown", pywvcsv_takedown, METH_VARARGS,
"Take down CSV reading stuff." },
{ NULL, NULL, 0, NULL }, // sentinel
};
PyMODINIT_FUNC init_wvcsv()
{
Py_InitModule("_wvcsv", wvcsv_methods);
}