28 using namespace zypp::base;
34 map<string,string>
read(
const Pathname & _path )
36 DBG <<
"Load '" << _path <<
"'" << endl;
37 map<string,string> ret;
40 ifstream in( _path.asString().c_str() );
42 WAR <<
"Unable to load '" << _path <<
"'" << endl;
47 if ( *line.begin() !=
'#' ) {
51 if ( pos != string::npos ) {
53 string key =
str::trim( line.substr( 0, pos ) );
54 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;
88 PathInfo pi( path_r );
91 if ( ! pi.userMayRW() )
97 StrMatcher matches(
"^[ \t]*"+key_r+
"[ \t]*=", Match::REGEX );
100 [&](
int num_r, std::string line_r )->
bool
102 if ( !found && 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 (
auto line : lines )
125 if ( line[0] !=
'#' )
129 o << key_r <<
'=' << val_r << endl;
131 MIL << path_r <<
": " << key_r <<
'=' << val_r <<
" appended. " << endl;
148 bool writeStringVal(
const Pathname & path_r,
const std::string & key_r,
const std::string & val_r,
const std::string & newcomment_r )
int exchange(const Pathname &lpath, const Pathname &rpath)
Exchanges two files or directories.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Convenience errno wrapper.
unsigned split(const C_Str &line_r, _OutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
String matching (STRING|SUBSTRING|GLOB|REGEX).
std::string escape(const C_Str &str_r, const char sep_r)
Escape desired character c using a backslash.
Provide a new empty temporary file and delete it when no longer needed.
map< string, string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
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.
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.
std::string getline(std::istream &str)
Read one line from stream.
Convenient building of std::string via std::ostream::operator<<.
std::string trim(const std::string &s, const Trim trim_r)
int forEachLine(std::istream &str_r, function< bool(int, std::string)> consume_r)
Simple lineparser: Call functor consume_r for each line.
const char * c_str() const
String representation.
Base class for Exception.