A Command Interpreter with support for used defined commands, subsystems, macros, help and parameter parsing.

Files at this revision

API Documentation at this revision

Comitter:
wvd_vegt
Date:
Wed Feb 15 09:42:30 2012 +0000
Parent:
22:5192a468d7fa
Child:
24:1c0cbb592352
Commit message:
Some bugfixes and more ini file related print* methods (including commented ini file entries).

Changed in this revision

cmdb.cpp Show annotated file Show diff for this revision Revisions of this file
cmdb.h Show annotated file Show diff for this revision Revisions of this file
--- a/cmdb.cpp	Tue Nov 22 12:57:13 2011 +0000
+++ b/cmdb.cpp	Wed Feb 15 09:42:30 2012 +0000
@@ -271,6 +271,10 @@
     return serial.printf(msg);
 }
 
+int   Cmdb::println(const char *msg) {
+    return serial.printf("%s\r\n", msg);
+}
+
 int   Cmdb::printsection(const char *section) {
     return printf("[%s]\r\n", section);
 }
@@ -281,7 +285,22 @@
 
 int   Cmdb::printerror(const char *errormsg) {
     int a = printsection("Error");
-    return a==0?a:a+printmsg("%s\r\n", errormsg);
+    return a==0?a:a+printmsg(errormsg);
+}
+
+int   Cmdb::printerrorf(const char *format, ...) {
+    char buf[256];
+
+    int a = printsection("Error");
+    
+    va_list args;
+    va_start(args, format);
+
+    vsnprintf(buf, sizeof(buf), format, args);
+
+    va_end(args);
+
+    return a +  printf("Msg=%s\r\n", buf);
 }
 
 int   Cmdb::printvaluef(const char *key, const char *format, ...) {
@@ -298,31 +317,30 @@
 }
 
 int   Cmdb::printvaluef(const char *key, const int width, const char *comment, const char *format, ...) {
-    printf("%s=",key);
-
-    int  result = 0;
     char buf[256];
-    int  cnt = 0;
+    
+    int  cnt = printf("%s=",key);
 
     va_list args;
     va_start(args, format);
 
-    cnt = vsnprintf(buf, sizeof(buf), format, args);
-    cnt +=strlen(key) + 1;
-
+    vsnprintf(buf, sizeof(buf), format, args);
+   
     va_end(args);
 
     if (comment!=NULL) {
+        cnt += printf("%s", buf);
+
         if (cnt<width) {
-            result = printf("%-*s ;%s\r\n", width - cnt + 3, buf, comment);
+            cnt += printf("%-*s ; %s\r\n", width - cnt - 1, "", comment);
         } else {
-            result = printf("%s ;%s\r\n", buf, comment);
+            cnt += printf("%s ; %s\r\n", "", comment);
         }
     } else {
-        result = printf("%s\r\n",buf);
+        cnt += printf("%s\r\n",buf);
     }
 
-    return result;
+    return cnt;
 }
 
 int   Cmdb::printvalue(const char *key, const char *value, const char *comment, const int width) {
@@ -334,20 +352,27 @@
 
         cnt = snprintf(buf, sizeof(buf), "%s=%s", key, value);
 
-        if (cnt<width) {
-            return printf("%-*s ;%s\r\n", width, buf, comment);
+        if (cnt<=width) {
+            return printf("%-*s ; %s\r\n", width - cnt + 1, buf, comment);
         } else {
-            return printf("%s ;%s\r\n", buf, comment);
+            return printf("%s ; %s\r\n", buf, comment);
         }
     } else {
         return printf("%s=%s\r\n", key, value);
     }
 }
 
+int   Cmdb::printcomment(const char *comment, const int width) {
+    return printf("%-*s; %s\r\n", width, "", comment); 
+}
+
 char  Cmdb::printch(const char ch) {
     return serial.putc(ch);
 }
 
