Panda3D
|
00001 // Filename: lineStreamBuf.cxx 00002 // Created by: drose (26Feb00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "lineStreamBuf.h" 00016 00017 #ifndef HAVE_STREAMSIZE 00018 // Some compilers--notably SGI--don't define this for us. 00019 typedef int streamsize; 00020 #endif 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Function: LineStreamBuf::Constructor 00024 // Access: Public 00025 // Description: 00026 //////////////////////////////////////////////////////////////////// 00027 LineStreamBuf:: 00028 LineStreamBuf() { 00029 _has_newline = false; 00030 00031 // The LineStreamBuf doesn't actually need a buffer--it's happy 00032 // writing characters one at a time, since they're just getting 00033 // stuffed into a string. (Although the code is written portably 00034 // enough to use a buffer correctly, if we had one.) 00035 setg(0, 0, 0); 00036 setp(0, 0); 00037 } 00038 00039 //////////////////////////////////////////////////////////////////// 00040 // Function: LineStreamBuf::Destructor 00041 // Access: Public, Virtual 00042 // Description: 00043 //////////////////////////////////////////////////////////////////// 00044 LineStreamBuf:: 00045 ~LineStreamBuf() { 00046 sync(); 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: LineStreamBuf::get_line 00051 // Access: Public 00052 // Description: Extracts the next line of text from the 00053 // LineStreamBuf, and sets the has_newline() flag 00054 // according to whether this line had a trailing newline 00055 // or not. 00056 //////////////////////////////////////////////////////////////////// 00057 string LineStreamBuf:: 00058 get_line() { 00059 // Extract the data up to, but not including, the next newline 00060 // character. 00061 size_t nl = _data.find('\n'); 00062 if (nl == string::npos) { 00063 // No trailing newline; return the remainder of the string. 00064 _has_newline = false; 00065 string result = _data; 00066 _data = ""; 00067 return result; 00068 } 00069 00070 _has_newline = true; 00071 string result = _data.substr(0, nl); 00072 _data = _data.substr(nl + 1); 00073 return result; 00074 } 00075 00076 //////////////////////////////////////////////////////////////////// 00077 // Function: LineStreamBuf::sync 00078 // Access: Public, Virtual 00079 // Description: Called by the system ostream implementation when the 00080 // buffer should be flushed to output (for instance, on 00081 // destruction). 00082 //////////////////////////////////////////////////////////////////// 00083 int LineStreamBuf:: 00084 sync() { 00085 streamsize n = pptr() - pbase(); 00086 write_chars(pbase(), n); 00087 pbump(-n); // Reset pptr(). 00088 return 0; // EOF to indicate write full. 00089 } 00090 00091 //////////////////////////////////////////////////////////////////// 00092 // Function: LineStreamBuf::overflow 00093 // Access: Public, Virtual 00094 // Description: Called by the system ostream implementation when its 00095 // internal buffer is filled, plus one character. 00096 //////////////////////////////////////////////////////////////////// 00097 int LineStreamBuf:: 00098 overflow(int ch) { 00099 streamsize n = pptr() - pbase(); 00100 00101 if (n != 0 && sync() != 0) { 00102 return EOF; 00103 } 00104 00105 if (ch != EOF) { 00106 // Write one more character. 00107 char c = ch; 00108 write_chars(&c, 1); 00109 } 00110 00111 return 0; 00112 }