]> git.sur5r.net Git - c128-kasse/commitdiff
kasse: move inputting nicknames w/ completion in its own function
authorMaik Fischer <maikf@qu.cx>
Sun, 29 Oct 2017 17:14:32 +0000 (18:14 +0100)
committerMaik Fischer <maikf@qu.cx>
Mon, 30 Oct 2017 10:12:59 +0000 (11:12 +0100)
Makefile
include/general.h
src/general.c
src/kasse.c

index 8322b5cf033d7e6be343c7dfe423d9e598f30a7a..bf11dd85cc130a6a4234aa4aacffd2f56aefbd14 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -36,7 +36,7 @@ kasse: build/config.o build/kasse.o build/general.o build/credit_manager.o build
 itemz: build/config.o build/itemz.o build/general.o build/credit_manager.o build/c128time.o build/print.o build/globals.o
        ${LD} -Ln $@.lbl -t c128 $^ -o $@
 
-cat: build/general.o build/cat.o
+cat: build/general.o build/cat.o build/config.o build/print.o build/globals.o
        ${LD} -Ln $@.lbl -t c128 $^ -o $@
 
 charmap: build/print_charmap.o build/vdc_util.o build/vdc_patch_charset.o
index 0133e300fa0e7bb4943f380fc4383a77a8dc8f6b..0cd284405cdb17db09038445ff1be943d65e4319 100644 (file)
@@ -2,6 +2,7 @@
 #define GENERAL_H_
 
 #include <peekpoke.h>
+#include <stdint.h>
 #include "vdc_patch_charset.h"
 
 typedef unsigned char BYTE;
@@ -16,6 +17,7 @@ char *get_input(void);
 BYTE cgetn_input(char *s, BYTE len);
 int16_t cget_number(int16_t default_val);
 void cget_return(void);
+uint8_t cget_nickname(char *buf, uint8_t len);
 char retry_or_quit(void);
 char *format_euro(char *s, int maxlen, int cent);
 void c128_perror(BYTE, char *);
index d888a9a1183fe9a6cea4bbc6ceea1f8dc46ddae1..a036603613b0c1ba482f790c825051b3346ed1bd 100644 (file)
@@ -11,6 +11,7 @@
 #include <stdint.h>
 
 #include "general.h"
+#include "config.h"
 #include "vdc_patch_charset.h"
 
 /*
@@ -125,6 +126,80 @@ int16_t cget_number(int16_t default_val) {
   return atoi(buf);
 }
 
+uint8_t cget_nickname(char *nickname, uint8_t length) {
+  uint8_t i, x, y, matches;
+  char *uniquematch;
+  input_terminator_t terminator;
+
+  memset(nickname, '\0', length);
+
+  while (1) {
+    terminator = get_input_terminated_by(
+        INPUT_TERMINATOR_RETURN | INPUT_TERMINATOR_SPACE, nickname, length);
+
+    /* 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) {
+      return strlen(nickname);
+    }
+
+    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);
+    }
+  }
+}
+
+/* wait until user pressed RETURN, ignore all other input */
 void cget_return() {
   while (cgetc() != PETSCII_CR) {
   }
index eca3fce45a4870bf797b206ea480aa7426209cc5..ea2e6635b9d70acf9a5a2044dcaa5f8896131e2f 100644 (file)
@@ -148,9 +148,6 @@ static signed int buy(char *name, unsigned int price) {
   char rest[EUR_FORMAT_MINLEN];
   struct credits_t *credit;
 
-  memset(nickname, '\0', sizeof(nickname));
-  memset(rest, '\0', sizeof(rest));
-
   clrscr();
   cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
 
@@ -164,82 +161,9 @@ static signed int buy(char *name, unsigned int price) {
   }
 
   cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
-  {
-    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;
-      }
+  nickname_len = cget_nickname(nickname, sizeof(nickname));
 
-      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' && *nickname != PETSCII_SP) {
-    nickname_len = strlen(nickname);
+  if (nickname_len && *nickname != '\0' && *nickname != PETSCII_SP) {
     /* go through credits and remove the amount of money or set nickname
      * to NULL if no such credit could be found */
     credit = find_credit(nickname);
@@ -283,10 +207,6 @@ static signed int buy(char *name, unsigned int price) {
       cget_return();
       return 0;
     }
-  } else {
-    /* Ensure that nickname is NULL if it's empty because it's used in print_log
-     */
-    *nickname = '\0';
   }
 
   money += price * einheiten;