Able to parse simple values

This commit is contained in:
Leonetienne 2021-06-02 14:22:18 +02:00
parent c5b92f2e6e
commit b718bac6eb
20 changed files with 575 additions and 12 deletions

View File

@ -1,4 +1,5 @@
#include "FloatValue.h"
#include <sstream>
FloatValue::FloatValue(const long double& value)
:
@ -7,3 +8,15 @@ FloatValue::FloatValue(const long double& value)
{
return;
}
Value* FloatValue::Deepcopy() const
{
return new FloatValue(value);
}
std::string FloatValue::GetAsOsString() const
{
std::stringstream ss;
ss << "FloatValue: " << value;
return ss.str();
}

View File

@ -1,11 +1,21 @@
#pragma once
#include "Value.h"
#include <ostream>
class FloatValue : public Value
{
public:
FloatValue(const long double& value);
Value* Deepcopy() const override;
friend std::ostream& operator<< (std::ostream& os, const FloatValue& v)
{
return os << v.GetAsOsString();
}
std::string GetAsOsString() const override;
private:
long double value;
};

View File

@ -1,17 +1,141 @@
#include "Hazelnupp.h"
#include "VoidValue.h"
#include "IntValue.h"
#include "FloatValue.h"
#include "StringValue.h"
#include "ListValue.h"
#include "StringTools.h"
#include <iostream>
Hazelnupp::Hazelnupp()
{
return;
}
Hazelnupp::Hazelnupp(int argc, const char* const* argv)
Hazelnupp::Hazelnupp(const int argc, const char* const* argv)
{
Parse(argc, argv);
return;
}
void Hazelnupp::Parse(int argc, const char* const* argv)
void Hazelnupp::Parse(const int argc, const char* const* argv)
{
// Populate raw arguments
PopulateRawArgs(argc, argv);
executableName = std::string(rawArgs[0]);
std::size_t i = 1;
while (i < rawArgs.size())
{
if ((rawArgs[i].length() > 2) && (rawArgs[i].substr(0, 2) == "--"))
{
Parameter* param = nullptr;
i = ParseNextParameter(i, param);
parameters.insert(std::pair<std::string, Parameter*>(param->Key(), param));
}
else
i++;
}
std::cout << "Terminated." << std::endl;
return;
}
std::size_t Hazelnupp::ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par)
{
std::size_t i = parIndex;
const std::string key = rawArgs[parIndex];
std::vector<std::string> values;
// Get values
for (i++; i < rawArgs.size(); i++)
// If not another parameter
if ((rawArgs[i].length() < 2) || (rawArgs[i].substr(0, 2) != "--"))
values.emplace_back(rawArgs[i]);
else
{
break;
}
Value* parsedVal = EvaluateValues(values);
out_Par = new Parameter(key, parsedVal);
delete parsedVal;
parsedVal = nullptr;
return i;
}
void Hazelnupp::PopulateRawArgs(const int argc, const char* const* argv)
{
rawArgs.reserve(argc);
for (int i = 0; i < argc; i++)
rawArgs.emplace_back(std::string(argv[i]));
return;
}
bool Hazelnupp::HasParam(const std::string& key) const
{
return parameters.find(key) != parameters.end();
}
Value* Hazelnupp::EvaluateValues(const std::vector<std::string>& values)
{
// Void-type
if (values.size() == 0)
{
return new VoidValue;
}
// List-type
else if (values.size() > 1)
{
ListValue* newList = new ListValue();
for (const std::string& val : values)
{
Value* tmp = EvaluateValues(std::vector<std::string>({ val }));
newList->AddValue(tmp);
delete tmp;
}
return newList;
}
// Now we're only dealing with a single value
const std::string& val = values[0];
// String
if (!StringTools::IsNumeric(val, true))
return new StringValue(val);
// Numeric
bool isInt;
long double num;
if (StringTools::ParseNumber(val, isInt, num))
{
// Integer
if (isInt)
return new IntValue((long long int)num);
// Double
return new FloatValue(num);
}
// Failed
return nullptr;
}
const std::string& Hazelnupp::GetExecutableName() const
{
return executableName;
}
const Value* Hazelnupp::operator[](const std::string& key)
{
return parameters[key]->Value();
}

View File

