file_detect.h

00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, 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_FILE_DETECT_H
00024 #define O2SCL_FILE_DETECT_H
00025 
00026 #include <iostream>
00027 #include <fstream>
00028 #include <string>
00029 #include <o2scl/misc.h>
00030 #include <o2scl/err_hnd.h>
00031 #include <o2scl/text_file.h>
00032 #include <o2scl/binary_file.h>
00033 
00034 #ifndef DOXYGENP
00035 namespace o2scl {
00036 #endif
00037 
00038   /** \brief Read a (possibly compressed) file and automatically detect
00039       the file format 
00040 
00041       Really nasty hack. This works by copying the file to a temporary
00042       file in /tmp and then uncompressing it using a call to
00043       \c system("gunzip /tmp/filename") . When the file is
00044       closed, the temporary file is removed using 'rm -f'.
00045 
00046       If the filename ends with ".gz" or ".bz2", then input_detect
00047       will try to uncompress it (using gunzip or bunzip2), otherwise,
00048       the file will be treated as normal.
00049 
00050       Note that there must be enough disk space in the temporary 
00051       directory for the uncompressed file or the read will fail.
00052     
00053       \future Allow the user to specify the compression commands
00054       in configure, or at least specify the path to gzip, bzip2, etc.
00055 
00056   */
00057   class file_detect {
00058   public:
00059 
00060     file_detect();
00061 
00062     virtual ~file_detect() {};
00063 
00064     /** \brief Open an input file with the given name
00065 
00066         If the filename ends with ".gz" or ".bz2", then the file is
00067         assumed to be compressed.
00068 
00069         It is important to note that the file is not closed until
00070         file_detect::close() method is called. 
00071     */
00072     in_file_format *open(const char *s);
00073     
00074     /** \brief Close an input file */
00075     virtual int close();
00076 
00077     /** \brief Return \c true  if the opened file was
00078         originally compressed */
00079     virtual bool is_compressed() { return compressed; }
00080 
00081     /** \brief Return \c true  if the opened file was
00082         a binary file */
00083     virtual bool is_binary() { return binary; }
00084 
00085 #ifndef DOXYGEN_INTERNAL
00086 
00087   protected:
00088 
00089     /// The temporary filename
00090     std::string temp_filename;
00091     /// The user-supplied filename
00092     std::string user_filename;
00093     /// The input file
00094     in_file_format *iffp;
00095     /// True if the file was compressed
00096     bool compressed;
00097     /// True if the file was a binary file
00098     bool binary;
00099 
00100 #endif
00101   
00102   };
00103 
00104 }
00105 
00106 #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