Panda3D
|
00001 // Filename: stringStream.I 00002 // Created by: drose (03Jul07) 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 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: StringStream::Constructor 00018 // Access: Published 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 INLINE StringStream:: 00022 StringStream() : iostream(&_buf) { 00023 } 00024 00025 //////////////////////////////////////////////////////////////////// 00026 // Function: StringStream::Constructor 00027 // Access: Published 00028 // Description: This version of the constructor preloads the buffer 00029 // with the indicated data. 00030 //////////////////////////////////////////////////////////////////// 00031 INLINE StringStream:: 00032 StringStream(const string &source) : iostream(&_buf) { 00033 set_data(source); 00034 } 00035 00036 //////////////////////////////////////////////////////////////////// 00037 // Function: StringStream::clear_data 00038 // Access: Published 00039 // Description: Empties the buffer. 00040 //////////////////////////////////////////////////////////////////// 00041 INLINE void StringStream:: 00042 clear_data() { 00043 _buf.clear(); 00044 } 00045 00046 //////////////////////////////////////////////////////////////////// 00047 // Function: StringStream::get_data_size 00048 // Access: Published 00049 // Description: Returns the number of characters available to be read 00050 // from the data stream. 00051 //////////////////////////////////////////////////////////////////// 00052 INLINE size_t StringStream:: 00053 get_data_size() { 00054 flush(); 00055 return _buf.get_data().size(); 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: StringStream::get_data 00060 // Access: Published 00061 // Description: Returns the contents of the data stream as a string. 00062 //////////////////////////////////////////////////////////////////// 00063 INLINE string StringStream:: 00064 get_data() { 00065 flush(); 00066 const pvector<unsigned char> &data = _buf.get_data(); 00067 if (!data.empty()) { 00068 return string((char *)&data[0], data.size()); 00069 } 00070 return string(); 00071 } 00072 00073 //////////////////////////////////////////////////////////////////// 00074 // Function: StringStream::set_data 00075 // Access: Published 00076 // Description: Replaces the contents of the data stream. This 00077 // implicitly reseeks to 0. 00078 //////////////////////////////////////////////////////////////////// 00079 INLINE void StringStream:: 00080 set_data(const string &data) { 00081 _buf.clear(); 00082 if (!data.empty()) { 00083 pvector<unsigned char> pv; 00084 pv.insert(pv.end(), (const unsigned char *)&data[0], (const unsigned char *)&data[0] + data.size()); 00085 _buf.swap_data(pv); 00086 } 00087 } 00088 00089 //////////////////////////////////////////////////////////////////// 00090 // Function: StringStream::swap_data 00091 // Access: Published 00092 // Description: Swaps the indicated buffer for the contents of the 00093 // internal buffer. 00094 //////////////////////////////////////////////////////////////////// 00095 INLINE void StringStream:: 00096 swap_data(pvector<unsigned char> &data) { 00097 flush(); 00098 _buf.swap_data(data); 00099 }