Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | $Id$ |
| 4 | |
| 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 Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 54 | info@digicool.com |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 55 | Digital Creations L.C. |
| 56 | |
| 57 | (540) 371-6909 |
| 58 | |
| 59 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 60 | */ |
| 61 | static 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" |
| 82 | " an_input_stream.reset() # OK, start over, note no seek yet\n" |
| 83 | " 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 Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 87 | "\n" |
| 88 | "$Id$\n" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 89 | ; |
| 90 | |
| 91 | #include "Python.h" |
| 92 | #include "import.h" |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 93 | #include "cStringIO.h" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 95 | #define UNLESS(E) if(!(E)) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 97 | /* Declarations for objects of type StringO */ |
| 98 | |
| 99 | typedef struct { |
| 100 | PyObject_HEAD |
| 101 | char *buf; |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 102 | int pos, string_size, buf_size, closed, softspace; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 103 | } Oobject; |
| 104 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 105 | /* Declarations for objects of type StringI */ |
| 106 | |
| 107 | typedef struct { |
| 108 | PyObject_HEAD |
| 109 | char *buf; |
| 110 | int pos, string_size, closed; |
| 111 | PyObject *pbuf; |
| 112 | } Iobject; |
| 113 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 114 | static char O_reset__doc__[] = |
| 115 | "reset() -- Reset the file position to the beginning" |
| 116 | ; |
| 117 | |
| 118 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 119 | O_reset(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 120 | self->pos = 0; |
| 121 | |
| 122 | Py_INCREF(Py_None); |
| 123 | return Py_None; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static char O_tell__doc__[] = |
| 128 | "tell() -- get the current position."; |
| 129 | |
| 130 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 131 | O_tell(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 132 | return PyInt_FromLong(self->pos); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | static 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 | |
| 140 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 141 | O_seek(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 142 | int position, mode = 0; |
| 143 | |
| 144 | UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) |
| 145 | { |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | if (mode == 2) |
| 150 | { |
| 151 | position += self->string_size; |
| 152 | } |
| 153 | else if (mode == 1) |
| 154 | { |
| 155 | position += self->pos; |
| 156 | } |
| 157 | |
| 158 | self->pos = (position > self->string_size ? self->string_size : |
| 159 | (position < 0 ? 0 : position)); |
| 160 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 161 | Py_INCREF(Py_None); |
| 162 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static char O_read__doc__[] = |
| 166 | "read([s]) -- Read s characters, or the rest of the string" |
| 167 | ; |
| 168 | |
| 169 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 170 | O_cread(PyObject *self, char **output, int n) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 171 | int l; |
| 172 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 173 | l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 174 | if (n < 0 || n > l) |
| 175 | { |
| 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; |
| 237 | if(newl >= ((Oobject*)self)->buf_size) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 238 | { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 239 | ((Oobject*)self)->buf_size*=2; |
| 240 | if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1; |
| 241 | UNLESS(((Oobject*)self)->buf= |
| 242 | (char*)realloc(((Oobject*)self)->buf, |
| 243 | (((Oobject*)self)->buf_size) *sizeof(char))) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 244 | { |
| 245 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 246 | ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 247 | return -1; |
| 248 | } |
| 249 | } |
| 250 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 251 | memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 253 | ((Oobject*)self)->pos += l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 255 | if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 256 | { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 257 | ((Oobject*)self)->string_size = ((Oobject*)self)->pos; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return l; |
| 261 | } |
| 262 | |
| 263 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 264 | O_write(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 265 | PyObject *s; |
Barry Warsaw | 61a63e1 | 1997-01-14 17:38:28 +0000 | [diff] [blame] | 266 | char *c; |
| 267 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 268 | |
| 269 | UNLESS(PyArg_Parse(args, "O", &s)) return NULL; |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 270 | UNLESS(-1 != (l=PyString_Size(s))) return NULL; |
| 271 | UNLESS(c=PyString_AsString(s)) return NULL; |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 272 | UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 274 | Py_INCREF(Py_None); |
| 275 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 279 | O_getval(Oobject *self, PyObject *args) { |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 280 | PyObject *use_pos; |
| 281 | int s; |
| 282 | |
| 283 | use_pos=Py_None; |
| 284 | UNLESS(PyArg_ParseTuple(args,"|O",&use_pos)) return NULL; |
| 285 | if(PyObject_IsTrue(use_pos)) s=self->pos; |
| 286 | else s=self->string_size; |
| 287 | return PyString_FromStringAndSize(self->buf, s); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static PyObject * |
| 291 | O_cgetval(PyObject *self) { |
| 292 | return PyString_FromStringAndSize(((Oobject*)self)->buf, |
| 293 | ((Oobject*)self)->pos); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static char O_truncate__doc__[] = |
| 297 | "truncate(): truncate the file at the current position."; |
| 298 | |
| 299 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 300 | O_truncate(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 301 | self->string_size = self->pos; |
| 302 | Py_INCREF(Py_None); |
| 303 | return Py_None; |
| 304 | } |
| 305 | |
| 306 | static char O_isatty__doc__[] = "isatty(): always returns 0"; |
| 307 | |
| 308 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 309 | O_isatty(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 310 | return PyInt_FromLong(0); |
| 311 | } |
| 312 | |
| 313 | static char O_close__doc__[] = "close(): explicitly release resources held."; |
| 314 | |
| 315 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 316 | O_close(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 317 | free(self->buf); |
| 318 | |
| 319 | self->pos = self->string_size = self->buf_size = 0; |
| 320 | self->closed = 1; |
| 321 | |
| 322 | Py_INCREF(Py_None); |
| 323 | return Py_None; |
| 324 | } |
| 325 | |
| 326 | static char O_flush__doc__[] = "flush(): does nothing."; |
| 327 | |
| 328 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 329 | O_flush(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 330 | Py_INCREF(Py_None); |
| 331 | return Py_None; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | static char O_writelines__doc__[] = "blah"; |
| 336 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 337 | O_writelines(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 338 | PyObject *string_module = 0; |
| 339 | static PyObject *string_joinfields = 0; |
| 340 | |
| 341 | UNLESS(PyArg_Parse(args, "O", args)) |
| 342 | { |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | if (!string_joinfields) |
| 347 | { |
| 348 | UNLESS(string_module = PyImport_ImportModule("string")) |
| 349 | { |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | UNLESS(string_joinfields= |
| 354 | PyObject_GetAttrString(string_module, "joinfields")) |
| 355 | { |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | Py_DECREF(string_module); |
| 360 | } |
| 361 | |
| 362 | if (PyObject_Length(args) == -1) |
| 363 | { |
| 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | return O_write(self, |
| 368 | PyObject_CallFunction(string_joinfields, "Os", args, "")); |
| 369 | } |
| 370 | |
| 371 | static struct PyMethodDef O_methods[] = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 372 | {"write", (PyCFunction)O_write, 0, O_write__doc__}, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 373 | {"read", (PyCFunction)O_read, 1, O_read__doc__}, |
| 374 | {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 375 | {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, |
| 376 | {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, |
| 377 | {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 378 | {"getvalue", (PyCFunction)O_getval, 1, |
| 379 | "getvalue([use_pos]) -- Get the string value." |
| 380 | "\n" |
| 381 | "If use_pos is specified and is a true value, then the string returned\n" |
| 382 | "will include only the text up to the current file position.\n" |
| 383 | }, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 384 | {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, |
| 385 | {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, |
| 386 | {"close", (PyCFunction)O_close, 0, O_close__doc__}, |
| 387 | {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, |
| 388 | {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__}, |
| 389 | {NULL, NULL} /* sentinel */ |
| 390 | }; |
| 391 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 392 | |
| 393 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 394 | O_dealloc(Oobject *self) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 395 | free(self->buf); |
| 396 | PyMem_DEL(self); |
| 397 | } |
| 398 | |
| 399 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 400 | O_getattr(Oobject *self, char *name) { |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 401 | if (strcmp(name, "softspace") == 0) { |
| 402 | return PyInt_FromLong(self->softspace); |
| 403 | } |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 404 | return Py_FindMethod(O_methods, (PyObject *)self, name); |
| 405 | } |
| 406 | |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 407 | static int |
| 408 | O_setattr(Oobject *self, char *name, PyObject *value) { |
| 409 | long x; |
| 410 | if (strcmp(name, "softspace") != 0) { |
| 411 | PyErr_SetString(PyExc_AttributeError, name); |
| 412 | return -1; |
| 413 | } |
| 414 | x = PyInt_AsLong(value); |
| 415 | if (x == -1 && PyErr_Occurred()) |
| 416 | return -1; |
| 417 | self->softspace = x; |
| 418 | return 0; |
| 419 | } |
| 420 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 421 | static char Otype__doc__[] = |
| 422 | "Simple type for output to strings." |
| 423 | ; |
| 424 | |
| 425 | static PyTypeObject Otype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 426 | PyObject_HEAD_INIT(NULL) |
| 427 | 0, /*ob_size*/ |
| 428 | "StringO", /*tp_name*/ |
| 429 | sizeof(Oobject), /*tp_basicsize*/ |
| 430 | 0, /*tp_itemsize*/ |
| 431 | /* methods */ |
| 432 | (destructor)O_dealloc, /*tp_dealloc*/ |
| 433 | (printfunc)0, /*tp_print*/ |
| 434 | (getattrfunc)O_getattr, /*tp_getattr*/ |
Guido van Rossum | 3dc35b0 | 1997-04-11 19:56:06 +0000 | [diff] [blame] | 435 | (setattrfunc)O_setattr, /*tp_setattr*/ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 436 | (cmpfunc)0, /*tp_compare*/ |
| 437 | (reprfunc)0, /*tp_repr*/ |
| 438 | 0, /*tp_as_number*/ |
| 439 | 0, /*tp_as_sequence*/ |
| 440 | 0, /*tp_as_mapping*/ |
| 441 | (hashfunc)0, /*tp_hash*/ |
| 442 | (ternaryfunc)0, /*tp_call*/ |
| 443 | (reprfunc)0, /*tp_str*/ |
| 444 | |
| 445 | /* Space for future expansion */ |
| 446 | 0L,0L,0L,0L, |
| 447 | Otype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 448 | }; |
| 449 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 450 | static PyObject * |
| 451 | newOobject(int size) { |
| 452 | Oobject *self; |
| 453 | |
| 454 | self = PyObject_NEW(Oobject, &Otype); |
| 455 | if (self == NULL) |
| 456 | return NULL; |
| 457 | self->pos=0; |
| 458 | self->closed = 0; |
| 459 | self->string_size = 0; |
| 460 | self->softspace = 0; |
| 461 | |
| 462 | UNLESS(self->buf=malloc(size*sizeof(char))) |
| 463 | { |
| 464 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
| 465 | self->buf_size = 0; |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | self->buf_size=size; |
| 470 | return (PyObject*)self; |
| 471 | } |
| 472 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 473 | /* End of code for StringO objects */ |
| 474 | /* -------------------------------------------------------- */ |
| 475 | |
| 476 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 477 | I_close(Iobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 478 | Py_DECREF(self->pbuf); |
| 479 | |
| 480 | self->pos = self->string_size = 0; |
| 481 | self->closed = 1; |
| 482 | |
| 483 | Py_INCREF(Py_None); |
| 484 | return Py_None; |
| 485 | } |
| 486 | |
| 487 | static struct PyMethodDef I_methods[] = { |
| 488 | {"read", (PyCFunction)O_read, 1, O_read__doc__}, |
| 489 | {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, |
| 490 | {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, |
| 491 | {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, |
| 492 | {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, |
| 493 | {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, |
| 494 | {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, |
| 495 | {"close", (PyCFunction)I_close, 0, O_close__doc__}, |
| 496 | {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, |
| 497 | {NULL, NULL} /* sentinel */ |
| 498 | }; |
| 499 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 500 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 501 | I_dealloc(Iobject *self) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 502 | Py_DECREF(self->pbuf); |
| 503 | PyMem_DEL(self); |
| 504 | } |
| 505 | |
| 506 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 507 | I_getattr(Iobject *self, char *name) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 508 | return Py_FindMethod(I_methods, (PyObject *)self, name); |
| 509 | } |
| 510 | |
| 511 | static char Itype__doc__[] = |
| 512 | "Simple type for treating strings as input file streams" |
| 513 | ; |
| 514 | |
| 515 | static PyTypeObject Itype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 516 | PyObject_HEAD_INIT(NULL) |
| 517 | 0, /*ob_size*/ |
| 518 | "StringI", /*tp_name*/ |
| 519 | sizeof(Iobject), /*tp_basicsize*/ |
| 520 | 0, /*tp_itemsize*/ |
| 521 | /* methods */ |
| 522 | (destructor)I_dealloc, /*tp_dealloc*/ |
| 523 | (printfunc)0, /*tp_print*/ |
| 524 | (getattrfunc)I_getattr, /*tp_getattr*/ |
| 525 | (setattrfunc)0, /*tp_setattr*/ |
| 526 | (cmpfunc)0, /*tp_compare*/ |
| 527 | (reprfunc)0, /*tp_repr*/ |
| 528 | 0, /*tp_as_number*/ |
| 529 | 0, /*tp_as_sequence*/ |
| 530 | 0, /*tp_as_mapping*/ |
| 531 | (hashfunc)0, /*tp_hash*/ |
| 532 | (ternaryfunc)0, /*tp_call*/ |
| 533 | (reprfunc)0, /*tp_str*/ |
| 534 | |
| 535 | /* Space for future expansion */ |
| 536 | 0L,0L,0L,0L, |
| 537 | Itype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 540 | static PyObject * |
| 541 | newIobject(PyObject *s) { |
| 542 | Iobject *self; |
| 543 | char *buf; |
| 544 | int size; |
| 545 | |
| 546 | UNLESS(buf=PyString_AsString(s)) return NULL; |
| 547 | UNLESS(-1 != (size=PyString_Size(s))) return NULL; |
| 548 | UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL; |
| 549 | Py_INCREF(s); |
| 550 | self->buf=buf; |
| 551 | self->string_size=size; |
| 552 | self->pbuf=s; |
| 553 | self->pos=0; |
| 554 | self->closed = 0; |
| 555 | |
| 556 | return (PyObject*)self; |
| 557 | } |
| 558 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 559 | /* End of code for StringI objects */ |
| 560 | /* -------------------------------------------------------- */ |
| 561 | |
| 562 | |
| 563 | static char IO_StringIO__doc__[] = |
| 564 | "StringIO([s]) -- Return a StringIO-like stream for reading or writing" |
| 565 | ; |
| 566 | |
| 567 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 568 | IO_StringIO(PyObject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 569 | PyObject *s=0; |
| 570 | |
| 571 | UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL; |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 572 | if(s) return newIobject(s); |
| 573 | return newOobject(128); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* List of methods defined in the module */ |
| 577 | |
| 578 | static struct PyMethodDef IO_methods[] = { |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 579 | {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__}, |
| 580 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 581 | }; |
| 582 | |
| 583 | |
| 584 | /* Initialization function for the module (*must* be called initcStringIO) */ |
| 585 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 586 | static struct PycStringIO_CAPI CAPI = { |
| 587 | O_cread, |
| 588 | O_creadline, |
| 589 | O_cwrite, |
| 590 | O_cgetval, |
| 591 | newOobject, |
| 592 | newIobject, |
| 593 | &Itype, |
| 594 | &Otype, |
| 595 | }; |
| 596 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 597 | void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 598 | initcStringIO() { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 599 | PyObject *m, *d; |
| 600 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 601 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 602 | /* Create the module and add the functions */ |
| 603 | m = Py_InitModule4("cStringIO", IO_methods, |
| 604 | cStringIO_module_documentation, |
| 605 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 606 | |
| 607 | /* Add some symbolic constants to the module */ |
| 608 | d = PyModule_GetDict(m); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 609 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 610 | /* Export C API */ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 611 | Itype.ob_type=&PyType_Type; |
| 612 | Otype.ob_type=&PyType_Type; |
| 613 | PyDict_SetItemString(d,"cStringIO_CAPI", PyCObject_FromVoidPtr(&CAPI,NULL)); |
| 614 | |
| 615 | /* Export Types */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 616 | PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); |
| 617 | PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 618 | |
| 619 | /* Maybe make certain warnings go away */ |
| 620 | if(0) PycString_IMPORT; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 621 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 622 | /* Check for errors */ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame] | 623 | if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 624 | } |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 625 | |
Guido van Rossum | 142eeb8 | 1997-08-13 03:14:41 +0000 | [diff] [blame] | 626 | |
| 627 | /****************************************************************************** |
| 628 | |
| 629 | $Log$ |
| 630 | Revision 2.6 1997/08/13 03:14:41 guido |
| 631 | cPickle release 0.3 from Jim Fulton |
| 632 | |
| 633 | Revision 1.21 1997/06/19 18:51:42 jim |
| 634 | Added ident string. |
| 635 | |
| 636 | Revision 1.20 1997/06/13 20:50:50 jim |
| 637 | - Various changes to make gcc -Wall -pedantic happy, including |
| 638 | getting rid of staticforward declarations and adding pretend use |
| 639 | of two statics defined in .h file. |
| 640 | |
| 641 | Revision 1.19 1997/06/02 18:15:17 jim |
| 642 | Merged in guido's changes. |
| 643 | |
| 644 | Revision 1.18 1997/05/07 16:26:47 jim |
| 645 | getvalue() can nor be given an argument. If this argument is true, |
| 646 | then getvalue returns the text upto the current position. Otherwise |
| 647 | it returns all of the text. The default value of the argument is |
| 648 | false. |
| 649 | |
| 650 | Revision 1.17 1997/04/17 18:02:46 chris |
| 651 | getvalue() now returns entire string, not just the string up to |
| 652 | current position |
| 653 | |
| 654 | Revision 2.5 1997/04/11 19:56:06 guido |
| 655 | My own patch: support writable 'softspace' attribute. |
| 656 | |
| 657 | > Jim asked: What is softspace for? |
| 658 | |
| 659 | It's an old feature. The print statement uses this to remember |
| 660 | whether it should insert a space before the next item or not. |
| 661 | Implementation is in fileobject.c. |
| 662 | |
| 663 | Revision 1.11 1997/01/23 20:45:01 jim |
| 664 | ANSIfied it. |
| 665 | Changed way C API was exported. |
| 666 | |
| 667 | Revision 1.10 1997/01/02 15:19:55 chris |
| 668 | checked in to be sure repository is up to date. |
| 669 | |
| 670 | Revision 1.9 1996/12/27 21:40:29 jim |
| 671 | Took out some lamosities in interface, like returning self from |
| 672 | write. |
| 673 | |
| 674 | Revision 1.8 1996/12/23 15:52:49 jim |
| 675 | Added ifdef to check for CObject before using it. |
| 676 | |
| 677 | Revision 1.7 1996/12/23 15:22:35 jim |
| 678 | Finished implementation, adding full compatibility with StringIO, and |
| 679 | then some. |
| 680 | |
| 681 | *****************************************************************************/ |