gfile.h 6.17 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1 2 3 4 5 6 7 8 9 10
//========================================================================
//
// gfile.h
//
// Miscellaneous file and directory name manipulation.
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================

11 12 13 14
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
15 16 17
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
18
// Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
19
// Copyright (C) 2009, 2011, 2012, 2017, 2018 Albert Astals Cid <aacid@kde.org>
Kovid Goyal's avatar
Kovid Goyal committed
20
// Copyright (C) 2009 Kovid Goyal <kovid@kovidgoyal.net>
21
// Copyright (C) 2013 Adam Reichold <adamreichold@myopera.com>
22
// Copyright (C) 2013, 2017 Adrian Johnson <ajohnson@redneon.com>
23
// Copyright (C) 2014 Bogdan Cristea <cristeab@gmail.com>
24
// Copyright (C) 2014 Peter Breitenlohner <peb@mppmu.mpg.de>
Albert Astals Cid's avatar
Albert Astals Cid committed
25
// Copyright (C) 2017 Christoph Cullmann <cullmann@kde.org>
26
// Copyright (C) 2017 Thomas Freitag <Thomas.Freitag@alfa.de>
Mojca Miklavec's avatar
Mojca Miklavec committed
27
// Copyright (C) 2018 Mojca Miklavec <mojca@macports.org>
28 29 30 31 32 33
//
// 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
//
//========================================================================

Kristian Høgsberg's avatar
Kristian Høgsberg committed
34 35 36
#ifndef GFILE_H
#define GFILE_H

37
#include "poppler-config.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
38 39 40
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
Mojca Miklavec's avatar
Mojca Miklavec committed
41
#include <time.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
42
extern "C" {
Kovid Goyal's avatar
Kovid Goyal committed
43
#if defined(_WIN32)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
44 45 46 47
#  include <sys/stat.h>
#  ifdef FPTEX
#    include <win32lib.h>
#  else
48 49 50
#    ifndef NOMINMAX
#      define NOMINMAX
#    endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
51 52 53 54 55 56 57 58
#    include <windows.h>
#  endif
#elif defined(ACORN)
#elif defined(MACOS)
#  include <ctime.h>
#else
#  include <unistd.h>
#  include <sys/types.h>
59
#  if defined(VMS)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
60
#    include "vms_dirent.h"
61
#  elif defined(HAVE_DIRENT_H)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
62 63 64 65 66
#    include <dirent.h>
#    define NAMLEN(d) strlen((d)->d_name)
#  else
#    define dirent direct
#    define NAMLEN(d) (d)->d_namlen
67
#    ifdef HAVE_SYS_NDIR_H
Kristian Høgsberg's avatar
Kristian Høgsberg committed
68 69
#      include <sys/ndir.h>
#    endif
70
#    ifdef HAVE_SYS_DIR_H
Kristian Høgsberg's avatar
Kristian Høgsberg committed
71 72
#      include <sys/dir.h>
#    endif
73
#    ifdef HAVE_NDIR_H
Kristian Høgsberg's avatar
Kristian Høgsberg committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#      include <ndir.h>
#    endif
#  endif
#endif
}
#include "gtypes.h"

class GooString;

//------------------------------------------------------------------------

// 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>.
90
extern GooString *appendToPath(GooString *path, const char *fileName);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

// 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);

// 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.
109
extern GBool openTempFile(GooString **name, FILE **f, const char *mode);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
110

111
#ifdef _WIN32
112 113 114 115 116 117 118 119 120 121 122 123
// Convert a file name from Latin-1 to UTF-8.
extern GooString *fileNameToUTF8(char *path);

// Convert a file name from UCS-2 to UTF-8.
extern GooString *fileNameToUTF8(wchar_t *path);
#endif

// Open a file.  On Windows, this converts the path from UTF-8 to
// UCS-2 and calls _wfopen (if available).  On other OSes, this simply
// calls fopen.
extern FILE *openFile(const char *path, const char *mode);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
124 125 126 127
// Just like fgets, but handles Unix, Mac, and/or DOS end-of-line
// conventions.
extern char *getLine(char *buf, int size, FILE *f);

128 129 130 131 132 133 134
// Like fseek/ftell but uses platform specific variants that support large files
extern int Gfseek(FILE *f, Goffset offset, int whence);
extern Goffset Gftell(FILE *f);

// Largest offset supported by Gfseek/Gftell
extern Goffset GoffsetMax();

135 136 137 138 139 140 141
//------------------------------------------------------------------------
// GooFile
//------------------------------------------------------------------------

class GooFile
{
public:
142 143 144
  GooFile(const GooFile &) = delete;
  GooFile& operator=(const GooFile &other) = delete;

145 146 147 148 149 150 151 152 153
  int read(char *buf, int n, Goffset offset) const;
  Goffset size() const;
  
  static GooFile *open(const GooString *fileName);
  
#ifdef _WIN32
  static GooFile *open(const wchar_t *fileName);
  
  ~GooFile() { CloseHandle(handle); }
154 155

  // Asuming than on windows you can't change files that are already open
156
  bool modificationTimeChangedSinceOpen() const;
157 158
  
private:
159
  GooFile(HANDLE handleA);
160
  HANDLE handle;
161
  struct _FILETIME modifiedTimeOnOpen;
162 163
#else
  ~GooFile() { close(fd); }
164 165

  bool modificationTimeChangedSinceOpen() const;
166 167
    
private:
168
  GooFile(int fdA);
169
  int fd;
170
  struct timespec modifiedTimeOnOpen;
171 172 173
#endif // _WIN32
};

Kristian Høgsberg's avatar
Kristian Høgsberg committed
174 175 176 177 178 179 180 181 182 183
//------------------------------------------------------------------------
// GDir and GDirEntry
//------------------------------------------------------------------------

class GDirEntry {
public:

  GDirEntry(char *dirPath, char *nameA, GBool doStat);
  ~GDirEntry();
  GooString *getName() { return name; }
184
  GooString *getFullPath() { return fullPath; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
185 186 187
  GBool isDir() { return dir; }

private:
188 189
  GDirEntry(const GDirEntry &other);
  GDirEntry& operator=(const GDirEntry &other);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
190 191

  GooString *name;		// dir/file name
192
  GooString *fullPath;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
193 194 195 196 197 198 199 200 201 202 203 204
  GBool dir;			// is it a directory?
};

class GDir {
public:

  GDir(char *name, GBool doStatA = gTrue);
  ~GDir();
  GDirEntry *getNextEntry();
  void rewind();

private:
205 206
  GDir(const GDir &other);
  GDir& operator=(const GDir &other);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
207 208 209

  GooString *path;		// directory path
  GBool doStat;			// call stat() for each entry?
Kovid Goyal's avatar
Kovid Goyal committed
210
#if defined(_WIN32)
211
  WIN32_FIND_DATAA ffd;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
212 213 214 215 216 217 218 219 220 221 222 223
  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