From eff0d77e07cdde7a18540fc1599c3e0cb63b99fb Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 17 Oct 2002 04:37:23 +0000 Subject: [PATCH] Ramfile::readline() --- panda/src/express/buffer.I | 1 + panda/src/express/buffer.cxx | 29 +++++++++++++++++++++++++++++ panda/src/express/buffer.h | 5 ++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/panda/src/express/buffer.I b/panda/src/express/buffer.I index 12e02a1b69..444832b2ba 100644 --- a/panda/src/express/buffer.I +++ b/panda/src/express/buffer.I @@ -33,4 +33,5 @@ get_length(void) const { //////////////////////////////////////////////////////////////////// INLINE Ramfile:: Ramfile(void) { + _pos = 0; } diff --git a/panda/src/express/buffer.cxx b/panda/src/express/buffer.cxx index 33c4e96788..7973362702 100644 --- a/panda/src/express/buffer.cxx +++ b/panda/src/express/buffer.cxx @@ -41,3 +41,32 @@ Buffer:: ~Buffer() { delete _buffer; } + + +//////////////////////////////////////////////////////////////////// +// Function: Ramfile::readline +// Access: Published +// Description: Assumes the stream represents a text file, and +// extracts one line up to and including the trailing +// newline character. Returns empty string when the end +// of file is reached. +// +// The interface here is intentionally designed to be +// similar to that for Python's File.readline() +// function. +//////////////////////////////////////////////////////////////////// +string Ramfile:: +readline() { + size_t start = _pos; + while (_pos < _data.length() && _data[_pos] != '\n') { + ++_pos; + } + + if (_pos < _data.length() && _data[_pos] == '\n') { + // Include the newline character also. + ++_pos; + } + + return _data.substr(start, _pos - start); +} + diff --git a/panda/src/express/buffer.h b/panda/src/express/buffer.h index 546f169f32..e743d8ca4e 100644 --- a/panda/src/express/buffer.h +++ b/panda/src/express/buffer.h @@ -52,9 +52,12 @@ private: //////////////////////////////////////////////////////////////////// class EXPCL_PANDAEXPRESS Ramfile { PUBLISHED: - INLINE Ramfile(void); + INLINE Ramfile(); + + string readline(); public: + size_t _pos; string _data; };