moved FileSystem functionality headers from <Utils.hpp> to dedicated
<FileSystem.hpp>
This commit is contained in:
parent
367ad5c92a
commit
b0b660f5a7
6 changed files with 95 additions and 32 deletions
|
|
@ -1 +1 @@
|
||||||
2020
|
2021
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
#include "../utils/Utils.hpp"
|
#include "FileSystem.hpp"
|
||||||
#include "Logger.hpp"
|
#include "Logger.hpp"
|
||||||
|
|
||||||
namespace ostd
|
namespace ostd
|
||||||
{
|
{
|
||||||
std::vector<std::filesystem::path> Utils::listFilesInDirectory(const String& directoryPath)
|
std::vector<std::filesystem::path> FileSystem::listFilesInDirectory(const String& directoryPath)
|
||||||
{
|
{
|
||||||
std::vector<std::filesystem::path> list;
|
std::vector<std::filesystem::path> list;
|
||||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
||||||
|
|
@ -14,7 +14,7 @@ namespace ostd
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::filesystem::path> Utils::listDirectoriesInDirectory(const String& directoryPath)
|
std::vector<std::filesystem::path> FileSystem::listDirectoriesInDirectory(const String& directoryPath)
|
||||||
{
|
{
|
||||||
std::vector<std::filesystem::path> list;
|
std::vector<std::filesystem::path> list;
|
||||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
||||||
|
|
@ -25,7 +25,7 @@ namespace ostd
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::filesystem::path> Utils::listDirectory(const String& directoryPath)
|
std::vector<std::filesystem::path> FileSystem::listDirectory(const String& directoryPath)
|
||||||
{
|
{
|
||||||
std::vector<std::filesystem::path> list;
|
std::vector<std::filesystem::path> list;
|
||||||
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
for (const auto& file : std::filesystem::directory_iterator(directoryPath.cpp_str()))
|
||||||
|
|
@ -33,7 +33,7 @@ namespace ostd
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::filesystem::path> Utils::listFilesInDirectoryRecursive(const String& directoryPath)
|
std::vector<std::filesystem::path> FileSystem::listFilesInDirectoryRecursive(const String& directoryPath)
|
||||||
{
|
{
|
||||||
std::vector<std::filesystem::path> list;
|
std::vector<std::filesystem::path> list;
|
||||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
||||||
|
|
@ -44,7 +44,7 @@ namespace ostd
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::filesystem::path> Utils::listDirectoriesInDirectoryRecursive(const String& directoryPath)
|
std::vector<std::filesystem::path> FileSystem::listDirectoriesInDirectoryRecursive(const String& directoryPath)
|
||||||
{
|
{
|
||||||
std::vector<std::filesystem::path> list;
|
std::vector<std::filesystem::path> list;
|
||||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
||||||
|
|
@ -55,7 +55,7 @@ namespace ostd
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::filesystem::path> Utils::listDirectoryRecursive(const String& directoryPath)
|
std::vector<std::filesystem::path> FileSystem::listDirectoryRecursive(const String& directoryPath)
|
||||||
{
|
{
|
||||||
std::vector<std::filesystem::path> list;
|
std::vector<std::filesystem::path> list;
|
||||||
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
for (const auto& file : std::filesystem::recursive_directory_iterator(directoryPath.cpp_str()))
|
||||||
|
|
@ -63,25 +63,27 @@ namespace ostd
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::filesystem::path Utils::getHomeDirPath(void)
|
|
||||||
|
std::filesystem::path FileSystem::getHomeDirPath(void)
|
||||||
{
|
{
|
||||||
String home_path = "";
|
String home_path = "";
|
||||||
#ifdef WINDOWS_OS
|
#ifdef WINDOWS_OS
|
||||||
home_path = String(getenv("HOMEDRIVE")) + "\\" + String(getenv("HOMEPATH"));
|
home_path = String(getenv("HOMEDRIVE")) + "\\" + String(getenv("HOMEPATH"));
|
||||||
#elif defined(LINUX_OS) || defined(MAC_OS)
|
#elif defined(LINUX_OS) || defined(MAC_OS)
|
||||||
home_path = String(getenv("HOME"));
|
home_path = String(getenv("HOME"));
|
||||||
#else
|
#else
|
||||||
home_path = "NULL";
|
home_path = "NULL";
|
||||||
#endif
|
#endif
|
||||||
return home_path;
|
return home_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::filesystem::path Utils::getWorkingDirPath(void)
|
std::filesystem::path FileSystem::getWorkingDirPath(void)
|
||||||
{
|
{
|
||||||
return std::filesystem::current_path();
|
return std::filesystem::current_path();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Utils::ensureDirectory(const String& path)
|
|
||||||
|
bool FileSystem::ensureDirectory(const String& path)
|
||||||
{
|
{
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
|
@ -110,7 +112,7 @@ namespace ostd
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Utils::deleteDirectory(const String& path)
|
bool FileSystem::deleteDirectory(const String& path)
|
||||||
{
|
{
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
try
|
try
|
||||||
|
|
@ -127,4 +129,47 @@ namespace ostd
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool FileSystem::directoryExists(const String& directoryPath)
|
||||||
|
{
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
fs::path p(directoryPath);
|
||||||
|
return fs::exists(p) && fs::is_directory(p);
|
||||||
|
}
|
||||||
|
catch (const fs::filesystem_error&)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FileSystem::fileExists(const String& filePath)
|
||||||
|
{
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
fs::path p(filePath);
|
||||||
|
return fs::exists(p) && !fs::is_directory(p);
|
||||||
|
}
|
||||||
|
catch (const fs::filesystem_error&)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FileSystem::pathExists(const String& path)
|
||||||
|
{
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
fs::path p(path);
|
||||||
|
return fs::exists(p);
|
||||||
|
}
|
||||||
|
catch (const fs::filesystem_error&)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
29
src/ostd/io/FileSystem.hpp
Normal file
29
src/ostd/io/FileSystem.hpp
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <ostd/string/String.hpp>
|
||||||
|
|
||||||
|
namespace ostd
|
||||||
|
{
|
||||||
|
class FileSystem
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static std::vector<std::filesystem::path> listFilesInDirectory(const String& directoryPath);
|
||||||
|
static std::vector<std::filesystem::path> listDirectoriesInDirectory(const String& directoryPath);
|
||||||
|
static std::vector<std::filesystem::path> listDirectory(const String& directoryPath);
|
||||||
|
static std::vector<std::filesystem::path> listFilesInDirectoryRecursive(const String& directoryPath);
|
||||||
|
static std::vector<std::filesystem::path> listDirectoriesInDirectoryRecursive(const String& directoryPath);
|
||||||
|
static std::vector<std::filesystem::path> listDirectoryRecursive(const String& directoryPath);
|
||||||
|
|
||||||
|
static std::filesystem::path getHomeDirPath(void);
|
||||||
|
static std::filesystem::path getWorkingDirPath(void);
|
||||||
|
|
||||||
|
static bool ensureDirectory(const String& path);
|
||||||
|
static bool deleteDirectory(const String& path);
|
||||||
|
|
||||||
|
static bool directoryExists(const String& directoryPath);
|
||||||
|
static bool fileExists(const String& filePath);
|
||||||
|
static bool pathExists(const String& path);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
#include <ostd/io/Errors.hpp>
|
#include <ostd/io/Errors.hpp>
|
||||||
#include <ostd/io/File.hpp>
|
#include <ostd/io/File.hpp>
|
||||||
|
#include <ostd/io/FileSystem.hpp>
|
||||||
#include <ostd/io/IOHandlers.hpp>
|
#include <ostd/io/IOHandlers.hpp>
|
||||||
#include <ostd/io/Json.hpp>
|
#include <ostd/io/Json.hpp>
|
||||||
#include <ostd/io/Logger.hpp>
|
#include <ostd/io/Logger.hpp>
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,7 @@ namespace ostd
|
||||||
inline String(void) { m_data = ""; }
|
inline String(void) { m_data = ""; }
|
||||||
inline String(const cpp_string& str) { m_data = str; }
|
inline String(const cpp_string& str) { m_data = str; }
|
||||||
inline String(const char* str) { m_data = str; }
|
inline String(const char* str) { m_data = str; }
|
||||||
|
inline String(const std::filesystem::path& path) { m_data = path; }
|
||||||
|
|
||||||
inline cpp_string cpp_str(void) const { return m_data; }
|
inline cpp_string cpp_str(void) const { return m_data; }
|
||||||
inline cpp_string& cpp_str_ref(void) { return m_data; }
|
inline cpp_string& cpp_str_ref(void) { return m_data; }
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <ostd/data_types/Types.hpp>
|
#include <ostd/data_types/Types.hpp>
|
||||||
#include <ostd/string/TextStyleParser.hpp>
|
#include <ostd/string/TextStyleParser.hpp>
|
||||||
|
|
@ -61,18 +60,6 @@ namespace ostd
|
||||||
//Implemented in <md5.cpp>
|
//Implemented in <md5.cpp>
|
||||||
static String md5(const String& str);
|
static String md5(const String& str);
|
||||||
|
|
||||||
//Implemented in <FileSystem.cpp>
|
|
||||||
static std::vector<std::filesystem::path> listFilesInDirectory(const String& directoryPath);
|
|
||||||
static std::vector<std::filesystem::path> listDirectoriesInDirectory(const String& directoryPath);
|
|
||||||
static std::vector<std::filesystem::path> listDirectory(const String& directoryPath);
|
|
||||||
static std::vector<std::filesystem::path> listFilesInDirectoryRecursive(const String& directoryPath);
|
|
||||||
static std::vector<std::filesystem::path> listDirectoriesInDirectoryRecursive(const String& directoryPath);
|
|
||||||
static std::vector<std::filesystem::path> listDirectoryRecursive(const String& directoryPath);
|
|
||||||
static std::filesystem::path getHomeDirPath(void);
|
|
||||||
static std::filesystem::path getWorkingDirPath(void);
|
|
||||||
static bool ensureDirectory(const String& path);
|
|
||||||
static bool deleteDirectory(const String& path);
|
|
||||||
|
|
||||||
//Implemented in <ShuntingYard.cpp>
|
//Implemented in <ShuntingYard.cpp>
|
||||||
static int32_t solveIntegerExpression(const String& expr);
|
static int32_t solveIntegerExpression(const String& expr);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue