Skip to content

Commit

Permalink
Remove gdir, we have std::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
svuorela authored and tsdgeos committed Jan 19, 2025
1 parent 7d9b708 commit b50390f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 261 deletions.
96 changes: 0 additions & 96 deletions goo/gdir.h

This file was deleted.

104 changes: 0 additions & 104 deletions goo/gfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#include <limits>
#include "GooString.h"
#include "gfile.h"
#include "gdir.h"

// Some systems don't define this, so just make it something reasonably
// large.
Expand Down Expand Up @@ -434,106 +433,3 @@ bool GooFile::modificationTimeChangedSinceOpen() const
}

#endif // _WIN32

//------------------------------------------------------------------------
// GDir and GDirEntry
//------------------------------------------------------------------------

GDirEntry::GDirEntry(const char *dirPath, const char *nameA, bool doStat)
{
#ifdef _WIN32
DWORD fa;
#else
struct stat st;
#endif

name = new GooString(nameA);
dir = false;
fullPath = new GooString(dirPath);
appendToPath(fullPath, nameA);
if (doStat) {
#ifdef _WIN32
fa = GetFileAttributesA(fullPath->c_str());
dir = (fa != 0xFFFFFFFF && (fa & FILE_ATTRIBUTE_DIRECTORY));
#else
if (stat(fullPath->c_str(), &st) == 0) {
dir = S_ISDIR(st.st_mode);
}
#endif
}
}

GDirEntry::~GDirEntry()
{
delete fullPath;
delete name;
}

GDir::GDir(const char *name, bool doStatA)
{
path = new GooString(name);
doStat = doStatA;
#ifdef _WIN32
std::unique_ptr<GooString> tmp = path->copy();
tmp->append("/*.*");
hnd = FindFirstFileA(tmp->c_str(), &ffd);
#else
dir = opendir(name);
#endif
}

GDir::~GDir()
{
delete path;
#ifdef _WIN32
if (hnd != INVALID_HANDLE_VALUE) {
FindClose(hnd);
hnd = INVALID_HANDLE_VALUE;
}
#else
if (dir) {
closedir(dir);
}
#endif
}

