! ! Fix a problem with getConfFileValue() that would not proerly extract ! a key's value if there would be a similar key with a prefix, like: ! ipv6_defaultrouter=".." ! defaultrouter=".." ! ! Introduce another version of getConfFileValue() that also takes a ! QString for a regular expression match after the key. This can be ! used to distinguish nameserver values for IPv4 and IPv6. ! ! Submitted by: bz ! Sponsored by: The FreeBSD Foundation ! Sponsored by: iXsystems ! Index: libpcbsd/pcbsd-utils.h =================================================================== --- libpcbsd/pcbsd-utils.h (revision 10156) +++ libpcbsd/pcbsd-utils.h (working copy) @@ -53,6 +53,7 @@ class Utils static bool setConfFileValue( QString oFile, QString oldKey, QString newKey, int occur ); static QString getConfFileValue( QString oFile, QString Key ); static QString getConfFileValue( QString oFile, QString Key, int occur ); + static QString getConfFileValue( QString oFile, QString Key, QString ValRx, int occur ); static QStringList runShellCommand( QString command ); static QString runShellCommandSearch( QString command, QString sch ); }; Index: libpcbsd/utils.cpp =================================================================== --- libpcbsd/utils.cpp (revision 10156) +++ libpcbsd/utils.cpp (working copy) @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -224,7 +225,7 @@ QString Utils::getConfFileValue( QString oFile, QS line = stream.readLine(); // line of text excluding '\n' // If the KEY is found in the line, continue processing - if ( line.indexOf(Key, 0) == -1 || line.indexOf("#", 0) == 0) + if ( line.indexOf("#", 0) == 0 || line.indexOf(Key, 0) == -1 || line.indexOf(Key, 0) > 0) continue; if ( found == occur) { @@ -248,6 +249,48 @@ QString Utils::getConfFileValue( QString oFile, QS return QString(); } +QString Utils::getConfFileValue( QString oFile, QString Key, QString ValRx, int occur ) +{ + int found = 1; + + QFile file( oFile ); + if ( ! file.open( QIODevice::ReadOnly ) ) { + return QString(); + } + + QString rxStr ( Key ); + rxStr.append( ValRx ); + QRegExp rx(rxStr); + QTextStream stream( &file ); + QString line; + while ( !stream.atEnd() ) { + line = stream.readLine(); // line of text excluding '\n' + + // If the KEY is not found in the line, continue processing + if ( line.indexOf("#", 0) == 0 || line.indexOf(rx, 0) == -1 || line.indexOf(rx, 0) > 0) + continue; + + if ( found == occur ) { + line.remove(line.indexOf(Key, 0), Key.length()); + + // Remove any quotes + if ( line.indexOf('"') == 0 ) + line = line.remove(0, 1); + + if ( line.indexOf('"') != -1 ) + line.truncate(line.indexOf('"')); + + file.close(); + return line; + } else { + found++; + } + } + + file.close(); + return QString(); +} + bool Utils::setConfFileValue( QString oFile, QString oldKey, QString newKey ) { return setConfFileValue(oFile, oldKey, newKey, -1);