Panda3D
 All Classes Functions Variables Enumerations
ramfile.cxx
1 // Filename: ramfile.cxx
2 // Created by: mike (09Jan97)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "ramfile.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: Ramfile::read
19 // Access: Published
20 // Description: Extracts and returns the indicated number of
21 // characters from the current data pointer, and
22 // advances the data pointer. If the data pointer
23 // exceeds the end of the buffer, returns empty string.
24 //
25 // The interface here is intentionally designed to be
26 // similar to that for Python's file.read() function.
27 ////////////////////////////////////////////////////////////////////
28 string Ramfile::
29 read(size_t length) {
30  size_t orig_pos = _pos;
31  _pos = min(_pos + length, _data.length());
32  return _data.substr(orig_pos, length);
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: Ramfile::readline
37 // Access: Published
38 // Description: Assumes the stream represents a text file, and
39 // extracts one line up to and including the trailing
40 // newline character. Returns empty string when the end
41 // of file is reached.
42 //
43 // The interface here is intentionally designed to be
44 // similar to that for Python's file.readline()
45 // function.
46 ////////////////////////////////////////////////////////////////////
47 string Ramfile::
49  size_t start = _pos;
50  while (_pos < _data.length() && _data[_pos] != '\n') {
51  ++_pos;
52  }
53 
54  if (_pos < _data.length() && _data[_pos] == '\n') {
55  // Include the newline character also.
56  ++_pos;
57  }
58 
59  return _data.substr(start, _pos - start);
60 }
61 
string read(size_t length)
Extracts and returns the indicated number of characters from the current data pointer, and advances the data pointer.
Definition: ramfile.cxx:29
string readline()
Assumes the stream represents a text file, and extracts one line up to and including the trailing new...
Definition: ramfile.cxx:48