Panda3D
fakestringstream.h
1 // Filename: fakestringstream.h
2 // Created by: cary (04Feb99)
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 #ifndef FAKESTRINGSTREAM_H
16 #define FAKESTRINGSTREAM_H
17 
18 #include <strstream.h>
19 #include <string.h>
20 #include <string>
21 
22 #ifdef HAVE_NAMESPACE
23 using namespace std;
24 #endif
25 
27 public:
29  _len = 0;
30  _str = "";
31  }
32  fake_istream_buffer(const string &source) {
33  _len = source.length();
34  if (_len == 0) {
35  _str = "";
36  } else {
37  _str = new char[_len];
38  memcpy(_str, source.data(), _len);
39  }
40  }
42  if (_len != 0) {
43  delete[] _str;
44  }
45  }
46 
47  int _len;
48  char *_str;
49 };
50 
51 class istringstream : public fake_istream_buffer, public istrstream {
52 public:
53  istringstream(const string &input) :
54  fake_istream_buffer(input),
55  istrstream(_str, _len) { }
56 };
57 
58 class ostringstream : public ostrstream {
59 public:
60  string str() {
61  // We must capture the length before we take the str().
62  int length = pcount();
63  char *s = ostrstream::str();
64  string result(s, length);
65  delete[] s;
66  return result;
67  }
68 };
69 
70 class stringstream : public fake_istream_buffer, public strstream {
71 public:
72  stringstream() : strstream() {
73  _owns_str = true;
74  }
75  stringstream(const string &input) :
76  fake_istream_buffer(input),
77  strstream(_str, _len, ios::in)
78  {
79  _owns_str = false;
80  }
81 
82  // str() doesn't seem to compile cross-platform too reliably--Irix
83  // doesn't define pcount() for some reason. On the other hand, why
84  // are you calling str() on a stringstream? Just use an
85  // ostringstream.
86 
87  /*
88  string str() {
89  int length = pcount();
90  char *s = strstream::str();
91  string result(s, length);
92  if (_owns_str) {
93  delete[] s;
94  }
95  return result;
96  }
97  */
98 
99 private:
100  bool _owns_str;
101 };
102 
103 #endif
STL namespace.