Panda3D
 All Classes Functions Variables Enumerations
stringStream.I
1 // Filename: stringStream.I
2 // Created by: drose (03Jul07)
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: StringStream::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE StringStream::
22 StringStream() : iostream(&_buf) {
23 }
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: StringStream::Constructor
27 // Access: Published
28 // Description: This version of the constructor preloads the buffer
29 // with the indicated data.
30 ////////////////////////////////////////////////////////////////////
31 INLINE StringStream::
32 StringStream(const string &source) : iostream(&_buf) {
33  set_data(source);
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: StringStream::clear_data
38 // Access: Published
39 // Description: Empties the buffer.
40 ////////////////////////////////////////////////////////////////////
41 INLINE void StringStream::
43  _buf.clear();
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: StringStream::get_data_size
48 // Access: Published
49 // Description: Returns the number of characters available to be read
50 // from the data stream.
51 ////////////////////////////////////////////////////////////////////
52 INLINE size_t StringStream::
54  flush();
55  return _buf.get_data().size();
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: StringStream::get_data
60 // Access: Published
61 // Description: Returns the contents of the data stream as a string.
62 ////////////////////////////////////////////////////////////////////
63 INLINE string StringStream::
65  flush();
66  const pvector<unsigned char> &data = _buf.get_data();
67  if (!data.empty()) {
68  return string((char *)&data[0], data.size());
69  }
70  return string();
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: StringStream::set_data
75 // Access: Published
76 // Description: Replaces the contents of the data stream. This
77 // implicitly reseeks to 0.
78 ////////////////////////////////////////////////////////////////////
79 INLINE void StringStream::
80 set_data(const string &data) {
81  _buf.clear();
82  if (!data.empty()) {
84  pv.insert(pv.end(), (const unsigned char *)&data[0], (const unsigned char *)&data[0] + data.size());
85  _buf.swap_data(pv);
86  }
87 }
88 
89 ////////////////////////////////////////////////////////////////////
90 // Function: StringStream::swap_data
91 // Access: Published
92 // Description: Swaps the indicated buffer for the contents of the
93 // internal buffer.
94 ////////////////////////////////////////////////////////////////////
95 INLINE void StringStream::
97  flush();
98  _buf.swap_data(data);
99 }
void clear()
Empties the buffer.
void swap_data(pvector< unsigned char > &data)
Swaps the indicated buffer for the contents of the internal buffer.
void set_data(const string &data)
Replaces the contents of the data stream.
Definition: stringStream.I:80
void clear_data()
Empties the buffer.
Definition: stringStream.I:42
string get_data()
Returns the contents of the data stream as a string.
Definition: stringStream.I:64
const pvector< unsigned char > & get_data() const
Returns a reference to the contents of the internal buffer, without any of the iostream buffer...
size_t get_data_size()
Returns the number of characters available to be read from the data stream.
Definition: stringStream.I:53
void swap_data(pvector< unsigned char > &data)
Swaps the indicated buffer for the contents of the internal buffer.
Definition: stringStream.I:96