From 43dab3059706fc02341dca6a1fc29ac41fc00ac4 Mon Sep 17 00:00:00 2001 From: luocai Date: Thu, 21 Dec 2023 17:40:32 +0800 Subject: [PATCH] Remove unused class CommandLineInterpreter. --- Universal/CMakeLists.txt | 1 - Universal/CommandLineInterpreter.cpp | 101 --------------------------- Universal/CommandLineInterpreter.h | 23 ------ 3 files changed, 125 deletions(-) delete mode 100644 Universal/CommandLineInterpreter.cpp delete mode 100644 Universal/CommandLineInterpreter.h diff --git a/Universal/CMakeLists.txt b/Universal/CMakeLists.txt index ae600a8..26e2bb1 100644 --- a/Universal/CMakeLists.txt +++ b/Universal/CMakeLists.txt @@ -3,7 +3,6 @@ find_package(Boost REQUIRED COMPONENTS log log_setup program_options) add_library(Universal BoostLog.h BoostLog.inl BoostLog.cpp BufferUtility.h BufferUtility.cpp - CommandLineInterpreter.h CommandLineInterpreter.cpp DateTime.h DateTime.cpp FunctionTraits.h IoContext.h IoContext.cpp diff --git a/Universal/CommandLineInterpreter.cpp b/Universal/CommandLineInterpreter.cpp deleted file mode 100644 index ee51d09..0000000 --- a/Universal/CommandLineInterpreter.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "CommandLineInterpreter.h" -#include "BoostLog.h" -#include -#include -#include - -CommandLineInterpreter::CommandLineInterpreter(const DescriptionPointer &description, const std::string &prompt) - : m_description(description), m_prompt(prompt) {} - -void CommandLineInterpreter::interpret(std::istream &inputStream) { - std::string command; - std::cout << m_prompt << std::flush; - - while (std::getline(inputStream, command, '\n')) { - handleReadLine(command); - std::cout << m_prompt << std::flush; - } -} - -void CommandLineInterpreter::handleReadLine(std::string line) { - using namespace boost::program_options; - if (m_description.expired()) { - LOG(error) << "description has expired."; - return; - } - auto description = m_description.lock(); - std::vector args; - - // huu, ugly... - args = splitCommandLine(std::string("--") + line); - - try { - variables_map vm; - store(command_line_parser(args).options(*description).run(), vm); - notify(vm); - } catch (boost::program_options::unknown_option &e) { - std::cerr << "error: " << e.what() << std::endl; - } catch (boost::program_options::invalid_command_line_syntax &e) { - std::cerr << "error: " << e.what() << std::endl; - } catch (boost::program_options::validation_error &e) { - std::cerr << "error: " << e.what() << std::endl; - } -} - -std::vector CommandLineInterpreter::splitCommandLine(const std::string &input) { - std::vector result; - - std::string::const_iterator i = input.begin(), e = input.end(); - for (; i != e; ++i) - if (!isspace((unsigned char)*i)) break; - - if (i != e) { - std::string current; - bool inside_quoted = false; - int backslash_count = 0; - - for (; i != e; ++i) { - if (*i == '"') { - // '"' preceded by even number (n) of backslashes generates - // n/2 backslashes and is a quoted block delimiter - if (backslash_count % 2 == 0) { - current.append(backslash_count / 2, '\\'); - inside_quoted = !inside_quoted; - // '"' preceded by odd number (n) of backslashes generates - // (n-1)/2 backslashes and is literal quote. - } else { - current.append(backslash_count / 2, '\\'); - current += '"'; - } - backslash_count = 0; - } else if (*i == '\\') { - ++backslash_count; - } else { - // Not quote or backslash. All accumulated backslashes should be - // added - if (backslash_count) { - current.append(backslash_count, '\\'); - backslash_count = 0; - } - if (isspace((unsigned char)*i) && !inside_quoted) { - // Space outside quoted section terminate the current argument - result.push_back(current); - current.resize(0); - for (; i != e && isspace((unsigned char)*i); ++i) - ; - --i; - } else { - current += *i; - } - } - } - - // If we have trailing backslashes, add them - if (backslash_count) current.append(backslash_count, '\\'); - - // If we have non-empty 'current' or we're still in quoted - // section (even if 'current' is empty), add the last token. - if (!current.empty() || inside_quoted) result.push_back(current); - } - return result; -} diff --git a/Universal/CommandLineInterpreter.h b/Universal/CommandLineInterpreter.h deleted file mode 100644 index 2c42080..0000000 --- a/Universal/CommandLineInterpreter.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef COMMANDLINEINTERPRETER_H -#define COMMANDLINEINTERPRETER_H - -#include -#include - -class CommandLineInterpreter { -public: - using DescriptionPointer = std::shared_ptr; - using DescriptionWeakPointer = std::weak_ptr; - CommandLineInterpreter(const DescriptionPointer &description, const std::string &prompt); - void interpret(std::istream &inputStream); - -protected: - void handleReadLine(std::string line); - static std::vector splitCommandLine(const std::string &input); - -private: - const DescriptionWeakPointer m_description; - std::string m_prompt; -}; - -#endif // COMMANDLINEINTERPRETER_H