+//Mode=1               ; Profile Position Mode
+//1234567890123456789012
+
 //------------------------------------------------------------------------------
 
 void  Cmdb::init(const char full) {
@@ -565,18 +590,15 @@
 
                 endptr = (char*)toks[i];
 
-                //Cardinal Types
                 switch (typ) {
+                    //Signed Cardinal Types
                     case 'd' :  //Check mod
                     case 'i' :  //Check mod
-                    case 'u' :  //Check mod
-                    case 'o' :  //Check mod
-                    case 'x' :  //Check mod
                         switch (mod) {
                             case 'b' : //char
                                 //test range
                                 l=strtol((char*)toks[i], &endptr, base);
-                                if (l>=MIN_BYTE && l<=MAX_BYTE) {
+                                if (l>=MIN_CHAR && l<=MAX_CHAR) {
                                     parms[i].type=PARM_CHAR;
                                     parms[i].val.uc =(unsigned char)l;
                                 } else {
@@ -618,6 +640,57 @@
                         }
 
                         break;
+
+                    //Unsigned Cardinal Types
+                    case 'u' :  //Check mod
+                    case 'o' :  //Check mod
+                    case 'x' :  //Check mod
+                        switch (mod) {
+                            case 'b' : //char
+                                //test range
+                                l=strtol((char*)toks[i], &endptr, base);
+                                if (l>=MIN_BYTE && l<=MAX_BYTE) {
+                                    parms[i].type=PARM_CHAR;
+                                    parms[i].val.uc =(unsigned char)l;
+                                } else {
+                                    error = i+1;
+                                }
+
+                                break;
+                            case 'h' : //short
+                                l=strtol((char*)toks[i], &endptr, base);
+                                if (l>=MIN_USHORT && l<=MAX_USHORT) {
+                                    parms[i].type=PARM_SHORT;
+                                    parms[i].val.w=(short)l;
+                                } else {
+                                    error = i+1;
+                                }
+
+                                break;
+                            case 'l' : //long
+                                l=strtol((char*)toks[i], &endptr, base);
+                                parms[i].type=PARM_LONG;
+                                parms[i].val.l=l;
+
+                                break;
+                            default: //int
+                                l=strtol((char*)toks[i], &endptr, base);
+                                if (l>=MIN_UINT && l<=MAX_UINT) {
+                                    parms[i].type=PARM_INT;
+                                    parms[i].val.l=(int)l;
+                                } else {
+                                    error = i+1;
+                                }
+                                break;
+                        }
+
+                        if (error==0 &&
+                                (endptr==toks[i]    //No Conversion at all.
+                                 || *endptr)) {       //Incomplete conversion.
+                            error = i+1;
+                        }
+
+                        break;
                 }
 
                 //Floating Point Types
--- a/cmdb.h	Tue Nov 22 12:57:13 2011 +0000
+++ b/cmdb.h	Wed Feb 15 09:42:30 2012 +0000
@@ -74,13 +74,25 @@
  *
  * @see http://www.daniweb.com/forums/thread18963.html
  */
-#define MIN_BYTE        std::numeric_limits<signed char>::min()
+#define MIN_BYTE        std::numeric_limits<unsigned char>::min()
 
 /** 8 bit limits.
  *
  * @see http://www.daniweb.com/forums/thread18963.html
  */
-#define MAX_BYTE        std::numeric_limits<signed char>::max()
+#define MAX_BYTE        std::numeric_limits<unsigned char>::max()
+
+/** 8 bit limits.
+ *
+ * @see http://www.daniweb.com/forums/thread18963.html
+ */
+#define MIN_CHAR        std::numeric_limits<signed char>::min()
+
+/** 8 bit limits.
+ *
+ * @see http://www.daniweb.com/forums/thread18963.html
+ */
+#define MAX_CHAR        std::numeric_limits<signed char>::max()
 
 /** 16 bit limits.
  *
@@ -94,6 +106,18 @@
  */
 #define MAX_SHORT     std::numeric_limits<short int>::max()
 
+/** 16 bit limits.
+ *
+ * @see http://www.daniweb.com/forums/thread18963.html
+ */
+#define MIN_USHORT     std::numeric_limits<unsigned short int>::min()
+
+/** 16 bit limits.
+ *
+ * @see http://www.daniweb.com/forums/thread18963.html
+ */
+#define MAX_USHORT     std::numeric_limits<unsigned short int>::max()
+
 /** 32 bit limits.
  *
  * @see http://www.daniweb.com/forums/thread18963.html
@@ -105,6 +129,13 @@
  *
  * @see http://www.daniweb.com/forums/thread18963.html
  */
+#define MIN_UINT       std::numeric_limits<unsigned int>::min()
+#define MAX_UINT       std::numeric_limits<unsigned int>::max()
+
+/** 32 bit limits.
+ *
+ * @see http://www.daniweb.com/forums/thread18963.html
+ */
 #define MIN_LONG      std::numeric_limits<long>::min()
 #define MAX_LONG      std::numeric_limits<long>::max()
 
@@ -478,6 +509,14 @@
      */
     int print(const char *msg);
 
+    /** println is simply printf without parameters using the serial parameter passed to the constructor.
+     *
+     * @parm msg the string to print followed by a crlf.
+     *
+     * @returns the printf return value.
+     */
+    int println(const char *msg);
+    
     /** printch is simply putc subsitute using the serial parameter passed to the constructor.
      *
      * @parm msg the string to print.
@@ -510,7 +549,7 @@
      *
      *  @returns the printf return value.
      */
-    int   Cmdb::printmsg(const char *msg);
+    int printmsg(const char *msg);
 
     /** printerror prints an inifile Error Section Header and Error Msg Key=Value pair.
      *  like:
@@ -519,12 +558,26 @@
      *
      *  Usage: cmdb.printerror("Data Size Incorrect");
      *
-     *  @parm errormsg the error msg to print.
+     * @parm errormsg the error msg to print.
      *
-     *  @returns the printf return value.
+     * @returns the printf return value.
      */
     int printerror(const char *errormsg);
 
+    /** printerror prints an inifile Error Section Header and Error Msg Key=Value pair.
+     *  like:
+     *
+     *  [Error]\r\nmsg={errormsg}\r\n
+     *
+     *  Usage: cmdb.printerrorf("Data Size Incorrect %d", 15);
+     *
+     * @parm format the error msg to print.
+     * @parm parameter to print.
+     *
+     * @returns the printf return value.
+     */
+    int printerrorf(const char *format, ...);
+    
     /** printvalue prints an inifile Key/Value Pair
      *  like:
      *
@@ -575,6 +628,8 @@
      */
     int printvalue(const char *key, const char *value, const char *comment = NoComment, const int width = DefComPos);
 
+    int printcomment(const char *comment, const int width = DefComPos);
+
 //------------------------------------------------------------------------------
 
     /** Initializes the parser (called by the constructor).