varpar.cpp v1.0.0-build
Parse variables in an env style for C++ from a .varpar file
Loading...
Searching...
No Matches
VP_Parse.cpp
Go to the documentation of this file.
1#include <filesystem>
2#include <iostream>
3#include <map>
4#include <fstream>
5#include <sstream>
6
8
9namespace fs = std::filesystem;
10
11#define __VP_ERROR_THROW_DEFAULT_MSG \
12 std::string("Parser(" + this->id + "): ")
13
14namespace vp
15{
16 Parser::Parser(const std::string &_id, const std::string &_file, const std::string &_extension)
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 }
47
49 {
50 this->parseRet = this->__parse();
51
52 return this->parseRet;
53 }
54
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 }
105
106 std::string Parser::getVal(const std::string &_key) const
107 {
108 if (this->parseRet.config.find(_key) != this->parseRet.config.end())
109 return this->parseRet.config.at(_key);
110
111 return "";
112 }
113
115 { return this->status; }
116
117 std::string Parser::getID() const
118 { return this->id; }
119}
#define __VP_ERROR_THROW_DEFAULT_MSG
Definition VP_Parse.cpp:11
std::string fpath
Definition VP_Parse.hpp:39
ParserStatus checkStatus() const
Get the constructed starts.
Definition VP_Parse.cpp:114
std::string extension
Definition VP_Parse.hpp:44
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.
Definition VP_Parse.cpp:16
std::string getVal(const std::string &_key) const
Get value of _key.
Definition VP_Parse.cpp:106
virtual ParserReturn __parse()
Private member of the parse function.
Definition VP_Parse.cpp:55
ParserReturn parse()
Public API wrapper for Parser::__parse() function.
Definition VP_Parse.cpp:48
std::string id
Definition VP_Parse.hpp:38
ParserReturn parseRet
Definition VP_Parse.hpp:42
std::string getID() const
Get the current object ID.
Definition VP_Parse.cpp:117
ParserStatus status
Definition VP_Parse.hpp:41
Returned parser information This includes the configuration, output and success of the Parser.
Definition VP_Parse.hpp:27
Set the error and if the Parser class was constructed.
Definition VP_Parse.hpp:16