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