Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions src/Store/Store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,73 @@ namespace ToolFramework {
Store::Store(){}


bool Store::Initialise(std::istream& inputstream, std::string filename){

bool all_ok = true;

std::string line;
while (getline(inputstream,line)){
if (line.size()>0){
if (line.at(0)=='#') continue;
std::string key="";
std::string value="";
std::stringstream stream(line);
if(stream>>key>>value){

// continue reading any further tokens on this line, appending to value as a space separated list.
// we also pre- and post-pend with '"' characters, so a line of 'myval cat dog potato #comment'
// will produce a value of '"cat dog potato"'
value='"'+value;
std::string tmp;
stream>>tmp;
while(tmp.length() && tmp[0]!='#'){
value+=" "+tmp;
tmp="";
stream>>tmp;
}

value+='"';

if(value!="\"\"") m_variables[key]=value;
} else {
std::clog<<"\033[38;5;196m WARNING!!!: Store::Initialise failed to parse line '"<<line;
if(!filename.empty()) std::clog<<"' in file '"<<filename;
std::clog<<"' \033[0m"<<std::endl;
all_ok = false;
}
}
}

return all_ok;

}

bool Store::Initialise(std::string filename){

bool all_ok;

std::ifstream file(filename.c_str());
std::string line;

if(file.is_open()){

while (getline(file,line)){
if (line.size()>0){
if (line.at(0)=='#')continue;
std::string key="";
std::string value="";
std::stringstream stream(line);
stream>>key>>value;
std::string tmp;
stream>>tmp;
value='"'+value;

while(tmp.length() && tmp[0]!='#'){
value+=" "+tmp;
tmp="";
stream>>tmp;
}
value+="\"";

if(value!="") m_variables[key]=value;
}

}
all_ok = Initialise(file);
file.close();
}
else{
std::cout<<"\033[38;5;196m WARNING!!!: Config file "<<filename<<" does not exist no config loaded \033[0m"<<std::endl;

} else{

std::clog<<"\033[38;5;196m WARNING!!!: Config file "<<filename<<" does not exist no config loaded \033[0m"<<std::endl;
return false;

}

return true;
return all_ok;
}

void Store::Print(){

for (std::map<std::string,std::string>::iterator it=m_variables.begin(); it!=m_variables.end(); ++it){

std::cout<< it->first << " => " << it->second <<std::endl;
std::clog<< it->first << " => " << it->second <<std::endl;

}

Expand Down
1 change: 1 addition & 0 deletions src/Store/Store.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace ToolFramework{
Store(); ////< Sinple constructor

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.
bool Initialise(std::istream& inputstream, std::string filename=""); ///< 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. @param filename The file this stream is in - only used for inclusion in error messages if provided.
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.
void Print(); ///< Prints the contents of the Store.
void Delete(); ///< Deletes all entries in the Store.
Expand Down
Loading