Fix include in pathTools.h

This commit is contained in:
Matthieu Gautier 2019-08-08 12:35:37 +02:00
parent ddeb862395
commit 72223d69fe
6 changed files with 64 additions and 73 deletions

View File

@ -20,46 +20,27 @@
#ifndef KIWIX_PATHTOOLS_H #ifndef KIWIX_PATHTOOLS_H
#define KIWIX_PATHTOOLS_H #define KIWIX_PATHTOOLS_H
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fstream>
#include <ios>
#include <iostream>
#include <sstream>
#include <string> #include <string>
#include <vector>
#ifdef _WIN32 bool isRelativePath(const std::string& path);
#include <direct.h> std::string computeAbsolutePath(const std::string& path, const std::string& relativePath);
#endif std::string computeRelativePath(const std::string& path, const std::string& absolutePath);
std::string removeLastPathElement(const std::string& path,
const bool removePreSeparator = false,
const bool removePostSeparator = false);
std::string appendToDirectory(const std::string& directoryPath, const std::string& filename);
#include "stringTools.h" unsigned int getFileSize(const std::string& path);
std::string getFileSizeAsString(const std::string& path);
using namespace std; std::string getFileContent(const std::string& path);
bool fileExists(const std::string& path);
bool isRelativePath(const string& path); bool makeDirectory(const std::string& path);
string computeAbsolutePath(const string path, const string relativePath); std::string makeTmpDirectory();
string computeRelativePath(const string path, const string absolutePath); bool copyFile(const std::string& sourcePath, const std::string& destPath);
string removeLastPathElement(const string path, std::string getLastPathElement(const std::string& path);
const bool removePreSeparator = false, std::string getExecutablePath();
const bool removePostSeparator = false); std::string getCurrentDirectory();
string appendToDirectory(const string& directoryPath, const string& filename); std::string getDataDirectory();
bool writeTextFile(const std::string& path, const std::string& content);
unsigned int getFileSize(const string& path);
string getFileSizeAsString(const string& path);
string getFileContent(const string& path);
bool fileExists(const string& path);
bool makeDirectory(const string& path);
string makeTmpDirectory();
bool copyFile(const string& sourcePath, const string& destPath);
string getLastPathElement(const string& path);
string getExecutablePath();
string getCurrentDirectory();
string getDataDirectory();
bool writeTextFile(const string& path, const string& content);
std::string getMimeTypeForFile(const std::string& filename); std::string getMimeTypeForFile(const std::string& filename);
#endif #endif

View File

@ -7,6 +7,7 @@
#include <chrono> #include <chrono>
#include <tools/otherTools.h> #include <tools/otherTools.h>
#include <tools/pathTools.h> #include <tools/pathTools.h>
#include <tools/stringTools.h>
#include <downloader.h> // For AriaError #include <downloader.h> // For AriaError
#ifdef _WIN32 #ifdef _WIN32

View File

@ -19,6 +19,7 @@
#include "downloader.h" #include "downloader.h"
#include "tools/pathTools.h" #include "tools/pathTools.h"
#include "tools/stringTools.h"
#include <algorithm> #include <algorithm>
#include <thread> #include <thread>
@ -174,7 +175,7 @@ Download* Downloader::getDownload(const std::string& did)
try { try {
m_knownDownloads.at(did).get()->updateStatus(true); m_knownDownloads.at(did).get()->updateStatus(true);
return m_knownDownloads.at(did).get(); return m_knownDownloads.at(did).get();
} catch(exception& e) { } catch(std::exception& e) {
for (auto gid : mp_aria->tellActive()) { for (auto gid : mp_aria->tellActive()) {
if (gid == did) { if (gid == did) {
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid)); m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));

View File

@ -10,6 +10,7 @@
#endif #endif
#include "tools/pathTools.h" #include "tools/pathTools.h"
#include "tools/stringTools.h"
namespace kiwix { namespace kiwix {

View File

@ -24,6 +24,7 @@
#include "tools/base64.h" #include "tools/base64.h"
#include "tools/regexTools.h" #include "tools/regexTools.h"
#include "tools/pathTools.h" #include "tools/pathTools.h"
#include "tools/stringTools.h"
#include <pugixml.hpp> #include <pugixml.hpp>
#include <algorithm> #include <algorithm>

View File

@ -29,7 +29,13 @@
#define getcwd _getcwd // stupid MSFT "deprecation" warning #define getcwd _getcwd // stupid MSFT "deprecation" warning
#endif #endif
#include "tools/stringTools.h"
#include <string.h>
#include <map> #include <map>
#include <vector>
#include <sys/stat.h>
#include <sstream>
#ifdef _WIN32 #ifdef _WIN32
const std::string SEPARATOR("\\"); const std::string SEPARATOR("\\");
@ -44,7 +50,7 @@ const std::string SEPARATOR("/");
#define PATH_MAX 1024 #define PATH_MAX 1024
#endif #endif
bool isRelativePath(const string& path) bool isRelativePath(const std::string& path)
{ {
#ifdef _WIN32 #ifdef _WIN32
return path.empty() || path.substr(1, 2) == ":\\" ? false : true; return path.empty() || path.substr(1, 2) == ":\\" ? false : true;
@ -53,7 +59,7 @@ bool isRelativePath(const string& path)
#endif #endif
} }
string computeRelativePath(const string path, const string absolutePath) std::string computeRelativePath(const std::string& path, const std::string& absolutePath)
{ {
std::vector<std::string> pathParts = kiwix::split(path, SEPARATOR); std::vector<std::string> pathParts = kiwix::split(path, SEPARATOR);
std::vector<std::string> absolutePathParts std::vector<std::string> absolutePathParts
@ -66,7 +72,7 @@ string computeRelativePath(const string path, const string absolutePath)
commonCount++; commonCount++;
} }
string relativePath; std::string relativePath;
#ifdef _WIN32 #ifdef _WIN32
/* On Windows you have a token more because the root is represented /* On Windows you have a token more because the root is represented
by a letter */ by a letter */
@ -86,9 +92,9 @@ string computeRelativePath(const string path, const string absolutePath)
} }
/* Warning: the relative path must be with slashes */ /* Warning: the relative path must be with slashes */
string computeAbsolutePath(const string path, const string relativePath) std::string computeAbsolutePath(const std::string& path, const std::string& relativePath)
{ {
string absolutePath; std::string absolutePath;
if (path.empty()) { if (path.empty()) {
char* path = NULL; char* path = NULL;
@ -100,7 +106,7 @@ string computeAbsolutePath(const string path, const string relativePath)
path = getcwd(path, size); path = getcwd(path, size);
#endif #endif
absolutePath = string(path) + SEPARATOR; absolutePath = std::string(path) + SEPARATOR;
} else { } else {
absolutePath = path.substr(path.length() - 1, 1) == SEPARATOR absolutePath = path.substr(path.length() - 1, 1) == SEPARATOR
? path ? path
@ -115,11 +121,11 @@ string computeAbsolutePath(const string path, const string relativePath)
char* token = strtok(cRelativePath, "/"); char* token = strtok(cRelativePath, "/");
while (token != NULL) { while (token != NULL) {
if (string(token) == "..") { if (std::string(token) == "..") {
absolutePath = removeLastPathElement(absolutePath, true, false); absolutePath = removeLastPathElement(absolutePath, true, false);
token = strtok(NULL, "/"); token = strtok(NULL, "/");
} else if (strcmp(token, ".") && strcmp(token, "")) { } else if (strcmp(token, ".") && strcmp(token, "")) {
absolutePath += string(token); absolutePath += std::string(token);
token = strtok(NULL, "/"); token = strtok(NULL, "/");
if (token != NULL) { if (token != NULL) {
absolutePath += SEPARATOR; absolutePath += SEPARATOR;
@ -132,11 +138,11 @@ string computeAbsolutePath(const string path, const string relativePath)
return absolutePath; return absolutePath;
} }
string removeLastPathElement(const string path, std::string removeLastPathElement(const std::string& path,
const bool removePreSeparator, const bool removePreSeparator,
const bool removePostSeparator) const bool removePostSeparator)
{ {
string newPath = path; std::string newPath = path;
size_t offset = newPath.find_last_of(SEPARATOR); size_t offset = newPath.find_last_of(SEPARATOR);
if (removePreSeparator && if (removePreSeparator &&
#ifndef _WIN32 #ifndef _WIN32
@ -151,18 +157,18 @@ string removeLastPathElement(const string path,
return newPath; return newPath;
} }
string appendToDirectory(const string& directoryPath, const string& filename) std::string appendToDirectory(const std::string& directoryPath, const std::string& filename)
{ {
string newPath = directoryPath + SEPARATOR + filename; std::string newPath = directoryPath + SEPARATOR + filename;
return newPath; return newPath;
} }
string getLastPathElement(const string& path) std::string getLastPathElement(const std::string& path)
{ {
return path.substr(path.find_last_of(SEPARATOR) + 1); return path.substr(path.find_last_of(SEPARATOR) + 1);
} }
unsigned int getFileSize(const string& path) unsigned int getFileSize(const std::string& path)
{ {
#ifdef _WIN32 #ifdef _WIN32
struct _stat filestatus; struct _stat filestatus;
@ -175,14 +181,14 @@ unsigned int getFileSize(const string& path)
return filestatus.st_size / 1024; return filestatus.st_size / 1024;
} }
string getFileSizeAsString(const string& path) std::string getFileSizeAsString(const std::string& path)
{ {
ostringstream convert; std::ostringstream convert;
convert << getFileSize(path); convert << getFileSize(path);
return convert.str(); return convert.str();
} }
string getFileContent(const string& path) std::string getFileContent(const std::string& path)
{ {
std::ifstream f(path, std::ios::in|std::ios::ate); std::ifstream f(path, std::ios::in|std::ios::ate);
std::string content; std::string content;
@ -196,14 +202,14 @@ string getFileContent(const string& path)
return content; return content;
} }
bool fileExists(const string& path) bool fileExists(const std::string& path)
{ {
#ifdef _WIN32 #ifdef _WIN32
return PathFileExists(path.c_str()); return PathFileExists(path.c_str());
#else #else
bool flag = false; bool flag = false;
fstream fin; std::fstream fin;
fin.open(path.c_str(), ios::in); fin.open(path.c_str(), std::ios::in);
if (fin.is_open()) { if (fin.is_open()) {
flag = true; flag = true;
} }
@ -212,7 +218,7 @@ bool fileExists(const string& path)
#endif #endif
} }
bool makeDirectory(const string& path) bool makeDirectory(const std::string& path)
{ {
#ifdef _WIN32 #ifdef _WIN32
int status = _mkdir(path.c_str()); int status = _mkdir(path.c_str());
@ -222,7 +228,7 @@ bool makeDirectory(const string& path)
return status == 0; return status == 0;
} }
string makeTmpDirectory() std::string makeTmpDirectory()
{ {
#ifdef _WIN32 #ifdef _WIN32
char cbase[MAX_PATH]; char cbase[MAX_PATH];
@ -233,16 +239,16 @@ string makeTmpDirectory()
GetTempFileName(cbase, "kiwix", 0, ctmp); GetTempFileName(cbase, "kiwix", 0, ctmp);
DeleteFile(ctmp); DeleteFile(ctmp);
_mkdir(ctmp); _mkdir(ctmp);
return string(ctmp); return std::string(ctmp);
#else #else
char _template_array[] = {"/tmp/kiwix-lib_XXXXXX"}; char _template_array[] = {"/tmp/kiwix-lib_XXXXXX"};
string dir = mkdtemp(_template_array); std::string dir = mkdtemp(_template_array);
return dir; return dir;
#endif #endif
} }
/* Try to create a link and if does not work then make a copy */ /* Try to create a link and if does not work then make a copy */
bool copyFile(const string& sourcePath, const string& destPath) bool copyFile(const std::string& sourcePath, const std::string& destPath)
{ {
try { try {
#ifndef _WIN32 #ifndef _WIN32
@ -254,15 +260,15 @@ bool copyFile(const string& sourcePath, const string& destPath)
#ifndef _WIN32 #ifndef _WIN32
} }
#endif #endif
} catch (exception& e) { } catch (std::exception& e) {
cerr << e.what() << endl; std::cerr << e.what() << std::endl;
return false; return false;
} }
return true; return true;
} }
string getExecutablePath() std::string getExecutablePath()
{ {
char binRootPath[PATH_MAX]; char binRootPath[PATH_MAX];
@ -283,7 +289,7 @@ string getExecutablePath()
return ""; return "";
} }
bool writeTextFile(const string& path, const string& content) bool writeTextFile(const std::string& path, const std::string& content)
{ {
std::ofstream file; std::ofstream file;
file.open(path.c_str()); file.open(path.c_str());
@ -292,15 +298,15 @@ bool writeTextFile(const string& path, const string& content)
return true; return true;
} }
string getCurrentDirectory() std::string getCurrentDirectory()
{ {
char* a_cwd = getcwd(NULL, 0); char* a_cwd = getcwd(NULL, 0);
string s_cwd(a_cwd); std::string s_cwd(a_cwd);
free(a_cwd); free(a_cwd);
return s_cwd; return s_cwd;
} }
string getDataDirectory() std::string getDataDirectory()
{ {
#ifdef _WIN32 #ifdef _WIN32
char* cDataDir = ::getenv("APPDATA"); char* cDataDir = ::getenv("APPDATA");