Panda3D
wordWrapStreamBuf.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 wordWrapStreamBuf.cxx
10  * @author drose
11  * @date 2000-06-28
12  */
13 
14 #include "wordWrapStreamBuf.h"
15 #include "wordWrapStream.h"
16 #include "programBase.h"
17 
18 #include "pnotify.h"
19 
20 /**
21  *
22  */
23 WordWrapStreamBuf::
24 WordWrapStreamBuf(WordWrapStream *owner, ProgramBase *program) :
25  _owner(owner),
26  _program(program)
27 {
28  _literal_mode = false;
29 }
30 
31 /**
32  *
33  */
34 WordWrapStreamBuf::
35 ~WordWrapStreamBuf() {
36  sync();
37 }
38 
39 /**
40  * Called by the system ostream implementation when the buffer should be
41  * flushed to output (for instance, on destruction).
42  */
43 int WordWrapStreamBuf::
44 sync() {
45  std::streamsize n = pptr() - pbase();
46  write_chars(pbase(), n);
47 
48  // Send all the data out now.
49  flush_data();
50 
51  return 0; // EOF to indicate write full.
52 }
53 
54 /**
55  * Called by the system ostream implementation when its internal buffer is
56  * filled, plus one character.
57  */
58 int WordWrapStreamBuf::
59 overflow(int ch) {
60  std::streamsize n = pptr() - pbase();
61 
62  if (n != 0 && sync() != 0) {
63  return EOF;
64  }
65 
66  if (ch != EOF) {
67  // Write one more character.
68  char c = ch;
69  write_chars(&c, 1);
70  }
71 
72  pbump(-n); // Reset pptr().
73  return 0;
74 }
75 
76 /**
77  * An internal function called by sync() and overflow() to store one or more
78  * characters written to the stream into the memory buffer.
79  */
80 void WordWrapStreamBuf::
81 write_chars(const char *start, int length) {
82  if (length > 0) {
83  set_literal_mode((_owner->flags() & Notify::get_literal_flag()) != 0);
84  std::string new_data(start, length);
85  size_t newline = new_data.find_first_of("\n\r");
86  size_t p = 0;
87  while (newline != std::string::npos) {
88  // The new data contains a newline; flush our data to that point.
89  _data += new_data.substr(p, newline - p + 1);
90  flush_data();
91  p = newline + 1;
92  newline = new_data.find_first_of("\n\r", p);
93  }
94 
95  // Save the rest for the next write.
96  _data += new_data.substr(p);
97  }
98 }
99 
100 /**
101  * Writes the contents of _data to the actual output stream, either word-
102  * wrapped or not as appropriate, and empties the contents of _data.
103  */
104 void WordWrapStreamBuf::
105 flush_data() {
106  if (!_data.empty()) {
107  if (_literal_mode) {
108  std::cerr << _data;
109  } else {
110  _program->show_text(_data);
111  }
112  _data = "";
113  }
114 }
WordWrapStream
A special ostream that formats all of its output through ProgramBase::show_text().
Definition: wordWrapStream.h:30
ProgramBase
This is intended to be the base class for most general-purpose utility programs in the PANDATOOL tree...
Definition: programBase.h:34
pnotify.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
wordWrapStreamBuf.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
programBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
ProgramBase::show_text
void show_text(const std::string &text)
Formats the indicated text to stderr with the known _terminal_width.
Definition: programBase.I:18
wordWrapStream.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Notify::get_literal_flag
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:108