chore: remove unused ArgParser

This commit is contained in:
sithlord48
2025-09-25 22:42:36 -04:00
committed by Chris Rizzitello
parent d98f8a524d
commit 5978694d95
9 changed files with 0 additions and 458 deletions

View File

@ -1,195 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "deskflow/ArgParser.h"
#include "base/Log.h"
#include "deskflow/App.h"
#ifdef WINAPI_MSWINDOWS
#include <VersionHelpers.h>
#endif
#include <QFileInfo>
#include <QSysInfo>
ArgParser::ArgParser(App *app) : m_app(app)
{
}
bool ArgParser::parseGenericArgs(int argc, const char *const *argv, int &i) const
{
if (isArg(i, argc, argv, "-h", "--help")) {
if (m_app) {
m_app->help();
}
} else {
// option not supported here
return false;
}
return true;
}
bool ArgParser::parseDeprecatedArgs(int argc, const char *const *argv, int &i) const
{
static const std::vector<const char *> deprecatedArgs = {"--crypto-pass", "--res-w", "--res-h",
"--prm-wc", "--prm-hc", "--log"};
for (auto &arg : deprecatedArgs) {
if (isArg(i, argc, argv, nullptr, arg)) {
LOG_NOTE("%s is deprecated", arg);
i++;
return true;
}
}
return false;
}
bool ArgParser::isArg(
int argi, int argc, const char *const *argv, const char *name1, const char *name2, int minRequiredParameters
)
{
if ((name1 != nullptr && strcmp(argv[argi], name1) == 0) || (name2 != nullptr && strcmp(argv[argi], name2) == 0)) {
// match. check args left.
if (argi + minRequiredParameters >= argc) {
LOG_PRINT("%s: missing arguments for `%s'" BYE, "deskflow-core", argv[argi], "deskflow-core");
return false;
}
return true;
}
// no match
return false;
}
void ArgParser::splitCommandString(const std::string_view &command, std::vector<std::string> &argv)
{
if (command.empty()) {
return;
}
size_t leftDoubleQuote = 0;
size_t rightDoubleQuote = 0;
searchDoubleQuotes(command, leftDoubleQuote, rightDoubleQuote);
size_t startPos = 0;
size_t space = command.find(" ", startPos);
while (space != std::string::npos) {
bool ignoreThisSpace = false;
// check if the space is between two double quotes
if (space > leftDoubleQuote && space < rightDoubleQuote) {
ignoreThisSpace = true;
} else if (space > rightDoubleQuote) {
searchDoubleQuotes(command, leftDoubleQuote, rightDoubleQuote, rightDoubleQuote + 1);
}
if (!ignoreThisSpace) {
auto subString = command.substr(startPos, space - startPos);
removeDoubleQuotes(subString);
argv.emplace_back(subString);
}
// find next space
if (ignoreThisSpace) {
space = command.find(" ", rightDoubleQuote + 1);
} else {
startPos = space + 1;
space = command.find(" ", startPos);
}
}
auto subString = command.substr(startPos, command.size());
removeDoubleQuotes(subString);
argv.emplace_back(subString);
}
bool ArgParser::searchDoubleQuotes(const std::string_view &command, size_t &left, size_t &right, size_t startPos)
{
bool result = false;
left = std::string::npos;
right = std::string::npos;
left = command.find("\"", startPos);
if (left != std::string::npos) {
right = command.find("\"", left + 1);
if (right != std::string::npos) {
result = true;
}
}
if (!result) {
left = 0;
right = 0;
}
return result;
}
void ArgParser::removeDoubleQuotes(std::string_view &arg)
{
// if string is surrounded by double quotes, remove them
if (arg[0] == '\"' && arg[arg.size() - 1] == '\"') {
arg = arg.substr(1, arg.size() - 2);
}
}
const char **ArgParser::getArgv(std::vector<std::string> &argsArray)
{
size_t argc = argsArray.size();
// caller is responsible for deleting the outer array only
// we use the c string pointers from argsArray and assign
// them to the inner array. So caller only need to use
// delete[] to delete the outer array
const auto **argv = new const char *[argc];
for (size_t i = 0; i < argc; i++) {
argv[i] = argsArray[i].c_str();
}
return argv;
}
std::string ArgParser::assembleCommand(
std::vector<std::string> &argsArray, const std::string_view &ignoreArg, int parametersRequired
)
{
std::string result;
for (auto it = argsArray.begin(); it != argsArray.end(); ++it) {
if (it->compare(ignoreArg) == 0) {
it = it + parametersRequired;
continue;
}
// if there is a space in this arg, use double quotes surround it
if ((*it).find(" ") != std::string::npos) {
(*it).insert(0, "\"");
(*it).push_back('\"');
}
result.append(*it);
// add space to saperate args
result.append(" ");
}
if (!result.empty()) {
// remove the tail space
result = result.substr(0, result.size() - 1);
}
return result;
}
bool ArgParser::checkUnexpectedArgs() const
{
return false;
}

