clean display format
authorLaurent Mazet <mazet@softndesign.org>
Sat, 28 Jan 2023 23:12:29 +0000 (00:12 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 28 Jan 2023 23:12:29 +0000 (00:12 +0100)
parser.c

index dcaa55b0d09e8d9b8adc32498a06eb4d7e8a7790..a1602a0f3b05360e8a34bfb29b7f25f5582e4b86 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -19,6 +19,7 @@ double *storage = NULL;
 
 #define DEFAULT_FORMAT "=> %.6g\n"
 char *format = NULL;
+char *minform = NULL;
 
 /* compare codes */
 
@@ -572,7 +573,7 @@ double store (int index, double value)
     if ((index > 0) && (index <= storage_size)) {
         storage[index - 1] = value;
     } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, storage_size));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return value;
 }
@@ -585,7 +586,7 @@ double recall (int index)
     if ((index > 0) && (index <= storage_size)) {
         return storage[index - 1];
     } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, storage_size));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return 0;
 }
@@ -598,7 +599,7 @@ double increase (int index)
     if ((index > 0) && (index <= storage_size)) {
         return storage[index - 1]++;
     } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, storage_size));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return 0;
 }
@@ -611,7 +612,7 @@ double decrease (int index)
     if ((index > 0) && (index <= storage_size)) {
         return storage[index - 1]--;
     } else {
-        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, storage_size));
+        VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
     }
     return 0;
 }
@@ -624,7 +625,8 @@ void display (void)
     }
     fprintf (stdout, "storage:");
     for (i = 0; i < storage_size; i++) {
-        fprintf (stdout, " %g", storage[i]);
+        fprintf (stdout, " ");
+        fprintf (stdout, minform, storage[i]);
     }
     fprintf (stdout, "\n");
 }
@@ -673,9 +675,11 @@ double program_do (element_t **prog, int nbcalls)
 void set_format (char *prompt, int precision)
 {
     char buffer[128] = {0};
-    sprintf (buffer, "%s%%.%dg\n", prompt, precision);
     free_format ();
+    sprintf (buffer, "%s%%.%dg\n", prompt, precision);
     format = strdup (buffer);
+    sprintf (buffer, "%%.%dg", precision);
+    minform = strdup (buffer);
 }
 
 void free_format ()
@@ -684,6 +688,10 @@ void free_format ()
         free (format);
         format = NULL;
     }
+    if (minform) {
+        free (minform);
+        minform = NULL;
+    }
 }
 
 double print (double value)