Main Page | Class List | File List | Class Members | File Members | Related Pages

file.h File Reference

File/disk routines. More...

#include <limits.h>
#include <linux/limits.h>
#include <dirent.h>

Go to the source code of this file.

Defines

#define PATH_MAX   4096
#define FILE_CLOSE(fhandle)

Functions

EXTERN_C_BEGIN LIBGIFT_EXPORT
BOOL 
file_exists (const char *file)
LIBGIFT_EXPORT BOOL file_direxists (const char *path)
LIBGIFT_EXPORT BOOL file_stat (const char *file, struct stat *stbuf)
LIBGIFT_EXPORT char * file_dirname (const char *file)
LIBGIFT_EXPORT char * file_basename (const char *file)
LIBGIFT_EXPORT char * file_secure_path (const char *path)
LIBGIFT_EXPORT char * file_read_line (FILE *f, char **outbuf)
 Reads a line from an opened file.

LIBGIFT_EXPORT char * file_expand_path (const char *path)
LIBGIFT_EXPORT BOOL file_create_path (const char *path, int mode)
LIBGIFT_EXPORT BOOL file_mkdir (const char *path, int mode)
LIBGIFT_EXPORT FILE * file_temp (char **out, char *module)
LIBGIFT_EXPORT BOOL file_rmdir (const char *path)
LIBGIFT_EXPORT BOOL file_cp (const char *src, const char *dst)
 Copy a file.

LIBGIFT_EXPORT BOOL file_mv (const char *src, const char *dst)
 Move a file.

LIBGIFT_EXPORT BOOL file_slurp (const char *path, char **data, unsigned long *len)
 Slurp in a file.

LIBGIFT_EXPORT BOOL file_dump (const char *path, const char *data, unsigned long len)
LIBGIFT_EXPORT FILE * file_open (const char *path, const char *mode)
LIBGIFT_EXPORT int file_close (FILE *f)
LIBGIFT_EXPORT int file_unlink (const char *path)
LIBGIFT_EXPORT DIR * file_opendir (const char *dir)
LIBGIFT_EXPORT struct dirent * file_readdir (DIR *dh)
LIBGIFT_EXPORT int file_closedir (DIR *dh)
LIBGIFT_EXPORT char * file_host_path (const char *gift_path)
 Converts a UNIX-style path to a host specific path.

LIBGIFT_EXPORT char * file_unix_path (const char *host_path)
 Converts a host specific path to a UNIX-style path.


Detailed Description

File/disk routines.

Abstraction from commonly used file manipulation routines. Also includes portability wrappers for readdir and friends.


Define Documentation

#define FILE_CLOSE fhandle   ) 
 

Value:

do { \ file_close (*(fhandle)); \ *(fhandle) = NULL; \ } while (0)
Wrapper for file_close which will set the value pointed to by fhandle to NULL.


Function Documentation

LIBGIFT_EXPORT char* file_basename const char *  file  ) 
 

Retrieve the last element of a fully qualified path.

LIBGIFT_EXPORT int file_close FILE *  f  ) 
 

Wrapper for fclose.

LIBGIFT_EXPORT int file_closedir DIR *  dh  ) 
 

Portable closedir.

LIBGIFT_EXPORT BOOL file_cp const char *  src,
const char *  dst
 

Copy a file.

Manually copy the contents of src to dst. dst will be created if it does not exist.

Parameters:
src Source location.
dst Destination location.
Return values:
TRUE Success, file has been copied
FALSE Unable to copy the file, partial writes to dst will be unlinked

LIBGIFT_EXPORT BOOL file_create_path const char *  path,
int  mode
 

Similar to mkdir -p, except that the path is expected to have a trailing file entry which will not be created as a directory. Note that the return value says nothing about whether the _file_ exists.

Return values:
TRUE Path exists.
FALSE Unable to fully construct the supplied path.

LIBGIFT_EXPORT BOOL file_direxists const char *  path  ) 
 

Similar to file_exists, except that the path is checked to be a directory instead of a regular file.

LIBGIFT_EXPORT char* file_dirname const char *  file  ) 
 

