Skip to content

Commit 2675bb6

Browse files
author
Marcus O'Flaherty
committed
add Store::Initialise based on stringstream rather than string filename. Return false on failure to parse a config file line (which isn't a comment or empty), rather than silently ignoring it
1 parent 232dded commit 2675bb6

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/Store/Store.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,51 @@ bool Store::Initialise(std::string filename){
99
std::ifstream file(filename.c_str());
1010
std::string line;
1111

12+
bool all_ok = true;
1213
if(file.is_open()){
1314

1415
while (getline(file,line)){
1516
if (line.size()>0){
16-
if (line.at(0)=='#')continue;
17-
std::string key;
18-
std::string value;
19-
std::stringstream stream(line);
20-
if(stream>>key>>value) m_variables[key]=value;
17+
if(line.at(0)=='#') continue;
18+
std::string key;
19+
std::string value;
20+
std::stringstream stream(line);
21+
if(stream>>key>>value) m_variables[key]=value;
22+
else {
23+
std::cout<<"Store::Initialise failed to parse line '"<<line<<"'"<<std::endl;
24+
all_ok = false;
25+
}
2126
}
22-
2327
}
28+
2429
file.close();
2530
}
2631
else{
2732
std::cout<<"\033[38;5;196m WARNING!!!: Config file "<<filename<<" does not exist no config loaded \033[0m"<<std::endl;
2833
return false;
2934
}
3035

31-
return true;
36+
return all_ok;
37+
}
38+
39+
bool Store::Initialise(std::stringstream& inputstream){
40+
41+
bool all_ok = true;
42+
std::string line;
43+
while(getline(inputstream, line)){
44+
if(line.empty()) continue;
45+
if(line[0]=='#') continue;
46+
std::string key;
47+
std::string value;
48+
std::stringstream stream(line);
49+
if(stream>>key>>value) m_variables[key]=value;
50+
else {
51+
std::cout<<"Store::Initialise failed to parse line '"<<line<<"'"<<std::endl;
52+
all_ok = false;
53+
}
54+
}
55+
56+
return all_ok;
3257
}
3358

3459
void Store::Print(){

src/Store/Store.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Store{
2424
Store(); ////< Sinple constructor
2525

2626
bool Initialise(std::string filename); ///< Initialises Store by reading in entries from an ASCII text file, when each line is a variable and its value in key value pairs. @param filename The filepath and name to the input file.
27+
bool Initialise(std::stringstream& inputstream); ///< Initialises Store by reading entries from an ASCII text stream. Each line is a variable and its value in key value pairs. @param inputstream The stream to read from.
2728
void JsonParser(std::string input); ///< Converts a flat JSON formatted string to Store entries in the form of key value pairs. @param input The input flat JSON string.
2829
void Print(); ///< Prints the contents of the Store.
2930
void Delete(); ///< Deletes all entries in the Store.

0 commit comments

Comments
 (0)