diff options
-rw-r--r-- | cfg.c | 22 | ||||
-rw-r--r-- | expr.c | 10 | ||||
-rw-r--r-- | hash.c | 51 | ||||
-rw-r--r-- | plugin_proc_stat.c | 20 |
4 files changed, 76 insertions, 27 deletions
@@ -1,4 +1,4 @@ -/* $Id: cfg.c,v 1.28 2004/01/16 05:04:53 reinelt Exp $^ +/* $Id: cfg.c,v 1.29 2004/01/18 06:54:08 reinelt Exp $^ * * config file stuff * @@ -23,6 +23,10 @@ * * * $Log: cfg.c,v $ + * Revision 1.29 2004/01/18 06:54:08 reinelt + * bug in expr.c fixed (thanks to Xavier) + * some progress with /proc/stat parsing + * * Revision 1.28 2004/01/16 05:04:53 reinelt * started plugin proc_stat which should parse /proc/stat * which again is a paint in the a** @@ -382,7 +386,7 @@ char *l4l_cfg_list (char *section) } -char *l4l_cfg_get_raw (char *section, char *key, char *defval) +static char *cfg_lookup (char *section, char *key) { int len; char *buffer; @@ -413,6 +417,15 @@ char *l4l_cfg_get_raw (char *section, char *key, char *defval) if (entry!=NULL) return entry->val; + return NULL; +} + + +char *l4l_cfg_get_raw (char *section, char *key, char *defval) +{ + char *val=cfg_lookup(section, key); + + if (val!=NULL) return val; return defval; } @@ -422,9 +435,10 @@ char *l4l_cfg_get (char *section, char *key, char *defval) char *expression; RESULT result = {0, 0.0, NULL}; - expression=cfg_get_raw(section, key, defval); + expression=cfg_lookup(section, key); - if (expression!=NULL && *expression!='\0') { + if (expression!=NULL) { + if (*expression=='\0') return ""; if (Eval(expression, &result)==0) { return R2S(&result); } @@ -1,4 +1,4 @@ -/* $Id: expr.c,v 1.4 2004/01/14 11:33:00 reinelt Exp $ +/* $Id: expr.c,v 1.5 2004/01/18 06:54:08 reinelt Exp $ * * expr ('y*') functions * This is only a workaround to make the Evaluator usable until @@ -24,6 +24,10 @@ * * * $Log: expr.c,v $ + * Revision 1.5 2004/01/18 06:54:08 reinelt + * bug in expr.c fixed (thanks to Xavier) + * some progress with /proc/stat parsing + * * Revision 1.4 2004/01/14 11:33:00 reinelt * new plugin 'uname' which does what it's called * text widget nearly finished @@ -67,7 +71,7 @@ int Expr(int index, char string[EXPR_TXT_LEN], double *val) return -1; sprintf(yn, "y%d", index); - expression = cfg_get(NULL, yn, NULL); + expression = cfg_get_raw(NULL, yn, NULL); if (!expression || !*expression) { error("Empty expression for 'y%d'", index); @@ -78,7 +82,7 @@ int Expr(int index, char string[EXPR_TXT_LEN], double *val) Eval(expression, &result); strcpy(string, R2S(&result)); - debug("%s: <%s> = '%s'",yn, expression, result); + debug("%s: <%s> = '%s'", yn, expression, string); if (isdigit(*string)) { double max, min; @@ -1,4 +1,4 @@ -/* $Id: hash.c,v 1.3 2004/01/16 07:26:25 reinelt Exp $ +/* $Id: hash.c,v 1.4 2004/01/18 06:54:08 reinelt Exp $ * * hashes (associative arrays) * @@ -23,6 +23,10 @@ * * * $Log: hash.c,v $ + * Revision 1.4 2004/01/18 06:54:08 reinelt + * bug in expr.c fixed (thanks to Xavier) + * some progress with /proc/stat parsing + * * Revision 1.3 2004/01/16 07:26:25 reinelt * moved various /proc parsing to own functions * made some progress with /proc/stat parsing @@ -76,29 +80,50 @@ static int hash_sort_f (const void *a, const void *b) } + // 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. +// If the table is flagged "sorted", the entry is looked +// up using the bsearch function. If the table is +// unsorted, it will be searched in a linear way if the entry +// does already exist. +// If the entry does already exist, it will be overwritten, +// and the table stays sorted (if it has been before). +// Otherwise, the entry is appended at the end, and // the table will be flagged 'unsorted' afterwards + void hash_set (HASH *Hash, char *key, char *val) { - int i; + HASH_ITEM *Item=NULL; + + // lookup using bsearch + if (Hash->sorted) { + Item=bsearch(key, Hash->Items, Hash->nItems, sizeof(HASH_ITEM), hash_lookup_f); + } + + // linear search + if (Item==NULL) { + int i; + for (i=0;i<Hash->nItems; i++) { + if (strcmp(key, Hash->Items[i].key)==0) { + Item=&(Hash->Items[i]); + break; + } + } + } // entry already exists? - for (i=0;i<Hash->nItems; i++) { - if (strcmp(key, Hash->Items[i].key)==0) { - if (Hash->Items[i].val) free (Hash->Items[i].val); - Hash->Items[i].val=strdup(val); - return; - } + if (Item!=NULL) { + if (Item->val) free (Item->val); + Item->val=strdup(val); + return; } - + // add entry Hash->sorted=0; Hash->nItems++; Hash->Items=realloc(Hash->Items,Hash->nItems*sizeof(HASH_ITEM)); - Hash->Items[i].key=strdup(key); - Hash->Items[i].val=strdup(val); + Hash->Items[Hash->nItems-1].key=strdup(key); + Hash->Items[Hash->nItems-1].val=strdup(val); } diff --git a/plugin_proc_stat.c b/plugin_proc_stat.c index 90ddd45..5c9f65e 100644 --- a/plugin_proc_stat.c +++ b/plugin_proc_stat.c @@ -1,4 +1,4 @@ -/* $Id: plugin_proc_stat.c,v 1.3 2004/01/16 11:12:26 reinelt Exp $ +/* $Id: plugin_proc_stat.c,v 1.4 2004/01/18 06:54:08 reinelt Exp $ * * plugin for /proc/stat parsing * @@ -23,6 +23,10 @@ * * * $Log: plugin_proc_stat.c,v $ + * Revision 1.4 2004/01/18 06:54:08 reinelt + * bug in expr.c fixed (thanks to Xavier) + * some progress with /proc/stat parsing + * * Revision 1.3 2004/01/16 11:12:26 reinelt * some bugs in plugin_xmms fixed, parsing moved to own function * plugin_proc_stat nearly finished @@ -85,6 +89,10 @@ static int renew(int msec) static void hash_set1 (char *key1, char *val) { + double number; + + number=atof(val); + hash_set (&Stat, key1, val); } @@ -93,8 +101,7 @@ static void hash_set2 (char *key1, char *key2, char *val) char key[32]; snprintf (key, sizeof(key), "%s.%s", key1, key2); - // debug ("Michi: hash_set(%s, %s)", key, val); - hash_set (&Stat, key, val); + hash_set1 (key, val); } static void hash_set3 (char *key1, char *key2, char *key3, char *val) @@ -102,8 +109,7 @@ static void hash_set3 (char *key1, char *key2, char *key3, char *val) char key[32]; snprintf (key, sizeof(key), "%s.%s.%s", key1, key2, key3); - debug ("Michi: hash_set(%s)=<%s>", key, val); - hash_set (&Stat, key, val); + hash_set1 (key, val); } @@ -114,7 +120,8 @@ static int parse_proc_stat (void) // update every 10 msec if (!renew(10)) return 0; - stream=fopen("/proc/stat", "r"); + // stream=fopen("/proc/stat", "r"); + stream=fopen("proc_stat", "r"); if (stream==NULL) { error ("fopen(/proc/stat) failed: %s", strerror(errno)); return -1; @@ -161,7 +168,6 @@ static int parse_proc_stat (void) hash_set3 ("disk_io", dev, "rblk", strtok(NULL, " ,")); hash_set3 ("disk_io", dev, "wio", strtok(NULL, " ,")); hash_set3 ("disk_io", dev, "wblk", strtok(NULL, " ,)")); - // Fixme: check this one... dev=strtok(NULL, " \t\n:()"); } } |