@ -1,15 +1,40 @@
#pragma once
#include "Parameter.h"
#include <unordered_map>
#include <vector>
class Hazelnupp
{
public:
Hazelnupp();
Hazelnupp(int argc, const char* const* argv);
Hazelnupp(const int argc, const char* const* argv);
//! Will parse command line arguments
void Parse(int argc, const char* const* argv);
void Parse(const int argc, const char* const* argv);
//! Will return argv[0], the name of the executable.
const std::string& GetExecutableName() const;
//! Will return the value given a key
const Value* operator[](const std::string& key);
//! Will check wether a parameter exists given a key, or not
bool HasParam(const std::string& key) const;
private:
//! Will translate the c-like args to an std::vector
void PopulateRawArgs(const int argc, const char* const* argv);
//! Will parse the next parameter. Returns the index of the next parameter.
std::size_t ParseNextParameter(const std::size_t parIndex, Parameter*& out_Par);
Value* EvaluateValues(const std::vector<std::string>& values);
std::string executableName; //! The path of the executable. Always argv[0]
std::unordered_map<std::string, Parameter*> parameters;
std::vector<std::string> rawArgs;
// EvaluateValues
friend class ListValue;
};

View File

@ -145,6 +145,7 @@
<ClCompile Include="ListValue.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Parameter.cpp" />
<ClCompile Include="StringTools.cpp" />
<ClCompile Include="StringValue.cpp" />
<ClCompile Include="Value.cpp" />
<ClCompile Include="VoidValue.cpp" />
@ -156,6 +157,7 @@
<ClInclude Include="ListValue.h" />
<ClInclude Include="Parameter.h" />
<ClInclude Include="DataType.h" />
<ClInclude Include="StringTools.h" />
<ClInclude Include="StringValue.h" />
<ClInclude Include="Value.h" />
<ClInclude Include="VoidValue.h" />

View File

@ -42,6 +42,9 @@
<ClCompile Include="ListValue.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="StringTools.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Hazelnupp.h">
@ -71,5 +74,8 @@
<ClInclude Include="FloatValue.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="StringTools.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
#include "IntValue.h"
#include <sstream>
IntValue::IntValue(const long long int& value)
:
@ -7,3 +8,15 @@ IntValue::IntValue(const long long int& value)
{
return;
}
Value* IntValue::Deepcopy() const
{
return new IntValue(value);
}
std::string IntValue::GetAsOsString() const
{
std::stringstream ss;
ss << "IntValue: " << value;
return ss.str();
}

View File

@ -6,6 +6,10 @@ class IntValue : public Value
public:
IntValue(const long long int& value);
Value* Deepcopy() const override;
std::string GetAsOsString() const override;
private:
long long int value;
};

View File

@ -1,4 +1,12 @@
#include "ListValue.h"
#include "Hazelnupp.h"
#include <sstream>
ListValue::ListValue() :
Value(DATA_TYPE::LIST)
{
return;
}
ListValue::~ListValue()
{
@ -10,8 +18,36 @@ ListValue::~ListValue()
return;
}
Value* ListValue::Deepcopy() const
{
ListValue* newList = new ListValue();
for (const Value* val : value)
newList->AddValue(val);
return newList;
}
void ListValue::AddValue(const Value* value)
{
this->value.emplace_back(new Value(*value));
this->value.emplace_back(value->Deepcopy());
return;
}
std::string ListValue::GetAsOsString() const
{
std::stringstream ss;
ss << "ListValue: [";
for (const Value* val : value)
{
ss << *val;
if (val != value.back())
ss << ", ";
}
ss << "]";
return ss.str();
}

View File

@ -5,11 +5,21 @@
class ListValue : public Value
{
public:
ListValue();
~ListValue();
Value* Deepcopy() const override;
//! Will add this value to the list
void AddValue(const Value* value);
std::string GetAsOsString() const override;
friend std::ostream& operator<< (std::ostream& os, const ListValue& v)
{
return os << v.GetAsOsString();
}
private:
std::vector<Value*> value;
};

View File

@ -1,9 +1,19 @@
#include "Parameter.h"
Parameter::Parameter(const std::string& key, const Value& value)
Parameter::Parameter(const std::string& key, const ::Value* value)
:
key { key },
value { value }
key{ key }
{
this->value = value->Deepcopy();
return;
}
const std::string& Parameter::Key() const
{
return key;
}
const ::Value* Parameter::Value() const
{
return value;
}

