filters

gfile.h
1//========================================================================
2//
3// gfile.h
4//
5// Miscellaneous file and directory name manipulation.
6//
7// Copyright 1996-2002 Glyph & Cog, LLC
8//
9//========================================================================
10
11#ifndef GFILE_H
12#define GFILE_H
13
14#include <aconf.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <stddef.h>
18#if defined(WIN32)
19# include <sys/stat.h>
20# ifdef FPTEX
21# include <win32lib.h>
22# else
23# include <windows.h>
24# endif
25#elif defined(ACORN)
26#elif defined(MACOS)
27# include <ctime.h>
28#else
29# include <unistd.h>
30# include <sys/types.h>
31# ifdef VMS
32# include "vms_dirent.h"
33# elif HAVE_DIRENT_H
34# include <dirent.h>
35# define NAMLEN(d) strlen((d)->d_name)
36# else
37# define dirent direct
38# define NAMLEN(d) (d)->d_namlen
39# if HAVE_SYS_NDIR_H
40# include <sys/ndir.h>
41# endif
42# if HAVE_SYS_DIR_H
43# include <sys/dir.h>
44# endif
45# if HAVE_NDIR_H
46# include <ndir.h>
47# endif
48# endif
49#endif
50#include "gtypes.h"
51
52class GString;
53
54//------------------------------------------------------------------------
55
56// Get home directory path.
57extern GString *getHomeDir();
58
59// Get current directory.
60extern GString *getCurrentDir();
61
62// Append a file name to a path string. <path> may be an empty
63// string, denoting the current directory). Returns <path>.
64extern GString *appendToPath(GString *path, const char *fileName);
65
66// Grab the path from the front of the file name. If there is no
67// directory component in <fileName>, returns an empty string.
68extern GString *grabPath(const char *fileName);
69
70// Is this an absolute path or file name?
71extern GBool isAbsolutePath(const char *path);
72
73// Make this path absolute by prepending current directory (if path is
74// relative) or prepending user's directory (if path starts with '~').
75extern GString *makePathAbsolute(GString *path);
76
77// Get the modification time for <fileName>. Returns 0 if there is an
78// error.
79extern time_t getModTime(const char *fileName);
80
81// Create a temporary file and open it for writing. If <ext> is not
82// NULL, it will be used as the file name extension. Returns both the
83// name and the file pointer. For security reasons, all writing
84// should be done to the returned file pointer; the file may be
85// reopened later for reading, but not for writing. The <mode> string
86// should be "w" or "wb". Returns true on success.
87extern GBool openTempFile(GString **name, FILE **f, const char *mode);//, char *ext);
88
89// Execute <command>. Returns true on success.
90extern GBool executeCommand(const char *cmd);
91
92// Just like fgets, but handles Unix, Mac, and/or DOS end-of-line
93// conventions.
94extern char *getLine(char *buf, int size, FILE *f);
95
96//------------------------------------------------------------------------
97// GDir and GDirEntry
98//------------------------------------------------------------------------
99
100class GDirEntry {
101public:
102
103 GDirEntry(const char *dirPath, const char *nameA, GBool doStat);
104 ~GDirEntry();
105 GString *getName() { return name; }
106 GBool isDir() { return dir; }
107
108private:
109
110 GString *name; // dir/file name
111 GBool dir; // is it a directory?
112};
113
114class GDir {
115public:
116
117 GDir(const char *name, GBool doStatA = gTrue);
118 ~GDir();
119 GDirEntry *getNextEntry();
120 void rewind();
121
122private:
123
124 GString *path; // directory path
125 GBool doStat; // call stat() for each entry?
126#if defined(WIN32)
127 WIN32_FIND_DATA ffd;
128 HANDLE hnd;
129#elif defined(ACORN)
130#elif defined(MACOS)
131#else
132 DIR *dir; // the DIR structure from opendir()
133#ifdef VMS
134 GBool needParent; // need to return an entry for [-]
135#endif
136#endif
137};
138
139#endif