Panda3D
stringStream.I
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 stringStream.I
10  * @author drose
11  * @date 2007-07-03
12  */
13 
14 /**
15  *
16  */
17 INLINE StringStream::
18 StringStream() : std::iostream(&_buf) {
19 }
20 
21 /**
22  * This version of the constructor preloads the buffer with the indicated
23  * data.
24  */
25 INLINE StringStream::
26 StringStream(const std::string &source) : std::iostream(&_buf) {
27  set_data(source);
28 }
29 
30 /**
31  * Empties the buffer.
32  */
33 INLINE void StringStream::
35  _buf.clear();
36 }
37 
38 /**
39  * Returns the number of characters available to be read from the data stream.
40  */
41 INLINE size_t StringStream::
43  flush();
44  return _buf.get_data().size();
45 }
46 
47 /**
48  * Returns the contents of the data stream as a string.
49  */
50 INLINE std::string StringStream::
51 get_data() {
52  flush();
53  const vector_uchar &data = _buf.get_data();
54  if (!data.empty()) {
55  return std::string((char *)&data[0], data.size());
56  }
57  return std::string();
58 }
59 
60 /**
61  * Replaces the contents of the data stream. This implicitly reseeks to 0.
62  */
63 INLINE void StringStream::
64 set_data(const std::string &data) {
65  _buf.clear();
66  if (!data.empty()) {
67  set_data((const unsigned char *)data.data(), data.size());
68  }
69 }
70 
71 /**
72  * Swaps the indicated buffer for the contents of the internal buffer.
73  */
74 INLINE void StringStream::
75 swap_data(vector_uchar &data) {
76  flush();
77  _buf.swap_data(data);
78 }
void clear()
Empties the buffer.
set_data
Replaces the contents of the data stream.
Definition: stringStream.h:42
const vector_uchar & get_data() const
Returns a reference to the contents of the internal buffer, without any of the iostream buffer.
void clear_data()
Empties the buffer.
Definition: stringStream.I:34
size_t get_data_size()
Returns the number of characters available to be read from the data stream.
Definition: stringStream.I:42
void swap_data(vector_uchar &data)
Swaps the indicated buffer for the contents of the internal buffer.
void swap_data(vector_uchar &data)
Swaps the indicated buffer for the contents of the internal buffer.
Definition: stringStream.I:75