reimplement path operations basename() and concat() in Common. these were the last bits remaining in ArchFile* so it was removed

This commit is contained in:
walker0643 2018-03-31 22:41:00 -04:00
parent 131a19d478
commit 4c04f39685
12 changed files with 69 additions and 261 deletions

View File

@ -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,

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
};

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "arch/unix/ArchFileUnix.h"
#include "common/DataDirectories.h"
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <cstring>
//
// 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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
};

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "arch/win32/ArchFileWindows.h"
#include "common/DataDirectories.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <shlobj.h>
#include <tchar.h>
#include <string.h>
//
// 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;
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
};

View File

@ -24,6 +24,7 @@
#include "barrier/ArgsBase.h"
#include "base/Log.h"
#include "base/String.h"
#include "common/PathUtilities.h"
#ifdef WINAPI_MSWINDOWS
#include <VersionHelpers.h>
@ -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

View File

@ -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) {

View File

@ -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) {

View File

@ -26,4 +26,3 @@ private:
static std::string _global;
static std::string _systemconfig;
};

View File

@ -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);
}

View File

@ -0,0 +1,14 @@
#pragma once
#include <string>
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() {}
};