Back to index

tetex-bin  3.0
gfile.h
Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // gfile.h
00004 //
00005 // Miscellaneous file and directory name manipulation.
00006 //
00007 // Copyright 1996-2003 Glyph & Cog, LLC
00008 //
00009 //========================================================================
00010 
00011 #ifndef GFILE_H
00012 #define GFILE_H
00013 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <stddef.h>
00017 #if defined(WIN32)
00018 #  include <sys/stat.h>
00019 #  ifdef FPTEX
00020 #    include <win32lib.h>
00021 #  else
00022 #    include <windows.h>
00023 #  endif
00024 #elif defined(ACORN)
00025 #elif defined(MACOS)
00026 #  include <ctime.h>
00027 #else
00028 #  include <unistd.h>
00029 #  include <sys/types.h>
00030 #  ifdef VMS
00031 #    include "vms_dirent.h"
00032 #  elif HAVE_DIRENT_H
00033 #    include <dirent.h>
00034 #    define NAMLEN(d) strlen((d)->d_name)
00035 #  else
00036 #    define dirent direct
00037 #    define NAMLEN(d) (d)->d_namlen
00038 #    if HAVE_SYS_NDIR_H
00039 #      include <sys/ndir.h>
00040 #    endif
00041 #    if HAVE_SYS_DIR_H
00042 #      include <sys/dir.h>
00043 #    endif
00044 #    if HAVE_NDIR_H
00045 #      include <ndir.h>
00046 #    endif
00047 #  endif
00048 #endif
00049 #include "gtypes.h"
00050 
00051 class GString;
00052 
00053 //------------------------------------------------------------------------
00054 
00055 // Get home directory path.
00056 extern GString *getHomeDir();
00057 
00058 // Get current directory.
00059 extern GString *getCurrentDir();
00060 
00061 // Append a file name to a path string.  <path> may be an empty
00062 // string, denoting the current directory).  Returns <path>.
00063 extern GString *appendToPath(GString *path, char *fileName);
00064 
00065 // Grab the path from the front of the file name.  If there is no
00066 // directory component in <fileName>, returns an empty string.
00067 extern GString *grabPath(char *fileName);
00068 
00069 // Is this an absolute path or file name?
00070 extern GBool isAbsolutePath(char *path);
00071 
00072 // Make this path absolute by prepending current directory (if path is
00073 // relative) or prepending user's directory (if path starts with '~').
00074 extern GString *makePathAbsolute(GString *path);
00075 
00076 // Get the modification time for <fileName>.  Returns 0 if there is an
00077 // error.
00078 extern time_t getModTime(char *fileName);
00079 
00080 // Create a temporary file and open it for writing.  If <ext> is not
00081 // NULL, it will be used as the file name extension.  Returns both the
00082 // name and the file pointer.  For security reasons, all writing
00083 // should be done to the returned file pointer; the file may be
00084 // reopened later for reading, but not for writing.  The <mode> string
00085 // should be "w" or "wb".  Returns true on success.
00086 extern GBool openTempFile(GString **name, FILE **f, char *mode, char *ext);
00087 
00088 // Execute <command>.  Returns true on success.
00089 extern GBool executeCommand(char *cmd);
00090 
00091 // Just like fgets, but handles Unix, Mac, and/or DOS end-of-line
00092 // conventions.
00093 extern char *getLine(char *buf, int size, FILE *f);
00094 
00095 //------------------------------------------------------------------------
00096 // GDir and GDirEntry
00097 //------------------------------------------------------------------------
00098 
00099 class GDirEntry {
00100 public:
00101 
00102   GDirEntry(char *dirPath, char *nameA, GBool doStat);
00103   ~GDirEntry();
00104   GString *getName() { return name; }
00105   GBool isDir() { return dir; }
00106 
00107 private:
00108 
00109   GString *name;            // dir/file name
00110   GBool dir;                // is it a directory?
00111 };
00112 
00113 class GDir {
00114 public:
00115 
00116   GDir(char *name, GBool doStatA = gTrue);
00117   ~GDir();
00118   GDirEntry *getNextEntry();
00119   void rewind();
00120 
00121 private:
00122 
00123   GString *path;            // directory path
00124   GBool doStat;                    // call stat() for each entry?
00125 #if defined(WIN32)
00126   WIN32_FIND_DATA ffd;
00127   HANDLE hnd;
00128 #elif defined(ACORN)
00129 #elif defined(MACOS)
00130 #else
00131   DIR *dir;                 // the DIR structure from opendir()
00132 #ifdef VMS
00133   GBool needParent;         // need to return an entry for [-]
00134 #endif
00135 #endif
00136 };
00137 
00138 #endif