00001 00002 /* 00003 ------------------------------------------------------------------- 00004 00005 Copyright (C) 2006, 2007, 2008, 2009, Andrew W. Steiner 00006 00007 This file is part of O2scl. 00008 00009 O2scl is free software; you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation; either version 3 of the License, or 00012 (at your option) any later version. 00013 00014 O2scl is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 GNU General Public License for more details. 00018 00019 You should have received a copy of the GNU General Public License 00020 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00021 00022 ------------------------------------------------------------------- 00023 */ 00024 #ifndef O2SCL_FILE_DETECT_H 00025 #define O2SCL_FILE_DETECT_H 00026 00027 #include <iostream> 00028 #include <fstream> 00029 #include <string> 00030 #include <o2scl/misc.h> 00031 #include <o2scl/err_hnd.h> 00032 #include <o2scl/text_file.h> 00033 #include <o2scl/binary_file.h> 00034 00035 #ifndef DOXYGENP 00036 namespace o2scl { 00037 #endif 00038 00039 /** \brief Read a (possibly compressed) file and automatically detect 00040 the file format 00041 00042 This class is experimental. 00043 00044 Really nasty hack. This works by copying the file to a temporary 00045 file in /tmp and then uncompressing it using a call to 00046 \c system("gunzip /tmp/filename") . When the file is 00047 closed, the temporary file is removed using 'rm -f'. 00048 00049 If the filename ends with ".gz" or ".bz2", then input_detect 00050 will try to uncompress it (using gunzip or bunzip2), otherwise, 00051 the file will be treated as normal. 00052 00053 Note that there must be enough disk space in the temporary 00054 directory for the uncompressed file or the read will fail. 00055 00056 \future Allow the user to specify the compression commands 00057 in configure, or at least specify the path to gzip, bzip2, etc. 00058 \future Use the boost pipes facility instead. 00059 00060 */ 00061 class file_detect { 00062 public: 00063 00064 file_detect(); 00065 00066 virtual ~file_detect() {}; 00067 00068 /** \brief Open an input file with the given name 00069 00070 If the filename ends with ".gz" or ".bz2", then the file is 00071 assumed to be compressed. 00072 00073 It is important to note that the file is not closed until 00074 file_detect::close() method is called. 00075 00076 If the file opening failed, zero will be returned. 00077 */ 00078 in_file_format *open(const char *s, bool err_on_fail=true); 00079 00080 /** \brief Close an input file */ 00081 virtual int close(); 00082 00083 /** \brief Return \c true if the opened file was 00084 originally compressed */ 00085 virtual bool is_compressed() { return compressed; } 00086 00087 /** \brief Return \c true if the opened file was 00088 a binary file */ 00089 virtual bool is_binary() { return binary; } 00090 00091 #ifndef DOXYGEN_INTERNAL 00092 00093 protected: 00094 00095 /// The temporary filename 00096 std::string temp_filename; 00097 /// The user-supplied filename 00098 std::string user_filename; 00099 /// The input file 00100 in_file_format *iffp; 00101 00102 /// True if the file was compressed 00103 bool compressed; 00104 00105 /// True if the file was a binary file 00106 bool binary; 00107 00108 #endif 00109 00110 }; 00111 00112 } 00113 00114 #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