[PC-BSD Commits] r8089 - pcbsd/stable/libpcbsd

svn at pcbsd.org svn at pcbsd.org
Tue Nov 30 08:52:17 PST 2010


Author: kris
Date: 2010-11-30 08:52:17 -0800 (Tue, 30 Nov 2010)
New Revision: 8089

Modified:
   pcbsd/stable/libpcbsd/pcbsd-utils.h
   pcbsd/stable/libpcbsd/utils.cpp
Log:

MFC additional functions in libpcbsd, some of which are used by newer network manager code



Modified: pcbsd/stable/libpcbsd/pcbsd-utils.h
===================================================================
--- pcbsd/stable/libpcbsd/pcbsd-utils.h	2010-11-30 16:47:47 UTC (rev 8088)
+++ pcbsd/stable/libpcbsd/pcbsd-utils.h	2010-11-30 16:52:17 UTC (rev 8089)
@@ -31,6 +31,28 @@
    static QString sysctl(QString sysctl);
    static long long sysctlAsInt(QString sysctl);
    static QString bytesToHumanReadable(long long bytes);
+   static QString getValFromPBIConf(QString key);
+   static QString getValFromPCBSDConf(QString key);
+   static QString getValFromPCConf(QString conf, QString key);
+   static QString getProxyURL();
+   static QString getProxyUser();
+   static QString getProxyPass();
+   static QString getProxyType();
+   static QString getProxyPort();
+   static QString getMasterMirror();
+   static bool setValPBIConf(QString key, QString val);
+   static bool setValPCBSDConf(QString key, QString val);
+   static bool setValPCConf(QString conf, QString key, QString val);
+   static bool setProxyURL(QString val);
+   static bool setProxyUser(QString val);
+   static bool setProxyPass(QString val);
+   static bool setProxyType(QString val);
+   static bool setProxyPort(QString val);
+   static bool setMasterMirror(QString val);
+   static bool setConfFileValue( QString oFile, QString oldKey, QString newKey );
+   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 );
 };
 
 #endif

