00001 /*!@file Util/FileUtil.H Utility routines for working with filenames and files */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/FileUtil.H $ 00035 // $Id: FileUtil.H 8790 2007-09-28 22:24:10Z rjpeters $ 00036 // 00037 00038 #ifndef UTIL_FILEUTIL_H_DEFINED 00039 #define UTIL_FILEUTIL_H_DEFINED 00040 00041 #include <cerrno> // for errno 00042 #include <cstdio> // for perror() 00043 #include <dirent.h> 00044 #include <fcntl.h> // for open() 00045 #include <iosfwd> 00046 #include <stdio.h> 00047 #include <string> 00048 #include <sys/file.h> 00049 #include <sys/types.h> 00050 00051 namespace rutz { template <class T> class shared_ptr; } 00052 00053 /// Like fopen(fname, "w"), with some special cases 00054 /** @param fname if fname is "" (the empty string), "-", "stdout", or 00055 "STDOUT", then the returned FILE* will be stdout and *owned will 00056 be set to false; if fname is "stderr" or "STDERR", then the 00057 returned FILE* will be stderr and *owned will be set to false; 00058 otherwise, the returned FILE* will be fopen(fname,"w") and *owned 00059 will be set to true (if fopen() fails then LFATAL() will be 00060 called) 00061 00062 @param owned must be non-null; on return, *owned will indicates 00063 whether the returned FILE* was newly fopen()-ed; if *owned is 00064 true, then the caller is responsible for eventually fclose()-ing 00065 the returned FILE* 00066 00067 @return guaranteed to be a non-null FILE* (if the internal fopen() 00068 fails, LFATAL() will be called) 00069 */ 00070 FILE* stdOutputFileOpen(const std::string& fname, bool* owned); 00071 00072 /// case-INsensitive check of whether str ends with ext 00073 bool hasExtension(const std::string& str, const std::string& ext); 00074 00075 /// get the filename extension, INCLUDING the dot (e.g. ".jpg") 00076 /** returns an empty string if there is no extension on the filename */ 00077 std::string dotExtension(const std::string& in); 00078 00079 /// get the filename extension, EXCLUDING the dot (e.g., "jpg") 00080 /** returns an empty string if there is no extension on the filename 00081 00082 @param base If non-null, the base portion of the filename, but NOT 00083 INCLUDING the dot, will be returned here. So, the original 00084 filename could be reconstructed as "BASE.EXT" 00085 */ 00086 std::string nodotExtension(const std::string& in, 00087 std::string* base = 0); 00088 00089 /// open a (possibly-compressed) file for reading 00090 /** If the filename ends in ".gz", then the file is assumed to be 00091 gzip-compressed (likewise, ".bz2" implies bzip2-compressed), and 00092 the returned istream object will transparently 00093 decompressed. Otherwise, the file will be assumed to be 00094 uncompressed. 00095 00096 If the file cannot be opened, an exception will be thrown. 00097 */ 00098 rutz::shared_ptr<std::istream> 00099 openMaybeCompressedFile(const std::string& fname); 00100 00101 /// split a path into a directory prefix (if any) and a filename 00102 /** the directory prefix will be empty if there are no path separators 00103 ('/') in the path 00104 00105 e.g. splitting "foo/bar.mpg" would give "foo/" and "bar.mpg" 00106 */ 00107 void splitPath(const std::string& fname, 00108 std::string& path, std::string& file); 00109 00110 /// mkdir the given directory 00111 /** Not an error if the directory already exists, but it is an error 00112 if a file (not a directory) exists with the given name. */ 00113 void makeDirectory(const std::string& dirname); 00114 00115 /// Returns true if fname refers to a directory. 00116 /** Returns false in any other case (e.g., fname refers to a regular 00117 file, or refers to no file at all, etc.) */ 00118 bool isDirectory(const char* fname); 00119 00120 /// Returns true if dirp refers to a directory. 00121 /** If dirent::d_type is available, use that; otherwise fall back to 00122 using a stat() call. */ 00123 bool isDirectory(const struct dirent* dirp); 00124 00125 //! Use fcntl to set and unset advisory locks on a file 00126 /*! You must include stdio.h, fcntl.h, cerrno and cstdio 00127 fl is of the type struct flock. 00128 This function is provided since fcntl is kind of weird to set 00129 and unset. Also, fcntl locks are different from flock locks 00130 for instance linux apache will only check fcntl based locks 00131 */ 00132 ushort lockFile(const int fd, struct flock &fl); 00133 //! Use fcntl to set and unset advisory locks on a file 00134 ushort unlockFile(const int fd, struct flock &fl); 00135 //! lock a file by name 00136 ushort lockFile(const std::string fileName, int &fd, struct flock &fl); 00137 //! unlock a file and close 00138 ushort unlockFile(const std::string fileName, const int fd, struct flock &fl); 00139 00140 // ###################################################################### 00141 /* So things look consistent in everyone's emacs... */ 00142 /* Local Variables: */ 00143 /* mode: c++ */ 00144 /* indent-tabs-mode: nil */ 00145 /* End: */ 00146 00147 #endif // UTIL_FILEUTIL_H_DEFINED