Panda3D

lineStreamBuf.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 lineStreamBuf.cxx
10 * @author drose
11 * @date 2000-02-26
12 */
13
14 #include "lineStreamBuf.h"
15
16 using std::string;
17
18 /**
19 *
20 */
21 LineStreamBuf::
22 LineStreamBuf() {
23 _has_newline = false;
24
25 // The LineStreamBuf doesn't actually need a buffer--it's happy writing
26 // characters one at a time, since they're just getting stuffed into a
27 // string. (Although the code is written portably enough to use a buffer
28 // correctly, if we had one.)
29 setg(nullptr, nullptr, nullptr);
30 setp(nullptr, nullptr);
31 }
32
33 /**
34 *
35 */
36 LineStreamBuf::
37 ~LineStreamBuf() {
38 sync();
39 }
40
41 /**
42 * Extracts the next line of text from the LineStreamBuf, and sets the
43 * has_newline() flag according to whether this line had a trailing newline or
44 * not.
45 */
46 string LineStreamBuf::
47 get_line() {
48 // Extract the data up to, but not including, the next newline character.
49 size_t nl = _data.find('\n');
50 if (nl == string::npos) {
51 // No trailing newline; return the remainder of the string.
52 _has_newline = false;
53 string result = _data;
54 _data = "";
55 return result;
56 }
57
58 _has_newline = true;
59 string result = _data.substr(0, nl);
60 _data = _data.substr(nl + 1);
61 return result;
62 }
63
64 /**
65 * Called by the system ostream implementation when the buffer should be
66 * flushed to output (for instance, on destruction).
67 */
68 int LineStreamBuf::
69 sync() {
70 std::streamsize n = pptr() - pbase();
71 write_chars(pbase(), n);
72 pbump(-(int)n); // Reset pptr().
73 return 0; // EOF to indicate write full.
74 }
75
76 /**
77 * Called by the system ostream implementation when its internal buffer is
78 * filled, plus one character.
79 */
80 int LineStreamBuf::
81 overflow(int ch) {
82 std::streamsize n = pptr() - pbase();
83
84 if (n != 0 && sync() != 0) {
85 return EOF;
86 }
87
88 if (ch != EOF) {
89 // Write one more character.
90 char c = (char)ch;
91 write_chars(&c, 1);
92 }
93
94 return 0;
95 }
LineStreamBuf::get_line
std::string get_line()
Extracts the next line of text from the LineStreamBuf, and sets the has_newline() flag according to w...
Definition: lineStreamBuf.cxx:47
lineStreamBuf.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.