00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, 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 This class is experimental. 00042 00043 Really nasty hack. This works by copying the file to a temporary 00044 file in /tmp and then uncompressing it using a call to 00045 \c system("gunzip /tmp/filename") . When the file is 00046 closed, the temporary file is removed using 'rm -f'. 00047 00048 If the filename ends with ".gz" or ".bz2", then input_detect 00049 will try to uncompress it (using gunzip or bunzip2), otherwise, 00050 the file will be treated as normal. 00051 00052 Note that there must be enough disk space in the temporary 00053 directory for the uncompressed file or the read will fail. 00054 00055 \future Allow the user to specify the compression commands 00056 in configure, or at least specify the path to gzip, bzip2, etc. 00057 \future Use the boost pipes facility instead. 00058 00059 */ 00060 class file_detect { 00061 public: 00062 00063 file_detect(); 00064 00065 virtual ~file_detect() {}; 00066 00067 /** \brief Open an input file with the given name 00068 00069 If the filename ends with ".gz" or ".bz2", then the file is 00070 assumed to be compressed. 00071 00072 It is important to note that the file is not closed until 00073 file_detect::close() method is called. 00074 */ 00075 in_file_format *open(const char *s); 00076 00077 /** \brief Close an input file */ 00078 virtual int close(); 00079 00080 /** \brief Return \c true if the opened file was 00081 originally compressed */ 00082 virtual bool is_compressed() { return compressed; } 00083 00084 /** \brief Return \c true if the opened file was 00085 a binary file */ 00086 virtual bool is_binary() { return binary; } 00087 00088 #ifndef DOXYGEN_INTERNAL 00089 00090 protected: 00091 00092 /// The temporary filename 00093 std::string temp_filename; 00094 /// The user-supplied filename 00095 std::string user_filename; 00096 /// The input file 00097 in_file_format *iffp; 00098 00099 /// True if the file was compressed 00100 bool compressed; 00101 00102 /// True if the file was a binary file 00103 bool binary; 00104 00105 #endif 00106 00107 }; 00108 00109 } 00110 00111 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page