[PC-BSD Commits] r7917 - pcbsd/current/src-qt4/libpcbsd
svn at pcbsd.org
svn at pcbsd.org
Wed Oct 27 12:18:39 PDT 2010
Author: kris
Date: 2010-10-27 12:18:39 -0700 (Wed, 27 Oct 2010)
New Revision: 7917
Modified:
pcbsd/current/src-qt4/libpcbsd/pcbsd-utils.h
pcbsd/current/src-qt4/libpcbsd/utils.cpp
Log:
Added functionality to libpcbsd for getting / setting values in config files like rc.conf
Modified: pcbsd/current/src-qt4/libpcbsd/pcbsd-utils.h
===================================================================
--- pcbsd/current/src-qt4/libpcbsd/pcbsd-utils.h 2010-10-27 18:03:49 UTC (rev 7916)
+++ pcbsd/current/src-qt4/libpcbsd/pcbsd-utils.h 2010-10-27 19:18:39 UTC (rev 7917)
@@ -45,6 +45,10 @@
static bool setProxyType(QString val);
static bool setProxyPort(QString val);
static bool setMasterMirror(QString val);
+ static bool setConfFileValue( QString oFile, QString oldKey, QString newKey, int occur );
+ static bool setConfFileValue( QString oFile, QString oldKey, QString newKey );
+ static QString getConfFileValue( QString oFile, QString Key, int occur = 1 );
+ static QString getConfFileValue( QString oFile, QString Key );
};
#endif
Modified: pcbsd/current/src-qt4/libpcbsd/utils.cpp
===================================================================
--- pcbsd/current/src-qt4/libpcbsd/utils.cpp 2010-10-27 18:03:49 UTC (rev 7916)
+++ pcbsd/current/src-qt4/libpcbsd/utils.cpp 2010-10-27 19:18:39 UTC (rev 7917)
@@ -193,3 +193,131 @@
bool Utils::setMasterMirror(QString val) {
return setValPBIConf("PBI_MIRROR", val);
}
+
+QString Utils::getConfFileValue( QString oFile, QString Key )
+{
+ return getConfFileValue(oFile, Key, -1);
+}
+
+QString Utils::getConfFileValue( QString oFile, QString Key, int occur )
+{
+ int found = 1;
+
+ QFile file( oFile );
+ if ( ! file.open( QIODevice::ReadOnly ) ) {
+ return QString();
+ }
+
+ QTextStream stream( &file );
+ QString line;
+ while ( !stream.atEnd() ) {
+ 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)
+ 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);
+}
+
+bool Utils::setConfFileValue( QString oFile, QString oldKey, QString newKey, int occur )
+{
+ // Lets the dev save a value into a specified config file.
+ // The occur value tells which occurance of "oldKey" to replace
+ // If occur is set to -1, it will remove any duplicates of "oldKey"
+
+ QStringList SavedFile;
+ int found = 1;
+
+ // Load the old file, find the oldKey, remove it and replace with newKey
+ QFile file( oFile );
+ if ( file.open( QIODevice::ReadOnly ) )
+ return false;
+
+ QTextStream stream( &file );
+ QString line;
+ while ( !stream.atEnd() ) {
+ line = stream.readLine(); // line of text excluding '\n'
+
+ // Key is not found at all
+ if ( line.indexOf(oldKey, 0) == -1 ) {
+ SavedFile << line ;
+ continue;
+ }
+
+ // Found the key, but it is commented out, so don't worry about this line
+ if ( line.indexOf("#", 0) == 0 ) {
+ SavedFile << line ;
+ continue;
+ }
+
+ // If the KEY is found, and we are just on wrong occurance, save it and continue to search
+ if ( occur != -1 && found != occur ) {
+ SavedFile << line ;
+ found++;
+ continue;
+ }
+
+ // If the KEY is found in the line and this matches the occurance that must be processed
+ if ( ! newKey.isEmpty() && found == occur )
+ {
+ SavedFile << newKey ;
+ newKey.clear();
+ found++;
+ continue;
+ }
+
+ // If the KEY is found and we just want one occurance of the key
+ if ( occur == -1 && ! newKey.isEmpty() ) {
+ SavedFile << newKey ;
+ newKey.clear();
+ found++;
+ continue;
+ }
+
+ }
+
+ file.close();
+
+ // Didn't find the key? Write it!
+ if ( ! newKey.isEmpty() )
+ SavedFile << newKey;
+
+
+ // Save the new file
+ QFile fileout( oFile );
+ if ( fileout.open( QIODevice::WriteOnly ) )
+ return false;
+
+ QTextStream streamout( &fileout );
+ for (int i = 0; i < SavedFile.size(); ++i)
+ streamout << SavedFile.at(i) << "\n";
+
+ fileout.close();
+
+ return true;
+
+}
More information about the Commits
mailing list