Use filesystem in getExiv2ConfigPath

It seems to have an important impact in performance. The execution of
ctest on Windows passed from 48.5 secs to 43.8. Check if this is the
same on Linux
main
Luis Díaz Más 3 years ago
parent 476a5e23f9
commit b543f3e973

@ -35,9 +35,10 @@
#include "utils.hpp" #include "utils.hpp"
// + standard includes // + standard includes
#include <string> #include <filesystem>
#include <fstream> #include <fstream>
#include <cstring>
namespace fs = std::filesystem;
#if defined(__MINGW32__) || defined(__MINGW64__) #if defined(__MINGW32__) || defined(__MINGW64__)
#ifndef __MINGW__ #ifndef __MINGW__
@ -53,25 +54,6 @@
#include <windows.h> #include <windows.h>
#include <direct.h> // _getcwd #include <direct.h> // _getcwd
#include <shlobj.h> #include <shlobj.h>
/* older SDKs not have these */
# ifndef CSIDL_MYMUSIC
# define CSIDL_MYMUSIC 13
# endif
# ifndef CSIDL_MYVIDEO
# define CSIDL_MYVIDEO 14
# endif
# ifndef CSIDL_INTERNET_CACHE
# define CSIDL_INTERNET_CACHE 32
# endif
# ifndef CSIDL_COMMON_APPDATA
# define CSIDL_COMMON_APPDATA 35
# endif
# ifndef CSIDL_MYPICTURES
# define CSIDL_MYPICTURES 0x27
# endif
# ifndef CSIDL_COMMON_DOCUMENTS
# define CSIDL_COMMON_DOCUMENTS 46
# endif
# ifndef CSIDL_PROFILE # ifndef CSIDL_PROFILE
# define CSIDL_PROFILE 40 # define CSIDL_PROFILE 40
# endif # endif
@ -97,7 +79,6 @@ namespace {
namespace Exiv2 { namespace Exiv2 {
namespace Internal { namespace Internal {
// C++17 use std::filesystem
// Function first looks for a config file in current working directory // Function first looks for a config file in current working directory
// on Win the file should be named "exiv2.ini" // on Win the file should be named "exiv2.ini"
// on Lin the file should be named ".exiv2" // on Lin the file should be named ".exiv2"
@ -105,36 +86,27 @@ namespace Exiv2 {
// which is the user profile path on win and the home dir on linux // which is the user profile path on win and the home dir on linux
std::string getExiv2ConfigPath() std::string getExiv2ConfigPath()
{ {
std::string dir;
#if defined(_MSC_VER) || defined(__MINGW__) #if defined(_MSC_VER) || defined(__MINGW__)
std::string inifile("exiv2.ini"); std::string inifile("exiv2.ini");
#else #else
std::string inifile(".exiv2"); std::string inifile(".exiv2");
#endif #endif
auto currentPath = fs::current_path();
// first lets get the current working directory to check if there is a config file auto iniPath = currentPath / inifile;
char buffer[1024]; if (fs::exists(iniPath)) {
#if defined(_MSC_VER) || defined(__MINGW__) return iniPath.string();
auto path = _getcwd(buffer, sizeof(buffer));
#else
auto path = getcwd(buffer, sizeof(buffer));
#endif
dir = std::string(path ? path : "");
auto const filename = dir + EXV_SEPARATOR_CHR + inifile;
// true if the file exists
if (std::ifstream(filename).good()) {
return filename;
} }
#if defined(_MSC_VER) || defined(__MINGW__) #if defined(_MSC_VER) || defined(__MINGW__)
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path))) { char buffer[1024];
dir = std::string(path); if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, buffer))) {
currentPath = buffer;
} }
#else #else
struct passwd* pw = getpwuid(getuid()); struct passwd* pw = getpwuid(getuid());
dir = std::string(pw ? pw->pw_dir : ""); currentPath = std::string(pw ? pw->pw_dir : "");
#endif #endif
return dir + EXV_SEPARATOR_CHR + inifile; return (currentPath / inifile).string();
} }
std::string readExiv2Config(const std::string& section, const std::string& value, const std::string& def) std::string readExiv2Config(const std::string& section, const std::string& value, const std::string& def)

Loading…
Cancel
Save