diff options
author | Ian Thompson <quornian@googlemail.com> | 2008-06-26 01:00:39 +0400 |
---|---|---|
committer | Ian Thompson <quornian@googlemail.com> | 2008-06-26 01:00:39 +0400 |
commit | fc392040dd035dc03899896cc56d337a8e97e78f (patch) | |
tree | 1fb4a21573b470c608628cd7760a402d088e5ebe /source | |
parent | e68834c75bc3e69641a6332dab071ebaa2d9d1d3 (diff) |
Added readline() and reset() for reading lines from a Blender Text Object and resetting the pointer to the beginning of the buffer. readline() will return the '\n' character and return '' when the end of the buffer is reached in accordance with other readline methods.
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/python/api2_2x/Text.c | 72 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Text.h | 2 | ||||
-rw-r--r-- | source/blender/python/api2_2x/doc/Text.py | 12 |
3 files changed, 74 insertions, 12 deletions
diff --git a/source/blender/python/api2_2x/Text.c b/source/blender/python/api2_2x/Text.c index 0c612a95f3a..836d824e3b2 100644 --- a/source/blender/python/api2_2x/Text.c +++ b/source/blender/python/api2_2x/Text.c @@ -40,6 +40,7 @@ #include "BLI_blenlib.h" #include "DNA_screen_types.h" #include "DNA_space_types.h" +#include "MEM_guardedalloc.h" #include "gen_utils.h" #include "gen_library.h" #include "../BPY_extern.h" @@ -93,6 +94,8 @@ struct PyMethodDef M_Text_methods[] = { static PyObject *Text_getFilename( BPy_Text * self ); static PyObject *Text_getNLines( BPy_Text * self ); static PyObject *Text_clear( BPy_Text * self ); +static PyObject *Text_reset( BPy_Text * self ); +static PyObject *Text_readline( BPy_Text * self ); static PyObject *Text_write( BPy_Text * self, PyObject * value ); static PyObject *Text_insert( BPy_Text * self, PyObject * value ); static PyObject *Text_set( BPy_Text * self, PyObject * args ); @@ -116,6 +119,10 @@ static PyMethodDef BPy_Text_methods[] = { "(str) - Change Text Object name"}, {"clear", ( PyCFunction ) Text_clear, METH_NOARGS, "() - Clear Text buffer"}, + {"reset", ( PyCFunction ) Text_reset, METH_NOARGS, + "() - Moves the IO pointer back to the start of the Text buffer for reading"}, + {"readline", ( PyCFunction ) Text_readline, METH_NOARGS, + "() - Reads a line of text from the buffer and returns it incrementing the internal IO pointer."}, {"write", ( PyCFunction ) Text_write, METH_O, "(line) - Append string 'str' to Text buffer"}, {"insert", ( PyCFunction ) Text_insert, METH_O, @@ -342,6 +349,8 @@ PyObject *Text_CreatePyObject( Text * txt ) "couldn't create BPy_Text PyObject" ); pytxt->text = txt; + pytxt->iol = 0; + pytxt->ioc = 0; return ( PyObject * ) pytxt; } @@ -391,23 +400,43 @@ static PyObject *Text_clear( BPy_Text * self) Py_RETURN_NONE; } -static PyObject *Text_set( BPy_Text * self, PyObject * args ) +static PyObject *Text_reset( BPy_Text * self ) { - int ival; - char *attr; + self->iol = 0; + self->ioc = 0; - if( !PyArg_ParseTuple( args, "si", &attr, &ival ) ) - return EXPP_ReturnPyObjError( PyExc_TypeError, - "expected a string and an int as arguments" ); + Py_RETURN_NONE; +} - if( strcmp( "follow_cursor", attr ) == 0 ) { - if( ival ) - self->text->flags |= EXPP_TEXT_MODE_FOLLOW; - else - self->text->flags &= EXPP_TEXT_MODE_FOLLOW; +static PyObject *Text_readline( BPy_Text * self ) +{ + PyObject *tmpstr; + TextLine *line; + int i; + + if( !self->text ) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "This object isn't linked to a Blender Text Object" ); + + for (i=0, line=self->text->lines.first; i<self->iol && line; i++, line=line->next); + + if (!line) { + PyErr_SetString( PyExc_StopIteration, "End of buffer reached" ); + return PyString_FromString( "" ); } - Py_RETURN_NONE; + if (self->ioc > line->len) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "Line length exceeded, text may have changed while reading" ); + + tmpstr = PyString_FromString( line->line + self->ioc ); + if (line->next) + PyString_ConcatAndDel( &tmpstr, PyString_FromString("\n") ); + + self->iol++; + self->ioc = 0; + + return tmpstr; } static PyObject *Text_write( BPy_Text * self, PyObject * value ) @@ -451,6 +480,25 @@ static PyObject *Text_insert( BPy_Text * self, PyObject * value ) Py_RETURN_NONE; } +static PyObject *Text_set( BPy_Text * self, PyObject * args ) +{ + int ival; + char *attr; + + if( !PyArg_ParseTuple( args, "si", &attr, &ival ) ) + return EXPP_ReturnPyObjError( PyExc_TypeError, + "expected a string and an int as arguments" ); + + if( strcmp( "follow_cursor", attr ) == 0 ) { + if( ival ) + self->text->flags |= EXPP_TEXT_MODE_FOLLOW; + else + self->text->flags &= EXPP_TEXT_MODE_FOLLOW; + } + + Py_RETURN_NONE; +} + static PyObject *Text_asLines( BPy_Text * self ) { TextLine *line; diff --git a/source/blender/python/api2_2x/Text.h b/source/blender/python/api2_2x/Text.h index 0c5e2607f03..eb64b6d43ea 100644 --- a/source/blender/python/api2_2x/Text.h +++ b/source/blender/python/api2_2x/Text.h @@ -41,6 +41,8 @@ extern PyTypeObject Text_Type; typedef struct { PyObject_HEAD Text * text; /* libdata must be second */ + int iol; /* index of line being read */ + int ioc; /* character offset in line being read */ } BPy_Text; PyObject *Text_Init( void ); diff --git a/source/blender/python/api2_2x/doc/Text.py b/source/blender/python/api2_2x/doc/Text.py index 920908eef81..1f38aadfa66 100644 --- a/source/blender/python/api2_2x/doc/Text.py +++ b/source/blender/python/api2_2x/doc/Text.py @@ -100,6 +100,18 @@ class Text: Clear this Text object: its buffer becomes empty. """ + def reset(): + """ + Reset the read IO pointer to the start of the buffer. + """ + + def readline(): + """ + Reads a line of text from the buffer from the current IO pointer + position to the end of the line. + @rtype: string + """ + def set(attribute, value): """ Set this Text's attributes. |