summaryrefslogtreecommitdiffstats
path: root/src/hdsensorslist.cpp
diff options
context:
space:
mode:
authorTimothy Pearson <kb9vqf@pearsoncomputing.net>2013-07-24 09:41:22 -0500
committerTimothy Pearson <kb9vqf@pearsoncomputing.net>2013-07-24 09:41:22 -0500
commitea3a9effca9bebfe18bfb7546da619ecbbb121f3 (patch)
tree54ceea8953098258666fe2459632470f5dc85c94 /src/hdsensorslist.cpp
parent191c434271e705161fbd01ed83b6d043a275bffc (diff)
downloadksensors-ea3a9effca9bebfe18bfb7546da619ecbbb121f3.tar.gz
ksensors-ea3a9effca9bebfe18bfb7546da619ecbbb121f3.zip
Apply build fixes
Diffstat (limited to 'src/hdsensorslist.cpp')
-rw-r--r--src/hdsensorslist.cpp155
1 files changed, 51 insertions, 104 deletions
diff --git a/src/hdsensorslist.cpp b/src/hdsensorslist.cpp
index 3f9c704..8cbfa13 100644
--- a/src/hdsensorslist.cpp
+++ b/src/hdsensorslist.cpp
@@ -23,7 +23,7 @@
#include <stdlib.h>
#include <qstringlist.h>
#include <klocale.h>
-#include <regex.h>
+#include <qregexp.h>
#include "sensor.h"
#include "hdsensorslist.h"
@@ -44,28 +44,25 @@ HDSensorsList::HDSensorsList(QObject *parent, const char * name): SensorsList(pa
QStringList disks;
if(!getDisks(disks)) return;
- ProcessExec proc;
- proc << "hddtemp" << "-q";
for(QStringList::Iterator it = disks.begin(); it != disks.end(); ++it ) {
+ ProcessExec proc;
+ proc << "hddtemp" << "-q";
proc << *it;
- }
-
- if(proc.runAndWait()) {
- double value;
- QString str;
- for(QStringList::Iterator it = disks.begin(); it != disks.end(); ++it ) {
- if(getDiskInfo(proc.getStdoutData(),*it,str,value)) {
- Sensor *sensor= new Sensor(this);
- sensor->setType(Sensor::lmTemp);
- sensor->setName(*it);
- sensor->setDescription(str);
- sensor->setValueMax (40 , Sensor::dgCelsius);
- sensor->setValueMin ( 0 , Sensor::dgCelsius);
- sensor->setValueIdeal(value, Sensor::dgCelsius);
- sensor->setValue (value, Sensor::dgCelsius);
- sensor->readConfig();
- }
- }
+ if(proc.runAndWait()) {
+ double value;
+ QString str;
+ if(getDiskInfo(proc.getStdoutData(),str,value)) {
+ Sensor *sensor= new Sensor(this);
+ sensor->setType(Sensor::lmTemp);
+ sensor->setName(*it);
+ sensor->setDescription(str);
+ sensor->setValueMax (40 , Sensor::dgCelsius);
+ sensor->setValueMin ( 0 , Sensor::dgCelsius);
+ sensor->setValueIdeal(value, Sensor::dgCelsius);
+ sensor->setValue (value, Sensor::dgCelsius);
+ sensor->readConfig();
+ }
+ }
}
}
@@ -90,50 +87,29 @@ void HDSensorsList::updateSensors()
if(params.count()>0) {
process= new ProcessExec;
-#if ( KDE_VERSION_MAJOR >= 3 )
*process << "hddtemp" << "-q" << params;
-#else
- *process << "hddtemp" << "-q";
- for (QStringList::Iterator it= params.begin(); it!=params.end(); ++it)
- *process << *it;
-#endif
connect( process, SIGNAL(processExited(KProcess *)), this, SLOT(slotProcessExited(KProcess *)) );
process->run();
}
}
-
void HDSensorsList::slotProcessExited(KProcess *)
{
QObjectList *list= (QObjectList *)children();
- if(!list) return;
+ if (!list) return;
- char *buf= process->getStdoutData();
+ if (process->outputErrors())
+ qWarning("HddTemp Error:\n%s", process->getStdoutData().ascii());
- if( process->outputErrors() ) {
- qWarning("HddTemp Error:\n%s", buf);
- }
-
- char *ptr;
- regex_t *preg;
- regmatch_t pmatch[1];
- preg = (regex_t*) malloc(sizeof(regex_t));
- regcomp(preg, ": [0-9]\\+.*C$", REG_NEWLINE);
-
- for(Sensor *obj= (Sensor *)list->first(); obj!=0; obj= (Sensor *)list->next()) {
- buf= strstr(buf,obj->name());
- if(!buf) break;
- if (regexec(preg, buf, 1, pmatch, 0) == 0)
- {
- ptr = buf + pmatch[0].rm_so + 2;
- buf += pmatch[0].rm_eo;
- obj->setValue(atof(ptr), Sensor::dgCelsius);
+ QStringList buf = QStringList::split(QChar('\n'), process->getStdoutData());
+ for(QStringList::Iterator it = buf.begin(); it != buf.end(); ++it ) {
+ for(Sensor *obj= (Sensor *)list->first(); obj!=0; obj= (Sensor *)list->next()) {
+ QRegExp rx(QString(obj->name()) + QString(":\\s+.+:\\s+(\\d+).*C"));
+ if (rx.search((*it)) > -1)
+ obj->setValue(rx.cap(1).toDouble(), Sensor::dgCelsius);
}
}
- regfree(preg);
- free(preg);
-
delete process;
process= 0;
}
@@ -141,78 +117,49 @@ void HDSensorsList::slotProcessExited(KProcess *)
// *************** Static methods
-bool HDSensorsList::getDiskInfo(const char *buf, QString disk, QString &name, double &value)
+bool HDSensorsList::getDiskInfo(const QString buf, QString &name, double &value)
{
- regex_t *preg;
- regmatch_t pmatch[1];
- preg = (regex_t*) malloc(sizeof(regex_t));
-
- // Take disk model
- char reg_string[30];
- snprintf(reg_string, sizeof(reg_string), "%s: .*: [0-9]", disk.latin1());
- regcomp(preg, reg_string, REG_NEWLINE);
- if (regexec(preg, buf, 1, pmatch, 0) == 0)
- {
- name="";
- pmatch[0].rm_eo -= 3;
- pmatch[0].rm_so += 10;
- buf += pmatch[0].rm_so;
- while (pmatch[0].rm_so < pmatch[0].rm_eo)
- {
- name += *buf;
- buf++;
- pmatch[0].rm_so++;
- }
- regfree(preg);
- name= name.stripWhiteSpace();
- if( name.length()>40 ) name= disk;
- }
- else
- {
- regfree(preg);
- free(preg);
- return false;
+ QRegExp rx(":\\s+(.+):\\s+(\\d+).*C");
+
+ if (rx.search(buf) > -1) {
+ bool ok;
+ name = rx.cap(1);
+ value = rx.cap(2).toDouble(&ok);
+ if (ok)
+ return true;
+ else
+ return false;
}
-
- // Take disk temperature
- regcomp(preg, ": [0-9]\\+.*C$", REG_NEWLINE);
- if (regexec(preg, buf, 1, pmatch, 0) == 0)
- {
- value= atof(buf + pmatch[0].rm_so + 2);
- regfree(preg);
- free(preg);
- }
- else
- {
- regfree(preg);
- free(preg);
+ else
return false;
- }
- return true;
}
bool HDSensorsList::isHddTempInstalled()
{
-ProcessExec proc;
+ ProcessExec proc;
- proc << "hddtemp" ;
+ proc << "hddtemp" << "-v" ;
if(proc.runAndWait()) {
- if(strstr(proc.getStdoutData(),"ERROR")==0) return true;
- qWarning("HddTemp Error:\n%s", proc.getStdoutData());
+ if(proc.getStdoutData().contains("ERROR")==0) return true;
+ qWarning("HddTemp Error:\n%s", proc.getStdoutData().ascii());
}
return false;
}
bool HDSensorsList::getDisks(QStringList &disks )
{
- DIR *dir= opendir("/proc/ide");
- if(!dir) return false;
+ DIR *dir;
+
+ /* Get a listing of the hard drives looking under sysfs first then falling back to /proc/ide */
+ if((dir = opendir ("/sys/block")) == NULL)
+ if ((dir = opendir ("/proc/ide")) == NULL)
+ return false;
QString str;
struct dirent *ptr;
while((ptr= readdir(dir))) {
- if(ptr->d_name[0]=='h' && ptr->d_name[1]=='d') {
- str.sprintf("/dev/hd%c",ptr->d_name[2]);
+ if((ptr->d_name[0]=='h' || ptr->d_name[0]=='s') && ptr->d_name[1]=='d') {
+ str.sprintf("/dev/%s",ptr->d_name);
disks << str;
}
}