Commit e12c84d7 authored by chris's avatar chris

*** empty log message ***

parent 32e6d1aa
/* /*
$Id: cStringIO.c,v 1.2 1996/07/18 13:08:34 jfulton Exp $ $Id: cStringIO.c,v 1.3 1996/10/07 20:51:38 chris Exp $
A simple fast partial StringIO replacement. A simple fast partial StringIO replacement.
...@@ -58,6 +58,9 @@ ...@@ -58,6 +58,9 @@
$Log: cStringIO.c,v $ $Log: cStringIO.c,v $
Revision 1.3 1996/10/07 20:51:38 chris
*** empty log message ***
Revision 1.2 1996/07/18 13:08:34 jfulton Revision 1.2 1996/07/18 13:08:34 jfulton
*** empty log message *** *** empty log message ***
...@@ -95,12 +98,14 @@ static char cStringIO_module_documentation[] = ...@@ -95,12 +98,14 @@ static char cStringIO_module_documentation[] =
; ;
#include "Python.h" #include "Python.h"
#include "import.h"
static PyObject *ErrorObject; static PyObject *ErrorObject;
#define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;} #define ASSIGN(V,E) {PyObject *__e; __e=(E); Py_XDECREF(V); (V)=__e;}
#define UNLESS(E) if(!(E)) #define UNLESS(E) if(!(E))
#define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V) #define UNLESS_ASSIGN(V,E) ASSIGN(V,E) UNLESS(V)
#define Py_ASSIGN(P,E) if(!PyObject_AssignExpression(&(P),(E))) return NULL
/* ----------------------------------------------------- */ /* ----------------------------------------------------- */
...@@ -109,7 +114,7 @@ static PyObject *ErrorObject; ...@@ -109,7 +114,7 @@ static PyObject *ErrorObject;
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
char *buf; char *buf;
int pos, size; int pos, string_size, buf_size, closed;
} Oobject; } Oobject;
staticforward PyTypeObject Otype; staticforward PyTypeObject Otype;
...@@ -121,7 +126,7 @@ staticforward PyTypeObject Otype; ...@@ -121,7 +126,7 @@ staticforward PyTypeObject Otype;
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
char *buf; char *buf;
int pos,size; int pos, string_size, closed;
PyObject *pbuf; PyObject *pbuf;
} Iobject; } Iobject;
...@@ -145,11 +150,57 @@ O_reset(self, args) ...@@ -145,11 +150,57 @@ O_reset(self, args)
return Py_None; return Py_None;
} }
static char O_tell__doc__[] =
"tell() -- get the current position.";
static PyObject *
O_tell(self, args)
Oobject *self;
PyObject *args;
{
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 *
O_seek(self, args)
Oobject *self;
PyObject *args;
{
int position, mode = 0;
UNLESS(PyArg_ParseTuple(args, "i|i", &position, &mode))
{
return NULL;
}
if (mode == 2)
{
position += self->string_size;
}
else if (mode == 1)
{
position += self->pos;
}
self->pos = (position > self->string_size ? self->string_size :
(position < 0 ? 0 : position));
return PyInt_FromLong(self->pos);
}
static char O_write__doc__[] = static char O_write__doc__[] =
"write(s) -- Write a string to the file" "write(s) -- Write a string to the file"
"\n\nNote (hack:) writing None resets the buffer" "\n\nNote (hack:) writing None resets the buffer"
; ;
static PyObject * static PyObject *
O_write(self, args) O_write(self, args)
Oobject *self; Oobject *self;
...@@ -157,7 +208,7 @@ O_write(self, args) ...@@ -157,7 +208,7 @@ O_write(self, args)
{ {
PyObject *s; PyObject *s;
char *c, *b; char *c, *b;
int l, newl; int l, newl, space_needed;
UNLESS(PyArg_Parse(args, "O", &s)) return NULL; UNLESS(PyArg_Parse(args, "O", &s)) return NULL;
if(s!=Py_None) if(s!=Py_None)
...@@ -165,23 +216,31 @@ O_write(self, args) ...@@ -165,23 +216,31 @@ O_write(self, args)
UNLESS(-1 != (l=PyString_Size(s))) return NULL; UNLESS(-1 != (l=PyString_Size(s))) return NULL;
UNLESS(c=PyString_AsString(s)) return NULL; UNLESS(c=PyString_AsString(s)) return NULL;
newl=self->pos+l; newl=self->pos+l;
if(newl > self->size) if(newl > self->buf_size)
{ {
self->size*=2; self->buf_size*=2;
if(self->size < newl) self->size=newl; if(self->buf_size < newl) self->buf_size=newl;
UNLESS(self->buf=(char*)realloc(self->buf, self->size*sizeof(char))) UNLESS(self->buf=(char*)realloc(self->buf, self->buf_size*sizeof(char)))
{ {
PyErr_SetString(PyExc_MemoryError,"out of memory"); PyErr_SetString(PyExc_MemoryError,"out of memory");
self->size=self->pos=0; self->buf_size=self->pos=0;
return NULL; return NULL;
} }
} }
memcpy(self->buf+self->pos,c,l); memcpy(self->buf+self->pos,c,l);
self->pos+=l;
self->pos += l;
if (self->string_size < self->pos)
{
self->string_size = self->pos;
}
} }
else else
{ {
self->pos=0; self->pos=0;
self->string_size = 0;
} }
Py_INCREF(self); Py_INCREF(self);
...@@ -192,7 +251,7 @@ static PyObject * ...@@ -192,7 +251,7 @@ static PyObject *
O_repr(self) O_repr(self)
Oobject *self; Oobject *self;
{ {
return PyString_FromStringAndSize(self->buf,self->pos); return PyString_FromStringAndSize(self->buf,self->string_size);
} }
static PyObject * static PyObject *
...@@ -203,10 +262,107 @@ O_getval(self,args) ...@@ -203,10 +262,107 @@ O_getval(self,args)
return PyString_FromStringAndSize(self->buf,self->pos); return PyString_FromStringAndSize(self->buf,self->pos);
} }
static char O_truncate__doc__[] =
"truncate(): truncate the file at the current position.";
static PyObject *
O_truncate(self, args)
Oobject *self;
PyObject *args;
{
self->string_size = self->pos;
Py_INCREF(Py_None);
return Py_None;
}
static char O_isatty__doc__[] = "isatty(): always returns 0";
static PyObject *
O_isatty(self, args)
Oobject *self;
PyObject *args;
{
return PyInt_FromLong(0);
}
static char O_close__doc__[] = "close(): explicitly release resources held.";
static PyObject *
O_close(self, args)
Oobject *self;
PyObject *args;
{
free(self->buf);
self->pos = self->string_size = self->buf_size = 0;
self->closed = 1;
Py_INCREF(Py_None);
return Py_None;
}
static char O_flush__doc__[] = "flush(): does nothing.";
static PyObject *
O_flush(self, args)
Oobject *self;
PyObject *args;
{
Py_INCREF(Py_None);
return Py_None;
}
static char O_writelines__doc__[] = "blah";
static PyObject *
O_writelines(self, args)
Oobject *self;
PyObject *args;
{
PyObject *string_module = 0;
static PyObject *string_joinfields = 0;
UNLESS(PyArg_Parse(args, "O", args))
{
return NULL;
}
if (!string_joinfields)
{
UNLESS(string_module = PyImport_ImportModule("string"))
{
return NULL;
}
UNLESS(string_joinfields=
PyObject_GetAttrString(string_module, "joinfields"))
{
return NULL;
}
Py_DECREF(string_module);
}
if (PyObject_Length(args) == -1)
{
return NULL;
}
return O_write(self,
PyObject_CallFunction(string_joinfields, "Os", args, ""));
}
static struct PyMethodDef O_methods[] = { static struct PyMethodDef O_methods[] = {
{"write", O_write, 0, O_write__doc__}, {"write", O_write, 0, O_write__doc__},
{"reset", O_reset, 0, O_reset__doc__}, {"reset", O_reset, 0, O_reset__doc__},
{"seek", O_seek, 1, O_seek__doc__},
{"tell", O_tell, 0, O_tell__doc__},
{"getvalue", O_getval, 0, "getvalue() -- Get the string value"}, {"getvalue", O_getval, 0, "getvalue() -- Get the string value"},
{"truncate", O_truncate, 0, O_truncate__doc__},
{"isatty", O_isatty, 0, O_isatty__doc__},
{"close", O_close, 0, O_close__doc__},
{"flush", O_flush, 0, O_flush__doc__},
{"writelines", O_writelines, 0, O_writelines__doc__},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
...@@ -222,13 +378,17 @@ newOobject(int size) ...@@ -222,13 +378,17 @@ newOobject(int size)
if (self == NULL) if (self == NULL)
return NULL; return NULL;
self->pos=0; self->pos=0;
self->closed = 0;
self->string_size = 0;
UNLESS(self->buf=malloc(size*sizeof(char))) UNLESS(self->buf=malloc(size*sizeof(char)))
{ {
PyErr_SetString(PyExc_MemoryError,"out of memory"); PyErr_SetString(PyExc_MemoryError,"out of memory");
self->size=0; self->buf_size = 0;
return NULL; return NULL;
} }
self->size=size;
self->buf_size=size;
return self; return self;
} }
...@@ -290,11 +450,17 @@ I_read(self, args) ...@@ -290,11 +450,17 @@ I_read(self, args)
Iobject *self; Iobject *self;
PyObject *args; PyObject *args;
{ {
int n=-1; int l, n=-1;
PyObject *s; PyObject *s;
UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL; UNLESS(PyArg_ParseTuple(args, "|i", &n)) return NULL;
if(n < 0) n=self->size-self->pos;
l = self->string_size - self->pos;
if (n < 0 || n > l)
{
n = l;
}
s=PyString_FromStringAndSize(self->buf+self->pos, n); s=PyString_FromStringAndSize(self->buf+self->pos, n);
self->pos+=n; self->pos+=n;
return s; return s;
...@@ -314,18 +480,37 @@ I_readline(self, args) ...@@ -314,18 +480,37 @@ I_readline(self, args)
char *n, *s; char *n, *s;
int l; int l;
for(n=self->buf+self->pos, s=self->buf+self->size; n < s && *n != '\n'; n++); for(n=self->buf+self->pos, s=self->buf+self->string_size; n < s && *n != '\n'; n++);
if(n < s) n++; if(n < s) n++;
r=PyString_FromStringAndSize(self->buf+self->pos, n - self->buf - self->pos); r=PyString_FromStringAndSize(self->buf+self->pos, n - self->buf - self->pos);
self->pos=n-self->buf; self->pos=n-self->buf;
return r; return r;
} }
static PyObject *
I_close(self, args)
Iobject *self;
PyObject *args;
{
Py_DECREF(self->pbuf);
self->pos = self->string_size = 0;
self->closed = 1;
Py_INCREF(Py_None);
return Py_None;
}
static struct PyMethodDef I_methods[] = { static struct PyMethodDef I_methods[] = {
{"read", I_read, 1, I_read__doc__}, {"read", I_read, 1, I_read__doc__},
{"readline", I_readline, 0, I_readline__doc__}, {"readline", I_readline, 0, I_readline__doc__},
{"reset", O_reset, 0, O_reset__doc__}, {"reset", O_reset, 0, O_reset__doc__},
{"seek", O_seek, 1, O_seek__doc__},
{"tell", O_tell, 0, O_tell__doc__},
{"truncate", O_truncate, 0, O_truncate__doc__},
{"isatty", O_isatty, 0, O_isatty__doc__},
{"close", I_close, 0, O_close__doc__},
{"flush", O_flush, 0, O_flush__doc__},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
...@@ -344,9 +529,10 @@ newIobject(PyObject *s) ...@@ -344,9 +529,10 @@ newIobject(PyObject *s)
UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL; UNLESS(self = PyObject_NEW(Iobject, &Itype)) return NULL;
Py_INCREF(s); Py_INCREF(s);
self->buf=buf; self->buf=buf;
self->size=size; self->string_size=size;
self->pbuf=s; self->pbuf=s;
self->pos=0; self->pos=0;
self->closed = 0;
return self; return self;
} }
...@@ -444,6 +630,8 @@ initcStringIO() ...@@ -444,6 +630,8 @@ initcStringIO()
/* XXXX Add constants here */ /* XXXX Add constants here */
/* Check for errors */ /* Check for errors */
if (PyErr_Occurred()) if (PyErr_Occurred())
Py_FatalError("can't initialize module cStringIO"); Py_FatalError("can't initialize module cStringIO");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment