Replace platform specific code with std::filesystem

This commit is contained in:
Luis Díaz Más 2022-02-12 19:43:11 +01:00
parent fdfb295cc4
commit 432555eae8
3 changed files with 21 additions and 52 deletions

View File

@ -23,6 +23,7 @@
#include <exiv2/exiv2.hpp>
#include "Jzon.h"
#include <filesystem>
#include <iostream>
#include <iomanip>
#include <cassert>
@ -41,20 +42,6 @@
# endif
#endif
#if defined(_MSC_VER) || defined(__MINGW__)
#include <windows.h>
#ifndef PATH_MAX
# define PATH_MAX 512
#endif
const char* realpath(const char* file,char* path)
{
GetFullPathName(file,PATH_MAX,path,NULL);
return path;
}
#else
#include <unistd.h>
#endif
struct Token {
std::string n; // the name eg "History"
bool a; // name is an array eg History[]
@ -249,8 +236,7 @@ void fileSystemPush(const char* path,Jzon::Node& nfs)
{
auto& fs = dynamic_cast<Jzon::Object&>(nfs);
fs.Add("path",path);
char resolved_path[2000]; // PATH_MAX];
fs.Add("realpath",realpath(path,resolved_path));
fs.Add("realpath", std::filesystem::absolute(std::filesystem::path(path)).string());
struct stat buf;
memset(&buf,0,sizeof(buf));

View File

@ -24,6 +24,7 @@
#include <exiv2/exiv2.hpp>
#include "unused.h"
#include <filesystem>
#include <iostream>
#include <iomanip>
#include <cassert>
@ -84,17 +85,6 @@ string getExifTime(time_t t);
time_t parseTime(const char* ,bool bAdjust=false);
int timeZoneAdjust();
// platform specific code
#if defined(_MSC_VER) || defined(__MINGW__)
char* realpath(const char* file,char* path)
{
char* result = (char*) malloc(_MAX_PATH);
if (result) GetFullPathName(file,_MAX_PATH,result,NULL);
return result ;
UNUSED(path);
}
#endif
// Command-line parser
class Options {
public:
@ -838,19 +828,13 @@ int main(int argc,const char* argv[])
if ( options.verbose ) printf("%s %s ",arg,types[type]) ;
if ( type == typeImage ) {
time_t t = readImageTime(std::string(arg)) ;
#ifdef __APPLE__
char buffer[1024];
#else
char* buffer = nullptr;
#endif
char* path = realpath(arg,buffer);
if ( t && path ) {
auto p = std::filesystem::absolute(std::filesystem::path(arg));
std::string path = p.string();
if ( t && !path.empty() ) {
if (options.verbose)
printf("%s %ld %s", path, static_cast<long int>(t), asctime(localtime(&t)));
printf("%s %ld %s", path.c_str(), static_cast<long int>(t), asctime(localtime(&t)));
gFiles.push_back(path);
}
if (path && path != buffer)
::free(path);
}
if ( type == typeUnknown ) {
fprintf(stderr,"error: illegal syntax %s\n",arg);

View File

@ -40,6 +40,7 @@
#include "i18n.h" // NLS support.
// + standard includes
#include <filesystem>
#include <string>
#include <iostream>
#include <iomanip>
@ -69,6 +70,8 @@
#define _setmode(a,b)
#endif
namespace fs = std::filesystem;
// *****************************************************************************
// local declarations
namespace {
@ -1786,29 +1789,25 @@ namespace {
return os.str();
} // tm2Str
std::string temporaryPath()
{
static int count = 0;
std::lock_guard<std::mutex> guard(cs);
std::string temporaryPath()
{
static int count = 0;
std::lock_guard<std::mutex> guard(cs);
#if defined(_MSC_VER) || defined(__MINGW__)
char lpTempPathBuffer[MAX_PATH];
GetTempPath(MAX_PATH,lpTempPathBuffer);
std::string tmp(lpTempPathBuffer);
tmp += "\\";
HANDLE process=0;
DWORD pid = ::GetProcessId(process);
#else
pid_t pid = ::getpid();
std::string tmp = "/tmp/";
#endif
std::string result = tmp + Exiv2::toString(pid) + "_" + std::to_string(count);
if (Exiv2::fileExists(result)) {
std::remove(result.c_str());
}
/// \todo check if we can use std::tmpnam
auto p = fs::temp_directory_path() / (Exiv2::toString(pid) + "_" + std::to_string(count));
if (fs::exists(p)) {
fs::remove(p);
}
return result;
}
return p.string();
}
int metacopy(const std::string& source,
const std::string& tgt,