forked from zotero/cross-xpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfile.h
234 lines (190 loc) · 6.74 KB
/
gfile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//========================================================================
//
// gfile.h
//
// Miscellaneous file and directory name manipulation.
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2006 Kristian Høgsberg <[email protected]>
// Copyright (C) 2009, 2011, 2012 Albert Astals Cid <[email protected]>
// Copyright (C) 2009 Kovid Goyal <[email protected]>
// Copyright (C) 2013 Adam Reichold <[email protected]>
// Copyright (C) 2013, 2017 Adrian Johnson <[email protected]>
// Copyright (C) 2014 Bogdan Cristea <[email protected]>
// Copyright (C) 2014 Peter Breitenlohner <[email protected]>
// Copyright (C) 2017 Christoph Cullmann <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#ifndef GFILE_H
#define GFILE_H
#include <aconf.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#if defined(_WIN32)
# include <sys/stat.h>
# ifdef FPTEX
# include <win32lib.h>
# else
# include <windows.h>
# endif
#elif defined(ACORN)
#elif defined(ANDROID)
#else
# include <unistd.h>
# include <sys/types.h>
#include <dirent.h>
#endif
#include "gtypes.h"
class GString;
//------------------------------------------------------------------------
// Get home directory path.
extern GString *getHomeDir();
// Get current directory.
extern GString *getCurrentDir();
// Append a file name to a path string. <path> may be an empty
// string, denoting the current directory). Returns <path>.
extern GString *appendToPath(GString *path, const 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 GString *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 GString *makePathAbsolute(GString *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(GString **name, FILE **f,
const char *mode, const char *ext);
// Create a directory. Returns true on success.
extern GBool createDir(char *path, int mode);
// Execute <command>. Returns true on success.
extern GBool executeCommand(char *cmd);
#ifdef _WIN32
// Convert a file name from Latin-1 to UTF-8.
extern GString *fileNameToUTF8(char *path);
// Convert a file name from UCS-2 to UTF-8.
extern GString *fileNameToUTF8(wchar_t *path);
// Convert a file name from UTF-8 to UCS-2. [out] has space for
// [outSize] wchar_t elements (including the trailing zero). Returns
// [out].
extern wchar_t *fileNameToUCS2(const char *path, wchar_t *out, size_t outSize);
#endif
// Open a file. On Windows, this converts the path from UTF-8 to
// UCS-2 and calls _wfopen(). On other OSes, this simply calls fopen().
extern FILE *openFile(const char *path, const char *mode);
#ifdef _WIN32
// If [wPath] is a Windows shortcut (.lnk file), read the target path
// and store it back into [wPath].
extern void readWindowsShortcut(wchar_t *wPath, size_t wPathSize);
#endif
// Create a directory. On Windows, this converts the path from UTF-8
// to UCS-2 and calls _wmkdir(), ignoring the mode argument. On other
// OSes, this simply calls mkdir().
extern int makeDir(const char *path, int mode);
// Just like fgets, but handles Unix, Mac, and/or DOS end-of-line
// conventions.
extern char *getLine(char *buf, int size, FILE *f);
// Type used by gfseek/gftell for file offsets. This will be 64 bits
// on systems that support it.
#if HAVE_FSEEKO
typedef off_t GFileOffset;
#define GFILEOFFSET_MAX 0x7fffffffffffffffLL
#elif HAVE_FSEEK64
typedef long long GFileOffset;
#define GFILEOFFSET_MAX 0x7fffffffffffffffLL
#elif HAVE_FSEEKI64
typedef __int64 GFileOffset;
#define GFILEOFFSET_MAX 0x7fffffffffffffffLL
#else
typedef long GFileOffset;
#define GFILEOFFSET_MAX LONG_MAX
#endif
// Like fseek, but uses a 64-bit file offset if available.
extern int gfseek(FILE *f, GFileOffset offset, int whence);
// Like ftell, but returns a 64-bit file offset if available.
extern GFileOffset gftell(FILE *f);
// On Windows, this gets the Unicode command line and converts it to
// UTF-8. On other systems, this is a nop.
extern void fixCommandLine(int *argc, char **argv[]);
//------------------------------------------------------------------------
// GooFile
//------------------------------------------------------------------------
class GooFile
{
public:
static GooFile *open(GString *fileName);
#ifdef _WIN32
static GooFile *open(const wchar_t *fileName);
~GooFile() { CloseHandle(handle); }
private:
GooFile(HANDLE handleA): handle(handleA) {}
HANDLE handle;
#else
~GooFile() { close(fd); }
private:
GooFile(int fdA) : fd(fdA) {}
int fd;
#endif // _WIN32
};
//------------------------------------------------------------------------
// GDir and GDirEntry
//------------------------------------------------------------------------
class GDirEntry {
public:
GDirEntry(char *dirPath, char *nameA, GBool doStat);
~GDirEntry();
GString *getName() { return name; }
GString *getFullPath() { return fullPath; }
GBool isDir() { return dir; }
private:
GDirEntry(const GDirEntry &other);
GDirEntry& operator=(const GDirEntry &other);
GString *name; // dir/file name
GString *fullPath;
GBool dir; // is it a directory?
};
class GDir {
public:
GDir(char *name, GBool doStatA = gTrue);
~GDir();
GDirEntry *getNextEntry();
void rewind();
private:
GDir(const GDir &other);
GDir& operator=(const GDir &other);
GString *path; // directory path
GBool doStat; // call stat() for each entry?
#if defined(_WIN32)
WIN32_FIND_DATAA ffd;
HANDLE hnd;
#elif defined(ACORN)
#elif defined(MACOS)
#else
DIR *dir; // the DIR structure from opendir()
#ifdef VMS
GBool needParent; // need to return an entry for [-]
#endif
#endif
};
#endif