18#include <zypp-core/base/Logger.h>
19#include <zypp-core/base/String.h>
20#include <zypp-core/base/Regex.h>
21#include <zypp-core/base/IOStream.h>
22#include <zypp-core/base/InputStream>
23#include <zypp-core/Pathname.h>
24#include <zypp-core/fs/PathInfo.h>
25#include <zypp-core/fs/TmpPath.h>
36 DBG <<
"Load '" << _path <<
"'" << endl;
37 std::map<std::string,std::string> ret;
40 std::ifstream in( _path.
asString().c_str() );
42 WAR <<
"Unable to load '" << _path <<
"'" << endl;
46 while( getline( in, line ) ) {
47 if ( *line.begin() !=
'#' ) {
49 std::string::size_type pos = line.find(
'=', 0 );
51 if ( pos != std::string::npos ) {
53 std::string key =
str::trim( line.substr( 0, pos ) );
54 std::string value =
str::trim( line.substr( pos + 1, line.length() - pos - 1 ) );
56 if ( value.length() >= 2
57 && *(value.begin()) ==
'"'
58 && *(value.rbegin()) ==
'"' )
60 value = value.substr( 1, value.length() - 2 );
62 if ( value.length() >= 2
63 && *(value.begin()) ==
'\''
64 && *(value.rbegin()) ==
'\'' )
66 value = value.substr( 1, value.length() - 2 );
68 XXX <<
"KEY: '" << key <<
"' VALUE: '" << value <<
"'" << endl;
76 MIL <<
"done reading '" << _path <<
"'" << endl;
80 bool write(
const Pathname & path_r,
const std::string & key_r,
const std::string & val_r,
const std::string & newcomment_r )
84 WAR <<
"Empty key in write " << path_r << endl;
100 [&](
int num_r, std::string line_r )->
bool
102 if ( !found && regex.
matches( line_r ) )
104 o << key_r <<
'=' << val_r << endl;
106 MIL << path_r <<
": " << key_r <<
'=' << val_r <<
" changed on line " << num_r << endl;
114 if ( newcomment_r.empty() )
116 WAR << path_r <<
": " << key_r <<
'=' << val_r <<
" can not be added (no comment provided)." << endl;
120 std::vector<std::string> lines;
121 str::split( newcomment_r, std::back_inserter(lines),
"\r\n" );
123 for (
const std::string & line : lines )
125 if ( line[0] !=
'#' )
129 o << key_r <<
'=' << val_r << endl;
131 MIL << path_r <<
": " << key_r <<
'=' << val_r <<
" appended. " << endl;
140 int res = exchange( tmpf.
path(), path_r );
148 bool writeStringVal(
const Pathname & path_r,
const std::string & key_r,
const std::string & val_r,
const std::string & newcomment_r )
Convenience errno wrapper.
Base class for Exception.
Wrapper class for stat/lstat.
const char * c_str() const
String representation.
const std::string & asString() const
String representation.
Provide a new empty temporary file and delete it when no longer needed.
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
bool matches(const char *s, str::smatch &matches, int flags=none) const
std::map< std::string, std::string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
bool write(const Pathname &path_r, const std::string &key_r, const std::string &val_r, const std::string &newcomment_r)
Add or change a value in sysconfig file path_r.
bool writeStringVal(const Pathname &path_r, const std::string &key_r, const std::string &val_r, const std::string &newcomment_r)
Convenience to add or change a string-value in sysconfig file path_r.
int forEachLine(std::istream &str_r, function< bool(int, std::string)> consume_r)
Simple lineparser: Call functor consume_r for each line.
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", const Trim trim_r=NO_TRIM)
Split line_r into words.
std::string trim(const std::string &s, const Trim trim_r)
Easy-to use interface to the ZYPP dependency resolver.
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.