]> git.sur5r.net Git - c128-kasse/blobdiff - src/kasse.c
no-op: remove unused variable
[c128-kasse] / src / kasse.c
index a1cc6bc69e64a37b2ec5e54e3eda1f2bc3e42653..ff244dc8df80591c32ba718472a32e34105226cf 100644 (file)
 #include "credit_manager.h"
 #include "c128time.h"
 #include "print.h"
+#include "version.h"
 // drucker 4 oder 5
 // graphic 4,0,10
 
-static void sane_exit() {
-       save_items();
-       save_credits();
-       exit(1);
+void print_item(BYTE i) {
+    char profit[10];
+    if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) {
+        cprintf("Preis %ld konnte nicht umgerechnet werden\r\n", status.status[i].price);
+        exit(1);
+    }
+    textcolor(TC_YELLOW);
+    cprintf("%2d", i);
+    textcolor(TC_LIGHT_GRAY);
+    cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \xDD%s, %3dx ",
+        status.status[i].item_name, profit, status.status[i].times_sold);
 }
 
 /* Hauptbildschirm ausgeben */
-static void print_screen() {
+static void print_screen(void) {
        BYTE i = 0;
        char *time = get_time();
        char profit[10];
@@ -37,41 +45,38 @@ static void print_screen() {
                exit(1);
        }
        textcolor(TC_CYAN);
-       cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r) " GV "\r");
+       cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r) " GV "\r\n");
        textcolor(TC_LIGHT_GRAY);
-       cprintf("\
-\r\nUhrzeit:     %s (wird nicht aktualisiert)\r\
-Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n",
-       time, profit, items_sold, (printing == 1 ? "ein" : "aus"));
+       cprintf("\r\nUhrzeit:     %s (wird nicht aktualisiert)\r\n"
+            "Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n",
+            time, profit, items_sold, (printing == 1 ? "ein" : "aus"));
        textcolor(TC_LIGHT_GRAY);
-       cprintf("      \xB0\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB2\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB2\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB2\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xAE\r\n");
+       cprintf("      \xB0"
+            "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
+            "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
+            "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
+            "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xAE\r\n");
        for (; i < min(status.num_items, 15); ++i) {
-               if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) {
-                       cprintf("Preis %ld konnte nicht umgerechnet werden\r\n", status.status[i].price);
-                       exit(1);
-               }
 
-               cprintf("      \x7D");
-               textcolor(TC_YELLOW);
-               cprintf("%2d", i);
-               textcolor(TC_LIGHT_GRAY);
-               cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \x7D%s, %3dx \x7D",
-                       status.status[i].item_name, profit, status.status[i].times_sold);
-               if ((i+16) < status.num_items) {
+        cprintf("      \xDD");
+        print_item(i);
+        cprintf("\xDD");
+
+        /* if we have more than 15 items, use the second column */
+               if ((i+15) < status.num_items) {
+            print_item(i+15);
+            cprintf("\xDD");
+               } else {
+            cprintf("              \xDD                \xDD");
+        }
 
-                       if (format_euro(profit, sizeof(profit), status.status[i+16].price) == NULL) {
-                               cprintf("Preis %ld konnte nicht umgerechnet werden\r\n", status.status[i+16].price);
-                               exit(1);
-                       }
-                       textcolor(TC_YELLOW);
-                       cprintf("%2d", i+16);
-                       textcolor(TC_LIGHT_GRAY);
-                       cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \x7D%s, %3dx \x7D",
-                               status.status[i+16].item_name, profit, status.status[i+16].times_sold);
-               } else cprintf("              \x7D                \x7D");
                cprintf("\r\n");
        }
-       cprintf("      \xAD\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB1\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB1\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xB1\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\x60\xBD\r\n");
+       cprintf("      \xAD"
+            "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
+            "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
+            "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
+            "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xBD\r\n");
        textcolor(TC_YELLOW);
        cprintf("   s");
        textcolor(TC_LIGHT_GRAY);
@@ -130,42 +135,141 @@ static signed int buy(char *name, unsigned int price) {
        int negative = 1;
        char entered[5] = {'1', 0, 0, 0, 0};
        BYTE i = 0, matches = 0;
-       BYTE c, nickname_len;
+       BYTE c, x, y, nickname_len;
        int einheiten;
-       char *input;
-       char nickname[11];
+       char nickname[NICKNAME_MAX_LEN+1];
        char rest[11];
        struct credits_t *credit;
 
+       memset(nickname, '\0', sizeof(nickname));
        memset(rest, ' ', sizeof(rest));
        rest[8] = '\0';
 
        clrscr();
        cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
+       x = wherex();
+       y = wherey();
        while (1) {
+               /* Buffer-Ende erreicht? */
+               if (i == 4)
+                       break;
+
                c = cgetc();
-               if (c == 13)
+               /* Enter */
+               if (c == PETSCII_CR)
                        break;
-               cputc(c);
+               /* Backspace */
+               if (c == PETSCII_DEL) {
+                       if (i == 0)
+                               continue;
+                       entered[--i] = '\0';
+                       cputcxy(x+i, y, ' ');
+                       gotoxy(x+i, y);
+                       continue;
+               }
                if (c == 27) {
                        cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
                        get_input();
                        return 1;
-               } else if (c == '-' && i == 0)
+               }
+               if (c == '-' && i == 0) {
                        negative = -1;
-               else if (c > 47 && c < 58)
+                       cputc(c);
+               } else if (c >= PETSCII_0 && c <= PETSCII_9) {
                        entered[i++] = c;
+                       cputc(c);
+               }
+
+               /* Ungültige Eingabe (keine Ziffer), einfach ignorieren */
        }
        einheiten = atoi(entered) * negative;
