oct-strstrm.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_octave_strstream_h)
00024 #define octave_octave_strstream_h 1
00025 
00026 #include <string>
00027 #include <sstream>
00028 
00029 #include "oct-stream.h"
00030 
00031 class
00032 octave_base_strstream : public octave_base_stream
00033 {
00034 public:
00035 
00036   octave_base_strstream (std::ios::openmode m = std::ios::out,
00037                          oct_mach_info::float_format ff
00038                            = oct_mach_info::native_float_format ())
00039     : octave_base_stream (m, ff) { }
00040 
00041   // Position a stream at OFFSET relative to ORIGIN.
00042 
00043   int seek (long, int);
00044 
00045   // Return current stream position.
00046 
00047   virtual long tell (void);
00048 
00049   // The name of the file.
00050 
00051   std::string name (void) const { return std::string (); }
00052 
00053   virtual std::streambuf *rdbuf (void) = 0;
00054 
00055   virtual bool bad (void) const = 0;
00056 
00057   virtual void clear (void) = 0;
00058 
00059 protected:
00060 
00061   ~octave_base_strstream (void) { }
00062 
00063 private:
00064 
00065   // No copying!
00066 
00067   octave_base_strstream (const octave_base_strstream&);
00068 
00069   octave_base_strstream& operator = (const octave_base_strstream&);
00070 };
00071 
00072 class
00073 octave_istrstream : public octave_base_strstream
00074 {
00075 public:
00076 
00077   octave_istrstream (const char *data,
00078                      std::ios::openmode arg_md = std::ios::out,
00079                      oct_mach_info::float_format ff
00080                        = oct_mach_info::native_float_format ())
00081     : octave_base_strstream (arg_md, ff), is (data) { }
00082 
00083   octave_istrstream (const std::string& data,
00084                      std::ios::openmode arg_md = std::ios::out,
00085                      oct_mach_info::float_format ff
00086                        = oct_mach_info::native_float_format ())
00087     : octave_base_strstream (arg_md, ff), is (data.c_str ()) { }
00088 
00089   static octave_stream
00090   create (const char *data, std::ios::openmode arg_md = std::ios::out,
00091           oct_mach_info::float_format ff
00092             = oct_mach_info::native_float_format ());
00093 
00094   static octave_stream
00095   create (const std::string& data, std::ios::openmode arg_md = std::ios::out,
00096           oct_mach_info::float_format ff
00097             = oct_mach_info::native_float_format ());
00098 
00099   // Return non-zero if EOF has been reached on this stream.
00100 
00101   bool eof (void) const { return is.eof (); }
00102 
00103   std::istream *input_stream (void) { return &is; }
00104 
00105   std::ostream *output_stream (void) { return 0; }
00106 
00107   long tell (void) { return is.tellg (); }
00108 
00109   std::streambuf *rdbuf (void) { return is ? is.rdbuf () : 0; }
00110 
00111   bool bad (void) const { return is.bad (); }
00112 
00113   void clear (void) { is.clear (); }
00114 
00115 protected:
00116 
00117   ~octave_istrstream (void) { }
00118 
00119 private:
00120 
00121   std::istringstream is;
00122 
00123   // No copying!
00124 
00125   octave_istrstream (const octave_istrstream&);
00126 
00127   octave_istrstream& operator = (const octave_istrstream&);
00128 };
00129 
00130 class
00131 octave_ostrstream : public octave_base_strstream
00132 {
00133 public:
00134 
00135   octave_ostrstream (std::ios::openmode arg_md = std::ios::out,
00136                      oct_mach_info::float_format ff
00137                        = oct_mach_info::native_float_format ())
00138     : octave_base_strstream (arg_md, ff), os () { }
00139 
00140   static octave_stream
00141   create (std::ios::openmode arg_md = std::ios::out,
00142           oct_mach_info::float_format ff
00143             = oct_mach_info::native_float_format ());
00144 
00145   // Return non-zero if EOF has been reached on this stream.
00146 
00147   bool eof (void) const { return os.eof (); }
00148 
00149   std::istream *input_stream (void) { return 0; }
00150 
00151   std::ostream *output_stream (void) { return &os; }
00152 
00153   std::string str (void) { return os.str (); }
00154 
00155   std::streambuf *rdbuf (void) { return os ? os.rdbuf () : 0; }
00156 
00157   bool bad (void) const { return os.bad (); }
00158 
00159   void clear (void) { os.clear (); }
00160 
00161 protected:
00162 
00163   ~octave_ostrstream (void) { }
00164 
00165 private:
00166 
00167   std::ostringstream os;
00168 
00169   // No copying!
00170 
00171   octave_ostrstream (const octave_ostrstream&);
00172 
00173   octave_ostrstream& operator = (const octave_ostrstream&);
00174 };
00175 
00176 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines