2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Revision:
7:1f2661b840ed
Parent:
6:83a6666a62d7
Child:
8:ba70bb062aa0
--- a/Config.cpp	Tue Dec 04 07:01:06 2018 +0000
+++ b/Config.cpp	Wed Dec 05 17:53:06 2018 +0000
@@ -2,6 +2,13 @@
 #include <ctype.h>
 
 
+Config::Config()
+{
+    itable = 0;
+    
+    return;
+}
+
 char *trim(char *s) 
 {
     char *t = s;
@@ -13,10 +20,71 @@
     
     // ltrim
     while (*s && isspace(*s)) 
-        s++;    
+        s++;
     
     return s;
-}    
+}
+
+
+// convert character to an int
+//
+int ctoi(char c)
+{
+  int i=-1;
+  
+  if (c >= '0' && c <= '9') {
+    i = c - '0';
+  }
+
+  //printf("char: %c  int %d\n", c, i); 
+ 
+  return i;
+}
+
+
+// convert string to floating point
+//
+double cvstof(char *s)
+{
+  double f=0.0;
+  double mult = 0.1;
+  bool neg = false;
+  //char dec = 1;
+  
+  // leading spaces
+  while (*s == ' ' || *s == '\t') {
+    s++;
+    if (*s == 0) break;
+  }
+
+  // What about negative numbers?
+  if (*s == '-') {
+    neg = true;
+    s++;
+  }
+
+  // before the decimal
+  //
+  while (*s != 0) {
+    if (*s == '.') {
+      s++;
+      break;
+    }
+    f = (f * 10.0) + (double) ctoi(*s);
+    s++;
+  }
+  // after the decimal
+  while (*s != 0 && *s >= '0' && *s <= '9') {
+    f += (double) ctoi(*s) * mult;
+    mult /= 10;
+    s++;
+  }
+  
+  // if we were negative...
+  if (neg) f = -f;
+  
+  return f;
+}
 
 
 int Config::load(char *filename) 
@@ -52,9 +120,7 @@
                 key = trim(key);
                 value = trim(value);
                 printf("<%s>=<%s>\n", key, value);
-                // look for key
-                // parse value
-                // set options
+                assign(key, value);
             }
         }
         fclose(fp);
@@ -62,3 +128,52 @@
     
     return status;
 }
+
+
+void Config::add(char *key, char format)
+{
+    if (key && itable < MAXTBL) {
+        strncpy(table[itable].key, key, MAXSTR);
+        table[itable].format = format;
+        itable++;
+    }
+    
+    return;
+}
+
+
+int Config::assign(char *key, char *value)
+{
+    int found = 0;
+    int status = 0;
+    char format = 0;
+
+    // lookup
+    for (int i=0; i < itable; i++) {
+        printf("%s\n", key);
+        if (!strncmp(table[i].key, key, MAXSTR)) {
+            found = 1;
+            format = table[i].format;
+            printf("found %s\n", key);
+            // assignment
+            switch (format) {
+            case DOUBLE :
+                table[i].value.d = atof(value);
+                break;
+            case INT :
+                table[i].value.i = atoi(value);
+                break;
+            default:
+                break;
+            }
+            break;
+        }
+    }
+
+    if (!found) {    
+        status = 1;
+        printf("key not found: %s\n", key);
+    }
+    
+    return status;
+}
\ No newline at end of file