+
+       if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
+               cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, druecke RETURN...\r\n");
+               cgetc();
+               return 1;
+       }
        
        toggle_videomode();
        cprintf("\r\n             *** VERKAUF ***\r\n\r\n");
        cprintf("%dx %s", einheiten, name);
        toggle_videomode();
-       
+
        cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
-       input = get_input();
-       strncpy(nickname, input, 11);
+       {
+               BYTE i;
+               BYTE x;
+               BYTE y;
+               BYTE matches;
+               char *uniquematch;
+               input_terminator_t terminator;
+               while (1) {
+                       terminator = get_input_terminated_by(
+                                       INPUT_TERMINATOR_RETURN |
+                                       INPUT_TERMINATOR_SPACE,
+                                       nickname,
+                                       sizeof(nickname));
+
+                       /* Clear the screen from any previous completions */
+                       x = wherex();
+                       y = wherey();
+                       for (i = 1; i < 7; i++) {
+                               /* "Completion:" is longer than NICKNAME_MAX_LEN */
+                               cclearxy(0, y + i, strlen("Completion:"));
+                       }
+                       gotoxy(x, y);
+
+                       if (terminator != INPUT_TERMINATOR_SPACE) {
+                               break;
+                       }
+
+                       matches = 0;
+                       uniquematch = NULL;
+                       for (i = 0; i < credits.num_items; i++) {
+                               if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) != 0) {
+                                       continue;
+                               }
+                               matches++;
+                               if (matches > 1) {
+                                       break;
+                               }
+                               uniquematch = credits.credits[i].nickname;
+                       }
+                       if (matches == 1) {
+                               /* Display the rest of the nickname */
+                               textcolor(TC_LIGHT_GREEN);
+                               cprintf("%s", uniquematch + strlen(nickname));
+                               textcolor(TC_LIGHT_GRAY);
+                               strcat(nickname, uniquematch + strlen(nickname));
+                       } else {
+                               /* Multiple nicknames match what was entered so far. Abort and
+                                * display all matches, then prompt the user again. */
+                               char completion[NICKNAME_MAX_LEN+1];
+                               BYTE len = strlen(nickname);
+                               x = wherex();
+                               y = wherey();
+                               cprintf("\r\nCompletion:\r\n");
+                               matches = 0;
+                               for (i = 0; i < credits.num_items; i++) {
+                                       if (strncmp(nickname, credits.credits[i].nickname, len) != 0) {
+                                               continue;
+                                       }
+                                       if (++matches == 5) {
+                                               cprintf("...\r\n");
+                                               break;
+                                       }
+                                       strcpy(completion, credits.credits[i].nickname);
+                                       *(completion + len) = '\0';
+                                       cprintf("%s", completion);
+                                       textcolor(TC_LIGHT_GREEN);
+                                       cprintf("%c", *(credits.credits[i].nickname + len));
+                                       textcolor(TC_LIGHT_GRAY);
+                                       cprintf("%s\r\n", completion + len + 1);
+                               }
+                               gotoxy(x, y);
+                       }
+               }
+       }
        if (*nickname != '\0') {
                toggle_videomode();
                cprintf(" fuer %s\r\n", nickname);
@@ -238,7 +342,7 @@ void buy_stock(BYTE n) {
        status.status[n].times_sold += buy(status.status[n].item_name, status.status[n].price);
 }
 
-void buy_custom() {
+void buy_custom(void) {
        BYTE c = 0, i = 0;
        int negative = 1;
        char entered[5] = {'1', 0, 0, 0, 0};
@@ -275,7 +379,7 @@ void buy_custom() {
        buy(name, price);
 }
 
-void set_time_interactive() {
+void set_time_interactive(void) {
        BYTE part[3] = {'0', '0', '\0'};
        BYTE tp1, tp2, tp3;
        char *time_input, *time;
@@ -296,13 +400,17 @@ void set_time_interactive() {
        cprintf("\r\nZeit gesetzt: %s\r\n", time);
 }
 
-int main() {
+int main(void) {
        char *c;
        char *time;
 
        if (VIDEOMODE == 40)
                toggle_videomode();
        clrscr();
+
+       /* Allocate logging buffer memory */
+       init_log();
+
        /* Set time initially, c128 doesn't know it */
        set_time_interactive();
 
@@ -322,7 +430,7 @@ int main() {
        sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
        print_the_buffer();
 
-       sprintf(print_buffer, "%cKasse gestartet um %s. Nutze logfile log-%u, zeile %d.\r", 17, time, log_num, log_lines_written);
+       sprintf(print_buffer, "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17, time, log_num, log_heap_offset);
        print_the_buffer();
 
        print_header();
@@ -346,7 +454,8 @@ int main() {
                } else if (*c == 's') {
                        save_items();
                        save_credits();
-                       cprintf("Statefile/Creditfile gesichert, druecke RETURN...\r\n");
+                       log_flush();
+                       cprintf("\r\nStatefile/Creditfile/Log gesichert, druecke RETURN...\r\n");
                        get_input();
                } else if (*c == 'g') {
                        credit_manager();