From 5005b7a0f0104112dc69fc3bec729a150872f850 Mon Sep 17 00:00:00 2001 From: Marcus O'Flaherty Date: Thu, 27 Apr 2023 17:21:08 +0100 Subject: [PATCH] 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 --- src/Store/Store.cpp | 79 +++++++++++++++++++++++++++++---------------- src/Store/Store.h | 1 + 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/Store/Store.cpp b/src/Store/Store.cpp index 2eaade9..f376a95 100644 --- a/src/Store/Store.cpp +++ b/src/Store/Store.cpp @@ -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 '"<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 "<::iterator it=m_variables.begin(); it!=m_variables.end(); ++it){ - std::cout<< it->first << " => " << it->second <first << " => " << it->second <