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

mstream.cc

Go to the documentation of this file.
00001 
00025 #include "magic/mobject.h"
00026 #include "magic/mstream.h"
00027 #include "magic/mpararr.h"
00028 
00029 BEGIN_NAMESPACE (MagiC);
00030 
00031 #ifdef SunOS
00032 #include <sys/varargs.h>
00033 #else
00034 #include <stdarg.h>
00035 #endif
00036 
00037 
00038 class IStreamRef : private IODevice {
00039   protected:
00040     inline void         addStream           () {IODevice::addStream();}
00041     inline void         removeStream        () {IODevice::removeStream();}
00042     inline int          streamCount         () const {return IODevice::streamCount();}
00043 
00044     friend class Stream;
00045 };
00046 
00048 //                                                                           //
00049 //                       ----                                                //
00050 //                      (      |       ___   ___                             //
00051 //                       ---  -+- |/\ /   )  ___| |/|/|                      //
00052 //                          )  |  |   |---  (   | | | |                      //
00053 //                      ___/    \ |    \__   \__| | | |                      //
00054 //                                                                           //
00056 
00057 Stream::Stream ()
00058 {
00059     mpDevice    = NULL;
00060     mOwnDevice  = false;
00061     mFormatMode = 0;
00062 }
00063 
00064 /*****************************************************************************/
00071 Stream::Stream (IODevice& dev) {
00072     mpDevice    = &dev;
00073     mOwnDevice  = false;
00074     mFormatMode = 0;
00075 }
00076 
00082 Stream::Stream (IODevice* dev)
00083 {
00084     mpDevice    = dev;
00085     mOwnDevice  = true;
00086     mFormatMode = 0;
00087 }
00088 
00094 Stream::Stream (String& buffer)
00095 {
00096     mpDevice    = new Buffer (buffer);
00097     mOwnDevice  = true;
00098     mFormatMode = 0;
00099 }
00100 
00106 Stream::Stream (FILE* strm)
00107 {
00108     mpDevice    = new File (strm);
00109     mOwnDevice  = true;
00110     mFormatMode = 0;
00111 }
00112 
00115 Stream::Stream (Stream& orig)
00116 {
00117     mpDevice    = orig.mpDevice;
00118     if (mpDevice)
00119         static_cast <IStreamRef*> (mpDevice)->addStream ();
00120 
00121     mOwnDevice  = orig.mOwnDevice;
00122     mFormatMode = orig.mFormatMode;
00123     mEncoding   = orig.mEncoding;
00124     mFlags      = orig.mFlags;
00125 }
00126 
00132 Stream::~Stream ()
00133 {
00134     if (mOwnDevice)
00135         if (mpDevice) {
00136             static_cast <IStreamRef*> (mpDevice)->removeStream ();
00137             if (static_cast <IStreamRef*> (mpDevice)->streamCount() <= 0)
00138                 delete mpDevice;
00139         }
00140 }
00141 
00151 void Stream::setDevice (IODevice* dev )
00152 {
00153     mpDevice = dev;
00154     mOwnDevice = true;
00155 }
00156 
00157 /*******************************************************************************
00158 ** Returns true if the stream is at end.
00159 **/
00160 bool Stream::atEnd () const
00161 {
00162     if (mpDevice)
00163         return mpDevice->atEnd ();
00164     else
00165         return true;
00166 }
00167 
00173 void Stream::copy (const Stream& other)
00174 {
00175     TRACELINE;
00176     if (mpDevice && mOwnDevice)
00177         delete mpDevice;
00178     
00179     mpDevice    = other.mpDevice;
00180     mOwnDevice  = false; // WARNING
00181     mFormatMode = other.mFormatMode;
00182     mEncoding   = other.mEncoding;
00183     mFlags      = other.mFlags;
00184 }
00185 
00186 
00188 //                                                                           //
00189 //                    ___   ----                                             //
00190 //                   |   | (      |       ___   ___                          //
00191 //                   |   |  ---  -+- |/\ /   )  ___| |/|/|                   //
00192 //                   |   |     )  |  |   |---  (   | | | |                   //
00193 //                   `___´ ___/    \ |    \__   \__| | | |                   //
00194 //                                                                           //
00196 
00197 /*****************************************************************************^
00198 * Construction from a FILE output stream.
00199 *
00200 *  Example:
00201 *  @code
00202 *       FILE* myfile = fopen ("myfile.ext", "w");
00203 *      OStream mystream (out);      // Attach to opened stream.
00204 *      OStream outstd (stdout); // Attach to standard output stream.
00205 *  @endcode
00206 *
00207 *  @note It is assumed that the FILE stream really is open.
00208 **/
00209 OStream::OStream (FILE* strm )
00210         : Stream (strm)
00211 {
00212     mAutoFlush = false;
00213 }
00214 
00217 OStream::OStream (OStream& orig) : Stream (orig)
00218 {
00219     mAutoFlush = orig.mAutoFlush;
00220 }
00221 
00231 OStream::OStream (String& buffer,   
00232                   int mode          )
00233         : Stream (new Buffer (buffer, mode | IO_Writable))
00234 {
00235 }
00236 
00239 void OStream::flush ()
00240 {
00241     if (mpDevice)
00242         mpDevice->flush ();
00243 }
00244 
00246 void OStream::copy (const OStream& other)
00247 {
00248     Stream::copy (other);
00249 
00250     mAutoFlush = other.mAutoFlush;
00251 }
00252 
00259 OStream& OStream::printf (const char* sformat, ...)
00260 {
00261     va_list args;
00262     va_start (args, sformat);
00263 
00264     *this << vstrformat (sformat, args);
00265     
00266     va_end (args);
00267 
00268     return *this;
00269 }
00270 
00271 
00272 
00274 //                                                                           //
00275 //                    ---  ----                                              //
00276 //                     |  (      |       ___   ___                           //
00277 //                     |   ---  -+- |/\ /   )  ___| |/|/|                    //
00278 //                     |      )  |  |   |---  (   | | | |                    //
00279 //                    _|_ ___/    \ |    \__   \__| | | |                    //
00280 //                                                                           //
00282 
00287 IStream::IStream (IODevice* dev)
00288         : Stream (dev)
00289 {
00290     /* Open the file if it wasn't opened already. */
00291     if (!mpDevice->isOpen())
00292         mpDevice->open (IO_Readable);
00293 }
00294 
00295 /*****************************************************************************/
00301 //bool IStream::openload (const char* filename /**< Filename to open. */)
00302 //{
00303 //  NOT_IMPLEMENTED;
00304 //  return false;
00305 //}
00306 
00307 END_NAMESPACE;
00308 

Generated on Thu Feb 10 20:06:42 2005 for LibMagiC by doxygen1.2.18