Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | 7d9b413 | 1998-11-25 16:17:32 +0000 | [diff] [blame] | 2 | * cStringIO.c,v 1.26 1998/10/01 22:30:56 jim Exp |
| 3 | * |
| 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 Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 54 | static 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 Rossum | 7d9b413 | 1998-11-25 16:17:32 +0000 | [diff] [blame] | 75 | " an_input_stream.seek(0) # OK, start over\n" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 76 | " 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 Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 80 | "\n" |
Guido van Rossum | 7d9b413 | 1998-11-25 16:17:32 +0000 | [diff] [blame] | 81 | "cStringIO.c,v 1.26 1998/10/01 22:30:56 jim Exp\n" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 82 | ; |
| 83 | |
| 84 | #include "Python.h" |
| 85 | #include "import.h" |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 86 | #include "cStringIO.h" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 88 | #define UNLESS(E) if(!(E)) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 90 | /* Declarations for objects of type StringO */ |
| 91 | |
| 92 | typedef struct { |
| 93 | PyObject_HEAD |
| 94 | char *buf; |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 95 | int pos, string_size, buf_size, closed, softspace; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 96 | } Oobject; |
| 97 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 98 | /* Declarations for objects of type StringI */ |
| 99 | |
| 100 | typedef struct { |
| 101 | PyObject_HEAD |
| 102 | char *buf; |
| 103 | int pos, string_size, closed; |
| 104 | PyObject *pbuf; |
| 105 | } Iobject; |
| 106 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 107 | static char O_reset__doc__[] = |
| 108 | "reset() -- Reset the file position to the beginning" |
| 109 | ; |
| 110 | |
| 111 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 112 | O_reset(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 113 | self->pos = 0; |
| 114 | |
| 115 | Py_INCREF(Py_None); |
| 116 | return Py_None; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | static char O_tell__doc__[] = |
| 121 | "tell() -- get the current position."; |
| 122 | |
| 123 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 124 | O_tell(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 125 | return PyInt_FromLong(self->pos); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static 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 | |
| 133 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 134 | O_seek(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 135 | int position, mode = 0; |
| 136 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 137 | UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
| 140 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 141 | if (mode == 2) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 142 | position += self->string_size; |
| 143 | } |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 144 | else if (mode == 1) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 145 | position += self->pos; |
| 146 | } |
| 147 | |
Guido van Rossum | 7d9b413 | 1998-11-25 16:17:32 +0000 | [diff] [blame] | 148 | 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 Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 162 | Py_INCREF(Py_None); |
| 163 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static char O_read__doc__[] = |
| 167 | "read([s]) -- Read s characters, or the rest of the string" |
| 168 | ; |
| 169 | |
| 170 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 171 | O_cread(PyObject *self, char **output, int n) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 172 | int l; |
| 173 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 174 | l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 175 | if (n < 0 || n > l) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 176 | n = l; |
| 177 | } |
| 178 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 179 | *output=((Oobject*)self)->buf + ((Oobject*)self)->pos; |
| 180 | ((Oobject*)self)->pos += n; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 181 | return n; |
| 182 | } |
| 183 | |
| 184 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 185 | O_read(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 186 | int n = -1; |
| 187 | char *output; |
| 188 | |
| 189 | UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL; |
| 190 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 191 | n=O_cread((PyObject*)self,&output,n); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 192 | |
| 193 | return PyString_FromStringAndSize(output, n); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static char O_readline__doc__[] = |
| 198 | "readline() -- Read one line" |
| 199 | ; |
| 200 | |
| 201 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 202 | O_creadline(PyObject *self, char **output) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 203 | char *n, *s; |
| 204 | int l; |
| 205 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 206 | for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos, |
| 207 | s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 208 | n < s && *n != '\n'; n++); |
| 209 | if (n < s) n++; |
| 210 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 211 | *output=((Oobject*)self)->buf + ((Oobject*)self)->pos; |
| 212 | l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos; |
| 213 | ((Oobject*)self)->pos += l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 214 | return l; |
| 215 | } |
| 216 | |
| 217 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 218 | O_readline(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 219 | int n; |
| 220 | char *output; |
| 221 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 222 | n=O_creadline((PyObject*)self,&output); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 223 | return PyString_FromStringAndSize(output, n); |
| 224 | } |
| 225 | |
| 226 | static char O_write__doc__[] = |
| 227 | "write(s) -- Write a string to the file" |
| 228 | "\n\nNote (hack:) writing None resets the buffer" |
| 229 | ; |
| 230 | |
| 231 | |
| 232 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 233 | O_cwrite(PyObject *self, char *c, int l) { |
Barry Warsaw | 61a63e1 | 1997-01-14 17:38:28 +0000 | [diff] [blame] | 234 | int newl; |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 236 | newl=((Oobject*)self)->pos+l; |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 237 | if(newl >= ((Oobject*)self)->buf_size) { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 238 | ((Oobject*)self)->buf_size*=2; |
| 239 | if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1; |
| 240 | UNLESS(((Oobject*)self)->buf= |
| 241 | (char*)realloc(((Oobject*)self)->buf, |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 242 | (((Oobject*)self)->buf_size) *sizeof(char))) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 243 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 244 | ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 245 | return -1; |
| 246 | } |
| 247 | } |
| 248 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 249 | memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 251 | ((Oobject*)self)->pos += l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 253 | if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 254 | ((Oobject*)self)->string_size = ((Oobject*)self)->pos; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return l; |
| 258 | } |
| 259 | |
| 260 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 261 | O_write(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 262 | PyObject *s; |
Barry Warsaw | 61a63e1 | 1997-01-14 17:38:28 +0000 | [diff] [blame] | 263 | char *c; |
| 264 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 265 | |
| 266 | UNLESS(PyArg_Parse(args, "O", &s)) return NULL; |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 267 | UNLESS(-1 != (l=PyString_Size(s))) return NULL; |
| 268 | UNLESS(c=PyString_AsString(s)) return NULL; |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 269 | UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 271 | Py_INCREF(Py_None); |
| 272 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 276 | O_getval(Oobject *self, PyObject *args) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 277 | PyObject *use_pos; |
| 278 | int s; |
| 279 | |
| 280 | use_pos=Py_None; |
| 281 | UNLESS(PyArg_ParseTuple(args,"|O",&use_pos)) return NULL; |
| 282 | if(PyObject_IsTrue(use_pos)) s=self->pos; |
| 283 | else s=self->string_size; |
| 284 | return PyString_FromStringAndSize(self->buf, s); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static PyObject * |
| 288 | O_cgetval(PyObject *self) { |
| 289 | return PyString_FromStringAndSize(((Oobject*)self)->buf, |
| 290 | ((Oobject*)self)->pos); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static char O_truncate__doc__[] = |
| 294 | "truncate(): truncate the file at the current position."; |
| 295 | |
| 296 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 297 | O_truncate(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 298 | self->string_size = self->pos; |
| 299 | Py_INCREF(Py_None); |
| 300 | return Py_None; |
| 301 | } |
| 302 | |
| 303 | static char O_isatty__doc__[] = "isatty(): always returns 0"; |
| 304 | |
| 305 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 306 | O_isatty(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 307 | return PyInt_FromLong(0); |
| 308 | } |
| 309 | |
| 310 | static char O_close__doc__[] = "close(): explicitly release resources held."; |
| 311 | |
| 312 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 313 | O_close(Oobject *self, PyObject *args) { |
Guido van Rossum | 15a4039 | 1997-09-03 00:09:26 +0000 | [diff] [blame] | 314 | if (self->buf != NULL) |
| 315 | free(self->buf); |
| 316 | self->buf = NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 317 | |
| 318 | self->pos = self->string_size = self->buf_size = 0; |
| 319 | self->closed = 1; |
| 320 | |
| 321 | Py_INCREF(Py_None); |
| 322 | return Py_None; |
| 323 | } |
| 324 | |
| 325 | static char O_flush__doc__[] = "flush(): does nothing."; |
| 326 | |
| 327 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 328 | O_flush(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 329 | Py_INCREF(Py_None); |
| 330 | return Py_None; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | static char O_writelines__doc__[] = "blah"; |
| 335 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 336 | O_writelines(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 337 | PyObject *string_module = 0; |
| 338 | static PyObject *string_joinfields = 0; |
| 339 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 340 | UNLESS(PyArg_Parse(args, "O", args)) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 341 | return NULL; |
| 342 | } |
| 343 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 344 | if (!string_joinfields) { |
| 345 | UNLESS(string_module = PyImport_ImportModule("string")) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | UNLESS(string_joinfields= |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 350 | PyObject_GetAttrString(string_module, "joinfields")) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | Py_DECREF(string_module); |
| 355 | } |
| 356 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 357 | if (PyObject_Length(args) == -1) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | return O_write(self, |
| 362 | PyObject_CallFunction(string_joinfields, "Os", args, "")); |
| 363 | } |
| 364 | |
| 365 | static struct PyMethodDef O_methods[] = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 366 | {"write", (PyCFunction)O_write, 0, O_write__doc__}, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 367 | {"read", (PyCFunction)O_read, 1, O_read__doc__}, |
| 368 | {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 369 | {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, |
| 370 | {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, |
| 371 | {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 372 | {"getvalue", (PyCFunction)O_getval, 1, |
| 373 | "getvalue([use_pos]) -- Get the string value." |
| 374 | "\n" |
| 375 | "If use_pos is specified and is a true value, then the string returned\n" |
| 376 | "will include only the text up to the current file position.\n" |
| 377 | }, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 378 | {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, |
| 379 | {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, |
| 380 | {"close", (PyCFunction)O_close, 0, O_close__doc__}, |
| 381 | {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, |
| 382 | {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__}, |
| 383 | {NULL, NULL} /* sentinel */ |
| 384 | }; |
| 385 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 386 | |
| 387 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 388 | O_dealloc(Oobject *self) { |
Guido van Rossum | 15a4039 | 1997-09-03 00:09:26 +0000 | [diff] [blame] | 389 | if (self->buf != NULL) |
| 390 | free(self->buf); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 391 | PyMem_DEL(self); |
| 392 | } |
| 393 | |
| 394 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 395 | O_getattr(Oobject *self, char *name) { |
Guido van Rossum | 7d9b413 | 1998-11-25 16:17:32 +0000 | [diff] [blame] | 396 | if (strcmp(name, "softspace") == 0) { |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 397 | return PyInt_FromLong(self->softspace); |
| 398 | } |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 399 | return Py_FindMethod(O_methods, (PyObject *)self, name); |
| 400 | } |
| 401 | |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 402 | static int |
| 403 | O_setattr(Oobject *self, char *name, PyObject *value) { |
| 404 | long x; |
| 405 | if (strcmp(name, "softspace") != 0) { |
| 406 | PyErr_SetString(PyExc_AttributeError, name); |
| 407 | return -1; |
| 408 | } |
| 409 | x = PyInt_AsLong(value); |
| 410 | if (x == -1 && PyErr_Occurred()) |
| 411 | return -1; |
| 412 | self->softspace = x; |
| 413 | return 0; |
| 414 | } |
| 415 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 416 | static char Otype__doc__[] = |
| 417 | "Simple type for output to strings." |
| 418 | ; |
| 419 | |
| 420 | static PyTypeObject Otype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 421 | PyObject_HEAD_INIT(NULL) |
| 422 | 0, /*ob_size*/ |
| 423 | "StringO", /*tp_name*/ |
| 424 | sizeof(Oobject), /*tp_basicsize*/ |
| 425 | 0, /*tp_itemsize*/ |
| 426 | /* methods */ |
| 427 | (destructor)O_dealloc, /*tp_dealloc*/ |
| 428 | (printfunc)0, /*tp_print*/ |
| 429 | (getattrfunc)O_getattr, /*tp_getattr*/ |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 430 | (setattrfunc)O_setattr, /*tp_setattr*/ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 431 | (cmpfunc)0, /*tp_compare*/ |
| 432 | (reprfunc)0, /*tp_repr*/ |
| 433 | 0, /*tp_as_number*/ |
| 434 | 0, /*tp_as_sequence*/ |
| 435 | 0, /*tp_as_mapping*/ |
| 436 | (hashfunc)0, /*tp_hash*/ |
| 437 | (ternaryfunc)0, /*tp_call*/ |
| 438 | (reprfunc)0, /*tp_str*/ |
| 439 | |
| 440 | /* Space for future expansion */ |
| 441 | 0L,0L,0L,0L, |
| 442 | Otype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 443 | }; |
| 444 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 445 | static PyObject * |
| 446 | newOobject(int size) { |
| 447 | Oobject *self; |
| 448 | |
| 449 | self = PyObject_NEW(Oobject, &Otype); |
| 450 | if (self == NULL) |
| 451 | return NULL; |
| 452 | self->pos=0; |
| 453 | self->closed = 0; |
| 454 | self->string_size = 0; |
| 455 | self->softspace = 0; |
| 456 | |
Guido van Rossum | fdde96c | 1997-12-04 01:13:01 +0000 | [diff] [blame] | 457 | UNLESS(self->buf=malloc(size*sizeof(char))) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 458 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
| 459 | self->buf_size = 0; |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | self->buf_size=size; |
| 464 | return (PyObject*)self; |
| 465 | } |
| 466 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 467 | /* End of code for StringO objects */ |
| 468 | /* -------------------------------------------------------- */ |
| 469 | |
| 470 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 471 | I_close(Iobject *self, PyObject *args) { |
Guido van Rossum | 15a4039 | 1997-09-03 00:09:26 +0000 | [diff] [blame] | 472 | Py_XDECREF(self->pbuf); |
| 473 | self->pbuf = NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 474 | |
| 475 | self->pos = self->string_size = 0; |
| 476 | self->closed = 1; |
| 477 | |
| 478 | Py_INCREF(Py_None); |
| 479 | return Py_None; |
| 480 | } |
| 481 | |
| 482 | static struct PyMethodDef I_methods[] = { |
| 483 | {"read", (PyCFunction)O_read, 1, O_read__doc__}, |
| 484 | {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, |
| 485 | {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, |
| 486 | {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, |
| 487 | {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, |
| 488 | {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, |
| 489 | {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, |
| 490 | {"close", (PyCFunction)I_close, 0, O_close__doc__}, |
| 491 | {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, |
| 492 | {NULL, NULL} /* sentinel */ |
| 493 | }; |
| 494 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 495 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 496 | I_dealloc(Iobject *self) { |
Guido van Rossum | 15a4039 | 1997-09-03 00:09:26 +0000 | [diff] [blame] | 497 | Py_XDECREF(self->pbuf); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 498 | PyMem_DEL(self); |
| 499 | } |
| 500 | |
| 501 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 502 | I_getattr(Iobject *self, char *name) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 503 | return Py_FindMethod(I_methods, (PyObject *)self, name); |
| 504 | } |
| 505 | |
| 506 | static char Itype__doc__[] = |
| 507 | "Simple type for treating strings as input file streams" |
| 508 | ; |
| 509 | |
| 510 | static PyTypeObject Itype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 511 | PyObject_HEAD_INIT(NULL) |
| 512 | 0, /*ob_size*/ |
| 513 | "StringI", /*tp_name*/ |
| 514 | sizeof(Iobject), /*tp_basicsize*/ |
| 515 | 0, /*tp_itemsize*/ |
| 516 | /* methods */ |
| 517 | (destructor)I_dealloc, /*tp_dealloc*/ |
| 518 | (printfunc)0, /*tp_print*/ |
| 519 | (getattrfunc)I_getattr, /*tp_getattr*/ |
| 520 | (setattrfunc)0, /*tp_setattr*/ |
| 521 | (cmpfunc)0, /*tp_compare*/ |
| 522 | (reprfunc)0, /*tp_repr*/ |
| 523 | 0, /*tp_as_number*/ |
| 524 | 0, /*tp_as_sequence*/ |
| 525 | 0, /*tp_as_mapping*/ |
| 526 | (hashfunc)0, /*tp_hash*/ |
| 527 | (ternaryfunc)0, /*tp_call*/ |
| 528 | (reprfunc)0, /*tp_str*/ |
| 529 | |
| 530 | /* Space for future expansion */ |
| 531 | 0L,0L,0L,0L, |
| 532 | Itype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 533 | }; |
| 534 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 535 | static PyObject * |
| 536 | newIobject(PyObject *s) { |
| 537 | Iobject *self; |
| 538 | char *buf; |
| 539 | int size; |
| 540 | |
| 541 | UNLESS(buf=PyString_AsString(s)) return NULL; |
| 542 | UNLESS(-1 != (size=PyString_Size(s))) return NULL; |
| 543 | UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL; |
| 544 | Py_INCREF(s); |
| 545 | self->buf=buf; |
| 546 | self->string_size=size; |
| 547 | self->pbuf=s; |
| 548 | self->pos=0; |
| 549 | self->closed = 0; |
| 550 | |
| 551 | return (PyObject*)self; |
| 552 | } |
| 553 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 554 | /* End of code for StringI objects */ |
| 555 | /* -------------------------------------------------------- */ |
| 556 | |
| 557 | |
| 558 | static char IO_StringIO__doc__[] = |
| 559 | "StringIO([s]) -- Return a StringIO-like stream for reading or writing" |
| 560 | ; |
| 561 | |
| 562 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 563 | IO_StringIO(PyObject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 564 | PyObject *s=0; |
| 565 | |
Guido van Rossum | 7d9b413 | 1998-11-25 16:17:32 +0000 | [diff] [blame] | 566 | UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL; |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 567 | if(s) return newIobject(s); |
| 568 | return newOobject(128); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* List of methods defined in the module */ |
| 572 | |
| 573 | static struct PyMethodDef IO_methods[] = { |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 574 | {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__}, |
| 575 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | |
| 579 | /* Initialization function for the module (*must* be called initcStringIO) */ |
| 580 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 581 | static struct PycStringIO_CAPI CAPI = { |
| 582 | O_cread, |
| 583 | O_creadline, |
| 584 | O_cwrite, |
| 585 | O_cgetval, |
| 586 | newOobject, |
| 587 | newIobject, |
| 588 | &Itype, |
| 589 | &Otype, |
| 590 | }; |
| 591 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 592 | void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 593 | initcStringIO() { |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 594 | PyObject *m, *d, *v; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 595 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 597 | /* Create the module and add the functions */ |
| 598 | m = Py_InitModule4("cStringIO", IO_methods, |
| 599 | cStringIO_module_documentation, |
| 600 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 601 | |
| 602 | /* Add some symbolic constants to the module */ |
| 603 | d = PyModule_GetDict(m); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 604 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 605 | /* Export C API */ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 606 | Itype.ob_type=&PyType_Type; |
| 607 | Otype.ob_type=&PyType_Type; |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 608 | PyDict_SetItemString(d,"cStringIO_CAPI", |
| 609 | v = PyCObject_FromVoidPtr(&CAPI,NULL)); |
| 610 | Py_XDECREF(v); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 611 | |
| 612 | /* Export Types */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 613 | PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); |
| 614 | PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 615 | |
| 616 | /* Maybe make certain warnings go away */ |
| 617 | if(0) PycString_IMPORT; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 619 | /* Check for errors */ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 620 | if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 621 | } |