std::unique_ptr<GDirEntry> GDir::getNextEntry()
{
#ifdef _WIN32
if (hnd != INVALID_HANDLE_VALUE) {
auto e = std::make_unique<GDirEntry>(path->c_str(), ffd.cFileName, doStat);
if (!FindNextFileA(hnd, &ffd)) {
FindClose(hnd);
hnd = INVALID_HANDLE_VALUE;
}
return e;
}
#else
struct dirent *ent;
if (dir) {
do {
ent = readdir(dir);
} while (ent && (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")));
if (ent) {
return std::make_unique<GDirEntry>(path->c_str(), ent->d_name, doStat);
}
}
#endif

return {};
}

void GDir::rewind()
{
#ifdef _WIN32
if (hnd != INVALID_HANDLE_VALUE)
FindClose(hnd);
std::unique_ptr<GooString> tmp = path->copy();
tmp->append("/*.*");
hnd = FindFirstFileA(tmp->c_str(), &ffd);
#else
if (dir) {
rewinddir(dir);
}
#endif
}
74 changes: 27 additions & 47 deletions poppler/GlobalParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include <cstring>
#include <cstdio>
#include <cctype>
#include <filesystem>
#ifdef _WIN32
# include <shlobj.h>
# include <mbstring.h>
Expand All @@ -71,10 +72,8 @@
# include <android/system_fonts.h>
#endif
#include "goo/glibc.h"
#include "goo/gmem.h"
#include "goo/GooString.h"
#include "goo/gfile.h"
#include "goo/gdir.h"
#include "Error.h"
#include "NameToCharCode.h"
#include "CharCodeToUnicode.h"
Expand Down Expand Up @@ -409,7 +408,7 @@ const SysFontInfo *SysFontList::find(const std::string &name, bool fixedWidth, b
// parsing
//------------------------------------------------------------------------

GlobalParams::GlobalParams(const char *customPopplerDataDir) : popplerDataDir(customPopplerDataDir)
GlobalParams::GlobalParams(const std::string &customPopplerDataDir) : popplerDataDir(customPopplerDataDir)
{
// scan the encoding in reverse because we want the lowest-numbered
// index for each char name ('space' is encoded twice)
Expand Down Expand Up @@ -466,49 +465,30 @@ GlobalParams::GlobalParams(const char *customPopplerDataDir) : popplerDataDir(cu

void GlobalParams::scanEncodingDirs()
{
GDir *dir;
std::unique_ptr<GDirEntry> entry;
const char *dataRoot = popplerDataDir ? popplerDataDir : POPPLER_DATADIR;
std::string dataRoot = !popplerDataDir.empty() ? popplerDataDir : std::string { POPPLER_DATADIR };

// allocate buffer large enough to append "/nameToUnicode"
size_t bufSize = strlen(dataRoot) + strlen("/nameToUnicode") + 1;
char *dataPathBuffer = new char[bufSize];

snprintf(dataPathBuffer, bufSize, "%s/nameToUnicode", dataRoot);
dir = new GDir(dataPathBuffer, true);
while (entry = dir->getNextEntry(), entry != nullptr) {
if (!entry->isDir()) {
parseNameToUnicode(entry->getFullPath());
std::error_code ec; // if ec is set, we also get the end iterator, so that's kind of okay. If not creating with a error code, we get an exception if poppler data is missing
for (const auto &entry : std::filesystem::directory_iterator { dataRoot + "/nameToUnicode", ec }) {
if (entry.is_regular_file()) {
parseNameToUnicode(entry.path());
}
}
delete dir;

snprintf(dataPathBuffer, bufSize, "%s/cidToUnicode", dataRoot);
dir = new GDir(dataPathBuffer, false);
while (entry = dir->getNextEntry(), entry != nullptr) {
addCIDToUnicode(entry->getName(), entry->getFullPath());
for (const auto &entry : std::filesystem::directory_iterator { dataRoot + "/cidToUnicode", ec }) {
addCIDToUnicode(entry.path().filename().string(), entry.path().string());
}
delete dir;

snprintf(dataPathBuffer, bufSize, "%s/unicodeMap", dataRoot);
dir = new GDir(dataPathBuffer, false);
while (entry = dir->getNextEntry(), entry != nullptr) {
addUnicodeMap(entry->getName(), entry->getFullPath());
for (const auto &entry : std::filesystem::directory_iterator { dataRoot + "/unicodeMap", ec }) {
addUnicodeMap(entry.path().filename().string(), entry.path().string());
}
delete dir;

snprintf(dataPathBuffer, bufSize, "%s/cMap", dataRoot);
dir = new GDir(dataPathBuffer, false);
while (entry = dir->getNextEntry(), entry != nullptr) {
addCMapDir(entry->getName(), entry->getFullPath());
toUnicodeDirs.push_back(entry->getFullPath()->copy());
for (const auto &entry : std::filesystem::directory_iterator { dataRoot + "/cMap", ec }) {
addCMapDir(entry.path().filename().string(), entry.path().string());
toUnicodeDirs.push_back(entry.path().string());
}
delete dir;

delete[] dataPathBuffer;
}

void GlobalParams::parseNameToUnicode(const GooString *name)
void GlobalParams::parseNameToUnicode(const std::filesystem::path &name)
{
char *tok1, *tok2;
FILE *f;
Expand All @@ -517,8 +497,8 @@ void GlobalParams::parseNameToUnicode(const GooString *name)
Unicode u;
char *tokptr;

if (!(f = openFile(name->c_str(), "r"))) {
error(errIO, -1, "Couldn't open 'nameToUnicode' file '{0:t}'", name);
if (!(f = openFile(name.string().c_str(), "r"))) {
error(errIO, -1, "Couldn't open 'nameToUnicode' file '{0:s}'", name.string().c_str());
return;
}
line = 1;
Expand All @@ -529,26 +509,26 @@ void GlobalParams::parseNameToUnicode(const GooString *name)
sscanf(tok1, "%x", &u);
nameToUnicodeText->add(tok2, u);
} else {
error(errConfig, -1, "Bad line in 'nameToUnicode' file ({0:t}:{1:d})", name, line);
error(errConfig, -1, "Bad line in 'nameToUnicode' file ({0:s}:{1:d})", name.string().c_str(), line);
}
++line;
}
fclose(f);
}

void GlobalParams::addCIDToUnicode(const GooString *collection, const GooString *fileName)
void GlobalParams::addCIDToUnicode(std::string &&collection, std::string &&fileName)
{
cidToUnicodes[collection->toStr()] = fileName->toStr();
cidToUnicodes[collection] = fileName;
}

void GlobalParams::addUnicodeMap(const GooString *encodingName, const GooString *fileName)
void GlobalParams::addUnicodeMap(std::string &&encodingName, std::string &&fileName)
{
unicodeMaps[encodingName->toStr()] = fileName->toStr();
unicodeMaps[encodingName] = fileName;
}

void GlobalParams::addCMapDir(const GooString *collection, const GooString *dir)
void GlobalParams::addCMapDir(std::string &&collection, std::string &&dir)
{
cMapDirs.emplace(collection->toStr(), dir->toStr());
cMapDirs.emplace(collection, dir);
}

bool GlobalParams::parseYesNo2(const char *token, bool *flag)
Expand Down Expand Up @@ -653,8 +633,8 @@ FILE *GlobalParams::findToUnicodeFile(const GooString *name)
FILE *f;

globalParamsLocker();
for (const std::unique_ptr<GooString> &dir : toUnicodeDirs) {
fileName = appendToPath(dir->copy().release(), name->c_str());
for (const std::string &dir : toUnicodeDirs) {
fileName = appendToPath(new GooString(dir), name->c_str());
f = openFile(fileName->c_str(), "r");
delete fileName;
if (f) {
Expand Down Expand Up @@ -1561,7 +1541,7 @@ GlobalParamsIniter::GlobalParamsIniter(ErrorCallback errorCallback)
const std::scoped_lock lock { mutex };

if (count == 0) {
globalParams = std::make_unique<GlobalParams>(!customDataDir.empty() ? customDataDir.c_str() : nullptr);
globalParams = std::make_unique<GlobalParams>(customDataDir);

setErrorCallback(errorCallback);
}
Expand Down
Loading

0 comments on commit b50390f

Please sign in to comment.