Panda3D
wordWrapStreamBuf.cxx
1 // Filename: wordWrapStreamBuf.cxx
2 // Created by: drose (28Jun00)
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 "wordWrapStreamBuf.h"
16 #include "wordWrapStream.h"
17 #include "programBase.h"
18 
19 #include "pnotify.h"
20 
21 #ifndef HAVE_STREAMSIZE
22 // Some compilers--notably SGI--don't define this for us.
23 typedef int streamsize;
24 #endif
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: WordWrapStreamBuf::Constructor
28 // Access: Public
29 // Description:
30 ////////////////////////////////////////////////////////////////////
31 WordWrapStreamBuf::
32 WordWrapStreamBuf(WordWrapStream *owner, ProgramBase *program) :
33  _owner(owner),
34  _program(program)
35 {
36  _literal_mode = false;
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: WordWrapStreamBuf::Destructor
41 // Access: Public, Virtual
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 WordWrapStreamBuf::
45 ~WordWrapStreamBuf() {
46  sync();
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: WordWrapStreamBuf::sync
51 // Access: Public, Virtual
52 // Description: Called by the system ostream implementation when the
53 // buffer should be flushed to output (for instance, on
54 // destruction).
55 ////////////////////////////////////////////////////////////////////
56 int WordWrapStreamBuf::
57 sync() {
58  streamsize n = pptr() - pbase();
59  write_chars(pbase(), n);
60 
61  // Send all the data out now.
62  flush_data();
63 
64  return 0; // EOF to indicate write full.
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: WordWrapStreamBuf::overflow
69 // Access: Public, Virtual
70 // Description: Called by the system ostream implementation when its
71 // internal buffer is filled, plus one character.
72 ////////////////////////////////////////////////////////////////////
73 int WordWrapStreamBuf::
74 overflow(int ch) {
75  streamsize n = pptr() - pbase();
76 
77  if (n != 0 && sync() != 0) {
78  return EOF;
79  }
80 
81  if (ch != EOF) {
82  // Write one more character.
83  char c = ch;
84  write_chars(&c, 1);
85  }
86 
87  pbump(-n); // Reset pptr().
88  return 0;
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: WordWrapStreamBuf::write_chars
93 // Access: Public
94 // Description: An internal function called by sync() and overflow()
95 // to store one or more characters written to the stream
96 // into the memory buffer.
97 ////////////////////////////////////////////////////////////////////
98 void WordWrapStreamBuf::
99 write_chars(const char *start, int length) {
100  if (length > 0) {
101  set_literal_mode((_owner->flags() & Notify::get_literal_flag()) != 0);
102  string new_data(start, length);
103  size_t newline = new_data.find_first_of("\n\r");
104  size_t p = 0;
105  while (newline != string::npos) {
106  // The new data contains a newline; flush our data to that point.
107  _data += new_data.substr(p, newline - p + 1);
108  flush_data();
109  p = newline + 1;
110  newline = new_data.find_first_of("\n\r", p);
111  }
112 
113  // Save the rest for the next write.
114  _data += new_data.substr(p);
115  }
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: WordWrapStreamBuf::flush_data
120 // Access: Private
121 // Description: Writes the contents of _data to the actual output
122 // stream, either word-wrapped or not as appropriate,
123 // and empties the contents of _data.
124 ////////////////////////////////////////////////////////////////////
125 void WordWrapStreamBuf::
126 flush_data() {
127  if (!_data.empty()) {
128  if (_literal_mode) {
129  cerr << _data;
130  } else {
131  _program->show_text(_data);
132  }
133  _data = "";
134  }
135 }
This is intended to be the base class for most general-purpose utility programs in the PANDATOOL tree...
Definition: programBase.h:37
static ios_fmtflags get_literal_flag()
Returns a flag that may be set on the Notify stream via setf() that, when set, enables "literal" mode...
Definition: notify.cxx:113
A special ostream that formats all of its output through ProgramBase::show_text().