[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15356] branches/soc-2008-quorn: Added readline() and reset() for reading lines from a Blender Text Object and resetting the pointer to the beginning of the buffer .

Ian Thompson quornian at googlemail.com
Wed Jun 25 23:01:34 CEST 2008


Revision: 15356
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15356
Author:   quorn
Date:     2008-06-25 23:00:39 +0200 (Wed, 25 Jun 2008)

Log Message:
-----------
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.

Modified Paths:
--------------
    branches/soc-2008-quorn/release/scripts/textplugin_suggest.py
    branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c
    branches/soc-2008-quorn/source/blender/python/api2_2x/Text.h
    branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py

Modified: branches/soc-2008-quorn/release/scripts/textplugin_suggest.py
===================================================================
--- branches/soc-2008-quorn/release/scripts/textplugin_suggest.py	2008-06-25 16:25:01 UTC (rev 15355)
+++ branches/soc-2008-quorn/release/scripts/textplugin_suggest.py	2008-06-25 21:00:39 UTC (rev 15356)
@@ -33,7 +33,7 @@
 	builtins = []
 	bi = dir(__builtin__)
 	for k in bi:
-		v = eval(k)
+		v = getattr(__builtin__, k)
 		if ismodule(v): t='m'
 		elif callable(v): t='f'
 		else: t='v'
@@ -47,10 +47,8 @@
 
 
 def getTokens(txt):
-	lines = txt.asLines()
-	str = '\n'.join(lines)
-	readline = StringIO(str).readline
-	g = generate_tokens(readline)
+	txt.reset()
+	g = generate_tokens(txt.readline)
 	tokens = []
 	for t in g: tokens.append(t)
 	return tokens

Modified: branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c
===================================================================
--- branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c	2008-06-25 16:25:01 UTC (rev 15355)
+++ branches/soc-2008-quorn/source/blender/python/api2_2x/Text.c	2008-06-25 21:00:39 UTC (rev 15356)
@@ -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 @@
 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 @@
 	 "(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 @@
 					      "couldn't create BPy_Text PyObject" );
 
 	pytxt->text = txt;
+	pytxt->iol = 0;
+	pytxt->ioc = 0;
 
 	return ( PyObject * ) pytxt;
 }
@@ -391,23 +400,43 @@
 	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 @@
 	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;

Modified: branches/soc-2008-quorn/source/blender/python/api2_2x/Text.h
===================================================================
--- branches/soc-2008-quorn/source/blender/python/api2_2x/Text.h	2008-06-25 16:25:01 UTC (rev 15355)
+++ branches/soc-2008-quorn/source/blender/python/api2_2x/Text.h	2008-06-25 21:00:39 UTC (rev 15356)
@@ -41,6 +41,8 @@
 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 );

Modified: branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py
===================================================================
--- branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py	2008-06-25 16:25:01 UTC (rev 15355)
+++ branches/soc-2008-quorn/source/blender/python/api2_2x/doc/Text.py	2008-06-25 21:00:39 UTC (rev 15356)
@@ -100,6 +100,18 @@
 		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.





More information about the Bf-blender-cvs mailing list