mirror of
https://github.com/lm-sensors/lm-sensors
synced 2025-09-01 14:55:27 +00:00
Use sscanf instead of regex to match the feature names. This is much faster.
As a side effect, this fixes a memory leak (the regex engine data was never freed.) git-svn-id: http://lm-sensors.org/svn/lm-sensors/branches/lm-sensors-3.0.0@4538 7894878c-1315-0410-8ee3-d5d059ff63e0
This commit is contained in:
43
lib/access.c
43
lib/access.c
@@ -20,7 +20,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <regex.h>
|
|
||||||
#include "access.h"
|
#include "access.h"
|
||||||
#include "sensors.h"
|
#include "sensors.h"
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
@@ -28,8 +27,6 @@
|
|||||||
#include "proc.h"
|
#include "proc.h"
|
||||||
#include "general.h"
|
#include "general.h"
|
||||||
|
|
||||||
#define GET_TYPE_REGEX "\\([[:alpha:]]\\{1,\\}\\)[[:digit:]]\\{0,\\}\\(_\\([[:alpha:]]\\{1,\\}\\)\\)\\{0,1\\}"
|
|
||||||
|
|
||||||
static int sensors_do_this_chip_sets(sensors_chip_name name);
|
static int sensors_do_this_chip_sets(sensors_chip_name name);
|
||||||
|
|
||||||
/* Compare two chips name descriptions, to see whether they could match.
|
/* Compare two chips name descriptions, to see whether they could match.
|
||||||
@@ -541,11 +538,10 @@ static struct feature_type_match cpu_matches[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static struct feature_type_match matches[] = {
|
static struct feature_type_match matches[] = {
|
||||||
{ "temp", SENSORS_FEATURE_TEMP, temp_matches },
|
{ "temp%d%c", SENSORS_FEATURE_TEMP, temp_matches },
|
||||||
{ "in", SENSORS_FEATURE_IN, in_matches },
|
{ "in%d%c", SENSORS_FEATURE_IN, in_matches },
|
||||||
{ "fan", SENSORS_FEATURE_FAN, fan_matches },
|
{ "fan%d%c", SENSORS_FEATURE_FAN, fan_matches },
|
||||||
{ "cpu", SENSORS_FEATURE_UNKNOWN, cpu_matches },
|
{ "cpu%d%c", SENSORS_FEATURE_UNKNOWN, cpu_matches },
|
||||||
{ "vrm", SENSORS_FEATURE_VRM, 0 },
|
|
||||||
{ 0 }
|
{ 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -553,39 +549,30 @@ static struct feature_type_match matches[] = {
|
|||||||
sensors_feature_type sensors_feature_get_type(
|
sensors_feature_type sensors_feature_get_type(
|
||||||
const sensors_feature_data *feature)
|
const sensors_feature_data *feature)
|
||||||
{
|
{
|
||||||
regmatch_t pmatch[4];
|
char c;
|
||||||
int size_first, size_second, retval, i;
|
int i, nr, count;
|
||||||
struct feature_type_match *submatches;
|
struct feature_type_match *submatches;
|
||||||
static regex_t reg;
|
|
||||||
static regex_t *preg = NULL;
|
|
||||||
|
|
||||||
if (!preg) {
|
/* vrm is special */
|
||||||
regcomp(®, GET_TYPE_REGEX, 0);
|
if (!strcmp(feature->name, "vrm"))
|
||||||
preg = ®
|
return SENSORS_FEATURE_VRM;
|
||||||
}
|
|
||||||
|
|
||||||
retval = regexec(preg, feature->name, 4, pmatch, 0);
|
|
||||||
|
|
||||||
if (retval == -1)
|
|
||||||
return SENSORS_FEATURE_UNKNOWN;
|
|
||||||
|
|
||||||
size_first = pmatch[1].rm_eo - pmatch[1].rm_so;
|
|
||||||
size_second = pmatch[3].rm_eo - pmatch[3].rm_so;
|
|
||||||
|
|
||||||
for(i = 0; matches[i].name != 0; i++)
|
for(i = 0; matches[i].name != 0; i++)
|
||||||
if (!strncmp(feature->name, matches[i].name, size_first))
|
if ((count = sscanf(feature->name, matches[i].name, &nr, &c)))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (matches[i].name == NULL) /* no match */
|
if (matches[i].name == NULL) /* no match */
|
||||||
return SENSORS_FEATURE_UNKNOWN;
|
return SENSORS_FEATURE_UNKNOWN;
|
||||||
else if (size_second == 0) /* single type */
|
else if (count == 1) /* single type */
|
||||||
return matches[i].type;
|
return matches[i].type;
|
||||||
else if (matches[i].submatches == NULL) /* not single type, but no submatches */
|
|
||||||
|
/* assert: count == 2 */
|
||||||
|
if (c != '_')
|
||||||
return SENSORS_FEATURE_UNKNOWN;
|
return SENSORS_FEATURE_UNKNOWN;
|
||||||
|
|
||||||
submatches = matches[i].submatches;
|
submatches = matches[i].submatches;
|
||||||
for(i = 0; submatches[i].name != 0; i++)
|
for(i = 0; submatches[i].name != 0; i++)
|
||||||
if (!strcmp(feature->name + pmatch[3].rm_so, submatches[i].name))
|
if (!strcmp(strchr(feature->name, '_') + 1, submatches[i].name))
|
||||||
return submatches[i].type;
|
return submatches[i].type;
|
||||||
|
|
||||||
return SENSORS_FEATURE_UNKNOWN;
|
return SENSORS_FEATURE_UNKNOWN;
|
||||||
|
Reference in New Issue
Block a user