View File

@ -1,44 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
#include <string>
#include <vector>
namespace deskflow {
class ServerArgs;
} // namespace deskflow
class App;
class ArgParser
{
public:
explicit ArgParser(App *app);
bool parseGenericArgs(int argc, const char *const *argv, int &i) const;
bool parseDeprecatedArgs(int argc, const char *const *argv, int &i) const;
static bool isArg(
int argi, int argc, const char *const *argv, const char *name1, const char *name2, int minRequiredParameters = 0
);
static void splitCommandString(const std::string_view &command, std::vector<std::string> &argv);
static bool searchDoubleQuotes(const std::string_view &command, size_t &left, size_t &right, size_t startPos = 0);
static void removeDoubleQuotes(std::string_view &arg);
static const char **getArgv(std::vector<std::string> &argsArray);
static std::string assembleCommand(
std::vector<std::string> &argsArray, const std::string_view &ignoreArg = std::string_view(),
int parametersRequired = 0
);
private:
bool checkUnexpectedArgs() const;
private:
App *m_app;
};

View File

@ -67,8 +67,6 @@ add_library(${lib_name} STATIC ${PLATFORM_CODE}
App.h
AppUtil.cpp
AppUtil.h
ArgParser.cpp
ArgParser.h
Chunk.cpp
Chunk.h
ClientApp.cpp

View File

@ -16,7 +16,6 @@
#include "common/Constants.h"
#include "common/ExitCodes.h"
#include "common/Settings.h"
#include "deskflow/ArgParser.h"
#include "deskflow/ProtocolTypes.h"
#include "deskflow/Screen.h"
#include "deskflow/ScreenException.h"

View File

@ -15,7 +15,6 @@
#include "common/ExitCodes.h"
#include "common/Settings.h"
#include "deskflow/App.h"
#include "deskflow/ArgParser.h"
#include "deskflow/Screen.h"
#include "deskflow/ScreenException.h"
#include "net/SocketException.h"

View File

@ -11,7 +11,6 @@
#include "arch/ArchException.h"
#include "base/String.h"
#include "common/Settings.h"
#include "deskflow/ArgParser.h"
#include "net/NetworkAddress.h"
#include "net/SocketMultiplexer.h"
#include "net/TSocketMultiplexerMethodJob.h"

View File

@ -1,175 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "ArgParserTests.h"
// This file is generated at build time
#include <common/Constants.h>
void ArgParserTests::initTestCase()
{
m_arch.init();
m_log.setFilter(LogLevel::Debug2);
}
void ArgParserTests::isArg()
{
int i = 1;
const int argc = 2;
const char *argShort[argc] = {"stub", "-t"};
QVERIFY(ArgParser::isArg(i, argc, argShort, "-t", NULL));
const char *argLong[argc] = {"stub", "--test"};
QVERIFY(ArgParser::isArg(i, argc, argLong, NULL, "--test"));
}
void ArgParserTests::missingArg()
{
int i = 1;
const int argc = 2;
const char *argv[argc] = {"stub", "-t"};
QVERIFY(!ArgParser::isArg(i, argc, argv, "-t", NULL, 1));
}
void ArgParserTests::withQuotes()
{
std::string command("\"stub\"");
size_t left = 0;
size_t right = 0;
QVERIFY(ArgParser::searchDoubleQuotes(command, left, right));
QCOMPARE(left, 0);
QCOMPARE(right, 5);
command = "stub";
left = 0;
right = 0;
QVERIFY(!ArgParser::searchDoubleQuotes(command, left, right));
QCOMPARE(0, left);
QCOMPARE(0, right);
command = "\"stub";
left = 0;
right = 0;
QVERIFY(!ArgParser::searchDoubleQuotes(command, left, right));
QCOMPARE(left, 0);
QCOMPARE(right, 0);
}
void ArgParserTests::splitCommand()
{
std::string command("stub");
std::vector<std::string> argv;
ArgParser::splitCommandString(command, argv);
QCOMPARE(argv.size(), 1);
QCOMPARE(argv.at(0), "stub");
command = "stub1 stub2";
argv.resize(0);
ArgParser::splitCommandString(command, argv);
QCOMPARE(2, argv.size());
QCOMPARE(argv.at(0), "stub1");
QCOMPARE(argv.at(1), "stub2");
command = "stub1 stub2 stub3";
argv.resize(0);
ArgParser::splitCommandString(command, argv);
QCOMPARE(3, argv.size());
QCOMPARE(argv.at(0), "stub1");
QCOMPARE(argv.at(1), "stub2");
QCOMPARE(argv.at(2), "stub3");
command = "\"stub1\" stub2 \"stub3 space\"";
argv.resize(0);
ArgParser::splitCommandString(command, argv);
QCOMPARE(3, argv.size());
QCOMPARE(argv.at(0), "stub1");
QCOMPARE(argv.at(1), "stub2");
QCOMPARE(argv.at(2), "stub3 space");
}
void ArgParserTests::getArgv()
{
std::vector<std::string> argArray;
argArray.push_back("stub1");
argArray.push_back("stub2");
argArray.push_back("stub3 space");
const char **argv = ArgParser::getArgv(argArray);
std::string row1(argv[0]);
std::string row2(argv[1]);
std::string row3(argv[2]);
QCOMPARE(row1, "stub1");
QCOMPARE(row2, "stub2");
QCOMPARE(row3, "stub3 space");
delete[] argv;
}
void ArgParserTests::assembleCommand()
{
std::vector<std::string> argArray;
argArray.push_back("stub1");
argArray.push_back("stub2");
std::string command = ArgParser::assembleCommand(argArray);
QCOMPARE(command, "stub1 stub2");
argArray.resize(0);
argArray.push_back("stub1");
argArray.push_back("stub2");
command = ArgParser::assembleCommand(argArray, "stub2");
QCOMPARE(command, "stub1");
argArray.resize(0);
argArray.push_back("stub1");
argArray.push_back("stub2");
argArray.push_back("stub3");
argArray.push_back("stub4");
command = ArgParser::assembleCommand(argArray, "stub2", 1);
QCOMPARE(command, "stub1 stub4");
argArray.resize(0);
argArray.push_back("stub1 space");
argArray.push_back("stub2");
argArray.push_back("stub3 space");
command = ArgParser::assembleCommand(argArray);
QCOMPARE(command, "\"stub1 space\" stub2 \"stub3 space\"");
}
void ArgParserTests::deprecatedArg_crypoPass_true()
{
int i = 1;
const int argc = 3;
const char *kCryptoPassCmd[argc] = {"stub", "--crypto-pass", "mock_pass"};
QVERIFY(m_parser.parseDeprecatedArgs(argc, kCryptoPassCmd, i));
QCOMPARE(i, 2);
}
void ArgParserTests::deprecatedArg_crypoPass_false()
{
int i = 1;
const int argc = 3;
const char *kCryptoPassCmd[argc] = {"stub", "--mock-arg", "mock_value"};
QVERIFY(!m_parser.parseDeprecatedArgs(argc, kCryptoPassCmd, i));
QCOMPARE(i, 1);
}
QTEST_MAIN(ArgParserTests)

View File

@ -1,31 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include "base/Log.h"
#include "deskflow/ArgParser.h"
#include <QTest>
class ArgParserTests : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
// Test are run in order top to bottom
void isArg();
void missingArg();
void withQuotes();
void splitCommand();
void getArgv();
void assembleCommand();
void deprecatedArg_crypoPass_true();
void deprecatedArg_crypoPass_false();
private:
Arch m_arch;
Log m_log;
ArgParser m_parser = ArgParser(nullptr);
};

View File

@ -5,14 +5,6 @@ if(WIN32)
set(extra_libs version)
endif()
create_test(
NAME ArgParserTests
DEPENDS app
LIBS arch base ${extra_libs}
SOURCE ArgParserTests.cpp
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/deskflow"
)
create_test(
NAME CoreArgParserTests
DEPENDS app