Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | 17d53ec | 1999-06-15 14:35:48 +0000 | [diff] [blame] | 2 | * cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp |
Guido van Rossum | 7d9b413 | 1998-11-25 16:17:32 +0000 | [diff] [blame] | 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" |
Fred Drake | aef1000 | 2000-06-19 13:17:41 +0000 | [diff] [blame] | 59 | "full generality of StringIO, but it provides enough for most\n" |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 60 | "applications and is especially useful in conjunction with the\n" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 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 | 17d53ec | 1999-06-15 14:35:48 +0000 | [diff] [blame] | 81 | "cStringIO.c,v 1.29 1999/06/15 14:10:27 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 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +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 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 91 | /* Declaration for file-like objects that manage data as strings |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 92 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 93 | 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 Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 98 | |
| 99 | typedef struct { |
| 100 | PyObject_HEAD |
| 101 | char *buf; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 102 | int pos, string_size; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 103 | } IOobject; |
| 104 | |
| 105 | #define IOOOBJECT(O) ((IOobject*)(O)) |
| 106 | |
| 107 | /* Declarations for objects of type StringO */ |
| 108 | |
| 109 | typedef 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 | |
| 119 | typedef struct { /* Subtype of IOobject */ |
| 120 | PyObject_HEAD |
| 121 | char *buf; |
| 122 | int pos, string_size; |
| 123 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 124 | PyObject *pbuf; |
| 125 | } Iobject; |
| 126 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 127 | /* IOobject (common) methods */ |
| 128 | |
| 129 | static char IO_flush__doc__[] = "flush(): does nothing."; |
| 130 | |
| 131 | static int |
| 132 | IO__opencheck(IOobject *self) { |
| 133 | UNLESS (self->buf) { |
| 134 | PyErr_SetString(PyExc_ValueError, |
| 135 | "I/O operation on closed file"); |
| 136 | return 0; |
| 137 | } |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | static PyObject * |
| 142 | IO_flush(IOobject *self, PyObject *args) { |
| 143 | |
| 144 | UNLESS (IO__opencheck(self)) return NULL; |
| 145 | UNLESS (PyArg_ParseTuple(args, ":flush")) return NULL; |
| 146 | |
| 147 | Py_INCREF(Py_None); |
| 148 | return Py_None; |
| 149 | } |
| 150 | |
| 151 | static char IO_getval__doc__[] = |
| 152 | "getvalue([use_pos]) -- Get the string value." |
| 153 | "\n" |
| 154 | "If use_pos is specified and is a true value, then the string returned\n" |
| 155 | "will include only the text up to the current file position.\n" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 156 | ; |
| 157 | |
| 158 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 159 | IO_cgetval(PyObject *self) { |
| 160 | UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
| 161 | return PyString_FromStringAndSize(((IOobject*)self)->buf, |
| 162 | ((IOobject*)self)->pos); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 165 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 166 | IO_getval(IOobject *self, PyObject *args) { |
| 167 | PyObject *use_pos=Py_None; |
| 168 | int s; |
| 169 | |
| 170 | UNLESS (IO__opencheck(self)) return NULL; |
| 171 | UNLESS (PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL; |
| 172 | |
| 173 | if (PyObject_IsTrue(use_pos)) { |
| 174 | s=self->pos; |
| 175 | if (s > self->string_size) s=self->string_size; |
| 176 | } |
| 177 | else |
| 178 | s=self->string_size; |
| 179 | return PyString_FromStringAndSize(self->buf, s); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 182 | static char IO_isatty__doc__[] = "isatty(): always returns 0"; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 183 | |
| 184 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 185 | IO_isatty(IOobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 186 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 187 | UNLESS (PyArg_ParseTuple(args, ":isatty")) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 188 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 189 | return PyInt_FromLong(0); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 192 | static char IO_read__doc__[] = |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 193 | "read([s]) -- Read s characters, or the rest of the string" |
| 194 | ; |
| 195 | |
| 196 | static int |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 197 | IO_cread(PyObject *self, char **output, int n) { |
| 198 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 199 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 200 | UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
| 201 | l = ((IOobject*)self)->string_size - ((IOobject*)self)->pos; |
| 202 | if (n < 0 || n > l) { |
| 203 | n = l; |
| 204 | if (n < 0) n=0; |
| 205 | } |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 206 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 207 | *output=((IOobject*)self)->buf + ((IOobject*)self)->pos; |
| 208 | ((IOobject*)self)->pos += n; |
| 209 | return n; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 213 | IO_read(IOobject *self, PyObject *args) { |
| 214 | int n = -1; |
| 215 | char *output; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 216 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 217 | UNLESS (PyArg_ParseTuple(args, "|i:read", &n)) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 218 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 219 | if ( (n=IO_cread((PyObject*)self,&output,n)) < 0) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 220 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 221 | return PyString_FromStringAndSize(output, n); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 224 | static char IO_readline__doc__[] = |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 225 | "readline() -- Read one line" |
| 226 | ; |
| 227 | |
| 228 | static int |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 229 | IO_creadline(PyObject *self, char **output) { |
| 230 | char *n, *s; |
| 231 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 232 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 233 | UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
| 234 | |
| 235 | for (n = ((IOobject*)self)->buf + ((IOobject*)self)->pos, |
| 236 | s = ((IOobject*)self)->buf + ((IOobject*)self)->string_size; |
| 237 | n < s && *n != '\n'; n++); |
| 238 | if (n < s) n++; |
| 239 | |
| 240 | *output=((IOobject*)self)->buf + ((IOobject*)self)->pos; |
| 241 | l = n - ((IOobject*)self)->buf - ((IOobject*)self)->pos; |
| 242 | ((IOobject*)self)->pos += l; |
| 243 | return l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 247 | IO_readline(IOobject *self, PyObject *args) { |
| 248 | int n, m=-1; |
| 249 | char *output; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 250 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 251 | UNLESS (PyArg_ParseTuple(args, "|i:readline", &m)) return NULL; |
| 252 | |
| 253 | if( (n=IO_creadline((PyObject*)self,&output)) < 0) return NULL; |
| 254 | if (m >= 0 && m < n) { |
| 255 | m = n - m; |
| 256 | n -= m; |
| 257 | self->pos -= m; |
| 258 | } |
| 259 | return PyString_FromStringAndSize(output, n); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 262 | static char IO_readlines__doc__[] = |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 263 | "readlines() -- Read all lines" |
| 264 | ; |
| 265 | |
| 266 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 267 | IO_readlines(IOobject *self, PyObject *args) { |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 268 | int n; |
| 269 | char *output; |
| 270 | PyObject *result, *line; |
| 271 | int hint = 0, length = 0; |
| 272 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 273 | UNLESS (PyArg_ParseTuple(args, "|i:readlines", &hint)) return NULL; |
| 274 | |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 275 | result = PyList_New(0); |
| 276 | if (!result) |
| 277 | return NULL; |
| 278 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 279 | while (1){ |
| 280 | if ( (n = IO_creadline((PyObject*)self,&output)) < 0) |
| 281 | goto err; |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 282 | if (n == 0) |
| 283 | break; |
| 284 | line = PyString_FromStringAndSize (output, n); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 285 | if (!line) |
| 286 | goto err; |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 287 | PyList_Append (result, line); |
| 288 | Py_DECREF (line); |
| 289 | length += n; |
| 290 | if (hint > 0 && length >= hint) |
| 291 | break; |
| 292 | } |
| 293 | return result; |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 294 | err: |
| 295 | Py_DECREF(result); |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | static char IO_reset__doc__[] = |
| 300 | "reset() -- Reset the file position to the beginning" |
| 301 | ; |
| 302 | |
| 303 | static PyObject * |
| 304 | IO_reset(IOobject *self, PyObject *args) { |
| 305 | |
| 306 | UNLESS (IO__opencheck(self)) return NULL; |
| 307 | UNLESS (PyArg_ParseTuple(args, ":reset")) return NULL; |
| 308 | |
| 309 | self->pos = 0; |
| 310 | |
| 311 | Py_INCREF(Py_None); |
| 312 | return Py_None; |
| 313 | } |
| 314 | |
| 315 | static char IO_tell__doc__[] = |
| 316 | "tell() -- get the current position."; |
| 317 | |
| 318 | static PyObject * |
| 319 | IO_tell(IOobject *self, PyObject *args) { |
| 320 | |
| 321 | UNLESS (IO__opencheck(self)) return NULL; |
| 322 | UNLESS (PyArg_ParseTuple(args, ":tell")) return NULL; |
| 323 | |
| 324 | return PyInt_FromLong(self->pos); |
| 325 | } |
| 326 | |
| 327 | static char IO_truncate__doc__[] = |
| 328 | "truncate(): truncate the file at the current position."; |
| 329 | |
| 330 | static PyObject * |
| 331 | IO_truncate(IOobject *self, PyObject *args) { |
| 332 | int pos = -1; |
| 333 | |
| 334 | UNLESS (IO__opencheck(self)) return NULL; |
| 335 | UNLESS (PyArg_ParseTuple(args, "|i:truncate", &pos)) return NULL; |
| 336 | if (pos < 0) pos = self->pos; |
| 337 | |
| 338 | if (self->string_size > pos) self->string_size = pos; |
| 339 | |
| 340 | Py_INCREF(Py_None); |
| 341 | return Py_None; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | /* Read-write object methods */ |
| 348 | |
| 349 | static char O_seek__doc__[] = |
| 350 | "seek(position) -- set the current position\n" |
| 351 | "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"; |
| 352 | |
| 353 | static PyObject * |
| 354 | O_seek(Oobject *self, PyObject *args) { |
| 355 | int position, mode = 0; |
| 356 | |
| 357 | UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
| 358 | UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) |
| 359 | return NULL; |
| 360 | |
| 361 | if (mode == 2) { |
| 362 | position += self->string_size; |
| 363 | } |
| 364 | else if (mode == 1) { |
| 365 | position += self->pos; |
| 366 | } |
| 367 | |
| 368 | if (position > self->buf_size) { |
| 369 | self->buf_size*=2; |
| 370 | if (self->buf_size <= position) self->buf_size=position+1; |
| 371 | UNLESS (self->buf=(char*) |
| 372 | realloc(self->buf,self->buf_size*sizeof(char))) { |
| 373 | self->buf_size=self->pos=0; |
| 374 | return PyErr_NoMemory(); |
| 375 | } |
| 376 | } |
| 377 | else if (position < 0) position=0; |
| 378 | |
| 379 | self->pos=position; |
| 380 | |
| 381 | while (--position >= self->string_size) self->buf[position]=0; |
| 382 | |
| 383 | Py_INCREF(Py_None); |
| 384 | return Py_None; |
Martin v. Löwis | c912a3a | 2000-09-19 11:06:46 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 387 | static char O_write__doc__[] = |
| 388 | "write(s) -- Write a string to the file" |
| 389 | "\n\nNote (hack:) writing None resets the buffer" |
| 390 | ; |
| 391 | |
| 392 | |
| 393 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 394 | O_cwrite(PyObject *self, char *c, int l) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 395 | int newl; |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 396 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 397 | UNLESS (IO__opencheck(IOOOBJECT(self))) return -1; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 398 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 399 | newl=((Oobject*)self)->pos+l; |
| 400 | if (newl >= ((Oobject*)self)->buf_size) { |
| 401 | ((Oobject*)self)->buf_size*=2; |
| 402 | if (((Oobject*)self)->buf_size <= newl) |
| 403 | ((Oobject*)self)->buf_size=newl+1; |
| 404 | UNLESS (((Oobject*)self)->buf= |
| 405 | (char*)realloc( |
| 406 | ((Oobject*)self)->buf, |
| 407 | (((Oobject*)self)->buf_size) *sizeof(char))) { |
| 408 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
| 409 | ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0; |
| 410 | return -1; |
| 411 | } |
| 412 | } |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 413 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 414 | memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 415 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 416 | ((Oobject*)self)->pos += l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 417 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 418 | if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) { |
| 419 | ((Oobject*)self)->string_size = ((Oobject*)self)->pos; |
| 420 | } |
| 421 | |
| 422 | return l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 426 | O_write(Oobject *self, PyObject *args) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 427 | PyObject *s; |
| 428 | char *c; |
| 429 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 430 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 431 | UNLESS (PyArg_ParseTuple(args, "O:write", &s)) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 432 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 433 | UNLESS (-1 != (l=PyString_Size(s))) return NULL; |
| 434 | UNLESS (c=PyString_AsString(s)) return NULL; |
| 435 | if (O_cwrite((PyObject*)self,c,l) < 0) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 436 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 437 | Py_INCREF(Py_None); |
| 438 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static char O_close__doc__[] = "close(): explicitly release resources held."; |
| 442 | |
| 443 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 444 | O_close(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 445 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 446 | UNLESS (PyArg_ParseTuple(args, ":close")) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 447 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 448 | if (self->buf != NULL) free(self->buf); |
| 449 | self->buf = NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 450 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 451 | self->pos = self->string_size = self->buf_size = 0; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 452 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 453 | Py_INCREF(Py_None); |
| 454 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
Guido van Rossum | 68de064 | 1999-02-08 17:03:27 +0000 | [diff] [blame] | 458 | static char O_writelines__doc__[] = |
| 459 | "writelines(sequence_of_strings): write each string"; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 460 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 461 | O_writelines(Oobject *self, PyObject *args) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 462 | PyObject *tmp = 0; |
| 463 | static PyObject *string_joinfields = 0; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 464 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 465 | UNLESS (PyArg_ParseTuple(args, "O:writelines", &args)) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 466 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 467 | if (!string_joinfields) { |
| 468 | UNLESS (tmp = PyImport_ImportModule("string")) return NULL; |
| 469 | string_joinfields=PyObject_GetAttrString(tmp, "joinfields"); |
| 470 | Py_DECREF(tmp); |
| 471 | UNLESS (string_joinfields) return NULL; |
| 472 | } |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 473 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 474 | if (PyObject_Size(args) < 0) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 475 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 476 | tmp = PyObject_CallFunction(string_joinfields, "Os", args, ""); |
| 477 | UNLESS (tmp) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 478 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 479 | args = Py_BuildValue("(O)", tmp); |
| 480 | Py_DECREF(tmp); |
| 481 | UNLESS (args) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 482 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 483 | tmp = O_write(self, args); |
| 484 | Py_DECREF(args); |
| 485 | return tmp; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static struct PyMethodDef O_methods[] = { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 489 | /* Common methods: */ |
| 490 | {"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__}, |
| 491 | {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__}, |
| 492 | {"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__}, |
| 493 | {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__}, |
| 494 | {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__}, |
| 495 | {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__}, |
| 496 | {"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__}, |
| 497 | {"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__}, |
| 498 | {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__}, |
| 499 | |
| 500 | /* Read-write StringIO specific methods: */ |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 501 | {"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 502 | {"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__}, |
| 503 | {"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__}, |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 504 | {"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__}, |
| 505 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 506 | }; |
| 507 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 508 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 509 | O_dealloc(Oobject *self) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 510 | if (self->buf != NULL) |
| 511 | free(self->buf); |
| 512 | PyObject_Del(self); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 516 | O_getattr(Oobject *self, char *name) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 517 | if (strcmp(name, "softspace") == 0) { |
| 518 | return PyInt_FromLong(self->softspace); |
| 519 | } |
| 520 | return Py_FindMethod(O_methods, (PyObject *)self, name); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 523 | static int |
| 524 | O_setattr(Oobject *self, char *name, PyObject *value) { |
| 525 | long x; |
| 526 | if (strcmp(name, "softspace") != 0) { |
| 527 | PyErr_SetString(PyExc_AttributeError, name); |
| 528 | return -1; |
| 529 | } |
| 530 | x = PyInt_AsLong(value); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 531 | if (x < 0 && PyErr_Occurred()) |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 532 | return -1; |
| 533 | self->softspace = x; |
| 534 | return 0; |
| 535 | } |
| 536 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 537 | static char Otype__doc__[] = |
| 538 | "Simple type for output to strings." |
| 539 | ; |
| 540 | |
| 541 | static PyTypeObject Otype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 542 | PyObject_HEAD_INIT(NULL) |
| 543 | 0, /*ob_size*/ |
| 544 | "StringO", /*tp_name*/ |
| 545 | sizeof(Oobject), /*tp_basicsize*/ |
| 546 | 0, /*tp_itemsize*/ |
| 547 | /* methods */ |
| 548 | (destructor)O_dealloc, /*tp_dealloc*/ |
| 549 | (printfunc)0, /*tp_print*/ |
| 550 | (getattrfunc)O_getattr, /*tp_getattr*/ |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 551 | (setattrfunc)O_setattr, /*tp_setattr*/ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 552 | (cmpfunc)0, /*tp_compare*/ |
| 553 | (reprfunc)0, /*tp_repr*/ |
| 554 | 0, /*tp_as_number*/ |
| 555 | 0, /*tp_as_sequence*/ |
| 556 | 0, /*tp_as_mapping*/ |
| 557 | (hashfunc)0, /*tp_hash*/ |
| 558 | (ternaryfunc)0, /*tp_call*/ |
| 559 | (reprfunc)0, /*tp_str*/ |
| 560 | |
| 561 | /* Space for future expansion */ |
| 562 | 0L,0L,0L,0L, |
| 563 | Otype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 564 | }; |
| 565 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 566 | static PyObject * |
| 567 | newOobject(int size) { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 568 | Oobject *self; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 569 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 570 | self = PyObject_New(Oobject, &Otype); |
| 571 | if (self == NULL) |
| 572 | return NULL; |
| 573 | self->pos=0; |
| 574 | self->string_size = 0; |
| 575 | self->softspace = 0; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 576 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 577 | UNLESS (self->buf=malloc(size*sizeof(char))) { |
| 578 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
| 579 | self->buf_size = 0; |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | self->buf_size=size; |
| 584 | return (PyObject*)self; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 587 | /* End of code for StringO objects */ |
| 588 | /* -------------------------------------------------------- */ |
| 589 | |
| 590 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 591 | I_close(Iobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 592 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 593 | UNLESS (PyArg_ParseTuple(args, ":close")) return NULL; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 594 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 595 | Py_XDECREF(self->pbuf); |
| 596 | self->pbuf = NULL; |
| 597 | self->buf = NULL; |
| 598 | |
| 599 | self->pos = self->string_size = 0; |
| 600 | |
| 601 | Py_INCREF(Py_None); |
| 602 | return Py_None; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static PyObject * |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 606 | I_seek(Iobject *self, PyObject *args) { |
| 607 | int position, mode = 0; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 608 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 609 | UNLESS (IO__opencheck(IOOOBJECT(self))) return NULL; |
| 610 | UNLESS (PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) |
| 611 | return NULL; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 612 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 613 | if (mode == 2) position += self->string_size; |
| 614 | else if (mode == 1) position += self->pos; |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 615 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 616 | if (position < 0) position=0; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 617 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 618 | self->pos=position; |
| 619 | |
| 620 | Py_INCREF(Py_None); |
| 621 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static struct PyMethodDef I_methods[] = { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 625 | /* Common methods: */ |
| 626 | {"flush", (PyCFunction)IO_flush, METH_VARARGS, IO_flush__doc__}, |
| 627 | {"getvalue", (PyCFunction)IO_getval, METH_VARARGS, IO_getval__doc__}, |
| 628 | {"isatty", (PyCFunction)IO_isatty, METH_VARARGS, IO_isatty__doc__}, |
| 629 | {"read", (PyCFunction)IO_read, METH_VARARGS, IO_read__doc__}, |
| 630 | {"readline", (PyCFunction)IO_readline, METH_VARARGS, IO_readline__doc__}, |
| 631 | {"readlines", (PyCFunction)IO_readlines,METH_VARARGS, IO_readlines__doc__}, |
| 632 | {"reset", (PyCFunction)IO_reset, METH_VARARGS, IO_reset__doc__}, |
| 633 | {"tell", (PyCFunction)IO_tell, METH_VARARGS, IO_tell__doc__}, |
| 634 | {"truncate", (PyCFunction)IO_truncate, METH_VARARGS, IO_truncate__doc__}, |
| 635 | |
| 636 | /* Read-only StringIO specific methods: */ |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 637 | {"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__}, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 638 | {"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__}, |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 639 | {NULL, NULL} |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 640 | }; |
| 641 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 642 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 643 | I_dealloc(Iobject *self) { |
Guido van Rossum | 15a4039 | 1997-09-03 00:09:26 +0000 | [diff] [blame] | 644 | Py_XDECREF(self->pbuf); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 645 | PyObject_Del(self); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 649 | I_getattr(Iobject *self, char *name) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 650 | return Py_FindMethod(I_methods, (PyObject *)self, name); |
| 651 | } |
| 652 | |
| 653 | static char Itype__doc__[] = |
| 654 | "Simple type for treating strings as input file streams" |
| 655 | ; |
| 656 | |
| 657 | static PyTypeObject Itype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 658 | PyObject_HEAD_INIT(NULL) |
| 659 | 0, /*ob_size*/ |
| 660 | "StringI", /*tp_name*/ |
| 661 | sizeof(Iobject), /*tp_basicsize*/ |
| 662 | 0, /*tp_itemsize*/ |
| 663 | /* methods */ |
| 664 | (destructor)I_dealloc, /*tp_dealloc*/ |
| 665 | (printfunc)0, /*tp_print*/ |
| 666 | (getattrfunc)I_getattr, /*tp_getattr*/ |
| 667 | (setattrfunc)0, /*tp_setattr*/ |
| 668 | (cmpfunc)0, /*tp_compare*/ |
| 669 | (reprfunc)0, /*tp_repr*/ |
| 670 | 0, /*tp_as_number*/ |
| 671 | 0, /*tp_as_sequence*/ |
| 672 | 0, /*tp_as_mapping*/ |
| 673 | (hashfunc)0, /*tp_hash*/ |
| 674 | (ternaryfunc)0, /*tp_call*/ |
| 675 | (reprfunc)0, /*tp_str*/ |
| 676 | |
| 677 | /* Space for future expansion */ |
| 678 | 0L,0L,0L,0L, |
| 679 | Itype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 680 | }; |
| 681 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 682 | static PyObject * |
| 683 | newIobject(PyObject *s) { |
| 684 | Iobject *self; |
| 685 | char *buf; |
| 686 | int size; |
Jeremy Hylton | 127b2ef | 2000-04-12 22:04:01 +0000 | [diff] [blame] | 687 | |
| 688 | if (!PyString_Check(s)) { |
| 689 | PyErr_Format(PyExc_TypeError, "expected string, %.200s found", |
| 690 | s->ob_type->tp_name); |
| 691 | return NULL; |
| 692 | } |
| 693 | buf = PyString_AS_STRING(s); |
| 694 | size = PyString_GET_SIZE(s); |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 695 | UNLESS (self = PyObject_New(Iobject, &Itype)) return NULL; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 696 | Py_INCREF(s); |
| 697 | self->buf=buf; |
| 698 | self->string_size=size; |
| 699 | self->pbuf=s; |
| 700 | self->pos=0; |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 701 | |
| 702 | return (PyObject*)self; |
| 703 | } |
| 704 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 705 | /* End of code for StringI objects */ |
| 706 | /* -------------------------------------------------------- */ |
| 707 | |
| 708 | |
| 709 | static char IO_StringIO__doc__[] = |
| 710 | "StringIO([s]) -- Return a StringIO-like stream for reading or writing" |
| 711 | ; |
| 712 | |
| 713 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 714 | IO_StringIO(PyObject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 715 | PyObject *s=0; |
| 716 | |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 717 | if (!PyArg_ParseTuple(args, "|O:StringIO", &s)) return NULL; |
| 718 | |
| 719 | if (s) return newIobject(s); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 720 | return newOobject(128); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* List of methods defined in the module */ |
| 724 | |
| 725 | static struct PyMethodDef IO_methods[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 726 | {"StringIO", (PyCFunction)IO_StringIO, |
| 727 | METH_VARARGS, IO_StringIO__doc__}, |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 728 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 729 | }; |
| 730 | |
| 731 | |
| 732 | /* Initialization function for the module (*must* be called initcStringIO) */ |
| 733 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 734 | static struct PycStringIO_CAPI CAPI = { |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 735 | IO_cread, |
| 736 | IO_creadline, |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 737 | O_cwrite, |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 738 | IO_cgetval, |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 739 | newOobject, |
| 740 | newIobject, |
| 741 | &Itype, |
| 742 | &Otype, |
| 743 | }; |
| 744 | |
Guido van Rossum | 476e49f | 1998-12-15 21:43:15 +0000 | [diff] [blame] | 745 | #ifndef DL_EXPORT /* declarations for DLL import/export */ |
| 746 | #define DL_EXPORT(RTYPE) RTYPE |
| 747 | #endif |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 748 | DL_EXPORT(void) |
Thomas Wouters | 58d0510 | 2000-07-24 14:43:35 +0000 | [diff] [blame] | 749 | initcStringIO(void) { |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 750 | PyObject *m, *d, *v; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 751 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 752 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 753 | /* Create the module and add the functions */ |
| 754 | m = Py_InitModule4("cStringIO", IO_methods, |
| 755 | cStringIO_module_documentation, |
| 756 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 757 | |
| 758 | /* Add some symbolic constants to the module */ |
| 759 | d = PyModule_GetDict(m); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 761 | /* Export C API */ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 762 | Itype.ob_type=&PyType_Type; |
| 763 | Otype.ob_type=&PyType_Type; |
Guido van Rossum | 9efe8ef | 1997-09-03 18:19:40 +0000 | [diff] [blame] | 764 | PyDict_SetItemString(d,"cStringIO_CAPI", |
| 765 | v = PyCObject_FromVoidPtr(&CAPI,NULL)); |
| 766 | Py_XDECREF(v); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 767 | |
| 768 | /* Export Types */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 769 | PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); |
| 770 | PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 771 | |
| 772 | /* Maybe make certain warnings go away */ |
Jim Fulton | e60de4d | 2000-10-06 19:24:23 +0000 | [diff] [blame] | 773 | if (0) PycString_IMPORT; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 774 | } |