Skip to content

Commit

Permalink
TgBot++: fslib: Use std::filesystem::path for path objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Feb 19, 2024
1 parent b4bc275 commit 112c430
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ struct file_priv {
};

static void *file_load(void) {
std::string home, line;
std::filesystem::path home;
std::string line;
static file_priv p{};
size_t count = 0;

if (!getHomePath(home)) {
LOG_E("Cannot find HOME");
return nullptr;
}
const auto confPath = (std::filesystem::path(home) / ".tgbot_config").string();
const auto confPath = (home / ".tgbot_config").string();
std::ifstream ifs(confPath);
if (ifs.fail()) {
LOG_E("Opening %s failed", confPath.c_str());
Expand Down
4 changes: 2 additions & 2 deletions src/fslib/FileSystemLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace fs = std::filesystem;

__attribute__((weak))
bool fileExists(const std::string& filename) {
bool fileExists(const std::filesystem::path& filename) {
bool rc;

std::error_code ec;
Expand All @@ -17,7 +17,7 @@ bool fileExists(const std::string& filename) {
return rc;
}

bool canExecute(const std::string& filename)
bool canExecute(const std::filesystem::path& filename)
{
std::error_code ec;

Expand Down
4 changes: 2 additions & 2 deletions src/fslib/FileSystemLib_posix.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <cstdlib>
#include <string>
#include <filesystem>

bool getHomePath(std::string& buf) {
bool getHomePath(std::filesystem::path& buf) {
auto buf_c = getenv("HOME");
if (buf_c) {
buf = buf_c;
Expand Down
9 changes: 5 additions & 4 deletions src/fslib/FileSystemLib_windows.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <shlobj.h>
#include <shlwapi.h>

#include <string>
#include <filesystem>

bool getHomePath(std::string& buf) {
bool getHomePath(std::filesystem::path& buf) {
CHAR userDir[MAX_PATH];
bool ret = SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, userDir));
if (ret) {
Expand All @@ -12,7 +12,8 @@ bool getHomePath(std::string& buf) {
return ret;
}

bool fileExists(const std::string& path) {
auto filepath = path.c_str();
bool fileExists(const std::filesystem::path& path) {
auto pathstr = path.string();
auto filepath = pathstr.c_str();
return PathFileExistsA(filepath) && !PathIsDirectoryA(filepath);
}
8 changes: 4 additions & 4 deletions src/include/FileSystemLib.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <filesystem>

#ifdef __WIN32
static inline const char path_env_delimiter = ';';
Expand All @@ -9,6 +9,6 @@ static inline const char path_env_delimiter = ':';
#endif

// Implemented sperately by OS
bool canExecute(const std::string& path);
bool getHomePath(std::string& buf);
bool fileExists(const std::string& filename);
bool canExecute(const std::filesystem::path& path);
bool getHomePath(std::filesystem::path& buf);
bool fileExists(const std::filesystem::path& filename);

0 comments on commit 112c430

Please sign in to comment.