binary_file.h

00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, 2008, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl 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
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_BINARY_FILE_H
00024 #define O2SCL_BINARY_FILE_H
00025 
00026 #include <iostream>
00027 #include <o2scl/misc.h>
00028 #include <o2scl/err_hnd.h>
00029 #include <o2scl/file_format.h>
00030 #include <o2scl/lib_settings.h>
00031 
00032 #ifndef DOXYGENP
00033 namespace o2scl {
00034 #endif
00035 
00036   /** 
00037       \brief Binary output file
00038 
00039 
00040    */
00041   class binary_out_file : public out_file_format {
00042   public:
00043 
00044     /** \brief Create a binary output file with name \c file_name  
00045      */
00046     binary_out_file(std::string file_name);
00047 
00048     virtual ~binary_out_file();
00049 
00050     /** \brief Output a bool variable
00051      */
00052     virtual int bool_out(bool dat, std::string name="");
00053 
00054     /** \brief Output a char variable
00055      */
00056     virtual int char_out(char dat, std::string name="");
00057 
00058     /** \brief Output a double variable
00059      */
00060     virtual int double_out(double dat, std::string name="");
00061 
00062     /** \brief Output a float variable
00063      */
00064     virtual int float_out(float dat, std::string name="");
00065 
00066     /** \brief Output an int variable
00067      */
00068     virtual int int_out(int dat, std::string name="");
00069 
00070     /** \brief Output an long variable
00071      */
00072     virtual int long_out(unsigned long int dat, std::string name="");
00073 
00074     /** \brief Output a string
00075      */
00076     virtual int string_out(std::string dat, std::string name="");
00077 
00078     /** \brief Output a word
00079      */
00080     virtual int word_out(std::string dat, std::string name="");
00081 
00082     /// Start an object
00083     virtual int start_object(std::string type, std::string name="");
00084     
00085     /** \brief End object output (does nothing for a binary file)
00086      */
00087     virtual int end_object();
00088 
00089     /** \brief End a line of output (does nothing for a binary file)
00090      */
00091     virtual int end_line();
00092   
00093     /** \brief Output initialization */
00094     virtual int init_file();
00095 
00096     /// Finish the file
00097     virtual int clean_up() {
00098       return 0;
00099     }
00100 
00101 #ifndef DOXYGEN_INTERNAL
00102 
00103   protected:
00104 
00105     friend class collection;
00106   
00107     /// True if the file is to be compressed
00108     bool compressed;
00109 
00110     /// True if the compression is to be performed by gzip
00111     bool gzip;
00112 
00113     /// The output stream
00114     std::ofstream outs;
00115 
00116     /// The filename specified by the user
00117     std::string user_filename;
00118 
00119     /// The temporary filename
00120     std::string temp_filename;
00121 
00122     /// \name The output format
00123     //@{
00124     int fill, precision;
00125     //std::_Ios_Fmtflags flags;
00126     //@}
00127 
00128 #endif
00129                            
00130   };
00131 
00132   /** 
00133       \brief Binary input file
00134 
00135 
00136    */
00137   class binary_in_file : public in_file_format {
00138   public:
00139 
00140     /** \brief Read an input file with name \c file_name  */
00141     binary_in_file(std::string file_name);
00142 
00143     virtual ~binary_in_file();
00144   
00145     virtual int bool_in(bool &dat, std::string name="");
00146     virtual int char_in(char &dat, std::string name="");
00147     virtual int double_in(double &dat, std::string name="");
00148     virtual int float_in(float &dat, std::string name="");
00149     virtual int int_in(int &dat, std::string name="");
00150     virtual int long_in(unsigned long int &dat, std::string name="");
00151     virtual int string_in(std::string &dat, std::string name="");
00152     virtual int word_in(std::string &dat, std::string name="");
00153     
00154     /// Read the initialization
00155     virtual int init_file();
00156 
00157     /// Clean up the file
00158     virtual int clean_up() {
00159       return 0;
00160     }
00161     
00162     /// Begin reading an object
00163     virtual int start_object(std::string &type, std::string &name);
00164     
00165     virtual int skip_object();
00166 
00167     /// Finish reading an object
00168     virtual int end_object() {
00169       return 0;
00170     }
00171 
00172 #ifndef DOXYGEN_INTERNAL
00173 
00174   protected:
00175 
00176     /// The input stream
00177     std::ifstream ins;
00178 
00179 #endif
00180   };
00181 
00182 }
00183 #endif

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page