]> git.webhop.me Git - lcd4linux.git/commitdiff
[lcd4linux @ 2004-01-18 06:54:08 by reinelt]
authorreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Sun, 18 Jan 2004 06:54:08 +0000 (06:54 +0000)
committerreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Sun, 18 Jan 2004 06:54:08 +0000 (06:54 +0000)
bug in expr.c fixed (thanks to Xavier)
some progress with /proc/stat parsing

git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@318 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

cfg.c
expr.c
hash.c
plugin_proc_stat.c

diff --git a/cfg.c b/cfg.c
index e604d0fe5aafd9acc8aabff9ec5504cf2d79bc92..6fbef65f4a01fcad9acd6fbf484cfd5c51958911 100644 (file)
--- a/cfg.c
+++ b/cfg.c
@@ -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
  *
  *
  *
  * $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);
     }
diff --git a/expr.c b/expr.c
index 09a6c33541963fac160b152c3314e9ccadae6cf8..f9e6102c93ca6991a977ed5018a189ad790786f9 100644 (file)
--- a/expr.c
+++ b/expr.c
@@ -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
  *
  *
  * $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;
diff --git a/hash.c b/hash.c
index 32be0a9065c5263f2852636a5af55ab42bb9c1a0..56a633065ceebd2f2a89c4eedc6cf01bed155675 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -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)
  *
  *
  *
  * $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);
 }
 
 
index 90ddd459beb65c16e1fbd288eef10f226a1a2c17..5c9f65eafb2b4704dbbfe99c581f4e1ddfb7ee4a 100644 (file)
@@ -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
  *
  *
  *
  * $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:()");
       }
     }