blob: ce5056240027b69cd7b0f9b982e4222fb9ecaa3d [file] [log] [blame]
// gfile.h
// Miscellaneous file and directory name manipulation.
// Copyright 1996-2003 Glyph & Cog, LLC
#ifndef GFILE_H
#define GFILE_H
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
extern "C" {
#if defined(WIN32)
# include <sys/stat.h>
# ifdef FPTEX
# include <win32lib.h>
# else
# include <windows.h>
# endif
#elif defined(ACORN)
#elif defined(MACOS)
# include <ctime.h>
# include <unistd.h>
# include <sys/types.h>
# ifdef VMS
# include "vms_dirent.h"
# include <dirent.h>
# define NAMLEN(d) strlen((d)->d_name)
# else
# define dirent direct
# define NAMLEN(d) (d)->d_namlen
# include <sys/ndir.h>
# endif
# include <sys/dir.h>
# endif
# include <ndir.h>
# endif
# endif
#include "gtypes.h"
class GooString;
// Get home directory path.
extern GooString *getHomeDir();
// Get current directory.
extern GooString *getCurrentDir();
// Append a file name to a path string. <path> may be an empty
// string, denoting the current directory). Returns <path>.
extern GooString *appendToPath(GooString *path, char *fileName);
// Grab the path from the front of the file name. If there is no
// directory component in <fileName>, returns an empty string.
extern GooString *grabPath(char *fileName);
// Is this an absolute path or file name?
extern GBool isAbsolutePath(char *path);
// Make this path absolute by prepending current directory (if path is
// relative) or prepending user's directory (if path starts with '~').
extern GooString *makePathAbsolute(GooString *path);
// Get the modification time for <fileName>. Returns 0 if there is an
// error.
extern time_t getModTime(char *fileName);
// Create a temporary file and open it for writing. If <ext> is not
// NULL, it will be used as the file name extension. Returns both the
// name and the file pointer. For security reasons, all writing
// should be done to the returned file pointer; the file may be
// reopened later for reading, but not for writing. The <mode> string
// should be "w" or "wb". Returns true on success.
extern GBool openTempFile(GooString **name, FILE **f, char *mode, char *ext);
// Execute <command>. Returns true on success.
extern GBool executeCommand(char *cmd);
// Just like fgets, but handles Unix, Mac, and/or DOS end-of-line
// conventions.
extern char *getLine(char *buf, int size, FILE *f);
// GDir and GDirEntry
class GDirEntry {
GDirEntry(char *dirPath, char *nameA, GBool doStat);
GooString *getName() { return name; }
GooString *getFullPath() { return fullPath; }
GBool isDir() { return dir; }
GooString *name; // dir/file name
GooString *fullPath;
GBool dir; // is it a directory?
class GDir {
GDir(char *name, GBool doStatA = gTrue);
GDirEntry *getNextEntry();
void rewind();
GooString *path; // directory path
GBool doStat; // call stat() for each entry?
#if defined(WIN32)
#elif defined(ACORN)
#elif defined(MACOS)
DIR *dir; // the DIR structure from opendir()
#ifdef VMS
GBool needParent; // need to return an entry for [-]