Retrieve the directory element from a fully qualified path.

LIBGIFT_EXPORT BOOL file_dump const char *  path,
const char *  data,
unsigned long  len
 

Dumps the supplied data to a file on disk.

Parameters:
path Location of the file to write.
data Data you wish to dump to the file.
len Length of the supplied data segment.
Return values:
TRUE File has been written successfully.
FALSE Failed to create the specified file, or no data was supplied.

EXTERN_C_BEGIN LIBGIFT_EXPORT BOOL file_exists const char *  file  ) 
 

Determine if a file exists on disk, and is a regular file.

Parameters:
file Pathname to stat.
Returns:
Boolean value indicating whether or not the path exists and is a regular file.

LIBGIFT_EXPORT char* file_expand_path const char *  path  ) 
 

Expand a shell-like path to a fully qualified path.

Returns:
Expanded path.

LIBGIFT_EXPORT char* file_host_path const char *  gift_path  ) 
 

Converts a UNIX-style path to a host specific path.

For example, on Windows /C/Program Files/giFT becomes C: Files.

Returns:
The host formatted path.

LIBGIFT_EXPORT BOOL file_mkdir const char *  path,
int  mode
 

Identical to mkdir -p. All path elements will be created with the supplied mode.

Returns:
Boolean success or failure.

LIBGIFT_EXPORT BOOL file_mv const char *  src,
const char *  dst
 

Move a file.

This function will attempt to use rename () first, and cp/unlink if that fails.

Parameters:
src Original location.
dst New location.
Return values:
TRUE Success, file has been moved.
FALSE Unable to completely move the file, src file left unlinked.

LIBGIFT_EXPORT FILE* file_open const char *  path,
const char *  mode
 

Wrapper for fopen.

LIBGIFT_EXPORT DIR* file_opendir const char *  dir  ) 
 

Portable opendir.

LIBGIFT_EXPORT char* file_read_line FILE *  f,
char **  outbuf
 

Reads a line from an opened file.

This function is designed to be used in a while loop as shown:

char *line = NULL; // open file while (file_read_line (f, &line)) { // manipulate line... } // close file

Parameters:
f Already opened FILE pointer
outbuf Reference to a local character pointer. Make sure the value held at the address is initialized to NULL.
Returns:
Dynamically allocated pointer to the data at this line. Subsequent calls to file_read_line will free this data, so don't modify the pointer's value (this does not mean that you cannot modify the memory at this address, of course).

LIBGIFT_EXPORT struct dirent* file_readdir DIR *  dh  ) 
 

Portable readdir.

LIBGIFT_EXPORT BOOL file_rmdir const char *  path  ) 
 

Identical to rm -rf.

Returns:
Boolean success or failure.

LIBGIFT_EXPORT char* file_secure_path const char *  path  ) 
 

Secure a path by stripping '.' and '..' (and /.+/ on Windows) and failing if the path does not begin with a slash.

Returns:
Duplicated memory containing the new secure path.

LIBGIFT_EXPORT BOOL file_slurp const char *  path,
char **  data,
unsigned long *  len
 

Slurp in a file.

This function creates a massive linear character array. Very unwise unless you really know what you're doing.

Parameters:
path Location of the file on disk you wish to slurp.
data Memory location to store the allocated array.
len Memory location to store the allocation size.
Return values:
TRUE File was slurped.
FALSE Failed to open path or allocate data.

LIBGIFT_EXPORT BOOL file_stat const char *  file,
struct stat *  stbuf
 

Wrapper around stat to catch stupid errors.

Returns:
Indicates error conditions using FALSE, instead of negative values.

LIBGIFT_EXPORT FILE* file_temp char **  out,
char *  module
 

Unused.

LIBGIFT_EXPORT char* file_unix_path const char *  host_path  ) 
 

Converts a host specific path to a UNIX-style path.

For example, on Windows C: Files becomes /C/Program Files/giFT.

Returns:
UNIX style path.

LIBGIFT_EXPORT int file_unlink const char *  path  ) 
 

Wrapper for unlink.


Generated on Sun Aug 22 08:07:10 2004 by doxygen 1.3.7