From 14234f8d4e52ad1c33b1bc804d9d08c8abbce30d Mon Sep 17 00:00:00 2001 From: Maik Fischer Date: Sun, 29 Oct 2017 18:14:32 +0100 Subject: [PATCH] kasse: move inputting nicknames w/ completion in its own function --- Makefile | 2 +- include/general.h | 2 ++ src/general.c | 75 ++++++++++++++++++++++++++++++++++++++++++ src/kasse.c | 84 ++--------------------------------------------- 4 files changed, 80 insertions(+), 83 deletions(-) diff --git a/Makefile b/Makefile index 8322b5c..bf11dd8 100644 --- 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 diff --git a/include/general.h b/include/general.h index 0133e30..0cd2844 100644 --- a/include/general.h +++ b/include/general.h @@ -2,6 +2,7 @@ #define GENERAL_H_ #include +#include #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 *); diff --git a/src/general.c b/src/general.c index d888a9a..a036603 100644 --- a/src/general.c +++ b/src/general.c @@ -11,6 +11,7 @@ #include #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) { } diff --git a/src/kasse.c b/src/kasse.c index eca3fce..ea2e663 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -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; -- 2.39.2