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