View File

@ -1,13 +1,22 @@
#pragma once
#include "Value.h"
#include <string>
#include <ostream>
class Parameter
{
public:
explicit Parameter(const std::string& key, const Value& value);
explicit Parameter(const std::string& key, const Value* value);
const std::string& Key() const;
const Value* Value() const;
friend std::ostream& operator<< (std::ostream& os, const Parameter& p)
{
return os << "{ Key: \"" << p.key << "\" -> " << *p.value << " }";
}
private:
std::string key;
Value value;
::Value* value;
};

188
Hazelnupp/StringTools.cpp Normal file
View File

@ -0,0 +1,188 @@
#include "StringTools.h"
bool StringTools::Contains(const std::string& str, const char c)
{
for (const char& i : str)
if (i == c)
return true;
return false;
}
std::string StringTools::Replace(const std::string str, const char find, const std::string subst)
{
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)
{
if (str[i] != find) ss << str[i];
else ss << subst;
}
return ss.str();
}
std::string StringTools::Replace(const std::string str, const std::string find, const std::string subst)
{
if (find.length() == 0) return str;
std::stringstream ss;
std::size_t posFound = 0;
std::size_t lastFound = 0;
while (posFound != std::string::npos)
{
lastFound = posFound;
posFound = str.find(find, posFound);
if (posFound != std::string::npos)
{
ss << str.substr(lastFound, posFound - lastFound) << subst;
posFound += find.length();
}
else
{
ss << str.substr(lastFound, (str.length()) - lastFound);
}
}
return ss.str();
}
bool StringTools::IsNumeric(const std::string str, const bool allowDecimalPoint)
{
if (str.length() == 0) return false;
bool alreadyParsedDecimalPoint = false;
std::size_t digitCount = 0;
for (std::size_t i = 0; i < str.length(); i++)
{
if (!(
((str[i] >= '0') && (str[i] <= '9')) ||
((str[i] == '-') && (i == 0)) ||
((str[i] == '.') && (allowDecimalPoint) && (!alreadyParsedDecimalPoint) && (digitCount > 0))
)) return false;
// Here we just have to check for the character. Not for any other conditions.
// Why? Because if these conditions failed, the function would have already returned false.
if (((str[i] >= '0') && (str[i] <= '9'))) digitCount++;
if (str[i] == '.') alreadyParsedDecimalPoint = true;
}
// Even if we did not find any invalid chars, we should still return false, if we found no digits at all.
return digitCount > 0;
}
bool StringTools::ParseNumber(const std::string str, bool& out_isInt, long double& out_number)
{
bool isNormalNum = true;
bool isNegative = false;
bool isDecimal = false;
if (str.length() == 0) return false;
if (str[0] == '-') isNegative = true;
if (Contains(str, '.')) isDecimal = true;
if (isDecimal)
{
try
{
out_number = std::stold(str);
out_isInt = false;
}
catch (std::invalid_argument)
{
return false;
}
catch (std::out_of_range)
{
return false;
}
}
else
{
try
{
out_number = (long double)std::stoll(str);
out_isInt = true;
}
catch (std::invalid_argument)
{
return false;
}
catch (std::out_of_range)
{
return false;
}
}
return true;
}
std::vector<std::string> StringTools::SplitString(const std::string str, const char delimiter)
{
if (str.length() == 0) return std::vector<std::string>();
return SplitString(str, delimiter);
}
std::vector<std::string> StringTools::SplitString(const std::string str, const std::string delimiter)
{
if (str.length() == 0) return std::vector<std::string>();
std::vector<std::string> parts;
if (delimiter.length() == 0) // If the delimiter is "" (empty), just split between every single char. Not useful, but logical
{
for (std::size_t i = 0; i < str.length(); i++)
{
parts.push_back(std::string({ str[i] }));
}
return parts;
}
std::size_t posFound = 0;
std::size_t lastFound = 0;
while (posFound != std::string::npos)
{
lastFound = posFound;
posFound = str.find(delimiter, posFound);
std::string found;
if (posFound != std::string::npos)
{
found = str.substr(lastFound, posFound - lastFound);
posFound += delimiter.length();
}
else
{
found = str.substr(lastFound, str.length() - lastFound);
}
parts.push_back(found);
}
return parts;
}
std::string StringTools::ToLower(const std::string str)
{
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)
{
if ((str[i] >= 'A') && (str[i] <= 'Z')) ss << (char)(((int)str[i]) + 32);
else if (str[i] == -60) ss << (char)-28; // AE => ae
else if (str[i] == -42) ss << (char)-10; // OE => oe
else if (str[i] == -36) ss << (char)-4; // UE => ue
else ss << str[i];
}
return ss.str();
}

