Skip to content

Commit

Permalink
Merge branch 'UshakovMV_Optimization'
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilLord666 committed May 12, 2017
2 parents 3c69ce9 + 7fc96f1 commit 9720071
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 33 deletions.
Binary file added MSVS/EasyCmdLineReader.1.1.nupkg
Binary file not shown.
2 changes: 1 addition & 1 deletion MSVS/EasyCmdUsageExample/EasyCmdUsageExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int main(int argc, char **argv)
std::vector<std::string> keyValueSeparatorViaEquals = { "=" };

EasySoftCmdLine::CommandLineOptionsParser parser(windowsCommandLinePrefixes, keyValueSeparatorViaEquals);
std::vector<EasySoftCmdLine::KeyValueOption> pairs = parser.Parse(argc, argv);
std::vector<EasySoftCmdLine::KeyValueOption> pairs = parser.Parse(argc, (const char**)argv);

std::cout << "Key-Value Pairs:" << std::endl << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CXX = g++
CXXFLAGS = -g -O2 -Wall
# 2. Compiler And Linker Keys (man gcc)
LIB_NAME = libEasyCmdLineReader.so
LIB_VERSION_OPTION = 1.0
LIB_VERSION_OPTION = 1.1
LIB_BUILD_DIRECTORY = .

