00001 // Filename: ramfile.cxx 00002 // Created by: mike (09Jan97) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "ramfile.h" 00016 00017 //////////////////////////////////////////////////////////////////// 00018 // Function: Ramfile::read 00019 // Access: Published 00020 // Description: Extracts and returns the indicated number of 00021 // characters from the current data pointer, and 00022 // advances the data pointer. If the data pointer 00023 // exceeds the end of the buffer, returns empty string. 00024 // 00025 // The interface here is intentionally designed to be 00026 // similar to that for Python's file.read() function. 00027 //////////////////////////////////////////////////////////////////// 00028 string Ramfile:: 00029 read(size_t length) { 00030 size_t orig_pos = _pos; 00031 _pos = min(_pos + length, _data.length()); 00032 return _data.substr(orig_pos, length); 00033 } 00034 00035 //////////////////////////////////////////////////////////////////// 00036 // Function: Ramfile::readline 00037 // Access: Published 00038 // Description: Assumes the stream represents a text file, and 00039 // extracts one line up to and including the trailing 00040 // newline character. Returns empty string when the end 00041 // of file is reached. 00042 // 00043 // The interface here is intentionally designed to be 00044 // similar to that for Python's file.readline() 00045 // function. 00046 //////////////////////////////////////////////////////////////////// 00047 string Ramfile:: 00048 readline() { 00049 size_t start = _pos; 00050 while (_pos < _data.length() && _data[_pos] != '\n') { 00051 ++_pos; 00052 } 00053 00054 if (_pos < _data.length() && _data[_pos] == '\n') { 00055 // Include the newline character also. 00056 ++_pos; 00057 } 00058 00059 return _data.substr(start, _pos - start); 00060 } 00061