00001 /** @file rutz/cstrstream.h like std::strstream, but not deprecated */ 00002 00003 /////////////////////////////////////////////////////////////////////// 00004 // 00005 // Copyright (c) 2002-2004 California Institute of Technology 00006 // Copyright (c) 2004-2007 University of Southern California 00007 // Rob Peters <rjpeters at usc dot edu> 00008 // 00009 // created: Fri Mar 7 13:12:33 2003 00010 // commit: $Id: cstrstream.h 8249 2007-04-12 06:03:40Z rjpeters $ 00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/cstrstream.h $ 00012 // 00013 // -------------------------------------------------------------------- 00014 // 00015 // This file is part of GroovX. 00016 // [http://ilab.usc.edu/rjpeters/groovx/] 00017 // 00018 // GroovX is free software; you can redistribute it and/or modify it 00019 // under the terms of the GNU General Public License as published by 00020 // the Free Software Foundation; either version 2 of the License, or 00021 // (at your option) any later version. 00022 // 00023 // GroovX is distributed in the hope that it will be useful, but 00024 // WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00026 // General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License 00029 // along with GroovX; if not, write to the Free Software Foundation, 00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00031 // 00032 /////////////////////////////////////////////////////////////////////// 00033 00034 #ifndef GROOVX_RUTZ_CSTRSTREAM_H_UTC20050626084019_DEFINED 00035 #define GROOVX_RUTZ_CSTRSTREAM_H_UTC20050626084019_DEFINED 00036 00037 #include <cstring> 00038 #include <istream> 00039 #include <streambuf> 00040 00041 namespace rutz 00042 { 00043 class imembuf; 00044 class imemstream; 00045 class icstrstream; 00046 } 00047 00048 /// An input streambuf that reads from memory. 00049 class rutz::imembuf : public std::streambuf 00050 { 00051 private: 00052 unsigned int m_len; 00053 const char* m_buf; 00054 char* m_owned_mem; 00055 00056 imembuf(const imembuf&); 00057 imembuf& operator=(const imembuf&); 00058 00059 public: 00060 /// Create using the given char array as the input buffer. 00061 imembuf(const char* s); 00062 00063 /// Create using the given char array and length as the input buffer. 00064 imembuf(const char* s, unsigned int len); 00065 00066 void make_owning(); 00067 00068 /// Destructor. 00069 virtual ~imembuf(); 00070 00071 /// Underflow operation. 00072 /** Since there's no "external data source", if we've come to the 00073 end of our current buffer, then we're just plain out of data. */ 00074 virtual int underflow(); 00075 }; 00076 00077 /// An input stream class based on imembuf. 00078 class rutz::imemstream : public std::istream 00079 { 00080 private: 00081 imembuf m_buf; 00082 public: 00083 /// Construct using the given char array as the input buffer. 00084 imemstream(const char* s); 00085 00086 /// Construct using the given char array and length as the input buffer. 00087 imemstream(const char* s, unsigned int len); 00088 }; 00089 00090 /// An input stream class based on imembuf that makes a private copy. 00091 class rutz::icstrstream : public std::istream 00092 { 00093 private: 00094 imembuf m_buf; 00095 public: 00096 /// Construct using the given char array as the input buffer. 00097 icstrstream(const char* s); 00098 }; 00099 00100 static const char __attribute__((used)) vcid_groovx_rutz_cstrstream_h_utc20050626084019[] = "$Id: cstrstream.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/cstrstream.h $"; 00101 #endif // !GROOVX_RUTZ_CSTRSTREAM_H_UTC20050626084019_DEFINED