blob: 494d895d3d91df9b3b1745343558f891640c70c3 [file] [log] [blame]
Guido van Rossum049cd901996-12-05 23:30:48 +00001/*
2
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003 cStringIO.c,v 1.23 1997/12/04 00:12:05 jim Exp
Guido van Rossum049cd901996-12-05 23:30:48 +00004
5 A simple fast partial StringIO replacement.
6
7
8
9 Copyright
10
11 Copyright 1996 Digital Creations, L.C., 910 Princess Anne
12 Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
13 rights reserved. Copyright in this software is owned by DCLC,
14 unless otherwise indicated. Permission to use, copy and
15 distribute this software is hereby granted, provided that the
16 above copyright notice appear in all copies and that both that
17 copyright notice and this permission notice appear. Note that
18 any product, process or technology described in this software
19 may be the subject of other Intellectual Property rights
20 reserved by Digital Creations, L.C. and are not licensed
21 hereunder.
22
23 Trademarks
24
25 Digital Creations & DCLC, are trademarks of Digital Creations, L.C..
26 All other trademarks are owned by their respective companies.
27
28 No Warranty
29
30 The software is provided "as is" without warranty of any kind,
31 either express or implied, including, but not limited to, the
32 implied warranties of merchantability, fitness for a particular
33 purpose, or non-infringement. This software could include
34 technical inaccuracies or typographical errors. Changes are
35 periodically made to the software; these changes will be
36 incorporated in new editions of the software. DCLC may make
37 improvements and/or changes in this software at any time
38 without notice.
39
40 Limitation Of Liability
41
42 In no event will DCLC be liable for direct, indirect, special,
43 incidental, economic, cover, or consequential damages arising
44 out of the use of or inability to use this software even if
45 advised of the possibility of such damages. Some states do not
46 allow the exclusion or limitation of implied warranties or
47 limitation of liability for incidental or consequential
48 damages, so the above limitation or exclusion may not apply to
49 you.
50
51 If you have questions regarding this software,
52 contact:
53
Guido van Rossum142eeb81997-08-13 03:14:41 +000054 info@digicool.com
Guido van Rossum049cd901996-12-05 23:30:48 +000055 Digital Creations L.C.
56
57 (540) 371-6909
58
59
Guido van Rossum049cd901996-12-05 23:30:48 +000060*/
61static char cStringIO_module_documentation[] =
62"A simple fast partial StringIO replacement.\n"
63"\n"
64"This module provides a simple useful replacement for\n"
65"the StringIO module that is written in C. It does not provide the\n"
66"full generality if StringIO, but it provides anough for most\n"
67"applications and is especially useful in conjuction with the\n"
68"pickle module.\n"
69"\n"
70"Usage:\n"
71"\n"
72" from cStringIO import StringIO\n"
73"\n"
74" an_output_stream=StringIO()\n"
75" an_output_stream.write(some_stuff)\n"
76" ...\n"
77" value=an_output_stream.getvalue() # str(an_output_stream) works too!\n"
78"\n"
79" an_input_stream=StringIO(a_string)\n"
80" spam=an_input_stream.readline()\n"
81" spam=an_input_stream.read(5)\n"
Fred Drake10032eb1998-04-11 19:54:54 +000082" an_input_stream.reset() # OK, start over\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000083" spam=an_input_stream.read() # and read it all\n"
84" \n"
85"If someone else wants to provide a more complete implementation,\n"
86"go for it. :-) \n"
Guido van Rossum142eeb81997-08-13 03:14:41 +000087"\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +000088"cStringIO.c,v 1.23 1997/12/04 00:12:05 jim Exp\n"
Guido van Rossum049cd901996-12-05 23:30:48 +000089;
90
91#include "Python.h"
92#include "import.h"
Guido van Rossum154417e1997-04-09 17:35:33 +000093#include "cStringIO.h"
Guido van Rossum049cd901996-12-05 23:30:48 +000094
Guido van Rossum049cd901996-12-05 23:30:48 +000095#define UNLESS(E) if(!(E))
Guido van Rossum049cd901996-12-05 23:30:48 +000096
Guido van Rossum049cd901996-12-05 23:30:48 +000097/* Declarations for objects of type StringO */
98
99typedef struct {
100 PyObject_HEAD
101 char *buf;
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000102 int pos, string_size, buf_size, closed, softspace;
Guido van Rossum049cd901996-12-05 23:30:48 +0000103} Oobject;
104
Guido van Rossum049cd901996-12-05 23:30:48 +0000105/* Declarations for objects of type StringI */
106
107typedef struct {
108 PyObject_HEAD
109 char *buf;
110 int pos, string_size, closed;
111 PyObject *pbuf;
112} Iobject;
113
Guido van Rossum049cd901996-12-05 23:30:48 +0000114static char O_reset__doc__[] =
115"reset() -- Reset the file position to the beginning"
116;
117
118static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000119O_reset(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000120 self->pos = 0;
121
122 Py_INCREF(Py_None);
123 return Py_None;
124}
125
126
127static char O_tell__doc__[] =
128"tell() -- get the current position.";
129
130static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000131O_tell(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000132 return PyInt_FromLong(self->pos);
133}
134
135
136static char O_seek__doc__[] =
137"seek(position) -- set the current position\n"
138"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
139
140static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000141O_seek(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000142 int position, mode = 0;
143
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000144 UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000145 return NULL;
146 }
147
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000148 if (mode == 2) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000149 position += self->string_size;
150 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000151 else if (mode == 1) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000152 position += self->pos;
153 }
154
155 self->pos = (position > self->string_size ? self->string_size :
156 (position < 0 ? 0 : position));
157
Guido van Rossum55702f81997-01-06 22:57:52 +0000158 Py_INCREF(Py_None);
159 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000160}
161
162static char O_read__doc__[] =
163"read([s]) -- Read s characters, or the rest of the string"
164;
165
166static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000167O_cread(PyObject *self, char **output, int n) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000168 int l;
169
Guido van Rossum154417e1997-04-09 17:35:33 +0000170 l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000171 if (n < 0 || n > l) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000172 n = l;
173 }
174
Guido van Rossum154417e1997-04-09 17:35:33 +0000175 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
176 ((Oobject*)self)->pos += n;
Guido van Rossum049cd901996-12-05 23:30:48 +0000177 return n;
178}
179
180static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000181O_read(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000182 int n = -1;
183 char *output;
184
185 UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL;
186
Guido van Rossum154417e1997-04-09 17:35:33 +0000187 n=O_cread((PyObject*)self,&output,n);
Guido van Rossum049cd901996-12-05 23:30:48 +0000188
189 return PyString_FromStringAndSize(output, n);
190}
191
192
193static char O_readline__doc__[] =
194"readline() -- Read one line"
195;
196
197static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000198O_creadline(PyObject *self, char **output) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000199 char *n, *s;
200 int l;
201
Guido van Rossum154417e1997-04-09 17:35:33 +0000202 for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos,
203 s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size;
Guido van Rossum049cd901996-12-05 23:30:48 +0000204 n < s && *n != '\n'; n++);
205 if (n < s) n++;
206
Guido van Rossum154417e1997-04-09 17:35:33 +0000207 *output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
208 l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos;
209 ((Oobject*)self)->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000210 return l;
211}
212
213static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000214O_readline(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000215 int n;
216 char *output;
217
Guido van Rossum154417e1997-04-09 17:35:33 +0000218 n=O_creadline((PyObject*)self,&output);
Guido van Rossum049cd901996-12-05 23:30:48 +0000219 return PyString_FromStringAndSize(output, n);
220}
221
222static char O_write__doc__[] =
223"write(s) -- Write a string to the file"
224"\n\nNote (hack:) writing None resets the buffer"
225;
226
227
228static int
Guido van Rossum154417e1997-04-09 17:35:33 +0000229O_cwrite(PyObject *self, char *c, int l) {
Barry Warsaw61a63e11997-01-14 17:38:28 +0000230 int newl;
Guido van Rossum55702f81997-01-06 22:57:52 +0000231
Guido van Rossum154417e1997-04-09 17:35:33 +0000232 newl=((Oobject*)self)->pos+l;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000233 if(newl >= ((Oobject*)self)->buf_size) {
Guido van Rossum154417e1997-04-09 17:35:33 +0000234 ((Oobject*)self)->buf_size*=2;
235 if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1;
236 UNLESS(((Oobject*)self)->buf=
237 (char*)realloc(((Oobject*)self)->buf,
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000238 (((Oobject*)self)->buf_size) *sizeof(char))) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000239 PyErr_SetString(PyExc_MemoryError,"out of memory");
Guido van Rossum154417e1997-04-09 17:35:33 +0000240 ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0;
Guido van Rossum049cd901996-12-05 23:30:48 +0000241 return -1;
242 }
243 }
244
Guido van Rossum154417e1997-04-09 17:35:33 +0000245 memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l);
Guido van Rossum049cd901996-12-05 23:30:48 +0000246
Guido van Rossum154417e1997-04-09 17:35:33 +0000247 ((Oobject*)self)->pos += l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000248
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000249 if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) {
Guido van Rossum154417e1997-04-09 17:35:33 +0000250 ((Oobject*)self)->string_size = ((Oobject*)self)->pos;
Guido van Rossum049cd901996-12-05 23:30:48 +0000251 }
252
253 return l;
254}
255
256static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000257O_write(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000258 PyObject *s;
Barry Warsaw61a63e11997-01-14 17:38:28 +0000259 char *c;
260 int l;
Guido van Rossum049cd901996-12-05 23:30:48 +0000261
262 UNLESS(PyArg_Parse(args, "O", &s)) return NULL;
Guido van Rossum55702f81997-01-06 22:57:52 +0000263 UNLESS(-1 != (l=PyString_Size(s))) return NULL;
264 UNLESS(c=PyString_AsString(s)) return NULL;
Guido van Rossum154417e1997-04-09 17:35:33 +0000265 UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000266
Guido van Rossum55702f81997-01-06 22:57:52 +0000267 Py_INCREF(Py_None);
268 return Py_None;
Guido van Rossum049cd901996-12-05 23:30:48 +0000269}
270
271static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000272O_getval(Oobject *self, PyObject *args) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000273 PyObject *use_pos;
274 int s;
275
276 use_pos=Py_None;
277 UNLESS(PyArg_ParseTuple(args,"|O",&use_pos)) return NULL;
278 if(PyObject_IsTrue(use_pos)) s=self->pos;
279 else s=self->string_size;
280 return PyString_FromStringAndSize(self->buf, s);
Guido van Rossum154417e1997-04-09 17:35:33 +0000281}
282
283static PyObject *
284O_cgetval(PyObject *self) {
285 return PyString_FromStringAndSize(((Oobject*)self)->buf,
286 ((Oobject*)self)->pos);
Guido van Rossum049cd901996-12-05 23:30:48 +0000287}
288
289static char O_truncate__doc__[] =
290"truncate(): truncate the file at the current position.";
291
292static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000293O_truncate(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000294 self->string_size = self->pos;
295 Py_INCREF(Py_None);
296 return Py_None;
297}
298
299static char O_isatty__doc__[] = "isatty(): always returns 0";
300
301static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000302O_isatty(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000303 return PyInt_FromLong(0);
304}
305
306static char O_close__doc__[] = "close(): explicitly release resources held.";
307
308static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000309O_close(Oobject *self, PyObject *args) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000310 if (self->buf != NULL)
311 free(self->buf);
312 self->buf = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000313
314 self->pos = self->string_size = self->buf_size = 0;
315 self->closed = 1;
316
317 Py_INCREF(Py_None);
318 return Py_None;
319}
320
321static char O_flush__doc__[] = "flush(): does nothing.";
322
323static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000324O_flush(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000325 Py_INCREF(Py_None);
326 return Py_None;
327}
328
329
330static char O_writelines__doc__[] = "blah";
331static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000332O_writelines(Oobject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000333 PyObject *string_module = 0;
334 static PyObject *string_joinfields = 0;
335
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000336 UNLESS(PyArg_Parse(args, "O", args)) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000337 return NULL;
338 }
339
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000340 if (!string_joinfields) {
341 UNLESS(string_module = PyImport_ImportModule("string")) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000342 return NULL;
343 }
344
345 UNLESS(string_joinfields=
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000346 PyObject_GetAttrString(string_module, "joinfields")) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000347 return NULL;
348 }
349
350 Py_DECREF(string_module);
351 }
352
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000353 if (PyObject_Length(args) == -1) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000354 return NULL;
355 }
356
357 return O_write(self,
358 PyObject_CallFunction(string_joinfields, "Os", args, ""));
359}
360
361static struct PyMethodDef O_methods[] = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000362 {"write", (PyCFunction)O_write, 0, O_write__doc__},
Guido van Rossum049cd901996-12-05 23:30:48 +0000363 {"read", (PyCFunction)O_read, 1, O_read__doc__},
364 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__},
Guido van Rossum049cd901996-12-05 23:30:48 +0000365 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__},
366 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__},
367 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__},
Guido van Rossum142eeb81997-08-13 03:14:41 +0000368 {"getvalue", (PyCFunction)O_getval, 1,
369 "getvalue([use_pos]) -- Get the string value."
370 "\n"
371 "If use_pos is specified and is a true value, then the string returned\n"
372 "will include only the text up to the current file position.\n"
373 },
Guido van Rossum049cd901996-12-05 23:30:48 +0000374 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__},
375 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
376 {"close", (PyCFunction)O_close, 0, O_close__doc__},
377 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
378 {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__},
379 {NULL, NULL} /* sentinel */
380};
381
Guido van Rossum049cd901996-12-05 23:30:48 +0000382
383static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000384O_dealloc(Oobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000385 if (self->buf != NULL)
386 free(self->buf);
Guido van Rossum049cd901996-12-05 23:30:48 +0000387 PyMem_DEL(self);
388}
389
390static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000391O_getattr(Oobject *self, char *name) {
Fred Drake7589b711998-07-17 14:30:58 +0000392 if (name[0] == 's' && strcmp(name, "softspace") == 0) {
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000393 return PyInt_FromLong(self->softspace);
394 }
Fred Drake7589b711998-07-17 14:30:58 +0000395 else if (name[0] == 'c' && strcmp(name, "closed") == 0) {
396 return PyInt_FromLong(self->closed);
397 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000398 return Py_FindMethod(O_methods, (PyObject *)self, name);
399}
400
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000401static int
402O_setattr(Oobject *self, char *name, PyObject *value) {
403 long x;
404 if (strcmp(name, "softspace") != 0) {
405 PyErr_SetString(PyExc_AttributeError, name);
406 return -1;
407 }
408 x = PyInt_AsLong(value);
409 if (x == -1 && PyErr_Occurred())
410 return -1;
411 self->softspace = x;
412 return 0;
413}
414
Guido van Rossum049cd901996-12-05 23:30:48 +0000415static char Otype__doc__[] =
416"Simple type for output to strings."
417;
418
419static PyTypeObject Otype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000420 PyObject_HEAD_INIT(NULL)
421 0, /*ob_size*/
422 "StringO", /*tp_name*/
423 sizeof(Oobject), /*tp_basicsize*/
424 0, /*tp_itemsize*/
425 /* methods */
426 (destructor)O_dealloc, /*tp_dealloc*/
427 (printfunc)0, /*tp_print*/
428 (getattrfunc)O_getattr, /*tp_getattr*/
Guido van Rossum3dc35b01997-04-11 19:56:06 +0000429 (setattrfunc)O_setattr, /*tp_setattr*/
Guido van Rossum154417e1997-04-09 17:35:33 +0000430 (cmpfunc)0, /*tp_compare*/
431 (reprfunc)0, /*tp_repr*/
432 0, /*tp_as_number*/
433 0, /*tp_as_sequence*/
434 0, /*tp_as_mapping*/
435 (hashfunc)0, /*tp_hash*/
436 (ternaryfunc)0, /*tp_call*/
437 (reprfunc)0, /*tp_str*/
438
439 /* Space for future expansion */
440 0L,0L,0L,0L,
441 Otype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000442};
443
Guido van Rossum142eeb81997-08-13 03:14:41 +0000444static PyObject *
445newOobject(int size) {
446 Oobject *self;
447
448 self = PyObject_NEW(Oobject, &Otype);
449 if (self == NULL)
450 return NULL;
451 self->pos=0;
452 self->closed = 0;
453 self->string_size = 0;
454 self->softspace = 0;
455
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000456 UNLESS(self->buf=malloc(size*sizeof(char))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000457 PyErr_SetString(PyExc_MemoryError,"out of memory");
458 self->buf_size = 0;
459 return NULL;
460 }
461
462 self->buf_size=size;
463 return (PyObject*)self;
464}
465
Guido van Rossum049cd901996-12-05 23:30:48 +0000466/* End of code for StringO objects */
467/* -------------------------------------------------------- */
468
469static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000470I_close(Iobject *self, PyObject *args) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000471 Py_XDECREF(self->pbuf);
472 self->pbuf = NULL;
Guido van Rossum049cd901996-12-05 23:30:48 +0000473
474 self->pos = self->string_size = 0;
475 self->closed = 1;
476
477 Py_INCREF(Py_None);
478 return Py_None;
479}
480
481static struct PyMethodDef I_methods[] = {
482 {"read", (PyCFunction)O_read, 1, O_read__doc__},
483 {"readline", (PyCFunction)O_readline, 0, O_readline__doc__},
484 {"reset", (PyCFunction)O_reset, 0, O_reset__doc__},
485 {"seek", (PyCFunction)O_seek, 1, O_seek__doc__},
486 {"tell", (PyCFunction)O_tell, 0, O_tell__doc__},
487 {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__},
488 {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__},
489 {"close", (PyCFunction)I_close, 0, O_close__doc__},
490 {"flush", (PyCFunction)O_flush, 0, O_flush__doc__},
491 {NULL, NULL} /* sentinel */
492};
493
Guido van Rossum049cd901996-12-05 23:30:48 +0000494static void
Guido van Rossum154417e1997-04-09 17:35:33 +0000495I_dealloc(Iobject *self) {
Guido van Rossum15a40391997-09-03 00:09:26 +0000496 Py_XDECREF(self->pbuf);
Guido van Rossum049cd901996-12-05 23:30:48 +0000497 PyMem_DEL(self);
498}
499
500static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000501I_getattr(Iobject *self, char *name) {
Fred Drake7589b711998-07-17 14:30:58 +0000502 if (name[0] == 'c' && strcmp(name,"closed") == 0) {
503 return PyInt_FromLong(self->closed);
504 }
Guido van Rossum049cd901996-12-05 23:30:48 +0000505 return Py_FindMethod(I_methods, (PyObject *)self, name);
506}
507
508static char Itype__doc__[] =
509"Simple type for treating strings as input file streams"
510;
511
512static PyTypeObject Itype = {
Guido van Rossum154417e1997-04-09 17:35:33 +0000513 PyObject_HEAD_INIT(NULL)
514 0, /*ob_size*/
515 "StringI", /*tp_name*/
516 sizeof(Iobject), /*tp_basicsize*/
517 0, /*tp_itemsize*/
518 /* methods */
519 (destructor)I_dealloc, /*tp_dealloc*/
520 (printfunc)0, /*tp_print*/
521 (getattrfunc)I_getattr, /*tp_getattr*/
522 (setattrfunc)0, /*tp_setattr*/
523 (cmpfunc)0, /*tp_compare*/
524 (reprfunc)0, /*tp_repr*/
525 0, /*tp_as_number*/
526 0, /*tp_as_sequence*/
527 0, /*tp_as_mapping*/
528 (hashfunc)0, /*tp_hash*/
529 (ternaryfunc)0, /*tp_call*/
530 (reprfunc)0, /*tp_str*/
531
532 /* Space for future expansion */
533 0L,0L,0L,0L,
534 Itype__doc__ /* Documentation string */
Guido van Rossum049cd901996-12-05 23:30:48 +0000535};
536
Guido van Rossum142eeb81997-08-13 03:14:41 +0000537static PyObject *
538newIobject(PyObject *s) {
539 Iobject *self;
540 char *buf;
541 int size;
542
543 UNLESS(buf=PyString_AsString(s)) return NULL;
544 UNLESS(-1 != (size=PyString_Size(s))) return NULL;
545 UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL;
546 Py_INCREF(s);
547 self->buf=buf;
548 self->string_size=size;
549 self->pbuf=s;
550 self->pos=0;
551 self->closed = 0;
552
553 return (PyObject*)self;
554}
555
Guido van Rossum049cd901996-12-05 23:30:48 +0000556/* End of code for StringI objects */
557/* -------------------------------------------------------- */
558
559
560static char IO_StringIO__doc__[] =
561"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
562;
563
564static PyObject *
Guido van Rossum154417e1997-04-09 17:35:33 +0000565IO_StringIO(PyObject *self, PyObject *args) {
Guido van Rossum049cd901996-12-05 23:30:48 +0000566 PyObject *s=0;
567
Guido van Rossum2a502d81998-07-24 19:53:54 +0000568 UNLESS(PyArg_ParseTuple(args, "|S", &s)) return NULL;
Guido van Rossum154417e1997-04-09 17:35:33 +0000569 if(s) return newIobject(s);
570 return newOobject(128);
Guido van Rossum049cd901996-12-05 23:30:48 +0000571}
572
573/* List of methods defined in the module */
574
575static struct PyMethodDef IO_methods[] = {
Guido van Rossum55702f81997-01-06 22:57:52 +0000576 {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__},
577 {NULL, NULL} /* sentinel */
Guido van Rossum049cd901996-12-05 23:30:48 +0000578};
579
580
581/* Initialization function for the module (*must* be called initcStringIO) */
582
Guido van Rossum154417e1997-04-09 17:35:33 +0000583static struct PycStringIO_CAPI CAPI = {
584 O_cread,
585 O_creadline,
586 O_cwrite,
587 O_cgetval,
588 newOobject,
589 newIobject,
590 &Itype,
591 &Otype,
592};
593
Guido van Rossum049cd901996-12-05 23:30:48 +0000594void
Guido van Rossum154417e1997-04-09 17:35:33 +0000595initcStringIO() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000596 PyObject *m, *d, *v;
Guido van Rossum049cd901996-12-05 23:30:48 +0000597
Guido van Rossum154417e1997-04-09 17:35:33 +0000598
Guido van Rossum049cd901996-12-05 23:30:48 +0000599 /* Create the module and add the functions */
600 m = Py_InitModule4("cStringIO", IO_methods,
601 cStringIO_module_documentation,
602 (PyObject*)NULL,PYTHON_API_VERSION);
603
604 /* Add some symbolic constants to the module */
605 d = PyModule_GetDict(m);
Guido van Rossum049cd901996-12-05 23:30:48 +0000606
Guido van Rossum55702f81997-01-06 22:57:52 +0000607 /* Export C API */
Guido van Rossum154417e1997-04-09 17:35:33 +0000608 Itype.ob_type=&PyType_Type;
609 Otype.ob_type=&PyType_Type;
Guido van Rossum9efe8ef1997-09-03 18:19:40 +0000610 PyDict_SetItemString(d,"cStringIO_CAPI",
611 v = PyCObject_FromVoidPtr(&CAPI,NULL));
612 Py_XDECREF(v);
Guido van Rossum154417e1997-04-09 17:35:33 +0000613
614 /* Export Types */
Guido van Rossum049cd901996-12-05 23:30:48 +0000615 PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
616 PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
Guido van Rossum142eeb81997-08-13 03:14:41 +0000617
618 /* Maybe make certain warnings go away */
619 if(0) PycString_IMPORT;
Guido van Rossum049cd901996-12-05 23:30:48 +0000620
Guido van Rossum049cd901996-12-05 23:30:48 +0000621 /* Check for errors */
Guido van Rossum154417e1997-04-09 17:35:33 +0000622 if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO");
Guido van Rossum049cd901996-12-05 23:30:48 +0000623}