51 G_LOG(
"MapFile::read: start" ) ;
59 m_keys.reserve( m_map.size() ) ;
60 for( StringMap::iterator p = m_map.begin() ; p != m_map.end() ; ++p )
61 m_keys.push_back( (*p).first ) ;
67 for( OptionMap::const_iterator p = map.
begin() ; p != map.
end() ; )
69 std::string key = (*p).first ;
72 std::string
value = (*p).second.valued() ? map.
value(key) : yes ;
76 while( p != map.
end() && (*p).first == key )
81 void G::MapFile::readFrom(
const G::Path & path )
83 std::ifstream stream( path.
str().c_str() ) ;
85 throw ReadError( path.
str() ) ;
86 G_LOG(
"MapFile::read: reading [" << path.
str() <<
"]" ) ;
90 void G::MapFile::readFrom( std::istream & ss )
109 if( part.size() == 0U )
113 value =
G::Str::tail( line , line.find(key)+key.size() , std::string() ) ;
117 if( value.length() >= 2U && value.at(0U) ==
'"' && value.at(value.length()-1U) ==
'"' )
118 value = value.substr(1U,value.length()-2U) ;
126 bool G::MapFile::ignore(
const std::string & line )
const
128 std::string::size_type pos_interesting = line.find_first_not_of(
" \t\r#") ;
129 if( pos_interesting == std::string::npos )
132 std::string::size_type pos_hash = line.find(
"#") ;
133 if( pos_hash != std::string::npos && pos_hash < pos_interesting )
142 tmp.m_logging = false ;
143 tmp.readFrom( path ) ;
148 for( StringArray::const_iterator p = m_keys.begin() ; p != m_keys.end() ; ++p )
149 log( (*(m_map.find(*p))).first , (*(m_map.find(*p))).second ) ;
152 void G::MapFile::log(
const std::string & key ,
const std::string & value )
const
154 log( m_logging , key , value ) ;
157 void G::MapFile::log(
bool logging ,
const std::string & key ,
const std::string & value )
160 G_LOG(
"MapFile::item: " << key <<
"=[" <<
161 ( key.find(
"password") == std::string::npos ?
163 std::string(
"<not-logged>")
169 std::string value = m_map.find(key) == m_map.end() ? std::string() : (*m_map.find(key)).second ;
170 writeItem( stream , key , value ) ;
175 log(
true , key , value ) ;
176 const char * qq = value.find(
' ') == std::string::npos ?
"" :
"\"" ;
177 stream << key <<
"=" << qq << value << qq <<
"\n" ;
180 std::string G::MapFile::quote(
const std::string & s )
182 return s.find_first_of(
" \t") == std::string::npos ? s : (std::string()+
"\""+s+
"\"") ;
187 typedef std::list<std::string> List ;
188 List lines = read( path , allow_read_error ) ;
189 commentOut( lines ) ;
191 if( make_backup ) backup( path ) ;
192 save( path , lines , allow_write_error ) ;
195 G::MapFile::List G::MapFile::read(
const G::Path & path ,
bool allow_read_error )
const
198 std::ifstream file_in( path.
str().c_str() ) ;
199 if( !file_in.good() && !allow_read_error )
200 throw ReadError( path.
str() ) ;
201 while( file_in.good() )
204 if( !file_in ) break ;
205 line_list.push_back( line ) ;
210 void G::MapFile::commentOut( List & line_list )
const
212 for( List::iterator line_p = line_list.begin() ; line_p != line_list.end() ; ++line_p )
214 std::string & line = *line_p ;
215 if( line.empty() || line.at(0U) ==
'#' )
217 line = std::string(1U,
'#') + line ;
221 void G::MapFile::replace( List & line_list )
const
223 for( StringMap::const_iterator map_p = m_map.begin() ; map_p != m_map.end() ; ++map_p )
226 for( List::iterator line_p = line_list.begin() ; line_p != line_list.end() ; ++line_p )
228 std::string & line = (*line_p) ;
229 if( line.empty() )
continue ;
232 if( part.size() == 0U )
continue ;
233 if( part.at(0U) == (*map_p).first )
235 std::string value = (*map_p).second ;
244 std::string value = (*map_p).second ;
250 void G::MapFile::backup(
const G::Path & path )
254 std::string timestamp = Date(now).string(Date::yyyy_mm_dd) + Time(now).hhmmss() ;
256 Process::Umask umask( Process::Umask::Tightest ) ;
257 File::copy( path , backup , File::NoThrow() ) ;
260 void G::MapFile::save(
const G::Path & path , List & line_list ,
bool allow_write_error )
262 std::ofstream file_out( path.
str().c_str() , std::ios_base::out | std::ios_base::trunc ) ;
263 std::copy( line_list.begin() , line_list.end() , std::ostream_iterator<std::string>(file_out,
"\n") ) ;
265 if( file_out.fail() && !allow_write_error )
266 throw WriteError( path.
str() ) ;
271 StringMap::const_iterator p = m_map.find( key ) ;
272 if( p == m_map.end() )
276 else if( (*p).second.empty() )
288 StringMap::const_iterator p = m_map.find( key ) ;
289 std::string result = ( p == m_map.end() || (*p).second.empty() ) ? default_ : (*p).second ;
295 return value( key , std::string(default_) ) ;
298 std::string G::MapFile::mandatoryValue(
const std::string & key )
const
300 if( m_map.find(key) == m_map.end() )
301 throw Missing( key ) ;
302 return value( key ) ;
307 return Path( mandatoryValue(key) ) ;
312 return Path( expand(mandatoryValue(key)) ) ;
317 return Path( value(key,default_.
str()) ) ;
322 return Path( expand(value(key,default_.
str())) ) ;
327 std::string s = value( key , std::string() ) ;
333 StringMap::iterator p = m_map.find( key ) ;
334 if( p != m_map.end() )
337 G_ASSERT( std::find(m_keys.begin(),m_keys.end(),key) != m_keys.end() ) ;
338 m_keys.erase( std::find(m_keys.begin(),m_keys.end(),key) ) ;
344 std::string value( value_in ) ;
351 size_t find_single( std::string & s ,
char c ,
size_t start_pos )
353 char cc[] = { c ,
'\0' } ;
354 size_t pos = start_pos ;
357 pos = s.find( cc , pos ) ;
358 if( pos == std::string::npos )
362 else if( (pos+1U) < s.length() && s.at(pos+1U) == c )
364 s.erase( pos , 1U ) ;
365 if( (pos+1U) == s.length() )
367 pos = std::string::npos ;
381 bool G::MapFile::expand_( std::string & value )
const
383 bool changed = false ;
386 size_t const npos = std::string::npos ;
387 while( end < value.length() )
389 start = find_single( value ,
'%' , end ) ;
390 if( start == npos ) break ;
391 end = value.find(
"%" , start+1U ) ;
392 if( end == npos ) break ;
394 std::string key = value.substr( start+1U , end-start-2U ) ;
395 StringMap::const_iterator p = m_map.find( key ) ;
396 if( p != m_map.end() )
398 size_t old = end - start ;
399 size_t new_ = (*p).second.length() ;
400 value.replace( start , old , (*p).second ) ;
411 if( m_map.find(key) == m_map.end() )
412 m_keys.push_back( key ) ;
418 return m_map.find( key ) != m_map.end() ;
std::string str() const
Returns the path string.
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
static std::string tail(const std::string &in, size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
static bool copy(const Path &from, const Path &to, const NoThrow &)
Copies a file. Returns false on error.
std::string value(const std::string &key, const std::string &default_=std::string()) const
Returns a string value from the map.
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
const G::StringArray & keys() const
Returns a reference to the ordered list of keys.
static BrokenDownTime local(EpochTime epoch_time)
Converts from epoch time to local broken-down-time.
static bool isPositive(const std::string &)
Returns true if the string has a positive meaning, such as "1", "true", "yes".
std::vector< std::string > StringArray
A std::vector of std::strings.
static void splitIntoTokens(const std::string &in, StringArray &out, const std::string &ws)
Splits the string into 'ws'-delimited tokens.
void editInto(const G::Path &path, bool make_backup, bool allow_read_error, bool allow_write_error) const
Edits an existing file so that its contents reflect this map.
bool contains(const std::string &) const
Returns true if the map contains the given key, but ignoring un-valued() 'off' options.
const_iterator begin() const
Returns the begin iterator.
static void check(const G::Path &)
Throws if the file is invalid.
void remove(const std::string &key)
Removes a value (if it exists).
static EpochTime now()
Returns the current epoch time.
static void trimLeft(std::string &s, const std::string &ws, size_type limit=0U)
Trims the lhs of s, taking off up to 'limit' of the 'ws' characters.
A multimap-like container for command-line options and their values.
G::Path pathValue(const std::string &key) const
Returns a mandatory path value from the map.
bool contains(const std::string &key) const
Returns true if the map contains the given key.
const G::StringMap & map() const
Returns a reference to the internal map.
static unsigned int toUInt(const std::string &s)
Converts string 's' to an unsigned int.
G::Path expandedPathValue(const std::string &key) const
Returns a mandatory path value from the map with expand().
static void trimRight(std::string &s, const std::string &ws, size_type limit=0U)
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
A class for reading and editing key=value files, supporting comments, creation of backup files...
unsigned int numericValue(const std::string &key, unsigned int default_) const
Returns a numeric value from the map.
void add(const std::string &key, const std::string &value)
Adds or updates a single item in the map.
std::string expand(const std::string &value) const
Does one-pass variable substitution for the given string.
const_iterator end() const
Returns the off-the-end iterator.
static std::string trimmed(const std::string &s, const std::string &ws)
Returns a trim()med version of s.
static std::string readLineFrom(std::istream &stream, const std::string &eol=std::string())
Reads a line from the stream using the given line terminator.
Path dirname() const
Returns the path without the rightmost part, ignoring "." parts.
MapFile()
Constructor for an empty map.
bool booleanValue(const std::string &key, bool default_) const
Returns a boolean value from the map.
void writeItem(std::ostream &, const std::string &key) const
Writes a single item from this map to the stream.
std::map< std::string, std::string > StringMap
A std::map of std::strings.
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
std::string value(const std::string &key, const std::string &default_=std::string()) const
Returns the value of the valued() option identified by the given key.
A Path object represents a file system path.
static std::string ws()
Returns a string of standard whitespace characters.
void log() const
Logs the contents.