varpar.cpp v1.0.0-build
Parse variables in an env style for C++ from a .varpar file
Loading...
Searching...
No Matches
vp::Parser Class Reference

#include <VP_Parse.hpp>

Public Member Functions

 Parser (const std::string &_id, const std::string &_file, const std::string &_extension=".varpar")
 Create the object, set errors if there are and set some configuration from the parameters.
 ~Parser ()
 Deconstructor.
ParserStatus checkStatus () const
 Get the constructed starts.
std::string getID () const
 Get the current object ID.
std::string getVal (const std::string &_key) const
 Get value of _key.
ParserReturn parse ()
 Public API wrapper for Parser::__parse() function.

Protected Member Functions

virtual ParserReturn __parse ()
 Private member of the parse function.

Private Attributes

std::string extension
std::string fpath
std::string id
ParserReturn parseRet
ParserStatus status

Detailed Description

Definition at line 35 of file VP_Parse.hpp.

Constructor & Destructor Documentation

◆ Parser()

vp::Parser::Parser ( const std::string & _id,
const std::string & _file,
const std::string & _extension = ".varpar" )

Create the object, set errors if there are and set some configuration from the parameters.

Parameters
_idSet the ID of the object
_fileSet the path of the file
_extensionSet the file extension

Definition at line 16 of file VP_Parse.cpp.

17 : id(_id), fpath(_file), extension(_extension)
18 {
19 if (!fs::exists(_file))
20 {
21 const std::string &error = __VP_ERROR_THROW_DEFAULT_MSG + _file + " : Does not exist";
22
23 std::cerr << error << "\n";
24
25 this->status.error = error;
26 this->status.constructed = false;
27
28 return;
29 }
30
31 const std::string &extensionSubstr = this->fpath.substr(fpath.size() - _extension.size());
32
33 if (extensionSubstr != _extension)
34 {
35 const std::string &error = __VP_ERROR_THROW_DEFAULT_MSG + _file + " : Does not have the required file extension, required is: " + _extension;
36
37 std::cerr << error << "\n";
38
39 this->status.error = error;
40 this->status.constructed = false;
41
42 return;
43 }
44
45 this->status.constructed = true;
46 }
#define __VP_ERROR_THROW_DEFAULT_MSG
Definition VP_Parse.cpp:11
std::string fpath
Definition VP_Parse.hpp:39
std::string extension
Definition VP_Parse.hpp:44
std::string id
Definition VP_Parse.hpp:38
ParserStatus status
Definition VP_Parse.hpp:41

References __VP_ERROR_THROW_DEFAULT_MSG, extension, fpath, id, and status.

◆ ~Parser()

vp::Parser::~Parser ( )

Deconstructor.

Member Function Documentation

◆ __parse()

ParserReturn vp::Parser::__parse ( )
protectedvirtual

Private member of the parse function.

Returns
ParserReturn Parsed information

Definition at line 55 of file VP_Parse.cpp.

56 {
57 std::vector<std::string> output;
58
59 std::map<std::string, std::string> conf;
60 std::ifstream file(this->fpath);
61 std::string line;
62
63 ssize_t lineNum = 0;
64
65 while (std::getline(file, line))
66 {
67 lineNum++;
68
69 // Skip if comment or empty line
70 if (line.empty() || line[0] == ';') continue;
71
72 std::istringstream iss(line);
73 std::string key, val;
74
75 if (std::getline(iss, key, '=') && std::getline(iss, val))
76 {
77 key.erase(0, key.find_first_not_of(" \t"));
78 key.erase(key.find_last_not_of(" \t") + 1);
79
80 val.erase(0, val.find_first_not_of(" \t"));
81 val.erase(val.find_last_not_of(" \t") + 1);
82
83 if (conf.find(key) != conf.end())
84 {
85 output.emplace_back(std::string("ERR: LINE: " + std::to_string(lineNum)));
86 output.emplace_back("Duplicate member : " + line);
87
88 return { conf, output, false };
89 }
90
91 conf[key] = val;
92 }
93
94 else
95 {
96 output.emplace_back(std::string("ERR: LINE: " + std::to_string(lineNum)));
97 output.emplace_back(std::string("No equality operator to set value for `" + line + "`"));
98
99 return { conf, output, false };
100 }
101 }
102
103 return { conf, output, true };
104 }

References fpath.

Referenced by parse().

◆ checkStatus()

ParserStatus vp::Parser::checkStatus ( ) const

Get the constructed starts.

Returns
ParserStatus

Definition at line 114 of file VP_Parse.cpp.

115 { return this->status; }

References status.

◆ getID()

std::string vp::Parser::getID ( ) const

Get the current object ID.

Returns
std::string Current object ID

Definition at line 117 of file VP_Parse.cpp.

118 { return this->id; }

References id.

◆ getVal()

std::string vp::Parser::getVal ( const std::string & _key) const

Get value of _key.

Parameters
_keyKey value to find
Returns
std::string Return the value

Definition at line 106 of file VP_Parse.cpp.

107 {
108 if (this->parseRet.config.find(_key) != this->parseRet.config.end())
109 return this->parseRet.config.at(_key);
110
111 return "";
112 }
ParserReturn parseRet
Definition VP_Parse.hpp:42

References parseRet.

◆ parse()

ParserReturn vp::Parser::parse ( )

Public API wrapper for Parser::__parse() function.

Returns
ParserReturn Parsed information

Definition at line 48 of file VP_Parse.cpp.

49 {
50 this->parseRet = this->__parse();
51
52 return this->parseRet;
53 }
virtual ParserReturn __parse()
Private member of the parse function.
Definition VP_Parse.cpp:55

References __parse(), and parseRet.

Member Data Documentation

◆ extension

std::string vp::Parser::extension
private

Definition at line 44 of file VP_Parse.hpp.

Referenced by Parser().

◆ fpath

std::string vp::Parser::fpath
private

Definition at line 39 of file VP_Parse.hpp.

Referenced by Parser(), and __parse().

◆ id

std::string vp::Parser::id
private

Definition at line 38 of file VP_Parse.hpp.

Referenced by Parser(), and getID().

◆ parseRet

ParserReturn vp::Parser::parseRet
private

Definition at line 42 of file VP_Parse.hpp.

Referenced by getVal(), and parse().

◆ status

ParserStatus vp::Parser::status
private

Definition at line 41 of file VP_Parse.hpp.

Referenced by Parser(), and checkStatus().


The documentation for this class was generated from the following files: