FileUtil.C File Reference

#include "Util/FileUtil.H"
#include "Util/log.H"
#include "rutz/bzip2stream.h"
#include "rutz/gzstreambuf.h"
#include "rutz/shared_ptr.h"
#include <cstring>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Include dependency graph for FileUtil.C:

Go to the source code of this file.

Functions

FILE * stdOutputFileOpen (const std::string &fname, bool *owned)
 Like fopen(fname, "w"), with some special cases.
bool hasExtension (const std::string &str, const std::string &ext)
 case-INsensitive check of whether str ends with ext
std::string dotExtension (const std::string &in)
 get the filename extension, INCLUDING the dot (e.g. ".jpg")
std::string nodotExtension (const std::string &in, std::string *base_out)
 get the filename extension, EXCLUDING the dot (e.g., "jpg")
rutz::shared_ptr< std::istream > openMaybeCompressedFile (const std::string &fname)
 open a (possibly-compressed) file for reading
void splitPath (const std::string &fname, std::string &path, std::string &file)
 split a path into a directory prefix (if any) and a filename
void makeDirectory (const std::string &dirname)
 mkdir the given directory
bool isDirectory (const char *fname)
 Returns true if fname refers to a directory.
bool isDirectory (const struct dirent *dirp)
 Returns true if dirp refers to a directory.
ushort lockFile (const int fd, struct flock &fl)
 Use fcntl to set and unset advisory locks on a file.
ushort unlockFile (const int fd, struct flock &fl)
 Use fcntl to set and unset advisory locks on a file.
ushort lockFile (const std::string fileName, int &fd, struct flock &fl)
 lock a file by name
ushort unlockFile (const std::string fileName, const int fd, struct flock &fl)
 unlock a file and close

Detailed Description

Utility routines for working with filenames and files

Definition in file FileUtil.C.


Function Documentation

std::string dotExtension ( const std::string in  ) 

get the filename extension, INCLUDING the dot (e.g. ".jpg")

returns an empty string if there is no extension on the filename

Definition at line 95 of file FileUtil.C.

bool hasExtension ( const std::string str,
const std::string ext 
)

case-INsensitive check of whether str ends with ext

Definition at line 84 of file FileUtil.C.

Referenced by UcbMpegOutputStream::makeEncoder(), and openMaybeCompressedFile().

bool isDirectory ( const struct dirent *  dirp  ) 

Returns true if dirp refers to a directory.

If dirent::d_type is available, use that; otherwise fall back to using a stat() call.

Definition at line 209 of file FileUtil.C.

References isDirectory().

bool isDirectory ( const char *  fname  ) 

Returns true if fname refers to a directory.

Returns false in any other case (e.g., fname refers to a regular file, or refers to no file at all, etc.)

Definition at line 195 of file FileUtil.C.

Referenced by isDirectory().

ushort lockFile ( const std::string  fileName,
int &  fd,
struct flock &  fl 
)

lock a file by name

Definition at line 259 of file FileUtil.C.

References lockFile().

ushort lockFile ( const int  fd,
struct flock &  fl 
)

Use fcntl to set and unset advisory locks on a file.

You must include stdio.h, fcntl.h, cerrno and cstdio fl is of the type struct flock. This function is provided since fcntl is kind of weird to set and unset. Also, fcntl locks are different from flock locks for instance linux apache will only check fcntl based locks

Definition at line 222 of file FileUtil.C.

Referenced by lockFile().

void makeDirectory ( const std::string dirname  ) 

mkdir the given directory

Not an error if the directory already exists, but it is an error if a file (not a directory) exists with the given name.

Definition at line 177 of file FileUtil.C.

Referenced by submain().

std::string nodotExtension ( const std::string in,
std::string base = 0 
)

get the filename extension, EXCLUDING the dot (e.g., "jpg")

returns an empty string if there is no extension on the filename

Parameters:
base If non-null, the base portion of the filename, but NOT INCLUDING the dot, will be returned here. So, the original filename could be reconstructed as "BASE.EXT"

Definition at line 107 of file FileUtil.C.

Referenced by MrawvDecoder::MrawvDecoder().

rutz::shared_ptr<std::istream> openMaybeCompressedFile ( const std::string fname  ) 

open a (possibly-compressed) file for reading

If the filename ends in ".gz", then the file is assumed to be gzip-compressed (likewise, ".bz2" implies bzip2-compressed), and the returned istream object will transparently decompressed. Otherwise, the file will be assumed to be uncompressed.

If the file cannot be opened, an exception will be thrown.

Definition at line 139 of file FileUtil.C.

References hasExtension(), rutz::ibzip2open(), rutz::igzopen(), and rutz::make_shared().

void splitPath ( const std::string fname,
std::string path,
std::string file 
)

split a path into a directory prefix (if any) and a filename

the directory prefix will be empty if there are no path separators ('/') in the path

e.g. splitting "foo/bar.mpg" would give "foo/" and "bar.mpg"

Definition at line 160 of file FileUtil.C.

FILE* stdOutputFileOpen ( const std::string fname,
bool *  owned 
)

Like fopen(fname, "w"), with some special cases.

Parameters:
fname if fname is "" (the empty string), "-", "stdout", or "STDOUT", then the returned FILE* will be stdout and *owned will be set to false; if fname is "stderr" or "STDERR", then the returned FILE* will be stderr and *owned will be set to false; otherwise, the returned FILE* will be fopen(fname,"w") and *owned will be set to true (if fopen() fails then LFATAL() will be called)
owned must be non-null; on return, *owned will indicates whether the returned FILE* was newly fopen()-ed; if *owned is true, then the caller is responsible for eventually fclose()-ing the returned FILE*
Returns:
guaranteed to be a non-null FILE* (if the internal fopen() fails, LFATAL() will be called)

Definition at line 55 of file FileUtil.C.

Referenced by addNoise().

ushort unlockFile ( const std::string  fileName,
const int  fd,
struct flock &  fl 
)

unlock a file and close

Definition at line 266 of file FileUtil.C.

References unlockFile().

ushort unlockFile ( const int  fd,
struct flock &  fl 
)

Use fcntl to set and unset advisory locks on a file.

Definition at line 244 of file FileUtil.C.

Referenced by unlockFile().

Generated on Sun May 8 08:19:18 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3