Modified: pcbsd/stable/libpcbsd/utils.cpp
===================================================================
--- pcbsd/stable/libpcbsd/utils.cpp	2010-11-30 16:47:47 UTC (rev 8088)
+++ pcbsd/stable/libpcbsd/utils.cpp	2010-11-30 16:52:17 UTC (rev 8089)
@@ -1,6 +1,6 @@
 /***************************************************************************
- *   Copyright (C) 2008 by Tim McCormick   *
- *   tim at pcbsd.org   *
+ *   Copyright (C) 2008 by Tim McCormick 				   *
+ *   tim at pcbsd.org   						  	   *
  *                                                                         *
  *   Permission is hereby granted, free of charge, to any person obtaining *
  *   a copy of this software and associated documentation files (the       *
@@ -25,10 +25,19 @@
 #include <sys/types.h>
 #include <sys/sysctl.h>
 
-#include <qstring.h>
+#include <QString>
+#include <QStringList>
+#include <QFile>
+#include <QTextStream>
 
 #include "pcbsd-utils.h"
 
+#include "../config.h"
+
+// The default pbi.conf file
+QString PBI_ETCCONF(PREFIX + "/etc/pbi.conf");
+QString PCBSD_ETCCONF(PREFIX + "/etc/PCBSD.conf");
+
 QString Utils::sysctl(QString sysctl)
 {
    char result[1000];
@@ -49,8 +58,7 @@
 QString Utils::bytesToHumanReadable(long long bytes)
 {
    QString suffix[] = {"B", "KB", "MB", "GB", "TB"};
-   long long remainder;
-   long long div = 1024;
+   long long remainder = 0;
    int i = 0;
    while ( bytes > 1024 && (i < 4) )
    {
@@ -74,3 +82,262 @@
    QString result = QString::number(bytes) + pointval;
    return result += suffix[i];
 }
+
+
+// Function to get a value from a specific key in the pbi.conf file
+QString Utils::getValFromPBIConf(QString key) {
+	return getValFromPCConf(PBI_ETCCONF, key);
+}
+
+// Function to get a value from a specific key in the pcbsd.conf file
+QString Utils::getValFromPCBSDConf(QString key) {
+	return getValFromPCConf(PCBSD_ETCCONF, key);
+}
+
+QString Utils::getValFromPCConf(QString conf, QString key) {
+  
+  // Load from conf the requested key
+  QFile confFile(conf);
+  if ( confFile.open( QIODevice::ReadOnly ) ) {
+        QTextStream stream( &confFile );
+        stream.setCodec("UTF-8");
+        QString line;
+        while ( !stream.atEnd() ) {
+            line = stream.readLine();
+	    if ( line.indexOf(key + ": ") == 0 ) {
+   		confFile.close();
+		return line.replace(key + ": ", "");
+	    }
+
+        }
+   confFile.close();
+  }
+
+  return QString();
+}
+
+QString Utils::getProxyURL() {
+	return getValFromPCBSDConf("PCBSD_PROXYURL");
+}
+
+QString Utils::getProxyUser() {
+	return getValFromPCBSDConf("PCBSD_PROXYUSER");
+}
+
+QString Utils::getProxyPass() {
+	return getValFromPCBSDConf("PCBSD_PROXYPASS");
+}
+
+QString Utils::getProxyType() {
+	return getValFromPCBSDConf("PCBSD_PROXYTYPE");
+}
+
+QString Utils::getProxyPort() {
+	return getValFromPCBSDConf("PCBSD_PROXYPORT");
+}
+
+QString Utils::getMasterMirror() {
+	return getValFromPCBSDConf("PCBSD_MIRROR");
+}
+
+
+bool Utils::setValPBIConf(QString key, QString val)
+{
+	return setValPCConf(PBI_ETCCONF, key, val);
+}
+
+bool Utils::setValPCBSDConf(QString key, QString val)
+{
+	return setValPCConf(PCBSD_ETCCONF, key, val);
+}
+
+bool Utils::setValPCConf(QString conf, QString key, QString val)
+{
+  bool savedLine;
+  QStringList pbiConf;
+
+  // Read PBI_ETCCONF into memory
+  QFile confFile(conf);
+  if ( confFile.open( QIODevice::ReadOnly ) ) {
+        QTextStream stream( &confFile );
+        QString line;
+        while ( !stream.atEnd() ) {
+	    savedLine = false;
+            line = stream.readLine();
+
+	    if ( line.indexOf(key + ": ") == 0 ) {
+	      if ( ! val.isEmpty() )
+	        pbiConf << key + ": " + val;
+	      savedLine = true;
+	    }
+
+	    if (!savedLine )
+	      pbiConf << line;
+        }
+   confFile.close();
+  } else {
+    return false;
+  }
+
+  if ( confFile.open( QIODevice::WriteOnly ) ) {
+        QTextStream stream( &confFile );
+	for (int i = 0; i < pbiConf.count(); ++i)
+		stream << pbiConf.at(i) << "\n";
+	confFile.close();
+	return true;
+  }
+	
+  return false;
+}
+
+bool Utils::setProxyURL(QString val) {
+	return setValPCBSDConf("PCBSD_PROXYURL", val);
+}
+
+bool Utils::setProxyUser(QString val) {
+	return setValPCBSDConf("PCBSD_PROXYUSER", val);
+}
+
+bool Utils::setProxyPass(QString val) {
+	return setValPCBSDConf("PCBSD_PROXYPASS", val);
+}
+
+bool Utils::setProxyType(QString val) {
+	return setValPCBSDConf("PCBSD_PROXYTYPE", val);
+}
+
+bool Utils::setProxyPort(QString val) {
+	return setValPCBSDConf("PCBSD_PROXYPORT", val);
+}
+
+bool Utils::setMasterMirror(QString val) {
+	return setValPCBSDConf("PCBSD_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