diff options
Diffstat (limited to 'Build/source/texk/dvisvgm/dvisvgm-src/src/FileFinder.cpp')
-rw-r--r-- | Build/source/texk/dvisvgm/dvisvgm-src/src/FileFinder.cpp | 155 |
1 files changed, 62 insertions, 93 deletions
diff --git a/Build/source/texk/dvisvgm/dvisvgm-src/src/FileFinder.cpp b/Build/source/texk/dvisvgm/dvisvgm-src/src/FileFinder.cpp index 8caae75275a..591da5e6fe9 100644 --- a/Build/source/texk/dvisvgm/dvisvgm-src/src/FileFinder.cpp +++ b/Build/source/texk/dvisvgm/dvisvgm-src/src/FileFinder.cpp @@ -2,7 +2,7 @@ ** FileFinder.cpp ** ** ** ** This file is part of dvisvgm -- a fast DVI to SVG converter ** -** Copyright (C) 2005-2016 Martin Gieseking <martin.gieseking@uos.de> ** +** Copyright (C) 2005-2017 Martin Gieseking <martin.gieseking@uos.de> ** ** ** ** This program is free software; you can redistribute it and/or ** ** modify it under the terms of the GNU General Public License as ** @@ -21,14 +21,12 @@ #include <config.h> #ifdef MIKTEX - #include "MessageException.h" - #include "MiKTeXCom.h" - static MiKTeXCom *miktex=0; + #include "MiKTeXCom.hpp" #else #ifdef KPSE_CXX_UNSAFE extern "C" { #endif - #include <kpathsea/kpathsea.h> + #include <kpathsea/kpathsea.h> #ifdef KPSE_CXX_UNSAFE } #endif @@ -38,40 +36,30 @@ #include <fstream> #include <map> #include <set> -#include "FileFinder.h" -#include "FileSystem.h" -#include "FontMap.h" -#include "Message.h" -#include "Process.h" +#include "FileFinder.hpp" +#include "FileSystem.hpp" +#include "FontMap.hpp" +#include "Message.hpp" +#include "MessageException.hpp" +#include "Process.hpp" -// --------------------------------------------------- +const char *FileFinder::_argv0 = nullptr; +std::string FileFinder::_progname; +bool FileFinder::_enableMktex = false; -static bool _initialized = false; -static bool _mktex_enabled = false; -static std::set<std::string> _additional_dirs; -// --------------------------------------------------- - -static const char* find_file (const std::string &fname, const char *ftype); -static const char* find_mapped_file (std::string fname); -static const char* mktex (const std::string &fname); - - -/** Initializes the file finder. This function must be called before any other - * FileFinder function. +/** Constructs a new file finder. * @param[in] argv0 argv[0] of main() function * @param[in] progname name of application using the FileFinder * @param[in] enable_mktexmf if true, tfm and mf file generation is activated */ -void FileFinder::init (const char *argv0, const char *progname, bool enable_mktexmf) { - if (_initialized) - return; - - _mktex_enabled = enable_mktexmf; +FileFinder::FileFinder () { addLookupDir("."); // always lookup files in the current working directory #ifdef MIKTEX - miktex = new MiKTeXCom; + _miktex.reset(new MiKTeXCom); #else - kpse_set_program_name(argv0, progname); + if (_argv0 == nullptr) + _argv0 = ""; + kpse_set_program_name(_argv0, _progname.c_str()); // enable tfm and mf generation (actually invoked by calls of kpse_make_tex) kpse_set_program_enabled(kpse_tfm_format, 1, kpse_src_env); kpse_set_program_enabled(kpse_mf_format, 1, kpse_src_env); @@ -80,41 +68,26 @@ void FileFinder::init (const char *argv0, const char *progname, bool enable_mkte texlive_gs_init(); #endif #endif - _initialized = true; } -/** Cleans up the FileFinder. This function must be called before leaving the - * application's main() function. */ -void FileFinder::finish () { -#ifdef MIKTEX - if (miktex) { - delete miktex; - miktex = 0; - } -#endif - _initialized = false; +void FileFinder::init (const char *argv0, const char *progname, bool enable_mktexmf) { + _argv0 = argv0; + _progname = progname; + _enableMktex = enable_mktexmf; +} + + +FileFinder& FileFinder::instance () { + static FileFinder instance; + return instance; } /** Returns the version string of the underlying file searching library (kpathsea, MiKTeX) */ -std::string FileFinder::version () { +std::string FileFinder::version () const { #ifdef MIKTEX - bool autoinit=false; - try { - if (!_initialized) { - init("", "", false); - autoinit = true; - } - std::string ret = miktex->getVersion(); - if (autoinit) - finish(); - return ret; - } - catch (MessageException &e) { - if (autoinit) - finish(); - } + return _miktex->getVersion(); #else if (const char *v = strrchr(KPSEVERSION, ' ')) return (std::string(KPSEVERSION).substr(0, 9) == "kpathsea ") ? v+1 : KPSEVERSION; @@ -126,7 +99,7 @@ std::string FileFinder::version () { void FileFinder::addLookupDir (const std::string &path) { - _additional_dirs.insert(path); + _additionalDirs.insert(path); } @@ -135,18 +108,18 @@ void FileFinder::addLookupDir (const std::string &path) { * @param[in] fname name of file to look up * @param[in] ftype expected file format of file fname; if 0, it's derived from the filename suffix * @return file path on success, 0 otherwise */ -static const char* find_file (const std::string &fname, const char *ftype) { - if (!_initialized || fname.empty()) +const char* FileFinder::findFile (const std::string &fname, const char *ftype) const { + if (fname.empty()) return 0; static std::string buf; // try to lookup the file in the additionally specified directories - for (std::set<std::string>::iterator it=_additional_dirs.begin(); it != _additional_dirs.end(); ++it) { - if (it->at(0) == '/') + for (const std::string &dir : _additionalDirs) { + if (dir[0] == '/') buf.clear(); else buf = FileSystem::getcwd()+"/"; - buf += (*it) + "/" + fname; + buf += dir + "/" + fname; if (FileSystem::exists(buf)) return buf.c_str(); } @@ -164,7 +137,7 @@ static const char* find_file (const std::string &fname, const char *ftype) { #ifdef MIKTEX if (ext == "dll" || ext == "exe") { // lookup dll and exe files in the MiKTeX bin directory first - buf = miktex->getBinDir() + "/" + fname; + buf = _miktex->getBinDir() + "/" + fname; if (FileSystem::exists(buf)) return buf.c_str(); } @@ -177,7 +150,7 @@ static const char* find_file (const std::string &fname, const char *ftype) { return buf.empty() ? 0 : buf.c_str(); } try { - return miktex->findFile(fname.c_str()); + return _miktex->findFile(fname.c_str()); } catch (const MessageException &e) { return 0; @@ -193,23 +166,22 @@ static const char* find_file (const std::string &fname, const char *ftype) { return 0; } #endif - static std::map<std::string, kpse_file_format_type> types; - if (types.empty()) { - types["tfm"] = kpse_tfm_format; - types["pfb"] = kpse_type1_format; - types["vf"] = kpse_vf_format; - types["mf"] = kpse_mf_format; - types["ttc"] = kpse_truetype_format; - types["ttf"] = kpse_truetype_format; - types["otf"] = kpse_opentype_format; - types["map"] = kpse_fontmap_format; - types["cmap"] = kpse_cmap_format; - types["sty"] = kpse_tex_format; - types["enc"] = kpse_enc_format; - types["pro"] = kpse_tex_ps_header_format; - types["sfd"] = kpse_sfd_format; - } - std::map<std::string, kpse_file_format_type>::iterator it = types.find(ext.c_str()); + static std::map<std::string, kpse_file_format_type> types = { + {"tfm", kpse_tfm_format}, + {"pfb", kpse_type1_format}, + {"vf", kpse_vf_format}, + {"mf", kpse_mf_format}, + {"ttc", kpse_truetype_format}, + {"ttf", kpse_truetype_format}, + {"otf", kpse_opentype_format}, + {"map", kpse_fontmap_format}, + {"cmap", kpse_cmap_format}, + {"sty", kpse_tex_format}, + {"enc", kpse_enc_format}, + {"pro", kpse_tex_ps_header_format}, + {"sfd", kpse_sfd_format}, + }; + auto it = types.find(ext.c_str()); if (it == types.end()) return 0; @@ -230,7 +202,7 @@ static const char* find_file (const std::string &fname, const char *ftype) { * file can be found under this name. * @param[in] fname name of file to look up * @return file path on success, 0 otherwise */ -static const char* find_mapped_file (std::string fname) { +const char* FileFinder::findMappedFile (std::string fname) const { size_t pos = fname.rfind('.'); if (pos == std::string::npos) return 0; @@ -239,10 +211,10 @@ static const char* find_mapped_file (std::string fname) { if (const FontMap::Entry *entry = FontMap::instance().lookup(base)) { const char *path=0; if (entry->fontname.find('.') != std::string::npos) // does the mapped filename has an extension? - path = find_file(entry->fontname, 0); // look for that file + path = findFile(entry->fontname, 0); // look for that file else { // otherwise, use extension of unmapped file fname = entry->fontname + "." + ext; - (path = find_file(fname, 0)) || (path = mktex(fname)); + (path = findFile(fname, 0)) || (path = mktex(fname)); } return path; } @@ -253,12 +225,9 @@ static const char* find_mapped_file (std::string fname) { /** Runs external mktexFOO tool to create missing tfm or mf file. * @param[in] fname name of file to build * @return file path on success, 0 otherwise */ -static const char* mktex (const std::string &fname) { - if (!_initialized) - return 0; - +const char* FileFinder::mktex (const std::string &fname) const { size_t pos = fname.rfind('.'); - if (!_mktex_enabled || pos == std::string::npos) + if (!_enableMktex || pos == std::string::npos) return 0; std::string ext = fname.substr(pos+1); // file extension @@ -270,7 +239,7 @@ static const char* mktex (const std::string &fname) { // maketfm and makemf are located in miktex/bin which is in the search PATH std::string toolname = (ext == "tfm" ? "miktex-maketfm" : "miktex-makemf"); system((toolname+".exe "+fname).c_str()); - path = find_file(fname, 0); + path = findFile(fname, 0); #else kpse_file_format_type type = (ext == "tfm" ? kpse_tfm_format : kpse_mf_format); path = kpse_make_tex(type, fname.c_str()); @@ -290,9 +259,9 @@ static const char* mktex (const std::string &fname) { * @param[in] ftype type/format of file to look up * @param[in] extended if true, use fontmap lookup and mktexFOO calls * @return path to file on success, 0 otherwise */ -const char* FileFinder::lookup (const std::string &fname, const char *ftype, bool extended) { +const char* FileFinder::lookup (const std::string &fname, const char *ftype, bool extended) const { const char *path; - if ((path = find_file(fname, ftype)) || (extended && ((path = find_mapped_file(fname)) || (path = mktex(fname))))) + if ((path = findFile(fname, ftype)) || (extended && ((path = findMappedFile(fname)) || (path = mktex(fname))))) return path; return 0; } |