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