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

file.h

Go to the documentation of this file.
00001 #ifndef file_h
00002 #define file_h
00003 
00004 #include <iostream>
00005 #include <string>
00006 #include <list>
00007 
00008 // compatibility check
00009 #if __GNUC__ == 2
00010 #define ios_base ios
00011 #endif
00012 
00014 
00022 class File 
00023 {
00024 private:
00026 
00030   class stream 
00031   {
00032   public:
00033     enum Type 
00034       {
00035         FILE_STREAM,
00036         MEM_STREAM
00037       };
00038 
00039   protected:
00040 
00041     std::string name; 
00042 
00043     bool valid; 
00044 
00045     int line; 
00046 
00047     int count; 
00048 
00049     std::iostream *io;
00050 
00051     std::ios_base::openmode mode; 
00052 
00053     Type type;
00054 
00055   public:
00056     stream( std::string name, std::ios_base::openmode mode, Type type );
00057     virtual ~stream();
00058 
00059     bool is_valid() const { return valid; }
00060     bool operator!() const { return !valid; }
00061 
00063     void operator++(){ line++; }
00064 
00066     void operator--(){ line--; }
00067 
00069     std::iostream& get_io() { return *io; }
00070 
00071     template <class T> void operator<<( T t ) { *io << t; }
00072     template <class T> void operator>>( T& t ) { *io >> t; }
00073 
00074     int get_refs() const { return count; }
00075     int add_ref() { count++; return count; }
00076     int sub_ref() { count--; return count; }
00077 
00079     std::string get_name() const { return name; }
00080 
00081     std::ios_base::openmode get_mode() const { return mode; }
00082 
00083     void set_mode( std::ios_base::openmode m ) { mode = m; }
00084 
00086     virtual void trunc()=0;
00087 
00088     int get_line() const { return line; }
00089     void set_line( int l ) { line =  l; }
00090 
00091     // some io functions
00092     void seekg( long pos ) { io->seekg( pos ); line=0; }
00093     long tellg() { return io->tellg(); }
00094     void seekp( long pos ) { io->seekp( pos ); line=0; }
00095     long tellp() { return io->tellp(); }
00096     int eof() { return io->eof(); }
00097 
00098     std::ostream& flush() { return io->flush(); }
00099     std::ostream& put( char c ) { return io->put( c ); }
00100     std::ostream& write( std::string s ) { return io->write( s.c_str(), s.size() ); }
00101     std::ostream& write( std::string s, std::streamsize n ) { return io->write( s.c_str(), n ); }
00102 
00103     std::istream& get( char& c ) { return io->get( c ); }
00104     std::istream& read( char* ptr, std::streamsize n ) { return io->read( ptr, n ); }
00105     
00106     void clear() { io->clear(); }
00107 
00108     Type get_type() { return type; }
00109   };
00110 
00111   class file_stream : public stream 
00112   {
00113   public:
00114     file_stream( std::string name, std::ios_base::openmode mode );
00115     void trunc();
00116   };
00117 
00118   class mem_stream : public stream 
00119   {
00120   public:
00121     mem_stream( std::string name, std::ios_base::openmode mode );
00122     void trunc();
00123   };
00124 
00125 
00127 
00129   class file_handler 
00130   {
00131   private:
00132     typedef std::list<File> file_list;
00133     typedef file_list::iterator file_list_it;
00134 
00135     file_list files; 
00136 
00137   public:
00138     bool register_file( File& file );
00139     bool unregister_file( File& file );
00140 
00142     bool get_file( std::string name, File& file );
00143 
00144     bool has_file( std::string name ) {
00145       File f;
00146       return get_file( name, f );
00147     }
00148   };
00149 
00150   stream *s; 
00151 
00152   static bool use_mem;   
00153   static bool use_tmp;   
00154 
00155   mutable bool valid; 
00156 
00157   bool real; 
00158 
00159   static file_handler fh;  
00160 
00161   std::string init_name; 
00162 
00164   bool nocreate;
00165 
00166 public:
00167   File(); 
00168   File( std::string name, 
00169         std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out,  
00170         bool nocreate = false, 
00171         bool real = true);
00172 
00173   File( const File& file ); 
00174   ~File();
00175   
00176   void open( std::string name, 
00177              std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out,
00178              bool nocreate = false, 
00179              bool real = true )
00180   { *this = File( name, mode, nocreate, real ); }
00181   
00182   static bool use_handler(); 
00183   static void use_handler( bool u );
00184 
00185   static bool use_tmp_files(); 
00186   static void use_tmp_files( bool u );
00187   
00188   bool is_valid() const 
00189   {
00190     if( s )
00191       valid = s->is_valid();
00192     else
00193       valid = false;
00194 
00195     return valid;
00196   }
00197 
00198   bool operator!() const { return !is_valid(); }
00199 
00206   std::string get_name() const { if( is_valid() ) return s->get_name(); else return init_name; }
00207   
00208   template <class T> File& operator<<( const T t ) { *s << t; return *this; }
00209   template <class T> File& operator>>( T& t ) { *s >> t; return *this; }
00210 
00211   void seekg( long pos ) { s->seekg( pos ); }
00212   long tellg() { return s->tellg(); }
00213   void seekp( long pos ) { s->seekp( pos ); }
00214   long tellp() { return s->tellp(); }
00215   int eof() { return s->eof(); }
00216 
00218   std::iostream& get_io() { return s->get_io(); }
00219 
00224   void operator=( const File& file );
00225 
00226   std::ios_base::openmode get_mode() const { return s->get_mode(); }
00227 
00228   int get_line() const { return s->get_line(); }
00229   void set_line( int line ) { s->set_line( line ); }
00230 
00232   void operator++(){ ++(*s); }
00233 
00235   void operator--(){ --(*s); }
00236 
00237   std::ostream& flush() { return s->flush(); }
00238   std::ostream& put( char c ) { return s->put( c ); }
00239   std::ostream& write( std::string str ) { return s->write( str.c_str(), str.size() ); }
00240   std::ostream& write( std::string str, std::streamsize n ) { return s->write( str.c_str(), n ); }
00241   
00242   std::istream& get( char& c ) { return s->get( c ); }
00243   std::istream& read( char* ptr, std::streamsize n ) { return s->read( ptr, n ); }
00244 
00245   void clear() { s->clear(); }
00246 
00248   void trunc();  
00249 
00250 
00252   bool append( File other );
00253 
00254   void stats( File other ) { stats( other.get_io() ); }
00255   void stats( std::ostream &out );
00256 
00257   void dump( File other ) { dump( other.get_io() ); }
00258   void dump( std::ostream &out );
00259 
00261   static  bool fh_has_file( std::string name );
00262 
00263 private:
00264   void set_mode( std::ios_base::openmode mode );
00265 };
00266 
00267 namespace std {
00268 
00270   void getline( File& file, std::string& line );
00271 }
00272 
00273 
00275 bool operator == ( const File& f1, const File& f2 );
00276 
00277 #endif
00278 

Generated on Tue Nov 20 02:19:52 2001 for Leo<HTML> by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001