lib/common: Replace PathUtilities::concat with barrier::fs equivalent

This commit is contained in:
Povilas Kanapickas 2021-11-01 04:29:47 +02:00
parent bcafdc6783
commit e7d936b5d7
7 changed files with 24 additions and 50 deletions

View File

@ -164,7 +164,7 @@ App::initApp(int argc, const char** argv)
// parse command line // parse command line
parseArgs(argc, argv); parseArgs(argc, argv);
DataDirectories::profile(argsBase().m_profileDirectory); DataDirectories::profile(argsBase().m_profileDirectory.u8string());
// set log filter // set log filter
if (!CLOG->setFilter(argsBase().m_logFilter)) { if (!CLOG->setFilter(argsBase().m_logFilter)) {

View File

@ -288,10 +288,10 @@ ArgParser::parseGenericArgs(int argc, const char* const* argv, int& i)
argsBase().m_enableCrypto = false; argsBase().m_enableCrypto = false;
} }
else if (isArg(i, argc, argv, NULL, "--profile-dir", 1)) { else if (isArg(i, argc, argv, NULL, "--profile-dir", 1)) {
argsBase().m_profileDirectory = argv[++i]; argsBase().m_profileDirectory = barrier::fs::u8path(argv[++i]);
} }
else if (isArg(i, argc, argv, NULL, "--plugin-dir", 1)) { else if (isArg(i, argc, argv, NULL, "--plugin-dir", 1)) {
argsBase().m_pluginDirectory = argv[++i]; argsBase().m_pluginDirectory = barrier::fs::u8path(argv[++i]);
} }
else { else {
// option not supported here // option not supported here

View File

@ -43,7 +43,7 @@ m_dropTarget(""),
m_shouldExit(false), m_shouldExit(false),
m_barrierAddress(), m_barrierAddress(),
m_enableCrypto(true), m_enableCrypto(true),
m_profileDirectory(""), m_profileDirectory(),
m_pluginDirectory("") m_pluginDirectory("")
{ {
} }

View File

@ -19,6 +19,7 @@
#pragma once #pragma once
#include "base/String.h" #include "base/String.h"
#include "io/filesystem.h"
class ArgsBase { class ArgsBase {
public: public:
@ -50,6 +51,6 @@ public:
bool m_shouldExit; bool m_shouldExit;
String m_barrierAddress; String m_barrierAddress;
bool m_enableCrypto; bool m_enableCrypto;
String m_profileDirectory; barrier::fs::path m_profileDirectory;
String m_pluginDirectory; barrier::fs::path m_pluginDirectory;
}; };

View File

@ -40,7 +40,6 @@
#include "base/TMethodEventJob.h" #include "base/TMethodEventJob.h"
#include "common/Version.h" #include "common/Version.h"
#include "common/DataDirectories.h" #include "common/DataDirectories.h"
#include "common/PathUtilities.h"
#if SYSAPI_WIN32 #if SYSAPI_WIN32
#include "arch/win32/ArchMiscWindows.h" #include "arch/win32/ArchMiscWindows.h"
@ -129,11 +128,15 @@ ServerApp::help()
#endif #endif
// refer to custom profile directory even if not saved yet // refer to custom profile directory even if not saved yet
String profilePath = argsBase().m_profileDirectory; barrier::fs::path profile_path = argsBase().m_profileDirectory;
if (profilePath.empty()) { if (profile_path.empty()) {
profilePath = DataDirectories::profile(); profile_path = barrier::fs::u8path(DataDirectories::profile());
} }
auto usr_config_path = (profile_path / barrier::fs::u8path(USR_CONFIG_NAME)).u8string();
auto sys_config_path = (barrier::fs::u8path(DataDirectories::systemconfig()) /
barrier::fs::u8path(SYS_CONFIG_NAME)).u8string();
std::ostringstream buffer; std::ostringstream buffer;
buffer << "Start the barrier server component.\n" buffer << "Start the barrier server component.\n"
<< "\n" << "\n"
@ -156,8 +159,8 @@ ServerApp::help()
<< "\n" << "\n"
<< "If no configuration file pathname is provided then the first of the\n" << "If no configuration file pathname is provided then the first of the\n"
<< "following to load successfully sets the configuration:\n" << "following to load successfully sets the configuration:\n"
<< " " << PathUtilities::concat(profilePath, USR_CONFIG_NAME) << "\n" << " " << usr_config_path << "\n"
<< " " << PathUtilities::concat(DataDirectories::systemconfig(), SYS_CONFIG_NAME) << "\n"; << " " << sys_config_path << "\n";
LOG((CLOG_PRINT "%s", buffer.str().c_str())); LOG((CLOG_PRINT "%s", buffer.str().c_str()));
} }
@ -194,25 +197,25 @@ ServerApp::loadConfig()
// load the default configuration if no explicit file given // load the default configuration if no explicit file given
else { else {
String path = DataDirectories::profile(); auto path = barrier::fs::u8path(DataDirectories::profile());
if (!path.empty()) { if (!path.empty()) {
// complete path // complete path
path = PathUtilities::concat(path, USR_CONFIG_NAME); path /= barrier::fs::u8path(USR_CONFIG_NAME);
// now try loading the user's configuration // now try loading the user's configuration
if (loadConfig(path)) { if (loadConfig(path.u8string())) {
loaded = true; loaded = true;
args().m_configFile = path; args().m_configFile = path.u8string();
} }
} }
if (!loaded) { if (!loaded) {
// try the system-wide config file // try the system-wide config file
path = DataDirectories::systemconfig(); path = barrier::fs::u8path(DataDirectories::systemconfig());
if (!path.empty()) { if (!path.empty()) {
path = PathUtilities::concat(path, SYS_CONFIG_NAME); path /= barrier::fs::u8path(SYS_CONFIG_NAME);
if (loadConfig(path)) { if (loadConfig(path.u8string())) {
loaded = true; loaded = true;
args().m_configFile = path; args().m_configFile = path.u8string();
} }
} }
} }

View File

@ -44,32 +44,3 @@ std::string PathUtilities::basename(const std::string& path)
return path.substr(path.find_last_of(Delimiters) + 1); return path.substr(path.find_last_of(Delimiters) + 1);
} }
std::string PathUtilities::concat(const std::string& left, const std::string& right)
{
// although npos is usually (-1) we can't count on that so handle it explicitly
auto leftEnd = left.find_last_not_of(Delimiters);
if (leftEnd == std::string::npos)
leftEnd = 0;
else
++leftEnd;
auto rightStart = right.find_first_not_of(Delimiters, 0);
if (rightStart == std::string::npos) {
// both left/right are empty
if (left.size() == 0 && right.size() == 0)
return "";
// right is full of delims, left is okay
if (leftEnd > 0)
return left.substr(0, leftEnd);
// both left/right useless but at least one has delims
return std::string(1, DefaultDelimiter);
}
if (leftEnd == 0) {
// right is okay and not prefixed with delims, left is empty
if (left.size() == 0 && rightStart == 0)
return right.substr(rightStart);
// (right is okay and prefixed with delims) OR left is full of delims
return DefaultDelimiter + right.substr(rightStart);
}
// concatenation using both left and right
return left.substr(0, leftEnd) + DefaultDelimiter + right.substr(rightStart);
}

View File

@ -23,5 +23,4 @@ class PathUtilities
{ {
public: public:
static std::string basename(const std::string& path); static std::string basename(const std::string& path);
static std::string concat(const std::string& left, const std::string& right);
}; };