Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

dtool/src/dtoolbase/fakestringstream.h

Go to the documentation of this file.
00001 // Filename: fakestringstream.h
00002 // Created by:  cary (04Feb99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef FAKESTRINGSTREAM_H
00020 #define FAKESTRINGSTREAM_H
00021 
00022 #include <strstream.h>
00023 #include <string.h>
00024 #include <string>
00025 
00026 #ifdef HAVE_NAMESPACE
00027 using namespace std;
00028 #endif
00029 
00030 class fake_istream_buffer {
00031 public:
00032   fake_istream_buffer() {
00033     _len = 0;
00034     _str = "";
00035   }
00036   fake_istream_buffer(const string &source) {
00037     _len = source.length();
00038     if (_len == 0) {
00039       _str = "";
00040     } else {
00041       _str = new char[_len];
00042       memcpy(_str, source.data(), _len);
00043     }
00044   }
00045   ~fake_istream_buffer() {
00046     if (_len != 0) {
00047       delete[] _str;
00048     }
00049   }
00050 
00051   int _len;
00052   char *_str;
00053 };
00054 
00055 class istringstream : public fake_istream_buffer, public istrstream {
00056 public:
00057   istringstream(const string &input) :
00058     fake_istream_buffer(input),
00059     istrstream(_str, _len) { }
00060 };
00061 
00062 class ostringstream : public ostrstream {
00063 public:
00064   string str() {
00065     // We must capture the length before we take the str().
00066     int length = pcount();
00067     char *s = ostrstream::str();
00068     string result(s, length);
00069     delete[] s;
00070     return result;
00071   }
00072 };
00073 
00074 class stringstream : public fake_istream_buffer, public strstream {
00075 public:
00076   stringstream() : strstream() {
00077     _owns_str = true;
00078   }
00079   stringstream(const string &input) :
00080     fake_istream_buffer(input),
00081     strstream(_str, _len, ios::in)
00082   {
00083     _owns_str = false;
00084   }
00085 
00086   // str() doesn't seem to compile cross-platform too reliably--Irix
00087   // doesn't define pcount() for some reason.  On the other hand, why
00088   // are you calling str() on a stringstream?  Just use an
00089   // ostringstream.
00090 
00091   /*
00092   string str() {
00093     int length = pcount();
00094     char *s = strstream::str();
00095     string result(s, length);
00096     if (_owns_str) {
00097       delete[] s;
00098     }
00099     return result;
00100   }
00101   */
00102 
00103 private:
00104   bool _owns_str;
00105 };
00106 
00107 #endif

Generated on Thu May 1 22:12:58 2003 for DTool by doxygen1.3