1//========================================================================
2//
3// gfile.h
4//
5// Miscellaneous file and directory name manipulation.
6//
7// Copyright 1996-2003 Glyph & Cog, LLC
8//
9//========================================================================
10
11//========================================================================
12//
13// Modified under the Poppler project - http://poppler.freedesktop.org
14//
15// All changes made under the Poppler project to this file are licensed
16// under GPL version 2 or later
17//
18// Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
19// Copyright (C) 2009, 2011, 2012, 2017, 2018, 2021, 2022 Albert Astals Cid <aacid@kde.org>
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>
25// Copyright (C) 2017 Christoph Cullmann <cullmann@kde.org>
26// Copyright (C) 2017 Thomas Freitag <Thomas.Freitag@alfa.de>
27// Copyright (C) 2018 Mojca Miklavec <mojca@macports.org>
28// Copyright (C) 2019, 2021 Christian Persch <chpe@src.gnome.org>
29//
30// To see a description of the changes please see the Changelog file that
31// came with your tarball or type make ChangeLog if you are building from git
32//
33//========================================================================
34
35#ifndef GFILE_H
36#define GFILE_H
37
38#include "poppler-config.h"
39#include "poppler_private_export.h"
40#include <cstdio>
41#include <cstdlib>
42#include <cstddef>
43#include <ctime>
44#include <string>
45extern "C" {
46#if defined(_WIN32)
47# include <sys/stat.h>
48# ifdef FPTEX
49# include <win32lib.h>
50# else
51# ifndef NOMINMAX
52# define NOMINMAX
53# endif
54# include <windows.h>
55# endif
56#else
57# include <unistd.h>
58# include <sys/types.h>
59# if defined(HAVE_DIRENT_H)
60# include <dirent.h>
61# define NAMLEN(d) strlen((d)->d_name)
62# else
63# define dirent direct
64# define NAMLEN(d) (d)->d_namlen
65# ifdef HAVE_SYS_NDIR_H
66# include <sys/ndir.h>
67# endif
68# ifdef HAVE_SYS_DIR_H
69# include <sys/dir.h>
70# endif
71# ifdef HAVE_NDIR_H
72# include <ndir.h>
73# endif
74# endif
75#endif
76}
77
78#include <memory>
79
80class GooString;
81
82/* Integer type for all file offsets and file sizes */
83typedef long long Goffset;
84
85//------------------------------------------------------------------------
86
87// Append a file name to a path string. <path> may be an empty
88// string, denoting the current directory). Returns <path>.
89extern GooString POPPLER_PRIVATE_EXPORT *appendToPath(GooString *path, const char *fileName);
90
91#ifndef _WIN32
92// Open a file descriptor
93// Could be implemented on WIN32 too, but the only external caller of
94// this function is not used on WIN32
95extern int POPPLER_PRIVATE_EXPORT openFileDescriptor(const char *path, int flags);
96#endif
97
98// Open a file. On Windows, this converts the path from UTF-8 to
99// UCS-2 and calls _wfopen (if available). On other OSes, this simply
100// calls fopen.
101extern FILE POPPLER_PRIVATE_EXPORT *openFile(const char *path, const char *mode);
102
103// Just like fgets, but handles Unix, Mac, and/or DOS end-of-line
104// conventions.
105extern char POPPLER_PRIVATE_EXPORT *getLine(char *buf, int size, FILE *f);
106
107// Like fseek/ftell but uses platform specific variants that support large files
108extern int POPPLER_PRIVATE_EXPORT Gfseek(FILE *f, Goffset offset, int whence);
109extern Goffset POPPLER_PRIVATE_EXPORT Gftell(FILE *f);
110
111// Largest offset supported by Gfseek/Gftell
112extern Goffset GoffsetMax();
113
114//------------------------------------------------------------------------
115// GooFile
116//------------------------------------------------------------------------
117
118class POPPLER_PRIVATE_EXPORT GooFile
119{
120public:
121 GooFile(const GooFile &) = delete;
122 GooFile &operator=(const GooFile &other) = delete;
123
124 int read(char *buf, int n, Goffset offset) const;
125 Goffset size() const;
126
127 static std::unique_ptr<GooFile> open(const std::string &fileName);
128#ifndef _WIN32
129 static std::unique_ptr<GooFile> open(int fdA);
130#endif
131
132#ifdef _WIN32
133 static std::unique_ptr<GooFile> open(const wchar_t *fileName);
134
135 ~GooFile() { CloseHandle(handle); }
136
137 // Asuming than on windows you can't change files that are already open
138 bool modificationTimeChangedSinceOpen() const;
139
140private:
141 GooFile(HANDLE handleA);
142 HANDLE handle;
143 struct _FILETIME modifiedTimeOnOpen;
144#else
145 ~GooFile() { close(fd: fd); }
146
147 bool modificationTimeChangedSinceOpen() const;
148
149private:
150 explicit GooFile(int fdA);
151 int fd;
152 struct timespec modifiedTimeOnOpen;
153#endif // _WIN32
154};
155
156#endif
157

source code of poppler/goo/gfile.h