]> git.sur5r.net Git - c128-kasse/blobdiff - src/kasse.c
Do not crash when format_euro() fails
[c128-kasse] / src / kasse.c
index ea2e6635b9d70acf9a5a2044dcaa5f8896131e2f..d5426c2cdae47b15db30141f5fd15cae3d191c2e 100644 (file)
@@ -25,7 +25,7 @@
 // graphic 4,0,10
 
 void print_item(BYTE i) {
-  char profit[EUR_FORMAT_MINLEN];
+  char profit[EUR_FORMAT_MINLEN + 1];
   if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) {
     cprintf("Preis %ld konnte nicht umgerechnet werden\r\n",
             status.status[i].price);
@@ -42,11 +42,11 @@ void print_item(BYTE i) {
 static void print_screen(void) {
   BYTE i = 0;
   char *time = get_time();
-  char profit[EUR_FORMAT_MINLEN];
+  char profit[EUR_FORMAT_MINLEN + 1];
   clrscr();
   if (format_euro(profit, sizeof(profit), money) == NULL) {
     cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
-    exit(1);
+    profit[0]='\0';
   }
   textcolor(TC_CYAN);
   cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r, mxf) " GV "\r\n");
@@ -112,29 +112,47 @@ static void print_screen(void) {
 static void print_log(char *name, int item_price, int einheiten, char *nickname,
                       char *rest) {
   char *time = get_time();
-  char price[EUR_FORMAT_MINLEN];
-  /* Format:
-     Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
-     -- 6-stellig
-     Uhrzeit -- 8-stellig
-     Eintragname (= Getränk) -- 9-stellig
-     Preis (in Cents) -- 9-stellig
-     Anzahl -- 2-stellig
-     Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig
-     restguthaben (9-stellig)
-
-     + 7 leerzeichen
-     --> 48 zeichen
-     */
+  uint8_t n;
+  char price[EUR_FORMAT_MINLEN + 1];
   if (format_euro(price, sizeof(price), item_price) == NULL) {
     cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
     exit(1);
   }
 
-  sprintf(print_buffer, "%c[%3u] %s - %-" xstr(
-                            MAX_ITEM_NAME_LENGTH) "s - %s - %s - %d - an %s\r",
-          17, status.transaction_id, time, name, price, rest, einheiten,
-          (*nickname != '\0' ? nickname : "Unbekannt"));
+  /* TODO: teach the EUR sign to the printer.
+   * Until then, we just overwrite it with "E" */
+  price[EUR_FORMAT_MINLEN - 1] = 'E';
+  rest[EUR_FORMAT_MINLEN - 1] = 'E';
+
+  /* clang-format off */
+  n = snprintf(print_buffer, sizeof(print_buffer),
+        /* enable lower case letters -- 1 */
+        "%c"
+        /*  Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
+            -- 6-stellig */
+        "[%3u] "
+        /* Uhrzeit -- 8-stellig + 3 */
+        "%8s - "
+        /*  Eintragname (= Getränk) -- 9-stellig + 3 */
+        "%-" xstr(MAX_ITEM_NAME_LENGTH) "s - "
+        /*  Preis (in Cents) -- 7-stellig  + 3 */
+        "%" xstr(EUR_FORMAT_MINLEN) "s - "
+        /*  restguthaben (7-stellig) + 3 */
+        "%" xstr(EUR_FORMAT_MINLEN) "s - "
+        /*  Anzahl -- 2-stellig + 3 */
+        "%2d - "
+        /*  Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig + 4 */
+        "an %" xstr(NICKNAME_MAX_LEN)"s\r",
+        17, status.transaction_id, time, name, price, rest, einheiten,
+        (*nickname != '\0' ? nickname : "Unbekannt"));
+  /* clang-format on */
+  if (n > sizeof(print_buffer)) {
+    cprintf("\r\nprint_log(): print_buffer overflowed!\r\n"
+            "Wanted to write %d bytes\r\n%s\r\n",
+            n, print_buffer);
+    exit(1);
+  }
+
   status.transaction_id++;
   print_the_buffer();
 }
@@ -145,7 +163,7 @@ static signed int buy(char *name, unsigned int price) {
   BYTE c, nickname_len;
   int einheiten;
   char nickname[NICKNAME_MAX_LEN + 1];
-  char rest[EUR_FORMAT_MINLEN];
+  char rest[EUR_FORMAT_MINLEN + 1];
   struct credits_t *credit;
 
   clrscr();