38
Hazelnupp/StringTools.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#include <string>
#include <sstream>
#include <vector>
#include <cmath>
/// <summary>
/// Internal helper class. Feel free to use it tho, lol
/// </summary>
class StringTools
{
public:
//! Will return wether or not a given char is in a string
static bool Contains(const std::string& str, const char c);
//! Will replace a part of a string with another string
static std::string Replace(const std::string str, const char find, const std::string subst);
//! Will replace a part of a string with another string
static std::string Replace(const std::string str, const std::string find, const std::string subst);
//! Will return true if the given string consists only of digits (including signage)
static bool IsNumeric(const std::string str, const bool allowDecimalPoint = false);
//! Will convert the number in str to a number.
//! Returns wether or not the operation was successful.
//! Also returns wether the number is an integer, or floating point. If int, cast out_number to int.
static bool ParseNumber(const std::string str, bool& out_isInt, long double& out_number);
//! Will split a string by a delimiter char. The delimiter will be excluded!
static std::vector<std::string> SplitString(const std::string str, const char delimiter);
//! Will split a string by a delimiter string. The delimiter will be excluded!
static std::vector<std::string> SplitString(const std::string str, const std::string delimiter);
//! Will make a string all lower-case
static std::string ToLower(const std::string str);
};

View File

@ -1,4 +1,5 @@
#include "StringValue.h"
#include <sstream>
StringValue::StringValue(const std::string& value)
:
@ -7,3 +8,15 @@ StringValue::StringValue(const std::string& value)
{
return;
}
Value* StringValue::Deepcopy() const
{
return new StringValue(value);
}
std::string StringValue::GetAsOsString() const
{
std::stringstream ss;
ss << "StringValue: " << value;
return ss.str();
}

View File

@ -7,6 +7,15 @@ class StringValue : public Value
public:
StringValue(const std::string& value);
Value* Deepcopy() const override;
friend std::ostream& operator<< (std::ostream& os, const StringValue& v)
{
return os << v.GetAsOsString();
}
std::string GetAsOsString() const override;
private:
std::string value;
};

View File

@ -1,8 +1,21 @@
#pragma once
#include "DataType.h"
#include <ostream>
class Value
{
public:
//! Will return a deeopopy of this object
virtual Value* Deepcopy() const = 0;
friend std::ostream& operator<< (std::ostream& os, const Value& v)
{
return os << v.GetAsOsString();
}
//! Will return a string suitable for an std::ostream;
virtual std::string GetAsOsString() const = 0;
protected:
Value(DATA_TYPE type);

View File

@ -6,3 +6,13 @@ VoidValue::VoidValue()
{
return;
}
Value* VoidValue::Deepcopy() const
{
return new VoidValue();
}
std::string VoidValue::GetAsOsString() const
{
return "VoidValue";
}

View File

@ -5,4 +5,13 @@ class VoidValue : public Value
{
public:
VoidValue();
Value* Deepcopy() const override;
friend std::ostream& operator<< (std::ostream& os, const VoidValue& v)
{
return os << "VoidValue";
}
std::string GetAsOsString() const override;
};

View File

@ -1,6 +1,27 @@
#include <iostream>
#include <array>
#include "Hazelnupp.h"
int main()
int main(int argc, char** argv)
{
std::cout << "Hello World!\n";
std::string arg0 = "meinpfad";
std::string arg1 = "--word";
std::string arg2 = "1";
std::string arg3 = "--alfred";
std::string arg4 = "banane7";
std::array<const char*, 5> testArgv = {
arg0.data(),
arg1.data(),
arg2.data(),
arg3.data(),
arg4.data()
};
Hazelnupp args(testArgv.size(), testArgv.data());
//Hazelnupp args(argc, argv);
//std::cout << args.GetExecutableName() << std::endl;
return 0;
}