From 4c04f39685be93b87dc4159e6430d07421bbec46 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Sat, 31 Mar 2018 22:41:00 -0400 Subject: [PATCH] reimplement path operations basename() and concat() in Common. these were the last bits remaining in ArchFile* so it was removed --- src/lib/arch/Arch.h | 3 -- src/lib/arch/IArchFile.h | 51 ------------------ src/lib/arch/unix/ArchFileUnix.cpp | 60 ---------------------- src/lib/arch/unix/ArchFileUnix.h | 32 ------------ src/lib/arch/win32/ArchFileWindows.cpp | 71 -------------------------- src/lib/arch/win32/ArchFileWindows.h | 32 ------------ src/lib/barrier/ArgParser.cpp | 3 +- src/lib/barrier/ClientApp.cpp | 7 +-- src/lib/barrier/ServerApp.cpp | 11 ++-- src/lib/common/DataDirectories.h | 1 - src/lib/common/PathUtilities.cpp | 45 ++++++++++++++++ src/lib/common/PathUtilities.h | 14 +++++ 12 files changed, 69 insertions(+), 261 deletions(-) delete mode 100644 src/lib/arch/IArchFile.h delete mode 100644 src/lib/arch/unix/ArchFileUnix.cpp delete mode 100644 src/lib/arch/unix/ArchFileUnix.h delete mode 100644 src/lib/arch/win32/ArchFileWindows.cpp delete mode 100644 src/lib/arch/win32/ArchFileWindows.h create mode 100644 src/lib/common/PathUtilities.cpp create mode 100644 src/lib/common/PathUtilities.h diff --git a/src/lib/arch/Arch.h b/src/lib/arch/Arch.h index 42a73c2e..c062d6f3 100644 --- a/src/lib/arch/Arch.h +++ b/src/lib/arch/Arch.h @@ -40,7 +40,6 @@ #if SYSAPI_WIN32 # include "arch/win32/ArchConsoleWindows.h" # include "arch/win32/ArchDaemonWindows.h" -# include "arch/win32/ArchFileWindows.h" # include "arch/win32/ArchLogWindows.h" # include "arch/win32/ArchMiscWindows.h" # include "arch/win32/ArchMultithreadWindows.h" @@ -54,7 +53,6 @@ #elif SYSAPI_UNIX # include "arch/unix/ArchConsoleUnix.h" # include "arch/unix/ArchDaemonUnix.h" -# include "arch/unix/ArchFileUnix.h" # include "arch/unix/ArchLogUnix.h" # if HAVE_PTHREAD # include "arch/unix/ArchMultithreadPosix.h" @@ -86,7 +84,6 @@ typically at the beginning of \c main(). */ class Arch : public ARCH_CONSOLE, public ARCH_DAEMON, - public ARCH_FILE, public ARCH_LOG, public ARCH_MULTITHREAD, public ARCH_NETWORK, diff --git a/src/lib/arch/IArchFile.h b/src/lib/arch/IArchFile.h deleted file mode 100644 index 88cb7792..00000000 --- a/src/lib/arch/IArchFile.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include "common/IInterface.h" -#include "common/stdstring.h" -#include "base/String.h" - -//! Interface for architecture dependent file system operations -/*! -This interface defines the file system operations required by -barrier. Each architecture must implement this interface. -*/ -class IArchFile : public IInterface { -public: - //! @name manipulators - //@{ - - //! Extract base name - /*! - Find the base name in the given \c pathname. - */ - virtual const char* getBasename(const char* pathname) = 0; - - //! Concatenate path components - /*! - Concatenate pathname components with a directory separator - between them. This should not check if the resulting path - is longer than allowed by the system; we'll rely on the - system calls to tell us that. - */ - virtual std::string concatPath( - const std::string& prefix, - const std::string& suffix) = 0; -}; diff --git a/src/lib/arch/unix/ArchFileUnix.cpp b/src/lib/arch/unix/ArchFileUnix.cpp deleted file mode 100644 index 206f1f18..00000000 --- a/src/lib/arch/unix/ArchFileUnix.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "arch/unix/ArchFileUnix.h" -#include "common/DataDirectories.h" - -#include -#include -#include -#include -#include - -// -// ArchFileUnix -// - -const char* -ArchFileUnix::getBasename(const char* pathname) -{ - if (pathname == NULL) { - return NULL; - } - - const char* basename = strrchr(pathname, '/'); - if (basename != NULL) { - return basename + 1; - } - else { - return pathname; - } -} - -std::string -ArchFileUnix::concatPath(const std::string& prefix, - const std::string& suffix) -{ - std::string path; - path.reserve(prefix.size() + 1 + suffix.size()); - path += prefix; - if (path.size() == 0 || path[path.size() - 1] != '/') { - path += '/'; - } - path += suffix; - return path; -} diff --git a/src/lib/arch/unix/ArchFileUnix.h b/src/lib/arch/unix/ArchFileUnix.h deleted file mode 100644 index b39f9827..00000000 --- a/src/lib/arch/unix/ArchFileUnix.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include "arch/IArchFile.h" - -#define ARCH_FILE ArchFileUnix - -//! Unix implementation of IArchFile -class ArchFileUnix : public IArchFile { -public: - // IArchFile overrides - virtual const char* getBasename(const char* pathname); - virtual std::string concatPath(const std::string& prefix, - const std::string& suffix); -}; diff --git a/src/lib/arch/win32/ArchFileWindows.cpp b/src/lib/arch/win32/ArchFileWindows.cpp deleted file mode 100644 index 8f362551..00000000 --- a/src/lib/arch/win32/ArchFileWindows.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "arch/win32/ArchFileWindows.h" -#include "common/DataDirectories.h" - -#define WIN32_LEAN_AND_MEAN -#include -#include -#include -#include - -// -// ArchFileWindows -// - -const char* -ArchFileWindows::getBasename(const char* pathname) -{ - if (pathname == NULL) { - return NULL; - } - - // check for last slash - const char* basename = strrchr(pathname, '/'); - if (basename != NULL) { - ++basename; - } - else { - basename = pathname; - } - - // check for last backslash - const char* basename2 = strrchr(pathname, '\\'); - if (basename2 != NULL && basename2 > basename) { - basename = basename2 + 1; - } - - return basename; -} - -std::string -ArchFileWindows::concatPath(const std::string& prefix, - const std::string& suffix) -{ - std::string path; - path.reserve(prefix.size() + 1 + suffix.size()); - path += prefix; - if (path.size() == 0 || - (path[path.size() - 1] != '\\' && - path[path.size() - 1] != '/')) { - path += '\\'; - } - path += suffix; - return path; -} diff --git a/src/lib/arch/win32/ArchFileWindows.h b/src/lib/arch/win32/ArchFileWindows.h deleted file mode 100644 index 2bfa316d..00000000 --- a/src/lib/arch/win32/ArchFileWindows.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * barrier -- mouse and keyboard sharing utility - * Copyright (C) 2012-2016 Symless Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include "arch/IArchFile.h" - -#define ARCH_FILE ArchFileWindows - -//! Win32 implementation of IArchFile -class ArchFileWindows : public IArchFile { -public: - // IArchFile overrides - virtual const char* getBasename(const char* pathname); - virtual std::string concatPath(const std::string& prefix, - const std::string& suffix); -}; diff --git a/src/lib/barrier/ArgParser.cpp b/src/lib/barrier/ArgParser.cpp index 1ce918c7..df478ab9 100644 --- a/src/lib/barrier/ArgParser.cpp +++ b/src/lib/barrier/ArgParser.cpp @@ -24,6 +24,7 @@ #include "barrier/ArgsBase.h" #include "base/Log.h" #include "base/String.h" +#include "common/PathUtilities.h" #ifdef WINAPI_MSWINDOWS #include @@ -455,7 +456,7 @@ void ArgParser::updateCommonArgs(const char* const* argv) { argsBase().m_name = ARCH->getHostName(); - argsBase().m_pname = ARCH->getBasename(argv[0]); + argsBase().m_pname = PathUtilities::basename(argv[0]).c_str(); } bool diff --git a/src/lib/barrier/ClientApp.cpp b/src/lib/barrier/ClientApp.cpp index 87686f29..23c0ea36 100644 --- a/src/lib/barrier/ClientApp.cpp +++ b/src/lib/barrier/ClientApp.cpp @@ -40,10 +40,7 @@ #include "base/TMethodJob.h" #include "base/Log.h" #include "common/Version.h" - -#if SYSAPI_WIN32 -#include "arch/win32/ArchMiscWindows.h" -#endif +#include "common/PathUtilities.h" #if WINAPI_MSWINDOWS #include "platform/MSWindowsScreen.h" @@ -519,7 +516,7 @@ ClientApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc { // general initialization m_serverAddress = new NetworkAddress; - args().m_pname = ARCH->getBasename(argv[0]); + args().m_pname = PathUtilities::basename(argv[0]).c_str(); // install caller's output filter if (outputter != NULL) { diff --git a/src/lib/barrier/ServerApp.cpp b/src/lib/barrier/ServerApp.cpp index 240db855..c6e31a5d 100644 --- a/src/lib/barrier/ServerApp.cpp +++ b/src/lib/barrier/ServerApp.cpp @@ -40,6 +40,7 @@ #include "base/TMethodEventJob.h" #include "common/Version.h" #include "common/DataDirectories.h" +#include "common/PathUtilities.h" #if SYSAPI_WIN32 #include "arch/win32/ArchMiscWindows.h" @@ -143,8 +144,8 @@ ServerApp::help() << std::endl << "If no configuration file pathname is provided then the first of the" << std::endl << "following to load successfully sets the configuration:" << std::endl - << " $HOME/" << USR_CONFIG_NAME << std::endl - << " " << ARCH->concatPath(DataDirectories::systemconfig(), SYS_CONFIG_NAME) << std::endl; + << " " << PathUtilities::concat(DataDirectories::personal(), SYS_CONFIG_NAME) << std::endl + << " " << PathUtilities::concat(DataDirectories::systemconfig(), SYS_CONFIG_NAME) << std::endl; LOG((CLOG_PRINT "%s", buffer.str().c_str())); } @@ -185,7 +186,7 @@ ServerApp::loadConfig() String path = DataDirectories::personal(); if (!path.empty()) { // complete path - path = ARCH->concatPath(path, USR_CONFIG_NAME); + path = PathUtilities::concat(path, USR_CONFIG_NAME); // now try loading the user's configuration if (loadConfig(path)) { @@ -197,7 +198,7 @@ ServerApp::loadConfig() // try the system-wide config file path = DataDirectories::systemconfig(); if (!path.empty()) { - path = ARCH->concatPath(path, SYS_CONFIG_NAME); + path = PathUtilities::concat(path, SYS_CONFIG_NAME); if (loadConfig(path)) { loaded = true; args().m_configFile = path; @@ -780,7 +781,7 @@ ServerApp::runInner(int argc, char** argv, ILogOutputter* outputter, StartupFunc // general initialization m_barrierAddress = new NetworkAddress; args().m_config = new Config(m_events); - args().m_pname = ARCH->getBasename(argv[0]); + args().m_pname = PathUtilities::basename(argv[0]).c_str(); // install caller's output filter if (outputter != NULL) { diff --git a/src/lib/common/DataDirectories.h b/src/lib/common/DataDirectories.h index ddb903e4..edc604a6 100644 --- a/src/lib/common/DataDirectories.h +++ b/src/lib/common/DataDirectories.h @@ -26,4 +26,3 @@ private: static std::string _global; static std::string _systemconfig; }; - diff --git a/src/lib/common/PathUtilities.cpp b/src/lib/common/PathUtilities.cpp new file mode 100644 index 00000000..9859e048 --- /dev/null +++ b/src/lib/common/PathUtilities.cpp @@ -0,0 +1,45 @@ +#include "PathUtilities.h" + +// keep the default platform delimiter as the first in the list +#ifdef _WIN32 +static const char *Delimiters = "\\/"; +#else +static const char *Delimiters = "/"; +#endif + +static const char DefaultDelimiter = Delimiters[0]; + +std::string PathUtilities::basename(const std::string& path) +{ + 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) { + // l/r empty + if (left.size() == 0 && right.size() == 0) + return ""; + // r useless, l okay + if (leftEnd > 0) + return left.substr(0, leftEnd); + // both useless but one has a delim + return std::string(1, DefaultDelimiter); + } + if (leftEnd == 0) { + // r okay and not prefixed with delims, l empty + if (left.size() == 0 && rightStart == 0) + return right.substr(rightStart); + // r okay and prefixed with delims OR l full of delims + return DefaultDelimiter + right.substr(rightStart); + } + // normal concatenation + return left.substr(0, leftEnd) + DefaultDelimiter + right.substr(rightStart); +} diff --git a/src/lib/common/PathUtilities.h b/src/lib/common/PathUtilities.h new file mode 100644 index 00000000..993229b6 --- /dev/null +++ b/src/lib/common/PathUtilities.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +class PathUtilities +{ +public: + static std::string basename(const std::string& path); + static std::string concat(const std::string& left, const std::string& right); + +private: + // static class + PathUtilities() {} +};