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 | |
| 54 | Jim Fulton, jim@digicool.com |
| 55 | Digital Creations L.C. |
| 56 | |
| 57 | (540) 371-6909 |
| 58 | |
| 59 | |
| 60 | $Log$ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 61 | Revision 2.4 1997/04/09 17:35:33 guido |
| 62 | Unknown changes by Jim Fulton. |
Barry Warsaw | 61a63e1 | 1997-01-14 17:38:28 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 64 | Revision 1.16 1997/02/17 22:17:43 jim |
| 65 | *** empty log message *** |
Barry Warsaw | 61a63e1 | 1997-01-14 17:38:28 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 67 | Revision 1.14 1997/01/24 19:56:24 chris |
| 68 | undid last change |
| 69 | |
| 70 | Revision 1.13 1997/01/24 19:45:20 chris |
| 71 | extra byte in buffer no longer included in buf_size |
| 72 | |
| 73 | Revision 1.12 1997/01/24 19:38:28 chris |
| 74 | *** empty log message *** |
| 75 | |
| 76 | Revision 1.11 1997/01/23 20:45:01 jim |
| 77 | ANSIfied it. |
| 78 | Changed way C API was exported. |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 79 | |
| 80 | Revision 1.10 1997/01/02 15:19:55 chris |
| 81 | checked in to be sure repository is up to date. |
| 82 | |
| 83 | Revision 1.9 1996/12/27 21:40:29 jim |
| 84 | Took out some lamosities in interface, like returning self from |
| 85 | write. |
| 86 | |
| 87 | Revision 1.8 1996/12/23 15:52:49 jim |
| 88 | Added ifdef to check for CObject before using it. |
| 89 | |
| 90 | Revision 1.7 1996/12/23 15:22:35 jim |
| 91 | Finished implementation, adding full compatibility with StringIO, and |
| 92 | then some. |
| 93 | |
| 94 | We still need to take out some cStringIO oddities. |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 95 | |
| 96 | Revision 1.6 1996/10/15 18:42:07 jim |
| 97 | Added lots of casts to make warnings go away. |
| 98 | |
| 99 | Revision 1.5 1996/10/11 21:03:42 jim |
| 100 | *** empty log message *** |
| 101 | |
| 102 | Revision 1.4 1996/10/11 21:02:15 jim |
| 103 | *** empty log message *** |
| 104 | |
| 105 | Revision 1.3 1996/10/07 20:51:38 chris |
| 106 | *** empty log message *** |
| 107 | |
| 108 | Revision 1.2 1996/07/18 13:08:34 jfulton |
| 109 | *** empty log message *** |
| 110 | |
| 111 | Revision 1.1 1996/07/15 17:06:33 jfulton |
| 112 | Initial version. |
| 113 | |
| 114 | |
| 115 | */ |
| 116 | static char cStringIO_module_documentation[] = |
| 117 | "A simple fast partial StringIO replacement.\n" |
| 118 | "\n" |
| 119 | "This module provides a simple useful replacement for\n" |
| 120 | "the StringIO module that is written in C. It does not provide the\n" |
| 121 | "full generality if StringIO, but it provides anough for most\n" |
| 122 | "applications and is especially useful in conjuction with the\n" |
| 123 | "pickle module.\n" |
| 124 | "\n" |
| 125 | "Usage:\n" |
| 126 | "\n" |
| 127 | " from cStringIO import StringIO\n" |
| 128 | "\n" |
| 129 | " an_output_stream=StringIO()\n" |
| 130 | " an_output_stream.write(some_stuff)\n" |
| 131 | " ...\n" |
| 132 | " value=an_output_stream.getvalue() # str(an_output_stream) works too!\n" |
| 133 | "\n" |
| 134 | " an_input_stream=StringIO(a_string)\n" |
| 135 | " spam=an_input_stream.readline()\n" |
| 136 | " spam=an_input_stream.read(5)\n" |
| 137 | " an_input_stream.reset() # OK, start over, note no seek yet\n" |
| 138 | " spam=an_input_stream.read() # and read it all\n" |
| 139 | " \n" |
| 140 | "If someone else wants to provide a more complete implementation,\n" |
| 141 | "go for it. :-) \n" |
| 142 | ; |
| 143 | |
| 144 | #include "Python.h" |
| 145 | #include "import.h" |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 146 | #include "cStringIO.h" |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 148 | #define UNLESS(E) if(!(E)) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 149 | |
| 150 | /* ----------------------------------------------------- */ |
| 151 | |
| 152 | /* Declarations for objects of type StringO */ |
| 153 | |
| 154 | typedef struct { |
| 155 | PyObject_HEAD |
| 156 | char *buf; |
| 157 | int pos, string_size, buf_size, closed; |
| 158 | } Oobject; |
| 159 | |
| 160 | staticforward PyTypeObject Otype; |
| 161 | |
| 162 | /* ---------------------------------------------------------------- */ |
| 163 | |
| 164 | /* Declarations for objects of type StringI */ |
| 165 | |
| 166 | typedef struct { |
| 167 | PyObject_HEAD |
| 168 | char *buf; |
| 169 | int pos, string_size, closed; |
| 170 | PyObject *pbuf; |
| 171 | } Iobject; |
| 172 | |
| 173 | staticforward PyTypeObject Itype; |
| 174 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 175 | /* ---------------------------------------------------------------- */ |
| 176 | |
| 177 | static char O_reset__doc__[] = |
| 178 | "reset() -- Reset the file position to the beginning" |
| 179 | ; |
| 180 | |
| 181 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 182 | O_reset(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 183 | self->pos = 0; |
| 184 | |
| 185 | Py_INCREF(Py_None); |
| 186 | return Py_None; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | static char O_tell__doc__[] = |
| 191 | "tell() -- get the current position."; |
| 192 | |
| 193 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 194 | O_tell(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 195 | return PyInt_FromLong(self->pos); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | static char O_seek__doc__[] = |
| 200 | "seek(position) -- set the current position\n" |
| 201 | "seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF"; |
| 202 | |
| 203 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 204 | O_seek(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 205 | int position, mode = 0; |
| 206 | |
| 207 | UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode)) |
| 208 | { |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | if (mode == 2) |
| 213 | { |
| 214 | position += self->string_size; |
| 215 | } |
| 216 | else if (mode == 1) |
| 217 | { |
| 218 | position += self->pos; |
| 219 | } |
| 220 | |
| 221 | self->pos = (position > self->string_size ? self->string_size : |
| 222 | (position < 0 ? 0 : position)); |
| 223 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 224 | Py_INCREF(Py_None); |
| 225 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static char O_read__doc__[] = |
| 229 | "read([s]) -- Read s characters, or the rest of the string" |
| 230 | ; |
| 231 | |
| 232 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 233 | O_cread(PyObject *self, char **output, int n) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 234 | int l; |
| 235 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 236 | l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 237 | if (n < 0 || n > l) |
| 238 | { |
| 239 | n = l; |
| 240 | } |
| 241 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 242 | *output=((Oobject*)self)->buf + ((Oobject*)self)->pos; |
| 243 | ((Oobject*)self)->pos += n; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 244 | return n; |
| 245 | } |
| 246 | |
| 247 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 248 | O_read(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 249 | int n = -1; |
| 250 | char *output; |
| 251 | |
| 252 | UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL; |
| 253 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 254 | n=O_cread((PyObject*)self,&output,n); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 255 | |
| 256 | return PyString_FromStringAndSize(output, n); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | static char O_readline__doc__[] = |
| 261 | "readline() -- Read one line" |
| 262 | ; |
| 263 | |
| 264 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 265 | O_creadline(PyObject *self, char **output) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 266 | char *n, *s; |
| 267 | int l; |
| 268 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 269 | for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos, |
| 270 | s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 271 | n < s && *n != '\n'; n++); |
| 272 | if (n < s) n++; |
| 273 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 274 | *output=((Oobject*)self)->buf + ((Oobject*)self)->pos; |
| 275 | l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos; |
| 276 | ((Oobject*)self)->pos += l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 277 | return l; |
| 278 | } |
| 279 | |
| 280 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 281 | O_readline(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 282 | int n; |
| 283 | char *output; |
| 284 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 285 | n=O_creadline((PyObject*)self,&output); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 286 | return PyString_FromStringAndSize(output, n); |
| 287 | } |
| 288 | |
| 289 | static char O_write__doc__[] = |
| 290 | "write(s) -- Write a string to the file" |
| 291 | "\n\nNote (hack:) writing None resets the buffer" |
| 292 | ; |
| 293 | |
| 294 | |
| 295 | static int |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 296 | O_cwrite(PyObject *self, char *c, int l) { |
Barry Warsaw | 61a63e1 | 1997-01-14 17:38:28 +0000 | [diff] [blame] | 297 | int newl; |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 299 | newl=((Oobject*)self)->pos+l; |
| 300 | if(newl >= ((Oobject*)self)->buf_size) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 301 | { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 302 | ((Oobject*)self)->buf_size*=2; |
| 303 | if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1; |
| 304 | UNLESS(((Oobject*)self)->buf= |
| 305 | (char*)realloc(((Oobject*)self)->buf, |
| 306 | (((Oobject*)self)->buf_size) *sizeof(char))) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 307 | { |
| 308 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 309 | ((Oobject*)self)->buf_size=((Oobject*)self)->pos=0; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 310 | return -1; |
| 311 | } |
| 312 | } |
| 313 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 314 | memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 316 | ((Oobject*)self)->pos += l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 318 | if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 319 | { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 320 | ((Oobject*)self)->string_size = ((Oobject*)self)->pos; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | return l; |
| 324 | } |
| 325 | |
| 326 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 327 | O_write(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 328 | PyObject *s; |
Barry Warsaw | 61a63e1 | 1997-01-14 17:38:28 +0000 | [diff] [blame] | 329 | char *c; |
| 330 | int l; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 331 | |
| 332 | UNLESS(PyArg_Parse(args, "O", &s)) return NULL; |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 333 | UNLESS(-1 != (l=PyString_Size(s))) return NULL; |
| 334 | UNLESS(c=PyString_AsString(s)) return NULL; |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 335 | UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 337 | Py_INCREF(Py_None); |
| 338 | return Py_None; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 342 | O_getval(Oobject *self, PyObject *args) { |
| 343 | return PyString_FromStringAndSize(self->buf, self->pos); |
| 344 | } |
| 345 | |
| 346 | static PyObject * |
| 347 | O_cgetval(PyObject *self) { |
| 348 | return PyString_FromStringAndSize(((Oobject*)self)->buf, |
| 349 | ((Oobject*)self)->pos); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static char O_truncate__doc__[] = |
| 353 | "truncate(): truncate the file at the current position."; |
| 354 | |
| 355 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 356 | O_truncate(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 357 | self->string_size = self->pos; |
| 358 | Py_INCREF(Py_None); |
| 359 | return Py_None; |
| 360 | } |
| 361 | |
| 362 | static char O_isatty__doc__[] = "isatty(): always returns 0"; |
| 363 | |
| 364 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 365 | O_isatty(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 366 | return PyInt_FromLong(0); |
| 367 | } |
| 368 | |
| 369 | static char O_close__doc__[] = "close(): explicitly release resources held."; |
| 370 | |
| 371 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 372 | O_close(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 373 | free(self->buf); |
| 374 | |
| 375 | self->pos = self->string_size = self->buf_size = 0; |
| 376 | self->closed = 1; |
| 377 | |
| 378 | Py_INCREF(Py_None); |
| 379 | return Py_None; |
| 380 | } |
| 381 | |
| 382 | static char O_flush__doc__[] = "flush(): does nothing."; |
| 383 | |
| 384 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 385 | O_flush(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 386 | Py_INCREF(Py_None); |
| 387 | return Py_None; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | static char O_writelines__doc__[] = "blah"; |
| 392 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 393 | O_writelines(Oobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 394 | PyObject *string_module = 0; |
| 395 | static PyObject *string_joinfields = 0; |
| 396 | |
| 397 | UNLESS(PyArg_Parse(args, "O", args)) |
| 398 | { |
| 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | if (!string_joinfields) |
| 403 | { |
| 404 | UNLESS(string_module = PyImport_ImportModule("string")) |
| 405 | { |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | UNLESS(string_joinfields= |
| 410 | PyObject_GetAttrString(string_module, "joinfields")) |
| 411 | { |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | Py_DECREF(string_module); |
| 416 | } |
| 417 | |
| 418 | if (PyObject_Length(args) == -1) |
| 419 | { |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | return O_write(self, |
| 424 | PyObject_CallFunction(string_joinfields, "Os", args, "")); |
| 425 | } |
| 426 | |
| 427 | static struct PyMethodDef O_methods[] = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 428 | {"write", (PyCFunction)O_write, 0, O_write__doc__}, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 429 | {"read", (PyCFunction)O_read, 1, O_read__doc__}, |
| 430 | {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 431 | {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, |
| 432 | {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, |
| 433 | {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, |
| 434 | {"getvalue", (PyCFunction)O_getval, 0, "getvalue() -- Get the string value"}, |
| 435 | {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, |
| 436 | {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, |
| 437 | {"close", (PyCFunction)O_close, 0, O_close__doc__}, |
| 438 | {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, |
| 439 | {"writelines", (PyCFunction)O_writelines, 0, O_writelines__doc__}, |
| 440 | {NULL, NULL} /* sentinel */ |
| 441 | }; |
| 442 | |
| 443 | /* ---------- */ |
| 444 | |
| 445 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 446 | static PyObject * |
| 447 | newOobject(int size) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 448 | Oobject *self; |
| 449 | |
| 450 | self = PyObject_NEW(Oobject, &Otype); |
| 451 | if (self == NULL) |
| 452 | return NULL; |
| 453 | self->pos=0; |
| 454 | self->closed = 0; |
| 455 | self->string_size = 0; |
| 456 | |
| 457 | UNLESS(self->buf=malloc(size*sizeof(char))) |
| 458 | { |
| 459 | PyErr_SetString(PyExc_MemoryError,"out of memory"); |
| 460 | self->buf_size = 0; |
| 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | self->buf_size=size; |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 465 | return (PyObject*)self; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | |
| 469 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 470 | O_dealloc(Oobject *self) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 471 | free(self->buf); |
| 472 | PyMem_DEL(self); |
| 473 | } |
| 474 | |
| 475 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 476 | O_getattr(Oobject *self, char *name) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 477 | return Py_FindMethod(O_methods, (PyObject *)self, name); |
| 478 | } |
| 479 | |
| 480 | static char Otype__doc__[] = |
| 481 | "Simple type for output to strings." |
| 482 | ; |
| 483 | |
| 484 | static PyTypeObject Otype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 485 | PyObject_HEAD_INIT(NULL) |
| 486 | 0, /*ob_size*/ |
| 487 | "StringO", /*tp_name*/ |
| 488 | sizeof(Oobject), /*tp_basicsize*/ |
| 489 | 0, /*tp_itemsize*/ |
| 490 | /* methods */ |
| 491 | (destructor)O_dealloc, /*tp_dealloc*/ |
| 492 | (printfunc)0, /*tp_print*/ |
| 493 | (getattrfunc)O_getattr, /*tp_getattr*/ |
| 494 | (setattrfunc)0, /*tp_setattr*/ |
| 495 | (cmpfunc)0, /*tp_compare*/ |
| 496 | (reprfunc)0, /*tp_repr*/ |
| 497 | 0, /*tp_as_number*/ |
| 498 | 0, /*tp_as_sequence*/ |
| 499 | 0, /*tp_as_mapping*/ |
| 500 | (hashfunc)0, /*tp_hash*/ |
| 501 | (ternaryfunc)0, /*tp_call*/ |
| 502 | (reprfunc)0, /*tp_str*/ |
| 503 | |
| 504 | /* Space for future expansion */ |
| 505 | 0L,0L,0L,0L, |
| 506 | Otype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | /* End of code for StringO objects */ |
| 510 | /* -------------------------------------------------------- */ |
| 511 | |
| 512 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 513 | I_close(Iobject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 514 | Py_DECREF(self->pbuf); |
| 515 | |
| 516 | self->pos = self->string_size = 0; |
| 517 | self->closed = 1; |
| 518 | |
| 519 | Py_INCREF(Py_None); |
| 520 | return Py_None; |
| 521 | } |
| 522 | |
| 523 | static struct PyMethodDef I_methods[] = { |
| 524 | {"read", (PyCFunction)O_read, 1, O_read__doc__}, |
| 525 | {"readline", (PyCFunction)O_readline, 0, O_readline__doc__}, |
| 526 | {"reset", (PyCFunction)O_reset, 0, O_reset__doc__}, |
| 527 | {"seek", (PyCFunction)O_seek, 1, O_seek__doc__}, |
| 528 | {"tell", (PyCFunction)O_tell, 0, O_tell__doc__}, |
| 529 | {"truncate", (PyCFunction)O_truncate, 0, O_truncate__doc__}, |
| 530 | {"isatty", (PyCFunction)O_isatty, 0, O_isatty__doc__}, |
| 531 | {"close", (PyCFunction)I_close, 0, O_close__doc__}, |
| 532 | {"flush", (PyCFunction)O_flush, 0, O_flush__doc__}, |
| 533 | {NULL, NULL} /* sentinel */ |
| 534 | }; |
| 535 | |
| 536 | /* ---------- */ |
| 537 | |
| 538 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 539 | static PyObject * |
| 540 | newIobject(PyObject *s) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 541 | Iobject *self; |
| 542 | char *buf; |
| 543 | int size; |
| 544 | |
| 545 | UNLESS(buf=PyString_AsString(s)) return NULL; |
| 546 | UNLESS(-1 != (size=PyString_Size(s))) return NULL; |
| 547 | UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL; |
| 548 | Py_INCREF(s); |
| 549 | self->buf=buf; |
| 550 | self->string_size=size; |
| 551 | self->pbuf=s; |
| 552 | self->pos=0; |
| 553 | self->closed = 0; |
| 554 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 555 | return (PyObject*)self; |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
| 559 | static void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 560 | I_dealloc(Iobject *self) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 561 | Py_DECREF(self->pbuf); |
| 562 | PyMem_DEL(self); |
| 563 | } |
| 564 | |
| 565 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 566 | I_getattr(Iobject *self, char *name) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 567 | return Py_FindMethod(I_methods, (PyObject *)self, name); |
| 568 | } |
| 569 | |
| 570 | static char Itype__doc__[] = |
| 571 | "Simple type for treating strings as input file streams" |
| 572 | ; |
| 573 | |
| 574 | static PyTypeObject Itype = { |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 575 | PyObject_HEAD_INIT(NULL) |
| 576 | 0, /*ob_size*/ |
| 577 | "StringI", /*tp_name*/ |
| 578 | sizeof(Iobject), /*tp_basicsize*/ |
| 579 | 0, /*tp_itemsize*/ |
| 580 | /* methods */ |
| 581 | (destructor)I_dealloc, /*tp_dealloc*/ |
| 582 | (printfunc)0, /*tp_print*/ |
| 583 | (getattrfunc)I_getattr, /*tp_getattr*/ |
| 584 | (setattrfunc)0, /*tp_setattr*/ |
| 585 | (cmpfunc)0, /*tp_compare*/ |
| 586 | (reprfunc)0, /*tp_repr*/ |
| 587 | 0, /*tp_as_number*/ |
| 588 | 0, /*tp_as_sequence*/ |
| 589 | 0, /*tp_as_mapping*/ |
| 590 | (hashfunc)0, /*tp_hash*/ |
| 591 | (ternaryfunc)0, /*tp_call*/ |
| 592 | (reprfunc)0, /*tp_str*/ |
| 593 | |
| 594 | /* Space for future expansion */ |
| 595 | 0L,0L,0L,0L, |
| 596 | Itype__doc__ /* Documentation string */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | /* End of code for StringI objects */ |
| 600 | /* -------------------------------------------------------- */ |
| 601 | |
| 602 | |
| 603 | static char IO_StringIO__doc__[] = |
| 604 | "StringIO([s]) -- Return a StringIO-like stream for reading or writing" |
| 605 | ; |
| 606 | |
| 607 | static PyObject * |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 608 | IO_StringIO(PyObject *self, PyObject *args) { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 609 | PyObject *s=0; |
| 610 | |
| 611 | UNLESS(PyArg_ParseTuple(args, "|O", &s)) return NULL; |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 612 | if(s) return newIobject(s); |
| 613 | return newOobject(128); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* List of methods defined in the module */ |
| 617 | |
| 618 | static struct PyMethodDef IO_methods[] = { |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 619 | {"StringIO", (PyCFunction)IO_StringIO, 1, IO_StringIO__doc__}, |
| 620 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | |
| 624 | /* Initialization function for the module (*must* be called initcStringIO) */ |
| 625 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 626 | static struct PycStringIO_CAPI CAPI = { |
| 627 | O_cread, |
| 628 | O_creadline, |
| 629 | O_cwrite, |
| 630 | O_cgetval, |
| 631 | newOobject, |
| 632 | newIobject, |
| 633 | &Itype, |
| 634 | &Otype, |
| 635 | }; |
| 636 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 637 | void |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 638 | initcStringIO() { |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 639 | PyObject *m, *d; |
| 640 | |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 641 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 642 | /* Create the module and add the functions */ |
| 643 | m = Py_InitModule4("cStringIO", IO_methods, |
| 644 | cStringIO_module_documentation, |
| 645 | (PyObject*)NULL,PYTHON_API_VERSION); |
| 646 | |
| 647 | /* Add some symbolic constants to the module */ |
| 648 | d = PyModule_GetDict(m); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 649 | |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 650 | /* Export C API */ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 651 | Itype.ob_type=&PyType_Type; |
| 652 | Otype.ob_type=&PyType_Type; |
| 653 | PyDict_SetItemString(d,"cStringIO_CAPI", PyCObject_FromVoidPtr(&CAPI,NULL)); |
| 654 | |
| 655 | /* Export Types */ |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 656 | PyDict_SetItemString(d,"InputType", (PyObject*)&Itype); |
| 657 | PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype); |
| 658 | |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 659 | /* Check for errors */ |
Guido van Rossum | 154417e | 1997-04-09 17:35:33 +0000 | [diff] [blame^] | 660 | if (PyErr_Occurred()) Py_FatalError("can't initialize module cStringIO"); |
Guido van Rossum | 049cd90 | 1996-12-05 23:30:48 +0000 | [diff] [blame] | 661 | } |
Guido van Rossum | 55702f8 | 1997-01-06 22:57:52 +0000 | [diff] [blame] | 662 | |