00001 /** @file rutz/cstrstream.cc like std::strstream, but not deprecated */ 00002 00003 /////////////////////////////////////////////////////////////////////// 00004 // 00005 // Copyright (c) 2004-2007 University of Southern California 00006 // Rob Peters <rjpeters at usc dot edu> 00007 // 00008 // created: Fri Oct 8 15:45:46 2004 00009 // commit: $Id: cstrstream.cc 12074 2009-11-24 07:51:51Z itti $ 00010 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/cstrstream.cc $ 00011 // 00012 // -------------------------------------------------------------------- 00013 // 00014 // This file is part of GroovX. 00015 // [http://ilab.usc.edu/rjpeters/groovx/] 00016 // 00017 // GroovX is free software; you can redistribute it and/or modify it 00018 // under the terms of the GNU General Public License as published by 00019 // the Free Software Foundation; either version 2 of the License, or 00020 // (at your option) any later version. 00021 // 00022 // GroovX is distributed in the hope that it will be useful, but 00023 // WITHOUT ANY WARRANTY; without even the implied warranty of 00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00025 // General Public License for more details. 00026 // 00027 // You should have received a copy of the GNU General Public License 00028 // along with GroovX; if not, write to the Free Software Foundation, 00029 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00030 // 00031 /////////////////////////////////////////////////////////////////////// 00032 00033 #ifndef GROOVX_RUTZ_CSTRSTREAM_CC_UTC20050626084020_DEFINED 00034 #define GROOVX_RUTZ_CSTRSTREAM_CC_UTC20050626084020_DEFINED 00035 00036 #include "cstrstream.h" 00037 #include <cstdio> // for EOF 00038 00039 rutz::imembuf::imembuf(const char* s) : 00040 m_len(strlen(s)), 00041 m_buf(s), 00042 m_owned_mem(0) 00043 { 00044 setg(const_cast<char*>(m_buf), 00045 const_cast<char*>(m_buf), 00046 const_cast<char*>(m_buf) + m_len); 00047 } 00048 00049 rutz::imembuf::imembuf(const char* s, unsigned int len) : 00050 m_len(len), 00051 m_buf(s), 00052 m_owned_mem(0) 00053 { 00054 setg(const_cast<char*>(m_buf), 00055 const_cast<char*>(m_buf), 00056 const_cast<char*>(m_buf) + m_len); 00057 } 00058 00059 void rutz::imembuf::make_owning() 00060 { 00061 if (m_owned_mem == 0) 00062 { 00063 m_owned_mem = new char[m_len+1]; 00064 memcpy(m_owned_mem, m_buf, m_len+1); 00065 m_buf = 0; 00066 setg(m_owned_mem, m_owned_mem, m_owned_mem + m_len); 00067 } 00068 } 00069 00070 rutz::imembuf::~imembuf() 00071 { 00072 delete [] m_owned_mem; 00073 } 00074 00075 int rutz::imembuf::underflow() 00076 { 00077 if (gptr() < egptr()) 00078 { 00079 return *gptr(); 00080 } 00081 00082 // Since there's no "external data source", if we've come to the end 00083 // of our current buffer, then we're just plain out of data. 00084 return EOF; 00085 } 00086 00087 rutz::imemstream::imemstream(const char* s) 00088 : 00089 std::istream(&m_buf), m_buf(s) 00090 {} 00091 00092 rutz::imemstream::imemstream(const char* s, unsigned int len) 00093 : 00094 std::istream(&m_buf), m_buf(s, len) 00095 {} 00096 00097 rutz::icstrstream::icstrstream(const char* s) 00098 : 00099 std::istream(&m_buf), m_buf(s) 00100 { 00101 m_buf.make_owning(); 00102 } 00103 00104 static const char __attribute__((used)) vcid_groovx_rutz_cstrstream_cc_utc20050626084020[] = "$Id: cstrstream.cc 12074 2009-11-24 07:51:51Z itti $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/cstrstream.cc $"; 00105 #endif // !GROOVX_RUTZ_CSTRSTREAM_CC_UTC20050626084020_DEFINED