aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2004-01-16 05:04:53 +0000
committerreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>2004-01-16 05:04:53 +0000
commit5c148f2a8e46983d905053d3e09d67ef6948ccf4 (patch)
treee03170b68499b254fdcffe478b2d4786a7c867ce /hash.c
parent8bd25913d494772e695311c5745bbcc8a5a57e77 (diff)
downloadlcd4linux-5c148f2a8e46983d905053d3e09d67ef6948ccf4.tar.gz
[lcd4linux @ 2004-01-16 05:04:53 by reinelt]
started plugin proc_stat which should parse /proc/stat which again is a paint in the a** thinking over implementation methods of delta functions (CPU load, ...) git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@314 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c100
1 files changed, 97 insertions, 3 deletions
diff --git a/hash.c b/hash.c
index 02f0f65..fb9e8c1 100644
--- a/hash.c
+++ b/hash.c
@@ -1,4 +1,4 @@
-/* $Id: hash.c,v 1.1 2004/01/13 10:03:01 reinelt Exp $
+/* $Id: hash.c,v 1.2 2004/01/16 05:04:53 reinelt Exp $
*
* hashes (associative arrays)
*
@@ -23,6 +23,12 @@
*
*
* $Log: hash.c,v $
+ * Revision 1.2 2004/01/16 05:04:53 reinelt
+ * started plugin proc_stat which should parse /proc/stat
+ * which again is a paint in the a**
+ * thinking over implementation methods of delta functions
+ * (CPU load, ...)
+ *
* Revision 1.1 2004/01/13 10:03:01 reinelt
* new util 'hash' for associative arrays
* new plugin 'cpuinfo'
@@ -56,6 +62,16 @@ static int hash_lookup_f (const void *a, const void *b)
}
+// bsearch compare function for filter entries
+static int filter_lookup_f (const void *a, const void *b)
+{
+ char *key=(char*)a;
+ FILTER_ITEM *item=(FILTER_ITEM*)b;
+
+ return strcmp(key, item->key);
+}
+
+
// qsort compare function for hash tables
static int hash_sort_f (const void *a, const void *b)
{
@@ -66,6 +82,17 @@ static int hash_sort_f (const void *a, const void *b)
}
+// qsort compare function for filter tables
+static int filter_sort_f (const void *a, const void *b)
+{
+ FILTER_ITEM *ha=(FILTER_ITEM*)a;
+ FILTER_ITEM *hb=(FILTER_ITEM*)b;
+
+ return strcasecmp(ha->key, hb->key);
+}
+
+
+
// insert a key/val pair into the hash table
// the tbale will be searched linearly if the entry
// does already exist, for the table may not be sorted.
@@ -92,7 +119,7 @@ void hash_set (HASH *Hash, char *key, char *val)
}
-void *hash_get (HASH *Hash, char *key)
+char *hash_get (HASH *Hash, char *key)
{
HASH_ITEM *Item;
@@ -122,8 +149,75 @@ void hash_destroy (HASH *Hash)
// free hash table
free (Hash->Items);
}
-
+
Hash->nItems=0;
Hash->sorted=0;
Hash->Items=NULL;
}
+
+
+// insert a key/val pair into the filter table
+// the tbale will be searched linearly if the entry
+// does already exist, for the table may not be sorted.
+// the table will be flagged 'unsorted' afterwards
+void filter_set (FILTER *Filter, char *key, char *val)
+{
+ int i;
+
+ // entry already exists?
+ for (i=0;i<Filter->nItems; i++) {
+ if (strcmp(key, Filter->Items[i].key)==0) {
+ // if (Filter->Items[i].val) free (Filter->Items[i].val);
+ // Filter->Items[i].val=strdup(val);
+ return;
+ }
+ }
+
+ // add entry
+ Filter->sorted=0;
+ Filter->nItems++;
+ Filter->Items=realloc(Filter->Items,Filter->nItems*sizeof(FILTER_ITEM));
+ Filter->Items[i].key=strdup(key);
+
+
+ Filter->Items[i].Slots=strdup(val);
+}
+
+
+double filter_get (FILTER *Filter, char *key)
+{
+ FILTER_ITEM *Item;
+
+ if (!Filter->sorted) {
+ qsort(Filter->Items, Filter->nItems, sizeof(FILTER_ITEM), filter_sort_f);
+ Filter->sorted=1;
+ }
+
+ Item=bsearch(key, Filter->Items, Filter->nItems, sizeof(FILTER_ITEM), filter_lookup_f);
+ if (Item==NULL) return 0.0;
+ if (Item->Slots==NULL) return 0.0;
+ return Item->Slots[0].val;
+
+}
+
+
+void filter_destroy (FILTER *Filter)
+{
+ int i;
+
+ if (Filter->Items) {
+
+ // free all entries
+ for (i=0;i<Filter->nItems; i++) {
+ if (Filter->Items[i].key) free (Filter->Items[i].key);
+ if (Filter->Items[i].Slots) free (Filter->Items[i].Slots);
+ }
+
+ // free filter table
+ free (Filter->Items);
+ }
+
+ Filter->nItems=0;
+ Filter->sorted=0;
+ Filter->Items=NULL;
+}