Main Page   Class Hierarchy   Compound List   File List   Compound Members  

mstream.h

00001 /***************************************************************************
00002  *   This file is part of the MagiC++ library.                             *
00003  *                                                                         *
00004  *   Copyright (C) 1998-2001 Marko Grönroos <magi@iki.fi>                  *
00005  *                                                                         *
00006  ***************************************************************************
00007  *                                                                         *
00008  *  This library is free software; you can redistribute it and/or          *
00009  *  modify it under the terms of the GNU Library General Public            *
00010  *  License as published by the Free Software Foundation; either           *
00011  *  version 2 of the License, or (at your option) any later version.       *
00012  *                                                                         *
00013  *  This library is distributed in the hope that it will be useful,        *
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
00016  *  Library General Public License for more details.                       *
00017  *                                                                         *
00018  *  You should have received a copy of the GNU Library General Public      *
00019  *  License along with this library; see the file COPYING.LIB.  If         *
00020  *  not, write to the Free Software Foundation, Inc., 59 Temple Place      *
00021  *  - Suite 330, Boston, MA 02111-1307, USA.                               *
00022  *                                                                         *
00023  ***************************************************************************/
00024 
00025 #ifndef __MAGISTREAM_H__
00026 #define __MAGISTREAM_H__
00027 
00028 #include "magic/mobject.h"
00029 #include "magic/mstring.h"
00030 
00031 class File;
00032 class DumpContext;
00033 class Stream;
00034 class OStream;
00035 
00036 class IODevice : public Object {
00037   public:
00038 };
00039 
00040 class File : public IODevice {
00041   protected:
00042     FILE*   mpFile;
00043     String  mName;
00044   public:
00045                         File        (FILE* str=stdout) : mpFile (str) {;}
00046                         File        (const String& n) : mpFile(NULL), mName(n) {;}
00047     FILE*               getfp       () {return mpFile;}
00048     bool                isopen      () const {return mpFile;}
00049     //  bool                open        (bool mode=FMRead);
00050 };
00051 
00052 class Stream : public Object {
00053   protected:
00054     int     mode;       // Tallennusmoodi
00055     FILE*   stream;
00056   public:
00057                         Stream      (FILE* strm) {stream=strm; mode=0;}
00058     void                operator=   (FILE* strm) {stream=strm; mode=0;}
00059 
00060     void                close       ();
00061     
00062     friend DumpContext;
00063 };
00064 
00065 
00066 
00068 //                                                                           //
00069 //                    ___   ----                                             //
00070 //                   |   | (      |       ___   ___                          //
00071 //                   |   |  ---  -+- |/\ /   )  ___| |/|/|                   //
00072 //                   |   |     )  |  |   |---  (   | | | |                   //
00073 //                   `___´ ___/    \ |    \__   \__| | | |                   //
00074 //                                                                           //
00076 
00077 class OStream : public Stream {
00078   protected:
00079     bool            aflush;
00080     unsigned long   mUserFlags;
00081   public:
00082                         OStream     (FILE* strm=stdout);
00083                         OStream     (OStream& o);
00084 
00085     OStream&            print       (const char c);
00086     OStream&            print       (const char* str);
00087     void                flush       () {fflush (stream);}
00088     void                autoflush   (bool afl=true) {aflush=afl;}
00089     void                operator=   (const OStream& other);
00090 
00094     void                setFlag     (int flag, bool=true);
00095 
00099     bool                flag        (int flag) const;
00100 
00104     bool                operator[]  (int f) const {return flag(f);}
00105     
00106     // Virtual methods
00107     
00108     virtual OStream&        printf      (const char* format, ...);
00109     virtual OStream&        operator<<  (const char* str);
00110     virtual OStream&        operator<<  (int i);
00111     virtual OStream&        operator<<  (long i);
00112 
00113 };
00114 
00115 // Standard output and atandard error output
00116 extern OStream sout;
00117 extern OStream serr;
00118 
00120 //                                                                           //
00121 //         ___                     ___                                       //
00122 //         |  \               --  /   \        _    |   ___       |          //
00123 //         |   | |   | |/|/| |  ) |      __  |/ \  -+- /   ) \ / -+-         //
00124 //         |   | |   | | | | |--  |     /  \ |   |  |  |---   X   |          //
00125 //         |__/   \__! | | | |    \___/ \__/ |   |   \  \__  / \   \         //
00126 //                                                                           //
00128 
00129 class DumpContext : public OStream {
00130   protected:
00131     String          nextname;   // Seuraavan objektin nimi
00132     int             depth;      // Sisennystaso
00133     int             prevdepth;  // Sisennystaso viime tulostuksessa
00134     Array<String>*  attribs;    // Muistiinpannut attribuutit
00135     int             errst;
00136     
00137     int                 open        (const char* filename, int flag);
00138   public:
00139     bool    names;
00140     bool    classnames;
00141     
00142                         DumpContext     (FILE* strm=stdout);
00143                         DumpContext     (OStream& o);
00144                         ~DumpContext    ();
00145 
00146     DumpContext&        printf      (const char* format, ...);
00147 
00148     // Implementations
00149 
00150     DumpContext&        operator<<  (int i);
00151     DumpContext&        operator<<  (long i);
00152     DumpContext&        operator<<  (char i);
00153     DumpContext&        operator<<  (float i);
00154     DumpContext&        operator<<  (double i);
00155     DumpContext&        operator<<  (const char* str);
00156     DumpContext&        write       (const char* p, int n);
00157 
00158     // Asettaa seuraavan objektin nimen
00159     DumpContext&        name        (const char* nam) {nextname=nam; return *this;}
00160 
00161     const Array<String>&    getattribs  () const {return *attribs;}
00162 
00163   protected:
00164     // Tulostaa nimen (tekstimoodissa)
00165     DumpContext&        printName   ();
00166 
00167     // Tulostaa sisennyksen, jos haluttu
00168     void                printIndent ();
00169 
00170     // Tulostaa pilkun jos syvyystaso on sama kuin viime kutsulla
00171     void                printComma  ();
00172     
00173     friend DumpContext& operator<< (DumpContext& out, const Object& obj);
00174     friend DumpContext& operator>> (DumpContext& in, Object& obj);
00175 };
00176 
00177 // DumpContext& operator<< (DumpContext& dc, int i) {;}
00178 
00179 // Sokerointioperaatio name()-liitteen tekemiseksi luokan attribuutteihin
00180 // DumpContext& operator<< (DumpContext& out, const char* str);
00181 
00182 enum {  DUMP_LOAD=0, DUMP_SAVE=1, DUMP_RECORD=2,
00183         DUMP_BINARY=0, DUMP_TEXT=16, DUMP_FORMATTED=32,
00184         DUMP_VERINFO=64,
00185         DUMP_ADDRESSES=128, DUMP_CLASSNAMES};
00186 
00187 
00188 
00190 //                                                                           //
00191 //                    ---  ----                                              //
00192 //                     |  (      |       ___   ___                           //
00193 //                     |   ---  -+- |/\ /   )  ___| |/|/|                    //
00194 //                     |      )  |  |   |---  (   | | | |                    //
00195 //                    _|_ ___/    \ |    \__   \__| | | |                    //
00196 //                                                                           //
00198 
00199 class IStream : public Stream {
00200 
00201   public:
00202 
00203                 IStream     (FILE* strm=stdin) : Stream (strm) {;}
00204     
00205     // Avaa streamin tiedostoon lukua varten, palauttaa FALSE, jos
00206     // virhetilanne
00207     bool        openload    (const char* filename = NULL);
00208 
00209     // Lukee kokonaisluvun (todellisuudessa pitkän)
00210     IStream&    operator>>  (int& i);
00211 
00212     // Lukee pitkän kokonaisluvun
00213     IStream&    operator>>  (long& i);
00214 
00215     // Lukee yksittäisen merkin
00216     IStream&    operator>>  (char& i);
00217     IStream&    operator>>  (uchar& i) {return operator>> ((char&)i);}
00218 
00219     // Lukee liukuluvun (todellisuudessa pitkän)
00220     IStream&    operator>>  (float& i);
00221 
00222     // Lukee pitkän liukuluvun
00223     IStream&    operator>>  (double& i);
00224     //  IStream&    operator>>  (char* p)       {if (in) in->read (p, n); return *this;}
00225 
00226     // Lukee muistipuskurin
00227     IStream&    read        (char* p, int n);
00228 
00229     // const Array<String>& getattribs  () const {return *attribs;}
00230 };
00231 
00232 class TextStream {
00233   public:
00234     TextStream  (String& str, int mode);
00235     
00236   protected:
00237   private:
00238 };
00239 
00240 #endif

Generated at Tue Dec 4 19:53:27 2001 for MagiC++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001