LIB_COMPILE_OPTION = -fPIC
Expand Down
6 changes: 3 additions & 3 deletions src/KeyValueOption.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace EasySoftCmdLine
class EXPORT KeyValueOption
{
public:
KeyValueOption(std::string key, std::string value) :_key(key), _value(value) { }
std::string GetKey() {return _key;}
std::string GetValue() {return _value;}
KeyValueOption(const std::string& key, const std::string& value) :_key(key), _value(value) { }
const std::string& GetKey() const {return _key;}
const std::string& GetValue() const {return _value;}
private:
std::string _key;
std::string _value;
Expand Down
11 changes: 6 additions & 5 deletions src/manager/CommandLineOptionsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
namespace EasySoftCmdLine
{
CommandLineOptionsManager :: CommandLineOptionsManager(EasySoftCmdLine::ICommandLineOptionValidator* validator,
int argc, char** argv, bool linuxStyle, bool useSpaces, bool interactiveMode)
const int argc, const char** argv, const bool linuxStyle,
const bool useSpaces, const bool interactiveMode)
{
_validator = std::shared_ptr<EasySoftCmdLine::ICommandLineOptionValidator>(validator);
std::vector<std::string> prefix;
Expand All @@ -24,9 +25,9 @@ namespace EasySoftCmdLine
exit(1);
}

bool CommandLineOptionsManager :: TryGetValue(std::string key, std::string& outValue)
bool CommandLineOptionsManager :: TryGetValue(const std::string& key, std::string& outValue) const
{
std::vector<KeyValueOption>::iterator it = std::find_if(_programOptions.begin(), _programOptions.end(),
std::vector<KeyValueOption>::const_iterator it = std::find_if(_programOptions.begin(), _programOptions.end(),
[key](EasySoftCmdLine::KeyValueOption item)
{
return strcmp(item.GetKey().c_str(), key.c_str()) == 0;
Expand All @@ -37,7 +38,7 @@ namespace EasySoftCmdLine
return true;
}

bool CommandLineOptionsManager :: TryGetIntValue(std::string key, int& outValue)
bool CommandLineOptionsManager :: TryGetIntValue(const std::string& key, int& outValue) const
{
std::string value;
if(!TryGetValue(key, value))
Expand All @@ -46,7 +47,7 @@ namespace EasySoftCmdLine
return true;
}

bool CommandLineOptionsManager :: TryGetDoubleValue(std::string key, double& outValue)
bool CommandLineOptionsManager :: TryGetDoubleValue(const std::string& key, double& outValue) const
{
std::string value;
if(!TryGetValue(key, value))
Expand Down
10 changes: 5 additions & 5 deletions src/manager/CommandLineOptionsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace EasySoftCmdLine
class EXPORT CommandLineOptionsManager
{
public:
CommandLineOptionsManager(EasySoftCmdLine::ICommandLineOptionValidator* validator, int argc, char** argv,
bool linuxStyle = true, bool useSpaces = true, bool interactiveMode = false);
bool TryGetValue(std::string key, std::string& outValue);
bool TryGetIntValue(std::string key, int& outValue);
bool TryGetDoubleValue(std::string key, double& outValue);
CommandLineOptionsManager(EasySoftCmdLine::ICommandLineOptionValidator* validator, const int argc, const char** argv,
const bool linuxStyle = true, const bool useSpaces = true, const bool interactiveMode = false);
bool TryGetValue(const std::string& key, std::string& outValue) const;
bool TryGetIntValue(const std::string& key, int& outValue) const;
bool TryGetDoubleValue(const std::string& key, double& outValue) const;
std::vector<EasySoftCmdLine::KeyValueOption>& GetParsedOptions() {return _programOptions;}
private:
std::shared_ptr<EasySoftCmdLine::CommandLineOptionsParser> _parser;
Expand Down
17 changes: 9 additions & 8 deletions src/parser/CommandLineOptionsParser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <string.h>
#include <future>
#include "CommandLineOptionsParser.h"

namespace EasySoftCmdLine
Expand All @@ -10,7 +11,7 @@ namespace EasySoftCmdLine
_interactiveMode = interactiveMode;
}

std::vector<KeyValueOption>& CommandLineOptionsParser :: Parse(int argc, char** argv)
std::vector<KeyValueOption>& CommandLineOptionsParser :: Parse(const int argc, const char** argv)
{
try
{
Expand Down Expand Up @@ -56,25 +57,25 @@ namespace EasySoftCmdLine

std::string& CommandLineOptionsParser :: GetKeyValueSeparator(std::string& argumentString)
{
std::vector<std::string>:: iterator it;
std::vector<std::string>::iterator it;
for(it = _keyValueSeparators.begin(); it != _keyValueSeparators.end(); it++)
if(argumentString.find(*it) != std::string::npos)
return (*it);
throw ("Something goes wrong!");
return (*it);
throw std::runtime_error("Something goes wrong at getting key value separator!");
}

bool CommandLineOptionsParser :: CheckValueIsPresent(std::string& argumentString)
bool CommandLineOptionsParser :: CheckValueIsPresent(std::string& argumentString) const
{
std::vector<std::string>:: iterator it;
std::vector<std::string>::const_iterator it;
for(it = _keyValueSeparators.begin(); it != _keyValueSeparators.end(); it++)
if(argumentString.find(*it) != std::string::npos)
return true;
return false;
}

bool CommandLineOptionsParser :: CheckIsKey(std::string& argumentString)
bool CommandLineOptionsParser :: CheckIsKey(std::string& argumentString) const
{
std::vector<std::string>:: iterator it;
std::vector<std::string>::const_iterator it;
for(it = _optionPrefixes.begin(); it!= _optionPrefixes.end(); it++)
if(strncmp((*it).c_str(), argumentString.c_str(), (*it).size()) == 0)
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/parser/CommandLineOptionsParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ namespace EasySoftCmdLine
{
public:
CommandLineOptionsParser(std::vector<std::string>& optionPrefixes, std::vector<std::string>& keyValueSeparators, bool interactiveMode = false);
std::vector<EasySoftCmdLine::KeyValueOption>& Parse(int argc, char** argv);
std::vector<EasySoftCmdLine::KeyValueOption>& Parse(const int argc, const char** argv);
private:
bool CheckIsKey(std::string& argumentString);
bool CheckValueIsPresent(std::string& argumentString);
bool CheckIsKey(std::string& argumentString) const;
bool CheckValueIsPresent(std::string& argumentString) const;
std::string& GetKeyValueSeparator(std::string& argumentString);
private:
bool _interactiveMode;
Expand Down
2 changes: 1 addition & 1 deletion src/validator/ICommandLineOptionValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace EasySoftCmdLine
class EXPORT ICommandLineOptionValidator
{
public:
virtual bool Validate(std::vector<EasySoftCmdLine::KeyValueOption> options, bool useWithoutOptions = true, bool printHelp = true);
virtual bool Validate(const std::vector<EasySoftCmdLine::KeyValueOption>& options, const bool useWithoutOptions = true, const bool printHelp = true) const;
virtual ~ICommandLineOptionValidator(){}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/validator/ICommandLineOptionsValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace EasySoftCmdLine
{
bool ICommandLineOptionValidator :: Validate(std::vector<EasySoftCmdLine::KeyValueOption> options, bool useWithoutOptions, bool printHelp)
bool ICommandLineOptionValidator :: Validate(const std::vector<EasySoftCmdLine::KeyValueOption>& options, const bool useWithoutOptions, const bool printHelp) const
{
if(!useWithoutOptions && options.size() < 2)
{
Expand Down
10 changes: 5 additions & 5 deletions tests/parser/TestCommandLineOptionsParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ namespace EasySoftCmdLineTests

//std::string simpleValidString = "./someapp --help";
//std::string simpleValidString2 = "./someapp -l \"abcdefgh\" --verbose";
char* onlyHelpOption[] = {"./someapp", "--help"};
const char* onlyHelpOption[] = {"./someapp", "--help"};
std::vector<EasySoftCmdLine::KeyValueOption> expectedOnlyHelpOptions = {EasySoftCmdLine::KeyValueOption(EXECUTION_PATH_KEY, "./someapp"),
EasySoftCmdLine::KeyValueOption("--help", "")};
char* optionsWithRealValues[] = {"./someapp", "-l", "abcdefgh", "--verbose"};
const char* optionsWithRealValues[] = {"./someapp", "-l", "abcdefgh", "--verbose"};
std::vector<EasySoftCmdLine::KeyValueOption> expectedOptionsWithRealValues = {EasySoftCmdLine::KeyValueOption(EXECUTION_PATH_KEY, "./someapp"),
EasySoftCmdLine::KeyValueOption("-l", "abcdefgh"),
EasySoftCmdLine::KeyValueOption("--verbose", "")};
char* optionsWithRealValuesSeparatedWithEquals[] = {"./someapp", "-l=abcdefgh", "--device=/dev/usb1", "--subcarrier=2"};
const char* optionsWithRealValuesSeparatedWithEquals[] = {"./someapp", "-l=abcdefgh", "--device=/dev/usb1", "--subcarrier=2"};
std::vector<EasySoftCmdLine::KeyValueOption> expectedOptionsWithRealValuesSeparatedWithEquals = {EasySoftCmdLine::KeyValueOption(EXECUTION_PATH_KEY, "./someapp"),
EasySoftCmdLine::KeyValueOption("-l", "abcdefgh"),
EasySoftCmdLine::KeyValueOption("--device", "/dev/usb1"),
EasySoftCmdLine::KeyValueOption("--subcarrier", "2")};
char* optionsWithoutPrefixes[] = {"./someapp", "abc", "def=def", "aaa", "lll"};
const char* optionsWithoutPrefixes[] = {"./someapp", "abc", "def=def", "aaa", "lll"};
std::vector<EasySoftCmdLine::KeyValueOption> expectedOptionsWithoutPrefixes = {EasySoftCmdLine::KeyValueOption(EXECUTION_PATH_KEY, "./someapp")};
char* optionsWithRealValuesSeparatedWithEqualsWinStyle[] = {".\\someapp.exe", "/l=abcdefgh", "/device=usb1", "/subcarrier=2"};
const char* optionsWithRealValuesSeparatedWithEqualsWinStyle[] = {".\\someapp.exe", "/l=abcdefgh", "/device=usb1", "/subcarrier=2"};
std::vector<EasySoftCmdLine::KeyValueOption> expectedOptionsWithRealValuesSeparatedWithEqualsWinStyle = {EasySoftCmdLine::KeyValueOption(EXECUTION_PATH_KEY, ".\\someapp.exe"),
EasySoftCmdLine::KeyValueOption("/l", "abcdefgh"),
EasySoftCmdLine::KeyValueOption("/device", "usb1"),
Expand Down

0 comments on commit 9720071

Please sign in to comment.