Panda3D
 All Classes Functions Variables Enumerations
lineStreamBuf.cxx
1 // Filename: lineStreamBuf.cxx
2 // Created by: drose (26Feb00)
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 "lineStreamBuf.h"
16 
17 #ifndef HAVE_STREAMSIZE
18 // Some compilers--notably SGI--don't define this for us.
19 typedef int streamsize;
20 #endif
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: LineStreamBuf::Constructor
24 // Access: Public
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 LineStreamBuf::
28 LineStreamBuf() {
29  _has_newline = false;
30 
31  // The LineStreamBuf doesn't actually need a buffer--it's happy
32  // writing characters one at a time, since they're just getting
33  // stuffed into a string. (Although the code is written portably
34  // enough to use a buffer correctly, if we had one.)
35  setg(0, 0, 0);
36  setp(0, 0);
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: LineStreamBuf::Destructor
41 // Access: Public, Virtual
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 LineStreamBuf::
45 ~LineStreamBuf() {
46  sync();
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: LineStreamBuf::get_line
51 // Access: Public
52 // Description: Extracts the next line of text from the
53 // LineStreamBuf, and sets the has_newline() flag
54 // according to whether this line had a trailing newline
55 // or not.
56 ////////////////////////////////////////////////////////////////////
57 string LineStreamBuf::
59  // Extract the data up to, but not including, the next newline
60  // character.
61  size_t nl = _data.find('\n');
62  if (nl == string::npos) {
63  // No trailing newline; return the remainder of the string.
64  _has_newline = false;
65  string result = _data;
66  _data = "";
67  return result;
68  }
69 
70  _has_newline = true;
71  string result = _data.substr(0, nl);
72  _data = _data.substr(nl + 1);
73  return result;
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function: LineStreamBuf::sync
78 // Access: Public, Virtual
79 // Description: Called by the system ostream implementation when the
80 // buffer should be flushed to output (for instance, on
81 // destruction).
82 ////////////////////////////////////////////////////////////////////
83 int LineStreamBuf::
84 sync() {
85  streamsize n = pptr() - pbase();
86  write_chars(pbase(), n);
87  pbump(-n); // Reset pptr().
88  return 0; // EOF to indicate write full.
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: LineStreamBuf::overflow
93 // Access: Public, Virtual
94 // Description: Called by the system ostream implementation when its
95 // internal buffer is filled, plus one character.
96 ////////////////////////////////////////////////////////////////////
97 int LineStreamBuf::
98 overflow(int ch) {
99  streamsize n = pptr() - pbase();
100 
101  if (n != 0 && sync() != 0) {
102  return EOF;
103  }
104 
105  if (ch != EOF) {
106  // Write one more character.
107  char c = ch;
108  write_chars(&c, 1);
109  }
110 
111  return 0;
112 }
string get_line()
Extracts the next line of text from the LineStreamBuf, and sets the has_newline() flag according to w...