1996-12-05 19:30:48 -04:00
|
|
|
/*
|
1999-06-15 11:35:48 -03:00
|
|
|
* cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp
|
1998-11-25 12:17:32 -04:00
|
|
|
*
|
|
|
|
* Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* o Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions, and the disclaimer that follows.
|
|
|
|
*
|
|
|
|
* o Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions, and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* o All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgement:
|
|
|
|
*
|
|
|
|
* This product includes software developed by Digital Creations
|
|
|
|
* and its contributors.
|
|
|
|
*
|
|
|
|
* o Neither the name of Digital Creations nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
|
|
|
|
* IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
|
|
|
|
* CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
|
|
|
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
|
|
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
#
|
|
|
|
# If you have questions regarding this software, contact:
|
|
|
|
#
|
|
|
|
# Digital Creations, L.C.
|
|
|
|
# 910 Princess Ann Street
|
|
|
|
# Fredericksburge, Virginia 22401
|
|
|
|
#
|
|
|
|
# info@digicool.com
|
|
|
|
#
|
|
|
|
# (540) 371-6909
|
|
|
|
*/
|
1996-12-05 19:30:48 -04:00
|
|
|
static char cStringIO_module_documentation[] =
|
|
|
|
"A simple fast partial StringIO replacement.\n"
|
|
|
|
"\n"
|
|
|
|
"This module provides a simple useful replacement for\n"
|
|
|
|
"the StringIO module that is written in C. It does not provide the\n"
|
2000-06-19 10:17:41 -03:00
|
|
|
"full generality of StringIO, but it provides enough for most\n"
|
2000-07-16 09:04:32 -03:00
|
|
|
"applications and is especially useful in conjunction with the\n"
|
1996-12-05 19:30:48 -04:00
|
|
|
"pickle module.\n"
|
|
|
|
"\n"
|
|
|
|
"Usage:\n"
|
|
|
|
"\n"
|
|
|
|
" from cStringIO import StringIO\n"
|
|
|
|
"\n"
|
|
|
|
" an_output_stream=StringIO()\n"
|
|
|
|
" an_output_stream.write(some_stuff)\n"
|
|
|
|
" ...\n"
|
|
|
|
" value=an_output_stream.getvalue() # str(an_output_stream) works too!\n"
|
|
|
|
"\n"
|
|
|
|
" an_input_stream=StringIO(a_string)\n"
|
|
|
|
" spam=an_input_stream.readline()\n"
|
|
|
|
" spam=an_input_stream.read(5)\n"
|
1998-11-25 12:17:32 -04:00
|
|
|
" an_input_stream.seek(0) # OK, start over\n"
|
1996-12-05 19:30:48 -04:00
|
|
|
" spam=an_input_stream.read() # and read it all\n"
|
|
|
|
" \n"
|
|
|
|
"If someone else wants to provide a more complete implementation,\n"
|
|
|
|
"go for it. :-) \n"
|
1997-08-13 00:14:41 -03:00
|
|
|
"\n"
|
1999-06-15 11:35:48 -03:00
|
|
|
"cStringIO.c,v 1.29 1999/06/15 14:10:27 jim Exp\n"
|
1996-12-05 19:30:48 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
#include "import.h"
|
1997-04-09 14:35:33 -03:00
|
|
|
#include "cStringIO.h"
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
#define UNLESS(E) if(!(E))
|
|
|
|
|
|
|
|
/* Declarations for objects of type StringO */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
char *buf;
|
1998-12-15 17:43:15 -04:00
|
|
|
int pos, string_size, buf_size, softspace;
|
1996-12-05 19:30:48 -04:00
|
|
|
} Oobject;
|
|
|
|
|
|
|
|
/* Declarations for objects of type StringI */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
char *buf;
|
1998-12-15 17:43:15 -04:00
|
|
|
int pos, string_size;
|
1996-12-05 19:30:48 -04:00
|
|
|
PyObject *pbuf;
|
|
|
|
} Iobject;
|
|
|
|
|
|
|
|
static char O_reset__doc__[] =
|
|
|
|
"reset() -- Reset the file position to the beginning"
|
|
|
|
;
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_reset(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
self->pos = 0;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char O_tell__doc__[] =
|
|
|
|
"tell() -- get the current position.";
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_tell(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
return PyInt_FromLong(self->pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char O_seek__doc__[] =
|
|
|
|
"seek(position) -- set the current position\n"
|
|
|
|
"seek(position, mode) -- mode 0: absolute; 1: relative; 2: relative to EOF";
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_seek(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
int position, mode = 0;
|
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS(PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) {
|
1996-12-05 19:30:48 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (mode == 2) {
|
1996-12-05 19:30:48 -04:00
|
|
|
position += self->string_size;
|
|
|
|
}
|
1997-12-03 21:13:01 -04:00
|
|
|
else if (mode == 1) {
|
1996-12-05 19:30:48 -04:00
|
|
|
position += self->pos;
|
|
|
|
}
|
|
|
|
|
1998-11-25 12:17:32 -04:00
|
|
|
if (position > self->buf_size) {
|
|
|
|
self->buf_size*=2;
|
|
|
|
if(self->buf_size <= position) self->buf_size=position+1;
|
|
|
|
UNLESS(self->buf=(char*)realloc(self->buf,self->buf_size*sizeof(char))) {
|
|
|
|
self->buf_size=self->pos=0;
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(position < 0) position=0;
|
|
|
|
|
|
|
|
self->pos=position;
|
|
|
|
|
|
|
|
while(--position >= self->string_size) self->buf[position]=0;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
1997-01-06 18:57:52 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char O_read__doc__[] =
|
|
|
|
"read([s]) -- Read s characters, or the rest of the string"
|
|
|
|
;
|
|
|
|
|
|
|
|
static int
|
1997-04-09 14:35:33 -03:00
|
|
|
O_cread(PyObject *self, char **output, int n) {
|
1996-12-05 19:30:48 -04:00
|
|
|
int l;
|
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
l = ((Oobject*)self)->string_size - ((Oobject*)self)->pos;
|
1997-12-03 21:13:01 -04:00
|
|
|
if (n < 0 || n > l) {
|
1996-12-05 19:30:48 -04:00
|
|
|
n = l;
|
1998-12-15 17:43:15 -04:00
|
|
|
if (n < 0) n=0;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
*output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
|
|
|
|
((Oobject*)self)->pos += n;
|
1996-12-05 19:30:48 -04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_read(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
int n = -1;
|
|
|
|
char *output;
|
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS(PyArg_ParseTuple(args, "|i:read", &n)) return NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
n=O_cread((PyObject*)self,&output,n);
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
return PyString_FromStringAndSize(output, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char O_readline__doc__[] =
|
|
|
|
"readline() -- Read one line"
|
|
|
|
;
|
|
|
|
|
|
|
|
static int
|
1997-04-09 14:35:33 -03:00
|
|
|
O_creadline(PyObject *self, char **output) {
|
1996-12-05 19:30:48 -04:00
|
|
|
char *n, *s;
|
|
|
|
int l;
|
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
for (n = ((Oobject*)self)->buf + ((Oobject*)self)->pos,
|
|
|
|
s = ((Oobject*)self)->buf + ((Oobject*)self)->string_size;
|
1996-12-05 19:30:48 -04:00
|
|
|
n < s && *n != '\n'; n++);
|
|
|
|
if (n < s) n++;
|
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
*output=((Oobject*)self)->buf + ((Oobject*)self)->pos;
|
|
|
|
l = n - ((Oobject*)self)->buf - ((Oobject*)self)->pos;
|
|
|
|
((Oobject*)self)->pos += l;
|
1996-12-05 19:30:48 -04:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_readline(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
int n;
|
|
|
|
char *output;
|
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
n=O_creadline((PyObject*)self,&output);
|
1996-12-05 19:30:48 -04:00
|
|
|
return PyString_FromStringAndSize(output, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char O_write__doc__[] =
|
|
|
|
"write(s) -- Write a string to the file"
|
|
|
|
"\n\nNote (hack:) writing None resets the buffer"
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
1997-04-09 14:35:33 -03:00
|
|
|
O_cwrite(PyObject *self, char *c, int l) {
|
1997-01-14 13:38:28 -04:00
|
|
|
int newl;
|
1997-01-06 18:57:52 -04:00
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
newl=((Oobject*)self)->pos+l;
|
1997-12-03 21:13:01 -04:00
|
|
|
if(newl >= ((Oobject*)self)->buf_size) {
|
1997-04-09 14:35:33 -03:00
|
|
|
((Oobject*)self)->buf_size*=2;
|
|
|
|
if(((Oobject*)self)->buf_size <= newl) ((Oobject*)self)->buf_size=newl+1;
|
|
|
|
UNLESS(((Oobject*)self)->buf=
|
|
|
|
(char*)realloc(((Oobject*)self)->buf,
|
1997-12-03 21:13:01 -04:00
|
|
|
(((Oobject*)self)->buf_size) *sizeof(char))) {
|
1996-12-05 19:30:48 -04:00
|
|
|
PyErr_SetString(PyExc_MemoryError,"out of memory");
|
1997-04-09 14:35:33 -03:00
|
|
|
((Oobject*)self)->buf_size=((Oobject*)self)->pos=0;
|
1996-12-05 19:30:48 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
memcpy(((Oobject*)((Oobject*)self))->buf+((Oobject*)self)->pos,c,l);
|
1996-12-05 19:30:48 -04:00
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
((Oobject*)self)->pos += l;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (((Oobject*)self)->string_size < ((Oobject*)self)->pos) {
|
1997-04-09 14:35:33 -03:00
|
|
|
((Oobject*)self)->string_size = ((Oobject*)self)->pos;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_write(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
PyObject *s;
|
1997-01-14 13:38:28 -04:00
|
|
|
char *c;
|
|
|
|
int l;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS(PyArg_ParseTuple(args, "O:write", &s)) return NULL;
|
1997-01-06 18:57:52 -04:00
|
|
|
UNLESS(-1 != (l=PyString_Size(s))) return NULL;
|
|
|
|
UNLESS(c=PyString_AsString(s)) return NULL;
|
1997-04-09 14:35:33 -03:00
|
|
|
UNLESS(-1 != O_cwrite((PyObject*)self,c,l)) return NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
1997-01-06 18:57:52 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
1998-12-15 17:43:15 -04:00
|
|
|
static char O_getval__doc__[] =
|
|
|
|
"getvalue([use_pos]) -- Get the string value."
|
|
|
|
"\n"
|
|
|
|
"If use_pos is specified and is a true value, then the string returned\n"
|
|
|
|
"will include only the text up to the current file position.\n"
|
|
|
|
;
|
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_getval(Oobject *self, PyObject *args) {
|
1998-12-15 17:43:15 -04:00
|
|
|
PyObject *use_pos=Py_None;
|
1997-08-13 00:14:41 -03:00
|
|
|
int s;
|
|
|
|
|
|
|
|
use_pos=Py_None;
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS(PyArg_ParseTuple(args,"|O:getval",&use_pos)) return NULL;
|
1998-12-15 17:43:15 -04:00
|
|
|
if(PyObject_IsTrue(use_pos)) {
|
|
|
|
s=self->pos;
|
|
|
|
if (s > self->string_size) s=self->string_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
s=self->string_size;
|
1997-08-13 00:14:41 -03:00
|
|
|
return PyString_FromStringAndSize(self->buf, s);
|
1997-04-09 14:35:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
O_cgetval(PyObject *self) {
|
|
|
|
return PyString_FromStringAndSize(((Oobject*)self)->buf,
|
|
|
|
((Oobject*)self)->pos);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char O_truncate__doc__[] =
|
|
|
|
"truncate(): truncate the file at the current position.";
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_truncate(Oobject *self, PyObject *args) {
|
1998-12-15 17:43:15 -04:00
|
|
|
if (self->string_size > self->pos)
|
|
|
|
self->string_size = self->pos;
|
1996-12-05 19:30:48 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char O_isatty__doc__[] = "isatty(): always returns 0";
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_isatty(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
return PyInt_FromLong(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char O_close__doc__[] = "close(): explicitly release resources held.";
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_close(Oobject *self, PyObject *args) {
|
1997-09-02 21:09:26 -03:00
|
|
|
if (self->buf != NULL)
|
|
|
|
free(self->buf);
|
|
|
|
self->buf = NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
self->pos = self->string_size = self->buf_size = 0;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char O_flush__doc__[] = "flush(): does nothing.";
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_flush(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-08 13:03:27 -04:00
|
|
|
static char O_writelines__doc__[] =
|
|
|
|
"writelines(sequence_of_strings): write each string";
|
1996-12-05 19:30:48 -04:00
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_writelines(Oobject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
PyObject *string_module = 0;
|
|
|
|
static PyObject *string_joinfields = 0;
|
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS(PyArg_ParseTuple(args, "O:writelines", &args)) {
|
1996-12-05 19:30:48 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
if (!string_joinfields) {
|
|
|
|
UNLESS(string_module = PyImport_ImportModule("string")) {
|
1996-12-05 19:30:48 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLESS(string_joinfields=
|
1997-12-03 21:13:01 -04:00
|
|
|
PyObject_GetAttrString(string_module, "joinfields")) {
|
1996-12-05 19:30:48 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(string_module);
|
|
|
|
}
|
|
|
|
|
2000-07-12 10:05:33 -03:00
|
|
|
if (PyObject_Size(args) == -1) {
|
1996-12-05 19:30:48 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-02-08 13:03:27 -04:00
|
|
|
{
|
|
|
|
PyObject *x = PyObject_CallFunction(string_joinfields,
|
|
|
|
"Os", args, "");
|
|
|
|
if (x == NULL)
|
|
|
|
return NULL;
|
|
|
|
args = Py_BuildValue("(O)", x);
|
|
|
|
Py_DECREF(x);
|
|
|
|
if (args == NULL)
|
|
|
|
return NULL;
|
|
|
|
x = O_write(self, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
return x;
|
|
|
|
}
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct PyMethodDef O_methods[] = {
|
1998-12-15 17:43:15 -04:00
|
|
|
{"write", (PyCFunction)O_write, METH_VARARGS, O_write__doc__},
|
|
|
|
{"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
|
|
|
|
{"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
|
|
|
|
{"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
|
|
|
|
{"seek", (PyCFunction)O_seek, METH_VARARGS, O_seek__doc__},
|
|
|
|
{"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
|
|
|
|
{"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__},
|
|
|
|
{"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__},
|
|
|
|
{"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__},
|
|
|
|
{"close", (PyCFunction)O_close, METH_VARARGS, O_close__doc__},
|
|
|
|
{"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__},
|
|
|
|
{"writelines", (PyCFunction)O_writelines, METH_VARARGS, O_writelines__doc__},
|
|
|
|
{NULL, NULL} /* sentinel */
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1997-04-09 14:35:33 -03:00
|
|
|
O_dealloc(Oobject *self) {
|
1997-09-02 21:09:26 -03:00
|
|
|
if (self->buf != NULL)
|
|
|
|
free(self->buf);
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_Del(self);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
O_getattr(Oobject *self, char *name) {
|
1998-11-25 12:17:32 -04:00
|
|
|
if (strcmp(name, "softspace") == 0) {
|
1997-04-11 16:56:06 -03:00
|
|
|
return PyInt_FromLong(self->softspace);
|
|
|
|
}
|
1996-12-05 19:30:48 -04:00
|
|
|
return Py_FindMethod(O_methods, (PyObject *)self, name);
|
|
|
|
}
|
|
|
|
|
1997-04-11 16:56:06 -03:00
|
|
|
static int
|
|
|
|
O_setattr(Oobject *self, char *name, PyObject *value) {
|
|
|
|
long x;
|
|
|
|
if (strcmp(name, "softspace") != 0) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
x = PyInt_AsLong(value);
|
|
|
|
if (x == -1 && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
self->softspace = x;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
static char Otype__doc__[] =
|
|
|
|
"Simple type for output to strings."
|
|
|
|
;
|
|
|
|
|
|
|
|
static PyTypeObject Otype = {
|
1997-04-09 14:35:33 -03:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
|
|
|
0, /*ob_size*/
|
|
|
|
"StringO", /*tp_name*/
|
|
|
|
sizeof(Oobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)O_dealloc, /*tp_dealloc*/
|
|
|
|
(printfunc)0, /*tp_print*/
|
|
|
|
(getattrfunc)O_getattr, /*tp_getattr*/
|
1997-04-11 16:56:06 -03:00
|
|
|
(setattrfunc)O_setattr, /*tp_setattr*/
|
1997-04-09 14:35:33 -03:00
|
|
|
(cmpfunc)0, /*tp_compare*/
|
|
|
|
(reprfunc)0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
(hashfunc)0, /*tp_hash*/
|
|
|
|
(ternaryfunc)0, /*tp_call*/
|
|
|
|
(reprfunc)0, /*tp_str*/
|
|
|
|
|
|
|
|
/* Space for future expansion */
|
|
|
|
0L,0L,0L,0L,
|
|
|
|
Otype__doc__ /* Documentation string */
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
static PyObject *
|
|
|
|
newOobject(int size) {
|
|
|
|
Oobject *self;
|
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
self = PyObject_New(Oobject, &Otype);
|
1997-08-13 00:14:41 -03:00
|
|
|
if (self == NULL)
|
|
|
|
return NULL;
|
|
|
|
self->pos=0;
|
|
|
|
self->string_size = 0;
|
|
|
|
self->softspace = 0;
|
|
|
|
|
1997-12-03 21:13:01 -04:00
|
|
|
UNLESS(self->buf=malloc(size*sizeof(char))) {
|
1997-08-13 00:14:41 -03:00
|
|
|
PyErr_SetString(PyExc_MemoryError,"out of memory");
|
|
|
|
self->buf_size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->buf_size=size;
|
|
|
|
return (PyObject*)self;
|
|
|
|
}
|
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
/* End of code for StringO objects */
|
|
|
|
/* -------------------------------------------------------- */
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
I_close(Iobject *self, PyObject *args) {
|
1997-09-02 21:09:26 -03:00
|
|
|
Py_XDECREF(self->pbuf);
|
|
|
|
self->pbuf = NULL;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
self->pos = self->string_size = 0;
|
1998-12-15 17:43:15 -04:00
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
I_seek(Oobject *self, PyObject *args) {
|
|
|
|
int position, mode = 0;
|
|
|
|
|
2000-02-29 09:59:29 -04:00
|
|
|
UNLESS(PyArg_ParseTuple(args, "i|i:seek", &position, &mode)) {
|
1998-12-15 17:43:15 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == 2) {
|
|
|
|
position += self->string_size;
|
|
|
|
}
|
|
|
|
else if (mode == 1) {
|
|
|
|
position += self->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(position < 0) position=0;
|
|
|
|
|
|
|
|
self->pos=position;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct PyMethodDef I_methods[] = {
|
1998-12-15 17:43:15 -04:00
|
|
|
{"read", (PyCFunction)O_read, METH_VARARGS, O_read__doc__},
|
|
|
|
{"readline", (PyCFunction)O_readline, METH_VARARGS, O_readline__doc__},
|
|
|
|
{"reset", (PyCFunction)O_reset, METH_VARARGS, O_reset__doc__},
|
|
|
|
{"seek", (PyCFunction)I_seek, METH_VARARGS, O_seek__doc__},
|
|
|
|
{"tell", (PyCFunction)O_tell, METH_VARARGS, O_tell__doc__},
|
|
|
|
{"getvalue", (PyCFunction)O_getval, METH_VARARGS, O_getval__doc__},
|
|
|
|
{"truncate", (PyCFunction)O_truncate, METH_VARARGS, O_truncate__doc__},
|
|
|
|
{"isatty", (PyCFunction)O_isatty, METH_VARARGS, O_isatty__doc__},
|
|
|
|
{"close", (PyCFunction)I_close, METH_VARARGS, O_close__doc__},
|
|
|
|
{"flush", (PyCFunction)O_flush, METH_VARARGS, O_flush__doc__},
|
|
|
|
{NULL, NULL}
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
1997-04-09 14:35:33 -03:00
|
|
|
I_dealloc(Iobject *self) {
|
1997-09-02 21:09:26 -03:00
|
|
|
Py_XDECREF(self->pbuf);
|
2000-05-03 20:44:39 -03:00
|
|
|
PyObject_Del(self);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
I_getattr(Iobject *self, char *name) {
|
1996-12-05 19:30:48 -04:00
|
|
|
return Py_FindMethod(I_methods, (PyObject *)self, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char Itype__doc__[] =
|
|
|
|
"Simple type for treating strings as input file streams"
|
|
|
|
;
|
|
|
|
|
|
|
|
static PyTypeObject Itype = {
|
1997-04-09 14:35:33 -03:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
|
|
|
0, /*ob_size*/
|
|
|
|
"StringI", /*tp_name*/
|
|
|
|
sizeof(Iobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)I_dealloc, /*tp_dealloc*/
|
|
|
|
(printfunc)0, /*tp_print*/
|
|
|
|
(getattrfunc)I_getattr, /*tp_getattr*/
|
|
|
|
(setattrfunc)0, /*tp_setattr*/
|
|
|
|
(cmpfunc)0, /*tp_compare*/
|
|
|
|
(reprfunc)0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
(hashfunc)0, /*tp_hash*/
|
|
|
|
(ternaryfunc)0, /*tp_call*/
|
|
|
|
(reprfunc)0, /*tp_str*/
|
|
|
|
|
|
|
|
/* Space for future expansion */
|
|
|
|
0L,0L,0L,0L,
|
|
|
|
Itype__doc__ /* Documentation string */
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
1997-08-13 00:14:41 -03:00
|
|
|
static PyObject *
|
|
|
|
newIobject(PyObject *s) {
|
|
|
|
Iobject *self;
|
|
|
|
char *buf;
|
|
|
|
int size;
|
2000-04-12 19:04:01 -03:00
|
|
|
|
|
|
|
if (!PyString_Check(s)) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "expected string, %.200s found",
|
|
|
|
s->ob_type->tp_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf = PyString_AS_STRING(s);
|
|
|
|
size = PyString_GET_SIZE(s);
|
2000-05-03 20:44:39 -03:00
|
|
|
UNLESS(self = PyObject_New(Iobject, &Itype)) return NULL;
|
1997-08-13 00:14:41 -03:00
|
|
|
Py_INCREF(s);
|
|
|
|
self->buf=buf;
|
|
|
|
self->string_size=size;
|
|
|
|
self->pbuf=s;
|
|
|
|
self->pos=0;
|
|
|
|
|
|
|
|
return (PyObject*)self;
|
|
|
|
}
|
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
/* End of code for StringI objects */
|
|
|
|
/* -------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static char IO_StringIO__doc__[] =
|
|
|
|
"StringIO([s]) -- Return a StringIO-like stream for reading or writing"
|
|
|
|
;
|
|
|
|
|
|
|
|
static PyObject *
|
1997-04-09 14:35:33 -03:00
|
|
|
IO_StringIO(PyObject *self, PyObject *args) {
|
1996-12-05 19:30:48 -04:00
|
|
|
PyObject *s=0;
|
|
|
|
|
2000-04-12 19:04:01 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|O:StringIO", &s))
|
|
|
|
return NULL;
|
1997-04-09 14:35:33 -03:00
|
|
|
if(s) return newIobject(s);
|
|
|
|
return newOobject(128);
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* List of methods defined in the module */
|
|
|
|
|
|
|
|
static struct PyMethodDef IO_methods[] = {
|
2000-08-02 23:06:16 -03:00
|
|
|
{"StringIO", (PyCFunction)IO_StringIO,
|
|
|
|
METH_VARARGS, IO_StringIO__doc__},
|
1997-01-06 18:57:52 -04:00
|
|
|
{NULL, NULL} /* sentinel */
|
1996-12-05 19:30:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialization function for the module (*must* be called initcStringIO) */
|
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
static struct PycStringIO_CAPI CAPI = {
|
|
|
|
O_cread,
|
|
|
|
O_creadline,
|
|
|
|
O_cwrite,
|
|
|
|
O_cgetval,
|
|
|
|
newOobject,
|
|
|
|
newIobject,
|
|
|
|
&Itype,
|
|
|
|
&Otype,
|
|
|
|
};
|
|
|
|
|
1998-12-15 17:43:15 -04:00
|
|
|
#ifndef DL_EXPORT /* declarations for DLL import/export */
|
|
|
|
#define DL_EXPORT(RTYPE) RTYPE
|
|
|
|
#endif
|
1998-12-04 14:50:17 -04:00
|
|
|
DL_EXPORT(void)
|
2000-07-24 11:43:35 -03:00
|
|
|
initcStringIO(void) {
|
1997-09-03 15:19:40 -03:00
|
|
|
PyObject *m, *d, *v;
|
1996-12-05 19:30:48 -04:00
|
|
|
|
1997-04-09 14:35:33 -03:00
|
|
|
|
1996-12-05 19:30:48 -04:00
|
|
|
/* Create the module and add the functions */
|
|
|
|
m = Py_InitModule4("cStringIO", IO_methods,
|
|
|
|
cStringIO_module_documentation,
|
|
|
|
(PyObject*)NULL,PYTHON_API_VERSION);
|
|
|
|
|
|
|
|
/* Add some symbolic constants to the module */
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
|
1997-01-06 18:57:52 -04:00
|
|
|
/* Export C API */
|
1997-04-09 14:35:33 -03:00
|
|
|
Itype.ob_type=&PyType_Type;
|
|
|
|
Otype.ob_type=&PyType_Type;
|
1997-09-03 15:19:40 -03:00
|
|
|
PyDict_SetItemString(d,"cStringIO_CAPI",
|
|
|
|
v = PyCObject_FromVoidPtr(&CAPI,NULL));
|
|
|
|
Py_XDECREF(v);
|
1997-04-09 14:35:33 -03:00
|
|
|
|
|
|
|
/* Export Types */
|
1996-12-05 19:30:48 -04:00
|
|
|
PyDict_SetItemString(d,"InputType", (PyObject*)&Itype);
|
|
|
|
PyDict_SetItemString(d,"OutputType", (PyObject*)&Otype);
|
1997-08-13 00:14:41 -03:00
|
|
|
|
|
|
|
/* Maybe make certain warnings go away */
|
|
|
|
if(0) PycString_IMPORT;
|
1996-12-05 19:30:48 -04:00
|
|
|
}
|