aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2004-01-21 14:29:03 +0000
committerreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2004-01-21 14:29:03 +0000
commit157395d9d4247afc971477497dcc51ef5441e660 (patch)
tree1a8558966c42105f5e7e2b2435c0b26482c515fd /hash.c
parent05ff9fc0c4691e4f60e9daa642eda28b34c02394 (diff)
downloadlcd4linux-157395d9d4247afc971477497dcc51ef5441e660.tar.gz
[lcd4linux @ 2004-01-21 14:29:03 by reinelt]
new helper 'hash_get_regex' which delivers the sum over regex matched items new function 'disk()' which uses this regex matching git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@333 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c67
1 files changed, 54 insertions, 13 deletions
diff --git a/hash.c b/hash.c
index 2254529..3d7a6e3 100644
--- a/hash.c
+++ b/hash.c
@@ -1,4 +1,4 @@
-/* $Id: hash.c,v 1.7 2004/01/21 10:48:17 reinelt Exp $
+/* $Id: hash.c,v 1.8 2004/01/21 14:29:03 reinelt Exp $
*
* hashes (associative arrays)
*
@@ -23,6 +23,10 @@
*
*
* $Log: hash.c,v $
+ * Revision 1.8 2004/01/21 14:29:03 reinelt
+ * new helper 'hash_get_regex' which delivers the sum over regex matched items
+ * new function 'disk()' which uses this regex matching
+ *
* Revision 1.7 2004/01/21 10:48:17 reinelt
* hash_age function added
*
@@ -65,12 +69,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <regex.h>
+//#include <sys/types.h>
#include "debug.h"
#include "hash.h"
-#define FILTER_SLOTS 64
+#define DELTA_SLOTS 64
// bsearch compare function for hash entries
@@ -177,22 +183,22 @@ void hash_set (HASH *Hash, char *key, char *val)
// insert a string into the hash table
// convert it into a number, and store it in the
-// filter table, too
-void hash_set_filter (HASH *Hash, char *key, char *val)
+// delta table, too
+void hash_set_delta (HASH *Hash, char *key, char *val)
{
double number=atof(val);
HASH_ITEM *Item;
Item=hash_set_string (Hash, key, val);
- // allocate filter table
+ // allocate delta table
if (Item->Slot==NULL) {
- Item->Slot = malloc(FILTER_SLOTS*sizeof(HASH_SLOT));
- memset(Item->Slot, 0, FILTER_SLOTS*sizeof(HASH_SLOT));
+ Item->Slot = malloc(DELTA_SLOTS*sizeof(HASH_SLOT));
+ memset(Item->Slot, 0, DELTA_SLOTS*sizeof(HASH_SLOT));
}
- // shift filter table
- memmove (Item->Slot+1, Item->Slot, (FILTER_SLOTS-1)*sizeof(HASH_SLOT));
+ // shift delta table
+ memmove (Item->Slot+1, Item->Slot, (DELTA_SLOTS-1)*sizeof(HASH_SLOT));
// set first entry
gettimeofday(&(Item->Slot[0].time), NULL);
@@ -237,8 +243,8 @@ int hash_age (HASH *Hash, char *key, char **value)
}
-// get a delta value from the filter table
-double hash_get_filter (HASH *Hash, char *key, int delay)
+// get a delta value from the delta table
+double hash_get_delta (HASH *Hash, char *key, int delay)
{
HASH_ITEM *Item;
timeval now, end;
@@ -250,6 +256,9 @@ double hash_get_filter (HASH *Hash, char *key, int delay)
if (Item==NULL) return 0.0;
if (Item->Slot==NULL) return 0.0;
+ // if delay is zero, return absolute value
+ if (delay==0) return Item->Slot[0].val;
+
// prepare timing values
now=Item->Slot[0].time;
end.tv_sec = now.tv_sec;
@@ -259,8 +268,8 @@ double hash_get_filter (HASH *Hash, char *key, int delay)
end.tv_usec += 1000000;
}
- // search filter slot
- for (i=1; i<FILTER_SLOTS; i++) {
+ // search delta slot
+ for (i=1; i<DELTA_SLOTS; i++) {
if (Item->Slot[i].time.tv_sec==0) break;
if (timercmp(&Item->Slot[i].time, &end, <)) break;
dt = (now.tv_sec - Item->Slot[i].time.tv_sec) + (now.tv_usec - Item->Slot[i].time.tv_usec)/1000000.0;
@@ -281,6 +290,38 @@ double hash_get_filter (HASH *Hash, char *key, int delay)
}
+// get a delta value from the delta table
+// key may contain regular expressions, and the sum
+// of all matching entries is returned.
+double hash_get_regex (HASH *Hash, char *key, int delay)
+{
+ double sum=0.0;
+ regex_t preg;
+ int i, err;
+
+ debug ("Michi: regex=<%s>", key);
+ err=regcomp(&preg, key, REG_ICASE|REG_NOSUB);
+ if (err!=0) {
+ char buffer[32];
+ regerror(err, &preg, buffer, sizeof(buffer));
+ error ("error in regular expression: %s", buffer);
+ return 0.0;
+ }
+
+ // force the table to be sorted by requesting anything
+ hash_lookup(Hash, "", 1);
+
+ for (i=0;i<Hash->nItems; i++) {
+ debug ("Michi: Testing <%s>", Hash->Items[i].key);
+ if (regexec(&preg, Hash->Items[i].key, 0, NULL, 0)==0) {
+ debug ("Michi: MATCHED <%s>", Hash->Items[i].key);
+ sum+=hash_get_delta(Hash, Hash->Items[i].key, delay);
+ }
+ }
+ return sum;
+}
+
+
void hash_destroy (HASH *Hash)
{
int i;