blob: 49007e2ff54f64cac7d7ea17e33ae7fc1b8dfe64 [file] [log] [blame]
Guido van Rossum049cd901996-12-05 23:30:48 +00001/*
Guido van Rossum17d53ec1999-06-15 14:35:48 +00002 * cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp
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"
Fred Drakeaef10002000-06-19 13:17:41 +000059"full generality of StringIO, but it provides enough for most\n"
Thomas Wouters7e474022000-07-16 12:04:32 +000060"applications and is especially useful in conjunction with the\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000061"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 Rossum17d53ec1999-06-15 14:35:48 +000081"cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\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
Jim Fultone60de4d2000-10-06 19:24:23 +000088#define UNLESS(E) if (!(E))
Guido van Rossum049cd901996-12-05 23:30:48 +000089
Guido van Rossum049cd901996-12-05 23:30:48 +000090
Jim Fultone60de4d2000-10-06 19:24:23 +000091/* Declaration for file-like objects that manage data as strings
Guido van Rossum049cd901996-12-05 23:30:48 +000092
Jim Fultone60de4d2000-10-06 19:24:23 +000093 The IOobject type should be though of as a common base type for
94 Iobjects, which provide input (read-only) StringIO objects and
95 Oobjects, which provide read-write objects. Most of the methods
96 depend only on common data.
97*/
Guido van Rossum049cd901996-12-05 23:30:48 +000098
99typedef struct {
100 PyObject_HEAD
101 char *buf;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000102 int pos, string_size;
Jim Fultone60de4d2000-10-06 19:24:23 +0000103} IOobject;
104
105#define IOOOBJECT(O) ((IOobject*)(O))
106
107/* Declarations for objects of type StringO */
108
109typedef struct { /* Subtype of IOobject */
110 PyObject_HEAD
111 char *buf;
112 int pos, string_size;
113
114 int buf_size, softspace;
115} Oobject;
116
117/* Declarations for objects of type StringI */
118
119typedef struct { /* Subtype of IOobject */
120 PyObject_HEAD
121 char *buf;
122 int pos, string_size;
Marc-André Lemburge47df7a2001-09-24 17:34:52 +0000123 /* We store a reference to the object here in order to keep
124 the buffer alive during the lifetime of the Iobject. */
Guido van Rossum049cd901996-12-05 23:30:48 +0000125 PyObject *pbuf;
126} Iobject;
127
Jim Fultone60de4d2000-10-06 19:24:23 +0000128/* IOobject (common) methods */
129
130static char IO_flush__doc__[] = "flush(): does nothing.";
131
132static int
133IO__opencheck(IOobject *self) {
134 UNLESS (self->buf) {
135 PyErr_SetString(PyExc_ValueError,
136 "I/O operation on closed file");
137 return 0;
138 }
139 return 1;
140}
141
142static PyObject *
143IO_flush(IOobject *self, PyObject *args) {
144
145 UNLESS (IO__opencheck(self)) return NULL;
146 UNLESS (PyArg_ParseTuple(args, ":flush")) return NULL;
147
148 Py_INCREF(Py_None);
149 return Py_None;
150}
151
152static char IO_getval__doc__[] =
153 "getvalue([use_pos]) -- Get the string value."
154 "\n"
155 "If use_pos is specified and is a true value, then the string returned\n"
156 "will include only the text up to the current file position.\n"
Guido van Rossum049cd901996-12-05 23:30:48 +0000157;
158
159static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000160IO_cgetval(PyObject *self) {
161 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
162 return PyString_FromStringAndSize(((IOobject*)self)->buf,
163 ((IOobject*)self)->pos);
Guido van Rossum049cd901996-12-05 23:30:48 +0000164}
165
Guido van Rossum049cd901996-12-05 23:30:48 +0000166static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000167IO_getval(IOobject *self, PyObject *args) {
168 PyObject *use_pos=Py_None;
169 int s;
170
171 UNLESS (IO__opencheck(self)) return NULL;
172 UNLESS (PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL;
173
174 if (PyObject_IsTrue(use_pos)) {
175 s=self->pos;
176 if (s > self->string_size) s=self->string_size;
177 }
178 else
179 s=self->string_size;
180 return PyString_FromStringAndSize(self->buf, s);
Guido van Rossum049cd901996-12-05 23:30:48 +0000181}
182
Jim Fultone60de4d2000-10-06 19:24:23 +0000183static char IO_isatty__doc__[] = "isatty(): always returns 0";
Guido van Rossum049cd901996-12-05 23:30:48 +0000184
185static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000186IO_isatty(IOobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000187
Jim Fultone60de4d2000-10-06 19:24:23 +0000188 UNLESS (PyArg_ParseTuple(args, ":isatty")) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000189
Jim Fultone60de4d2000-10-06 19:24:23 +0000190 return PyInt_FromLong(0);
Guido van Rossum049cd901996-12-05 23:30:48 +0000191}
192
Jim Fultone60de4d2000-10-06 19:24:23 +0000193static char IO_read__doc__[] =
Guido van Rossum049cd901996-12-05 23:30:48 +0000194"read([s]) -- Read s characters, or the rest of the string"
195;
196
197static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000198IO_cread(PyObject *self, char **output, int n) {
199 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000200
Jim Fultone60de4d2000-10-06 19:24:23 +0000201 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
202 l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos;
203 if (n < 0 || n > l) {
204 n = l;
205 if (n < 0) n=0;
206 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000207
Jim Fultone60de4d2000-10-06 19:24:23 +0000208 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
209 ((IOobject*)self)->pos += n;
210 return n;
Guido van Rossum049cd901996-12-05 23:30:48 +0000211}
212
213static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000214IO_read(IOobject *self, PyObject *args) {
215 int n = -1;
216 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000217
Jim Fultone60de4d2000-10-06 19:24:23 +0000218 UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000219
Jim Fultone60de4d2000-10-06 19:24:23 +0000220 if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000221
Jim Fultone60de4d2000-10-06 19:24:23 +0000222 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000223}
224
Jim Fultone60de4d2000-10-06 19:24:23 +0000225static char IO_readline__doc__[] =
Guido van Rossum049cd901996-12-05 23:30:48 +0000226"readline() -- Read one line"
227;
228
229static int
Jim Fultone60de4d2000-10-06 19:24:23 +0000230IO_creadline(PyObject *self, char **output) {
231 char *n, *s;
232 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000233
Jim Fultone60de4d2000-10-06 19:24:23 +0000234 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
235
236 for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos,
237 s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size;
238 n < s && *n != '\n'; n++);
239 if (n < s) n++;
240
241 *output=((IOobject*)self)->buf + ((IOobject*)self)->pos;
242 l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos;
243 ((IOobject*)self)->pos += l;
244 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000245}
246
247static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000248IO_readline(IOobject *self, PyObject *args) {
249 int n, m=-1;
250 char *output;
Guido van Rossum049cd901996-12-05 23:30:48 +0000251
Jim Fultone60de4d2000-10-06 19:24:23 +0000252 UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL;
253
254 if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL;
255 if (m >= 0 && m < n) {
256 m = n - m;
257 n -= m;
258 self->pos -= m;
259 }
260 return PyString_FromStringAndSize(output, n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000261}
262
Jim Fultone60de4d2000-10-06 19:24:23 +0000263static char IO_readlines__doc__[] =
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000264"readlines() -- Read all lines"
265;
266
267static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000268IO_readlines(IOobject *self, PyObject *args) {
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000269 int n;
270 char *output;
271 PyObject *result, *line;
272 int hint = 0, length = 0;
273
Jim Fultone60de4d2000-10-06 19:24:23 +0000274 UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL;
275
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000276 result = PyList_New(0);
277 if (!result)
278 return NULL;
279
Jim Fultone60de4d2000-10-06 19:24:23 +0000280 while (1){
281 if ( (n = IO_creadline((PyObject*)self,&output)) < 0)
282 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000283 if (n == 0)
284 break;
285 line = PyString_FromStringAndSize (output, n);
Jim Fultone60de4d2000-10-06 19:24:23 +0000286 if (!line)
287 goto err;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000288 PyList_Append (result, line);
289 Py_DECREF (line);
290 length += n;
291 if (hint > 0 && length >= hint)
292 break;
293 }
294 return result;
Jim Fultone60de4d2000-10-06 19:24:23 +0000295 err:
296 Py_DECREF(result);
297 return NULL;
298}
299
300static char IO_reset__doc__[] =
301"reset() -- Reset the file position to the beginning"
302;
303
304static PyObject *
305IO_reset(IOobject *self, PyObject *args) {
306
307 UNLESS (IO__opencheck(self)) return NULL;
308 UNLESS (PyArg_ParseTuple(args, ":reset")) return NULL;
309
310 self->pos = 0;
311
312 Py_INCREF(Py_None);
313 return Py_None;
314}
315
316static char IO_tell__doc__[] =
317"tell() -- get the current position.";
318
319static PyObject *
320IO_tell(IOobject *self, PyObject *args) {
321
322 UNLESS (IO__opencheck(self)) return NULL;
323 UNLESS (PyArg_ParseTuple(args, ":tell")) return NULL;
324
325 return PyInt_FromLong(self->pos);
326}
327
328static char IO_truncate__doc__[] =
329"truncate(): truncate the file at the current position.";
330
331static PyObject *
332IO_truncate(IOobject *self, PyObject *args) {
333 int pos = -1;
334
335 UNLESS (IO__opencheck(self)) return NULL;
336 UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL;
337 if (pos < 0) pos = self->pos;
338
339 if (self->string_size > pos) self->string_size = pos;
340
341 Py_INCREF(Py_None);
342 return Py_None;
343}
344
345
346
347
348/* Read-write object methods */
349
350static char O_seek__doc__[] =
351"seek(position) -- set the current position\n"
352"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
353
354static PyObject *
355O_seek(Oobject *self, PyObject *args) {
356 int position, mode = 0;
357
358 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
359 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
360 return NULL;
361
362 if (mode == 2) {
363 position += self->string_size;
364 }
365 else if (mode == 1) {
366 position += self->pos;
367 }
368
369 if (position > self->buf_size) {
370 self->buf_size*=2;
371 if (self->buf_size <= position) self->buf_size=position+1;
372 UNLESS (self->buf=(char*)
373 realloc(self->buf,self->buf_size*sizeof(char))) {
374 self->buf_size=self->pos=0;
375 return PyErr_NoMemory();
376 }
377 }
378 else if (position < 0) position=0;
379
380 self->pos=position;
381
382 while (--position >= self->string_size) self->buf[position]=0;
383
384 Py_INCREF(Py_None);
385 return Py_None;
Martin v. Löwisc912a3a2000-09-19 11:06:46 +0000386}
387
Guido van Rossum049cd901996-12-05 23:30:48 +0000388static char O_write__doc__[] =
389"write(s) -- Write a string to the file"
390"\n\nNote (hack:) writing None resets the buffer"
391;
392
393
394static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000395O_cwrite(PyObject *self, char *c, int l) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000396 int newl;
Guido van Rossum55702f81997-01-06 22:57:52 +0000397
Jim Fultone60de4d2000-10-06 19:24:23 +0000398 UNLESS (IO__opencheck(IOOOBJECT(self))) return -1;
Guido van Rossum049cd901996-12-05 23:30:48 +0000399
Jim Fultone60de4d2000-10-06 19:24:23 +0000400 newl=((Oobject*)self)->pos+l;
401 if (newl >= ((Oobject*)self)->buf_size) {
402 ((Oobject*)self)->buf_size*=2;
403 if (((Oobject*)self)->buf_size <= newl)
404 ((Oobject*)self)->buf_size=newl+1;
405 UNLESS (((Oobject*)self)->buf=
406 (char*)realloc(
407 ((Oobject*)self)->buf,
408 (((Oobject*)self)->buf_size) *sizeof(char))) {
409 PyErr_SetString(PyExc_MemoryError,"out of memory");
410 ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0;
411 return -1;
412 }
413 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000414
Jim Fultone60de4d2000-10-06 19:24:23 +0000415 memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l);
Guido van Rossum049cd901996-12-05 23:30:48 +0000416
Jim Fultone60de4d2000-10-06 19:24:23 +0000417 ((Oobject*)self)->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000418
Jim Fultone60de4d2000-10-06 19:24:23 +0000419 if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) {
420 ((Oobject*)self)->string_size = ((Oobject*)self)->pos;
421 }
422
423 return l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000424}
425
426static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000427O_write(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000428 char *c;
429 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000430
Marc-André Lemburge47df7a2001-09-24 17:34:52 +0000431 UNLESS (PyArg_ParseTuple(args, "s#:write", &c, &l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000432
Jim Fultone60de4d2000-10-06 19:24:23 +0000433 if (O_cwrite((PyObject*)self,c,l) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000434
Jim Fultone60de4d2000-10-06 19:24:23 +0000435 Py_INCREF(Py_None);
436 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000437}
438
439static char O_close__doc__[] = "close(): explicitly release resources held.";
440
441static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000442O_close(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000443
Jim Fultone60de4d2000-10-06 19:24:23 +0000444 UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000445
Jim Fultone60de4d2000-10-06 19:24:23 +0000446 if (self->buf != NULL) free(self->buf);
447 self->buf = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000448
Jim Fultone60de4d2000-10-06 19:24:23 +0000449 self->pos = self->string_size = self->buf_size = 0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000450
Jim Fultone60de4d2000-10-06 19:24:23 +0000451 Py_INCREF(Py_None);
452 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000453}
454
455
Guido van Rossum68de0641999-02-08 17:03:27 +0000456static char O_writelines__doc__[] =
457"writelines(sequence_of_strings): write each string";
Guido van Rossum049cd901996-12-05 23:30:48 +0000458static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000459O_writelines(Oobject *self, PyObject *args) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000460 PyObject *tmp = 0;
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000461 static PyObject *joiner = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000462
Jim Fultone60de4d2000-10-06 19:24:23 +0000463 UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000464
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000465 if (!joiner) {
466 PyObject *empty_string = PyString_FromString("");
467 if (empty_string == NULL)
468 return NULL;
469 joiner = PyObject_GetAttrString(empty_string, "join");
470 Py_DECREF(empty_string);
471 if (joiner == NULL)
472 return NULL;
473 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000474
Jim Fultone60de4d2000-10-06 19:24:23 +0000475 if (PyObject_Size(args) < 0) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000476
Jeremy Hyltoncafd4952001-02-09 23:44:22 +0000477 tmp = PyObject_CallFunction(joiner, "O", args);
Jim Fultone60de4d2000-10-06 19:24:23 +0000478 UNLESS (tmp) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000479
Jim Fultone60de4d2000-10-06 19:24:23 +0000480 args = Py_BuildValue("(O)", tmp);
481 Py_DECREF(tmp);
482 UNLESS (args) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000483
Jim Fultone60de4d2000-10-06 19:24:23 +0000484 tmp = O_write(self, args);
485 Py_DECREF(args);
486 return tmp;
Guido van Rossum049cd901996-12-05 23:30:48 +0000487}
488
489static struct PyMethodDef O_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000490 /* Common methods: */
491 {"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
492 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
493 {"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
494 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
495 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
496 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
497 {"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
498 {"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
499 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
500
501 /* Read-write StringIO specific methods: */
Guido van Rossum476e49f1998-12-15 21:43:15 +0000502 {"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000503 {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
504 {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000505 {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
506 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000507};
508
Guido van Rossum049cd901996-12-05 23:30:48 +0000509static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000510O_dealloc(Oobject *self) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000511 if (self->buf != NULL)
512 free(self->buf);
513 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000514}
515
516static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000517O_getattr(Oobject *self, char *name) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000518 if (strcmp(name, "softspace") == 0) {
519 return PyInt_FromLong(self->softspace);
520 }
521 return Py_FindMethod(O_methods, (PyObject *)self, name);
Guido van Rossum049cd901996-12-05 23:30:48 +0000522}
523
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000524static int
525O_setattr(Oobject *self, char *name, PyObject *value) {
526 long x;
527 if (strcmp(name, "softspace") != 0) {
528 PyErr_SetString(PyExc_AttributeError, name);
529 return -1;
530 }
531 x = PyInt_AsLong(value);
Jim Fultone60de4d2000-10-06 19:24:23 +0000532 if (x < 0 && PyErr_Occurred())
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000533 return -1;
534 self->softspace = x;
535 return 0;
536}
537
Guido van Rossum049cd901996-12-05 23:30:48 +0000538static char Otype__doc__[] =
539"Simple type for output to strings."
540;
541
542static PyTypeObject Otype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000543 PyObject_HEAD_INIT(NULL)
544 0, /*ob_size*/
545 "StringO", /*tp_name*/
546 sizeof(Oobject), /*tp_basicsize*/
547 0, /*tp_itemsize*/
548 /* methods */
549 (destructor)O_dealloc, /*tp_dealloc*/
550 (printfunc)0, /*tp_print*/
551 (getattrfunc)O_getattr, /*tp_getattr*/
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000552 (setattrfunc)O_setattr, /*tp_setattr*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000553 (cmpfunc)0, /*tp_compare*/
554 (reprfunc)0, /*tp_repr*/
555 0, /*tp_as_number*/
556 0, /*tp_as_sequence*/
557 0, /*tp_as_mapping*/
558 (hashfunc)0, /*tp_hash*/
559 (ternaryfunc)0, /*tp_call*/
560 (reprfunc)0, /*tp_str*/
561
562 /* Space for future expansion */
563 0L,0L,0L,0L,
564 Otype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000565};
566
Guido van Rossum142eeb81997-08-13 03:14:41 +0000567static PyObject *
568newOobject(int size) {
Jim Fultone60de4d2000-10-06 19:24:23 +0000569 Oobject *self;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000570
Jim Fultone60de4d2000-10-06 19:24:23 +0000571 self = PyObject_New(Oobject, &Otype);
572 if (self == NULL)
573 return NULL;
574 self->pos=0;
575 self->string_size = 0;
576 self->softspace = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000577
Jim Fultone60de4d2000-10-06 19:24:23 +0000578 UNLESS (self->buf=malloc(size*sizeof(char))) {
579 PyErr_SetString(PyExc_MemoryError,"out of memory");
580 self->buf_size = 0;
581 return NULL;
582 }
583
584 self->buf_size=size;
585 return (PyObject*)self;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000586}
587
Guido van Rossum049cd901996-12-05 23:30:48 +0000588/* End of code for StringO objects */
589/* -------------------------------------------------------- */
590
591static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000592I_close(Iobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000593
Jim Fultone60de4d2000-10-06 19:24:23 +0000594 UNLESS (PyArg_ParseTuple(args, ":close")) return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000595
Jim Fultone60de4d2000-10-06 19:24:23 +0000596 Py_XDECREF(self->pbuf);
597 self->pbuf = NULL;
598 self->buf = NULL;
599
600 self->pos = self->string_size = 0;
601
602 Py_INCREF(Py_None);
603 return Py_None;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000604}
605
606static PyObject *
Jim Fultone60de4d2000-10-06 19:24:23 +0000607I_seek(Iobject *self, PyObject *args) {
608 int position, mode = 0;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000609
Jim Fultone60de4d2000-10-06 19:24:23 +0000610 UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL;
611 UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode))
612 return NULL;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000613
Jim Fultone60de4d2000-10-06 19:24:23 +0000614 if (mode == 2) position += self->string_size;
615 else if (mode == 1) position += self->pos;
Guido van Rossum476e49f1998-12-15 21:43:15 +0000616
Jim Fultone60de4d2000-10-06 19:24:23 +0000617 if (position < 0) position=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000618
Jim Fultone60de4d2000-10-06 19:24:23 +0000619 self->pos=position;
620
621 Py_INCREF(Py_None);
622 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000623}
624
625static struct PyMethodDef I_methods[] = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000626 /* Common methods: */
627 {"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__},
628 {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__},
629 {"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__},
630 {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__},
631 {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__},
632 {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__},
633 {"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__},
634 {"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__},
635 {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__},
636
637 /* Read-only StringIO specific methods: */
Guido van Rossum476e49f1998-12-15 21:43:15 +0000638 {"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
Jim Fultone60de4d2000-10-06 19:24:23 +0000639 {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
Guido van Rossum476e49f1998-12-15 21:43:15 +0000640 {NULL, NULL}
Guido van Rossum049cd901996-12-05 23:30:48 +0000641};
642
Guido van Rossum049cd901996-12-05 23:30:48 +0000643static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000644I_dealloc(Iobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000645 Py_XDECREF(self->pbuf);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000646 PyObject_Del(self);
Guido van Rossum049cd901996-12-05 23:30:48 +0000647}
648
649static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000650I_getattr(Iobject *self, char *name) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000651 return Py_FindMethod(I_methods, (PyObject *)self, name);
652}
653
Barry Warsaw3e8be722001-09-22 04:36:49 +0000654static PyObject *
655I_getiter(Iobject *self)
656{
657 PyObject *myreadline = PyObject_GetAttrString((PyObject*)self,
658 "readline");
659 PyObject *emptystring = PyString_FromString("");
660 PyObject *iter = NULL;
661 if (!myreadline || !emptystring)
662 goto finally;
663
664 iter = PyCallIter_New(myreadline, emptystring);
665 finally:
666 Py_XDECREF(myreadline);
667 Py_XDECREF(emptystring);
668 return iter;
669}
670
671
Guido van Rossum049cd901996-12-05 23:30:48 +0000672static char Itype__doc__[] =
673"Simple type for treating strings as input file streams"
674;
675
676static PyTypeObject Itype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000677 PyObject_HEAD_INIT(NULL)
Barry Warsaw3e8be722001-09-22 04:36:49 +0000678 0, /*ob_size*/
679 "StringI", /*tp_name*/
680 sizeof(Iobject), /*tp_basicsize*/
681 0, /*tp_itemsize*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000682 /* methods */
Barry Warsaw3e8be722001-09-22 04:36:49 +0000683 (destructor)I_dealloc, /*tp_dealloc*/
684 (printfunc)0, /*tp_print*/
685 (getattrfunc)I_getattr, /*tp_getattr*/
686 (setattrfunc)0, /*tp_setattr*/
687 (cmpfunc)0, /*tp_compare*/
688 (reprfunc)0, /*tp_repr*/
689 0, /*tp_as_number*/
690 0, /*tp_as_sequence*/
691 0, /*tp_as_mapping*/
692 (hashfunc)0, /*tp_hash*/
693 (ternaryfunc)0, /*tp_call*/
694 (reprfunc)0, /*tp_str*/
695 0, /* tp_getattro */
696 0, /* tp_setattro */
697 0, /* tp_as_buffer */
698 Py_TPFLAGS_DEFAULT, /* tp_flags */
699 Itype__doc__, /* tp_doc */
700 0, /* tp_traverse */
701 0, /* tp_clear */
702 0, /* tp_richcompare */
703 0, /* tp_weaklistoffset */
704 (getiterfunc)I_getiter, /* tp_iter */
705 0, /* tp_iternext */
Guido van Rossum049cd901996-12-05 23:30:48 +0000706};
707
Guido van Rossum142eeb81997-08-13 03:14:41 +0000708static PyObject *
709newIobject(PyObject *s) {
710 Iobject *self;
711 char *buf;
712 int size;
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000713
Marc-André Lemburge47df7a2001-09-24 17:34:52 +0000714 if (PyObject_AsReadBuffer(s, (const void **)&buf, &size)) {
715 PyErr_Format(PyExc_TypeError, "expected read buffer, %.200s found",
Jeremy Hylton127b2ef2000-04-12 22:04:01 +0000716 s->ob_type->tp_name);
717 return NULL;
718 }
Jim Fultone60de4d2000-10-06 19:24:23 +0000719 UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000720 Py_INCREF(s);
721 self->buf=buf;
722 self->string_size=size;
723 self->pbuf=s;
724 self->pos=0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000725
726 return (PyObject*)self;
727}
728
Guido van Rossum049cd901996-12-05 23:30:48 +0000729/* End of code for StringI objects */
730/* -------------------------------------------------------- */
731
732
733static char IO_StringIO__doc__[] =
734"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
735;
736
737static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000738IO_StringIO(PyObject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000739 PyObject *s=0;
740
Jim Fultone60de4d2000-10-06 19:24:23 +0000741 if (!PyArg_ParseTuple(args, "|O:StringIO", &s)) return NULL;
742
743 if (s) return newIobject(s);
Guido van Rossum154417e1997-04-09 17:35:33 +0000744 return newOobject(128);
Guido van Rossum049cd901996-12-05 23:30:48 +0000745}
746
747/* List of methods defined in the module */
748
749static struct PyMethodDef IO_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000750 {"StringIO", (PyCFunction)IO_StringIO,
751 METH_VARARGS, IO_StringIO__doc__},
Guido van Rossum55702f81997-01-06 22:57:52 +0000752 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000753};
754
755
756/* Initialization function for the module (*must* be called initcStringIO) */
757
Guido van Rossum154417e1997-04-09 17:35:33 +0000758static struct PycStringIO_CAPI CAPI = {
Jim Fultone60de4d2000-10-06 19:24:23 +0000759 IO_cread,
760 IO_creadline,
Guido van Rossum154417e1997-04-09 17:35:33 +0000761 O_cwrite,
Jim Fultone60de4d2000-10-06 19:24:23 +0000762 IO_cgetval,
Guido van Rossum154417e1997-04-09 17:35:33 +0000763 newOobject,
764 newIobject,
765 &Itype,
766 &Otype,
767};
768
Guido van Rossum476e49f1998-12-15 21:43:15 +0000769#ifndef DL_EXPORT /* declarations for DLL import/export */
770#define DL_EXPORT(RTYPE) RTYPE
771#endif
Guido van Rossum3886bb61998-12-04 18:50:17 +0000772DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +0000773initcStringIO(void) {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000774 PyObject *m, *d, *v;
Guido van Rossum049cd901996-12-05 23:30:48 +0000775
Guido van Rossum154417e1997-04-09 17:35:33 +0000776
Guido van Rossum049cd901996-12-05 23:30:48 +0000777 /* Create the module and add the functions */
778 m = Py_InitModule4("cStringIO", IO_methods,
779 cStringIO_module_documentation,
780 (PyObject*)NULL,PYTHON_API_VERSION);
781
782 /* Add some symbolic constants to the module */
783 d = PyModule_GetDict(m);
Guido van Rossum049cd901996-12-05 23:30:48 +0000784
Guido van Rossum55702f81997-01-06 22:57:52 +0000785 /* Export C API */
Guido van Rossum154417e1997-04-09 17:35:33 +0000786 Itype.ob_type=&PyType_Type;
787 Otype.ob_type=&PyType_Type;
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000788 PyDict_SetItemString(d,"cStringIO_CAPI",
789 v = PyCObject_FromVoidPtr(&CAPI,NULL));
790 Py_XDECREF(v);
Guido van Rossum154417e1997-04-09 17:35:33 +0000791
792 /* Export Types */
Guido van Rossum049cd901996-12-05 23:30:48 +0000793 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
794 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
Guido van Rossum142eeb81997-08-13 03:14:41 +0000795
796 /* Maybe make certain warnings go away */
Jim Fultone60de4d2000-10-06 19:24:23 +0000797 if (0) PycString_IMPORT;
Guido van Rossum049cd901996-12-05 23:30:48 +0000798}