blob: 27be4980bf7fe4550f03cf1edf14b71802293819 [file] [log] [blame]
Guido van Rossum049cd901996-12-05 23:30:48 +00001/*
Guido van Rossum476e49f1998-12-15 21:43:15 +00002 * $Id$
Guido van Rossum7d9b4131998-11-25 16:17:32 +00003 *
4 * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * o Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the disclaimer that follows.
13 *
14 * o Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions, and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * o All advertising materials mentioning features or use of this
20 * software must display the following acknowledgement:
21 *
22 * This product includes software developed by Digital Creations
23 * and its contributors.
24 *
25 * o Neither the name of Digital Creations nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 *
30 * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
31 * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
33 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
34 * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
36 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
37 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
39 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
40 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 * DAMAGE.
42 *
43 #
44 # If you have questions regarding this software, contact:
45 #
46 # Digital Creations, L.C.
47 # 910 Princess Ann Street
48 # Fredericksburge, Virginia 22401
49 #
50 # info@digicool.com
51 #
52 # (540) 371-6909
53 */
Guido van Rossum049cd901996-12-05 23:30:48 +000054static char cStringIO_module_documentation[] =
55"A simple fast partial StringIO replacement.\n"
56"\n"
57"This module provides a simple useful replacement for\n"
58"the StringIO module that is written in C. It does not provide the\n"
59"full generality if StringIO, but it provides anough for most\n"
60"applications and is especially useful in conjuction with the\n"
61"pickle module.\n"
62"\n"
63"Usage:\n"
64"\n"
65" from cStringIO import StringIO\n"
66"\n"
67" an_output_stream=StringIO()\n"
68" an_output_stream.write(some_stuff)\n"
69" ...\n"
70" value=an_output_stream.getvalue() # str(an_output_stream) works too!\n"
71"\n"
72" an_input_stream=StringIO(a_string)\n"
73" spam=an_input_stream.readline()\n"
74" spam=an_input_stream.read(5)\n"
Guido van Rossum7d9b4131998-11-25 16:17:32 +000075" an_input_stream.seek(0) # OK, start over\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000076" spam=an_input_stream.read() # and read it all\n"
77" \n"
78"If someone else wants to provide a more complete implementation,\n"
79"go for it. :-) \n"
Guido van Rossum142eeb81997-08-13 03:14:41 +000080"\n"
Guido van Rossum476e49f1998-12-15 21:43:15 +000081"$Id$\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000082;
83
84#include "Python.h"
85#include "import.h"
Guido van Rossum154417e1997-04-09 17:35:33 +000086#include "cStringIO.h"
Guido van Rossum049cd901996-12-05 23:30:48 +000087
Guido van Rossum049cd901996-12-05 23:30:48 +000088#define UNLESS(E) if(!(E))
Guido van Rossum049cd901996-12-05 23:30:48 +000089
Guido van Rossum049cd901996-12-05 23:30:48 +000090/* Declarations for objects of type StringO */
91
92typedef struct {
93 PyObject_HEAD
94 char *buf;
Guido van Rossum476e49f1998-12-15 21:43:15 +000095 int pos, string_size, buf_size, softspace;
Guido van Rossum049cd901996-12-05 23:30:48 +000096} Oobject;
97
Guido van Rossum049cd901996-12-05 23:30:48 +000098/* Declarations for objects of type StringI */
99
100typedef struct {
101 PyObject_HEAD
102 char *buf;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000103 int pos, string_size;
Guido van Rossum049cd901996-12-05 23:30:48 +0000104 PyObject *pbuf;
105} Iobject;
106
Guido van Rossum049cd901996-12-05 23:30:48 +0000107static char O_reset__doc__[] =
108"reset() -- Reset the file position to the beginning"
109;
110
111static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000112O_reset(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000113 self->pos = 0;
114
115 Py_INCREF(Py_None);
116 return Py_None;
117}
118
119
120static char O_tell__doc__[] =
121"tell() -- get the current position.";
122
123static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000124O_tell(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000125 return PyInt_FromLong(self->pos);
126}
127
128
129static char O_seek__doc__[] =
130"seek(position) -- set the current position\n"
131"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
132
133static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000134O_seek(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000135 int position, mode = 0;
136
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000137 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000138 return NULL;
139 }
140
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000141 if (mode == 2) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000142 position += self->string_size;
143 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000144 else if (mode == 1) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000145 position += self->pos;
146 }
147
Guido van Rossum7d9b4131998-11-25 16:17:32 +0000148 if (position > self->buf_size) {
149 self->buf_size*=2;
150 if(self->buf_size <= position) self->buf_size=position+1;
151 UNLESS(self->buf=(char*)realloc(self->buf,self->buf_size*sizeof(char))) {
152 self->buf_size=self->pos=0;
153 return PyErr_NoMemory();
154 }
155 }
156 else if(position < 0) position=0;
157
158 self->pos=position;
159
160 while(--position >= self->string_size) self->buf[position]=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000161
Guido van Rossum55702f81997-01-06 22:57:52 +0000162 Py_INCREF(Py_None);
163 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000164}
165
166static char O_read__doc__[] =
167"read([s]) -- Read s characters, or the rest of the string"
168;
169
170static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000171O_cread(PyObject *self, char **output, int n) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000172 int l;
173
Guido van Rossum154417e1997-04-09 17:35:33 +0000174 l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000175 if (n < 0 || n > l) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000176 n = l;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000177 if (n < 0) n=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000178 }
179
Guido van Rossum154417e1997-04-09 17:35:33 +0000180 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
181 ((Oobject*)self)->pos += n;
Guido van Rossum049cd901996-12-05 23:30:48 +0000182 return n;
183}
184
185static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000186O_read(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000187 int n = -1;
188 char *output;
189
190 UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL;
191
Guido van Rossum154417e1997-04-09 17:35:33 +0000192 n=O_cread((PyObject*)self,&output,n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000193
194 return PyString_FromStringAndSize(output, n);
195}
196
197
198static char O_readline__doc__[] =
199"readline() -- Read one line"
200;
201
202static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000203O_creadline(PyObject *self, char **output) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000204 char *n, *s;
205 int l;
206
Guido van Rossum154417e1997-04-09 17:35:33 +0000207 for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos,
208 s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size;
Guido van Rossum049cd901996-12-05 23:30:48 +0000209 n < s && *n != '\n'; n++);
210 if (n < s) n++;
211
Guido van Rossum154417e1997-04-09 17:35:33 +0000212 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
213 l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos;
214 ((Oobject*)self)->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000215 return l;
216}
217
218static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000219O_readline(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000220 int n;
221 char *output;
222
Guido van Rossum154417e1997-04-09 17:35:33 +0000223 n=O_creadline((PyObject*)self,&output);
Guido van Rossum049cd901996-12-05 23:30:48 +0000224 return PyString_FromStringAndSize(output, n);
225}
226
227static char O_write__doc__[] =
228"write(s) -- Write a string to the file"
229"\n\nNote (hack:) writing None resets the buffer"
230;
231
232
233static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000234O_cwrite(PyObject *self, char *c, int l) {
Barry Warsaw61a63e11997-01-14 17:38:28 +0000235 int newl;
Guido van Rossum55702f81997-01-06 22:57:52 +0000236
Guido van Rossum154417e1997-04-09 17:35:33 +0000237 newl=((Oobject*)self)->pos+l;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000238 if(newl >= ((Oobject*)self)->buf_size) {
Guido van Rossum154417e1997-04-09 17:35:33 +0000239 ((Oobject*)self)->buf_size*=2;
240 if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1;
241 UNLESS(((Oobject*)self)->buf=
242 (char*)realloc(((Oobject*)self)->buf,
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000243 (((Oobject*)self)->buf_size) *sizeof(char))) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000244 PyErr_SetString(PyExc_MemoryError,"out of memory");
Guido van Rossum154417e1997-04-09 17:35:33 +0000245 ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000246 return -1;
247 }
248 }
249
Guido van Rossum154417e1997-04-09 17:35:33 +0000250 memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l);
Guido van Rossum049cd901996-12-05 23:30:48 +0000251
Guido van Rossum154417e1997-04-09 17:35:33 +0000252 ((Oobject*)self)->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000253
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000254 if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) {
Guido van Rossum154417e1997-04-09 17:35:33 +0000255 ((Oobject*)self)->string_size = ((Oobject*)self)->pos;
Guido van Rossum049cd901996-12-05 23:30:48 +0000256 }
257
258 return l;
259}
260
261static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000262O_write(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000263 PyObject *s;
Barry Warsaw61a63e11997-01-14 17:38:28 +0000264 char *c;
265 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000266
Guido van Rossum476e49f1998-12-15 21:43:15 +0000267 UNLESS(PyArg_ParseTuple(args, "O", &s)) return NULL;
Guido van Rossum55702f81997-01-06 22:57:52 +0000268 UNLESS(-1 != (l=PyString_Size(s))) return NULL;
269 UNLESS(c=PyString_AsString(s)) return NULL;
Guido van Rossum154417e1997-04-09 17:35:33 +0000270 UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000271
Guido van Rossum55702f81997-01-06 22:57:52 +0000272 Py_INCREF(Py_None);
273 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000274}
275
Guido van Rossum476e49f1998-12-15 21:43:15 +0000276static char O_getval__doc__[] =
277 "getvalue([use_pos]) -- Get the string value."
278 "\n"
279 "If use_pos is specified and is a true value, then the string returned\n"
280 "will include only the text up to the current file position.\n"
281;
282
Guido van Rossum049cd901996-12-05 23:30:48 +0000283static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000284O_getval(Oobject *self, PyObject *args) {
Guido van Rossum476e49f1998-12-15 21:43:15 +0000285 PyObject *use_pos=Py_None;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000286 int s;
287
288 use_pos=Py_None;
289 UNLESS(PyArg_ParseTuple(args,"|O",&use_pos)) return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000290 if(PyObject_IsTrue(use_pos)) {
291 s=self->pos;
292 if (s > self->string_size) s=self->string_size;
293 }
294 else
295 s=self->string_size;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000296 return PyString_FromStringAndSize(self->buf, s);
Guido van Rossum154417e1997-04-09 17:35:33 +0000297}
298
299static PyObject *
300O_cgetval(PyObject *self) {
301 return PyString_FromStringAndSize(((Oobject*)self)->buf,
302 ((Oobject*)self)->pos);
Guido van Rossum049cd901996-12-05 23:30:48 +0000303}
304
305static char O_truncate__doc__[] =
306"truncate(): truncate the file at the current position.";
307
308static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000309O_truncate(Oobject *self, PyObject *args) {
Guido van Rossum476e49f1998-12-15 21:43:15 +0000310 if (self->string_size > self->pos)
311 self->string_size = self->pos;
Guido van Rossum049cd901996-12-05 23:30:48 +0000312 Py_INCREF(Py_None);
313 return Py_None;
314}
315
316static char O_isatty__doc__[] = "isatty(): always returns 0";
317
318static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000319O_isatty(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000320 return PyInt_FromLong(0);
321}
322
323static char O_close__doc__[] = "close(): explicitly release resources held.";
324
325static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000326O_close(Oobject *self, PyObject *args) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000327 if (self->buf != NULL)
328 free(self->buf);
329 self->buf = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000330
331 self->pos = self->string_size = self->buf_size = 0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000332
333 Py_INCREF(Py_None);
334 return Py_None;
335}
336
337static char O_flush__doc__[] = "flush(): does nothing.";
338
339static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000340O_flush(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000341 Py_INCREF(Py_None);
342 return Py_None;
343}
344
345
346static char O_writelines__doc__[] = "blah";
347static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000348O_writelines(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000349 PyObject *string_module = 0;
350 static PyObject *string_joinfields = 0;
351
Guido van Rossum476e49f1998-12-15 21:43:15 +0000352 UNLESS(PyArg_ParseTuple(args, "O", args)) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000353 return NULL;
354 }
355
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000356 if (!string_joinfields) {
357 UNLESS(string_module = PyImport_ImportModule("string")) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000358 return NULL;
359 }
360
361 UNLESS(string_joinfields=
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000362 PyObject_GetAttrString(string_module, "joinfields")) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000363 return NULL;
364 }
365
366 Py_DECREF(string_module);
367 }
368
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000369 if (PyObject_Length(args) == -1) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000370 return NULL;
371 }
372
373 return O_write(self,
374 PyObject_CallFunction(string_joinfields, "Os", args, ""));
375}
376
377static struct PyMethodDef O_methods[] = {
Guido van Rossum476e49f1998-12-15 21:43:15 +0000378 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
379 {"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
380 {"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
381 {"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
382 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
383 {"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
384 {"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__},
385 {"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__},
386 {"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__},
387 {"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
388 {"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__},
389 {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
390 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000391};
392
Guido van Rossum049cd901996-12-05 23:30:48 +0000393
394static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000395O_dealloc(Oobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000396 if (self->buf != NULL)
397 free(self->buf);
Guido van Rossum049cd901996-12-05 23:30:48 +0000398 PyMem_DEL(self);
399}
400
401static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000402O_getattr(Oobject *self, char *name) {
Guido van Rossum7d9b4131998-11-25 16:17:32 +0000403 if (strcmp(name, "softspace") == 0) {
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000404 return PyInt_FromLong(self->softspace);
405 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000406 return Py_FindMethod(O_methods, (PyObject *)self, name);
407}
408
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000409static int
410O_setattr(Oobject *self, char *name, PyObject *value) {
411 long x;
412 if (strcmp(name, "softspace") != 0) {
413 PyErr_SetString(PyExc_AttributeError, name);
414 return -1;
415 }
416 x = PyInt_AsLong(value);
417 if (x == -1 && PyErr_Occurred())
418 return -1;
419 self->softspace = x;
420 return 0;
421}
422
Guido van Rossum049cd901996-12-05 23:30:48 +0000423static char Otype__doc__[] =
424"Simple type for output to strings."
425;
426
427static PyTypeObject Otype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000428 PyObject_HEAD_INIT(NULL)
429 0, /*ob_size*/
430 "StringO", /*tp_name*/
431 sizeof(Oobject), /*tp_basicsize*/
432 0, /*tp_itemsize*/
433 /* methods */
434 (destructor)O_dealloc, /*tp_dealloc*/
435 (printfunc)0, /*tp_print*/
436 (getattrfunc)O_getattr, /*tp_getattr*/
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000437 (setattrfunc)O_setattr, /*tp_setattr*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000438 (cmpfunc)0, /*tp_compare*/
439 (reprfunc)0, /*tp_repr*/
440 0, /*tp_as_number*/
441 0, /*tp_as_sequence*/
442 0, /*tp_as_mapping*/
443 (hashfunc)0, /*tp_hash*/
444 (ternaryfunc)0, /*tp_call*/
445 (reprfunc)0, /*tp_str*/
446
447 /* Space for future expansion */
448 0L,0L,0L,0L,
449 Otype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000450};
451
Guido van Rossum142eeb81997-08-13 03:14:41 +0000452static PyObject *
453newOobject(int size) {
454 Oobject *self;
455
456 self = PyObject_NEW(Oobject, &Otype);
457 if (self == NULL)
458 return NULL;
459 self->pos=0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000460 self->string_size = 0;
461 self->softspace = 0;
462
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000463 UNLESS(self->buf=malloc(size*sizeof(char))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000464 PyErr_SetString(PyExc_MemoryError,"out of memory");
465 self->buf_size = 0;
466 return NULL;
467 }
468
469 self->buf_size=size;
470 return (PyObject*)self;
471}
472
Guido van Rossum049cd901996-12-05 23:30:48 +0000473/* End of code for StringO objects */
474/* -------------------------------------------------------- */
475
476static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000477I_close(Iobject *self, PyObject *args) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000478 Py_XDECREF(self->pbuf);
479 self->pbuf = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000480
481 self->pos = self->string_size = 0;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000482
483 Py_INCREF(Py_None);
484 return Py_None;
485}
486
487static PyObject *
488I_seek(Oobject *self, PyObject *args) {
489 int position, mode = 0;
490
491 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) {
492 return NULL;
493 }
494
495 if (mode == 2) {
496 position += self->string_size;
497 }
498 else if (mode == 1) {
499 position += self->pos;
500 }
501
502 if(position < 0) position=0;
503
504 self->pos=position;
Guido van Rossum049cd901996-12-05 23:30:48 +0000505
506 Py_INCREF(Py_None);
507 return Py_None;
508}
509
510static struct PyMethodDef I_methods[] = {
Guido van Rossum476e49f1998-12-15 21:43:15 +0000511 {"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
512 {"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
513 {"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
514 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
515 {"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
516 {"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__},
517 {"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__},
518 {"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__},
519 {"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
520 {"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__},
521 {NULL, NULL}
Guido van Rossum049cd901996-12-05 23:30:48 +0000522};
523
Guido van Rossum049cd901996-12-05 23:30:48 +0000524static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000525I_dealloc(Iobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000526 Py_XDECREF(self->pbuf);
Guido van Rossum049cd901996-12-05 23:30:48 +0000527 PyMem_DEL(self);
528}
529
530static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000531I_getattr(Iobject *self, char *name) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000532 return Py_FindMethod(I_methods, (PyObject *)self, name);
533}
534
535static char Itype__doc__[] =
536"Simple type for treating strings as input file streams"
537;
538
539static PyTypeObject Itype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000540 PyObject_HEAD_INIT(NULL)
541 0, /*ob_size*/
542 "StringI", /*tp_name*/
543 sizeof(Iobject), /*tp_basicsize*/
544 0, /*tp_itemsize*/
545 /* methods */
546 (destructor)I_dealloc, /*tp_dealloc*/
547 (printfunc)0, /*tp_print*/
548 (getattrfunc)I_getattr, /*tp_getattr*/
549 (setattrfunc)0, /*tp_setattr*/
550 (cmpfunc)0, /*tp_compare*/
551 (reprfunc)0, /*tp_repr*/
552 0, /*tp_as_number*/
553 0, /*tp_as_sequence*/
554 0, /*tp_as_mapping*/
555 (hashfunc)0, /*tp_hash*/
556 (ternaryfunc)0, /*tp_call*/
557 (reprfunc)0, /*tp_str*/
558
559 /* Space for future expansion */
560 0L,0L,0L,0L,
561 Itype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000562};
563
Guido van Rossum142eeb81997-08-13 03:14:41 +0000564static PyObject *
565newIobject(PyObject *s) {
566 Iobject *self;
567 char *buf;
568 int size;
569
570 UNLESS(buf=PyString_AsString(s)) return NULL;
571 UNLESS(-1 != (size=PyString_Size(s))) return NULL;
572 UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL;
573 Py_INCREF(s);
574 self->buf=buf;
575 self->string_size=size;
576 self->pbuf=s;
577 self->pos=0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000578
579 return (PyObject*)self;
580}
581
Guido van Rossum049cd901996-12-05 23:30:48 +0000582/* End of code for StringI objects */
583/* -------------------------------------------------------- */
584
585
586static char IO_StringIO__doc__[] =
587"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
588;
589
590static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000591IO_StringIO(PyObject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000592 PyObject *s=0;
593
Guido van Rossum7d9b4131998-11-25 16:17:32 +0000594 UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL;
Guido van Rossum154417e1997-04-09 17:35:33 +0000595 if(s) return newIobject(s);
596 return newOobject(128);
Guido van Rossum049cd901996-12-05 23:30:48 +0000597}
598
599/* List of methods defined in the module */
600
601static struct PyMethodDef IO_methods[] = {
Guido van Rossum55702f81997-01-06 22:57:52 +0000602 {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__},
603 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000604};
605
606
607/* Initialization function for the module (*must* be called initcStringIO) */
608
Guido van Rossum154417e1997-04-09 17:35:33 +0000609static struct PycStringIO_CAPI CAPI = {
610 O_cread,
611 O_creadline,
612 O_cwrite,
613 O_cgetval,
614 newOobject,
615 newIobject,
616 &Itype,
617 &Otype,
618};
619
Guido van Rossum476e49f1998-12-15 21:43:15 +0000620#ifndef DL_EXPORT /* declarations for DLL import/export */
621#define DL_EXPORT(RTYPE) RTYPE
622#endif
Guido van Rossum3886bb61998-12-04 18:50:17 +0000623DL_EXPORT(void)
Guido van Rossum154417e1997-04-09 17:35:33 +0000624initcStringIO() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000625 PyObject *m, *d, *v;
Guido van Rossum049cd901996-12-05 23:30:48 +0000626
Guido van Rossum154417e1997-04-09 17:35:33 +0000627
Guido van Rossum049cd901996-12-05 23:30:48 +0000628 /* Create the module and add the functions */
629 m = Py_InitModule4("cStringIO", IO_methods,
630 cStringIO_module_documentation,
631 (PyObject*)NULL,PYTHON_API_VERSION);
632
633 /* Add some symbolic constants to the module */
634 d = PyModule_GetDict(m);
Guido van Rossum049cd901996-12-05 23:30:48 +0000635
Guido van Rossum55702f81997-01-06 22:57:52 +0000636 /* Export C API */
Guido van Rossum154417e1997-04-09 17:35:33 +0000637 Itype.ob_type=&PyType_Type;
638 Otype.ob_type=&PyType_Type;
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000639 PyDict_SetItemString(d,"cStringIO_CAPI",
640 v = PyCObject_FromVoidPtr(&CAPI,NULL));
641 Py_XDECREF(v);
Guido van Rossum154417e1997-04-09 17:35:33 +0000642
643 /* Export Types */
Guido van Rossum049cd901996-12-05 23:30:48 +0000644 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
645 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
Guido van Rossum142eeb81997-08-13 03:14:41 +0000646
647 /* Maybe make certain warnings go away */
648 if(0) PycString_IMPORT;
Guido van Rossum049cd901996-12-05 23:30:48 +0000649
Guido van Rossum049cd901996-12-05 23:30:48 +0000650 /* Check for errors */
Guido van Rossum154417e1997-04-09 17:35:33 +0000651 if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO");
Guido van Rossum049cd901996-12-05 23:30:48 +0000652}