From 1c11c51455432c328308d0361cc2a62e74c32feb Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 19 Mar 2004 13:44:27 +0000 Subject: [PATCH] robusity Ramfile interface --- panda/src/express/ramfile.I | 34 ++++++++++++++++++++++++++++++++++ panda/src/express/ramfile.cxx | 20 +++++++++++++++++++- panda/src/express/ramfile.h | 5 +++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/panda/src/express/ramfile.I b/panda/src/express/ramfile.I index 3aa6bcfff5..bf5e3e5fe8 100644 --- a/panda/src/express/ramfile.I +++ b/panda/src/express/ramfile.I @@ -25,3 +25,37 @@ INLINE Ramfile:: Ramfile() { _pos = 0; } + +//////////////////////////////////////////////////////////////////// +// Function: Ramfile::seek +// Access: Published +// Description: Moves the data pointer to the indicated byte +// position. It is not an error to move the pointer +// past the end of data. +//////////////////////////////////////////////////////////////////// +INLINE void Ramfile:: +seek(size_t pos) { + _pos = pos; +} + +//////////////////////////////////////////////////////////////////// +// Function: Ramfile::tell +// Access: Published +// Description: Returns the current data pointer position as a byte +// offset from the beginning of the stream. +//////////////////////////////////////////////////////////////////// +INLINE size_t Ramfile:: +tell() const { + return _pos; +} + +//////////////////////////////////////////////////////////////////// +// Function: Ramfile::get_data +// Access: Published +// Description: Returns the entire buffer contents as a string, +// regardless of the current data pointer. +//////////////////////////////////////////////////////////////////// +INLINE const string &Ramfile:: +get_data() const { + return _data; +} diff --git a/panda/src/express/ramfile.cxx b/panda/src/express/ramfile.cxx index f1370079c5..d68e4c27e3 100644 --- a/panda/src/express/ramfile.cxx +++ b/panda/src/express/ramfile.cxx @@ -18,6 +18,24 @@ #include "ramfile.h" +//////////////////////////////////////////////////////////////////// +// Function: Ramfile::read +// Access: Published +// Description: Extracts and returns the indicated number of +// characters from the current data pointer, and +// advances the data pointer. If the data pointer +// exceeds the end of the buffer, returns empty string. +// +// The interface here is intentionally designed to be +// similar to that for Python's file.read() function. +//////////////////////////////////////////////////////////////////// +string Ramfile:: +read(size_t length) { + size_t orig_pos = _pos; + _pos = min(_pos + length, _data.length()); + return _data.substr(orig_pos, length); +} + //////////////////////////////////////////////////////////////////// // Function: Ramfile::readline // Access: Published @@ -27,7 +45,7 @@ // of file is reached. // // The interface here is intentionally designed to be -// similar to that for Python's File.readline() +// similar to that for Python's file.readline() // function. //////////////////////////////////////////////////////////////////// string Ramfile:: diff --git a/panda/src/express/ramfile.h b/panda/src/express/ramfile.h index 83b541ef67..8986cd6dfd 100644 --- a/panda/src/express/ramfile.h +++ b/panda/src/express/ramfile.h @@ -32,8 +32,13 @@ class EXPCL_PANDAEXPRESS Ramfile { PUBLISHED: INLINE Ramfile(); + INLINE void seek(size_t pos); + INLINE size_t tell() const; + string read(size_t length); string readline(); + INLINE const string &get_data() const; + public: size_t _pos; string _data;