GLYLIB  0.3.0b
gly_fileutils.h
Go to the documentation of this file.
00001 /** \file  gly_fileutils.h
00002 \brief header file that loads utilities related to file I/O
00003 */
00004 #if !defined(GLYLIB_FILE_UTILITIES)
00005 #define GLYLIB_FILE_UTILITIES
00006 
00007 #include <sys/types.h> ///< Required for directory manipulations
00008 #include <dirent.h> ///< Required for directory manipulations
00009 #include <unistd.h> ///< Required to get the current working directory
00010 
00011 /** \addtogroup FILE_UTILS 
00012  * @{*/
00013 
00014 /****************  STRUCTURES *********************/
00015 /** A structure to hold sets of keywords and values that have been,
00016 for example, read from a file.  */
00017 typedef struct {
00018         int n; ///< The number of keys and values in this set
00019         char **K; ///< An array of n keys
00020         char **V; ///< An array of n values
00021 } gly_keysvals;
00022 
00023 typedef struct {
00024         char *N; /// the name of the file -- use strdup to allocate/copy
00025         FILE *F; /// the file pointer
00026 } fileset;
00027 /**< A structure for ease in passing sets of file info between functions */
00028 
00029 /// A structure for a function similar to the perl notion of "slurping" a file
00030 typedef struct {
00031         int n; /// number of lines
00032         char **L; /// each line
00033 } fileslurp; 
00034 
00035 /** List of files and directories generated for each state.  */
00036 //typedef struct{
00037         //char *N; ///< Name of this directory
00038         //int nF; ///< Number of plain files in the directory
00039         //char **F; ///< Names of the nF files
00040         //int *iF; ///< nF utility integers (mark for deletion or saving, etc.)
00041         //int nD; ///< Number of sub-directories
00042         //glylib_directory *D; ///< nD structures for the sub-directories
00043         //int *iD; ///< nD utility integers
00044 //} glylib_directory;
00045 
00046 
00047 
00048 /****************  FUNCTIONS *********************/
00049 
00050 /** Opens a file with error checking. 
00051 This function will open a file and then check to see if the file actually opened.  
00052 If not, it will write a descriptive error message and exit(1) the program. 
00053 Doing this saves many, many core dumps. */
00054 FILE *myfopen(const char* Name,const char* option);///< Opens a file with error checking.
00055 //char glyopendir(char *pathToDir);
00056 /** Like myfopen, but the freopen version. */
00057 FILE *myfreopen(const char* Name,const char*,FILE* option); ///< Like myfopen, but the freopen version.
00058 
00059 /** Complains (EEK!!!) if a file read fails.  Prints the following: "
00060 
00061 Unexpected surprise during read of file FileDesc.
00062 
00063 Exiting.
00064 
00065 "*/
00066 void  read_eek(const char *surprise, const char *FileDesc); ///< Complains (EEK!!!) if a file read fails.
00067 
00068  /** Complains (EEK!!!) if a file read fails. Prints the following: "
00069 
00070 " */
00071 void read_neek(const char *surprise,int line,int field); ///< Complains (EEK!!!) if a file read fails.
00072 
00073 /** Complains (EEK!!!) if a file read fails. */
00074 void read_fneek(const char *surprise, int line, int field, const char *FILENAME); ///< Complains (EEK!!!) if a file read fails.
00075 
00076 /** Returns the number of times the string str is found in file F */
00077 int sscan_file(FILE* F,const char* str); ///< Scans a file for instances of a string.
00078 
00079 /** Returns the number of times the character c is found in file F */
00080 int cscan_file(FILE* F,char c); 
00081 
00082 /** Slurps in the contents of an entire file or directory */
00083 fileslurp slurp_file(fileset F); 
00084 //fileslurp slurp_directory(char *pathToDir);
00085 
00086 /** Determines the current working directory. */
00087 const char *gly_get_current_working_directory(void);
00088 
00089 /** Reads in keyword/value pairs from a file, drops them in a
00090 structure designed for the purpose.
00091 
00092 Some notes: 
00093         - If ignore_hash is 0, everything from a hash mark (#) to the end of the line is ignored.
00094         - Any line not containing the separator SEP is ignored.
00095         - Keywords and values may not be separated by a newline.
00096         - If newline_sep is set to zero, everything* from the start of the line to the first instance 
00097                 of SEP will be regarded as the KEYWORD and everything* from the first instance of SEP
00098                 to the end of the line will be regarded as the VALUE. *Intial and final whitespace will 
00099                 be removed from both keyword and value, but all else, including internal whitespace,
00100                 will be retained.
00101         - If newline_sep is not set to zero, neither keywords nor values may contain whitespace.
00102         - SEP should be passed as a string complete with a null terminator.
00103 */
00104 
00105 gly_keysvals get_keysvals_from_slurp(fileslurp F, const char *SEP, int newline_sep, int ignore_hash);
00106 
00107 /** This is like get_keysvals_from_slurp (see) except it uses a 
00108         file and not a fileslurp. 
00109 
00110 In fact, this function merely slurps in your file and then calls get_keysvals_from_slurp 
00111 */
00112 gly_keysvals get_keysvals_from_file(fileset F, const char *SEP, int newline_sep, int ignore_hash);
00113 /*@}*/
00114 
00115 /*
00116    Deallocation routines
00117 */
00118 void deallocateFileslurp(fileslurp *F);
00119 
00120 #endif
 All Classes Files Functions Variables Typedefs Defines