From b1f598f80d8ca8a53678080e08bf487f97127c12 Mon Sep 17 00:00:00 2001 From: Maik Fischer Date: Sat, 28 Oct 2017 12:35:26 +0200 Subject: [PATCH] change ad-hoc number input code to use cget_number() --- include/general.h | 1 + src/credit_manager.c | 13 ++++----- src/general.c | 57 ++++++++++++++++++++++++++++++++++--- src/itemz.c | 27 ++++++++---------- src/kasse.c | 68 +++++++------------------------------------- 5 files changed, 81 insertions(+), 85 deletions(-) diff --git a/include/general.h b/include/general.h index e07176e..413fa46 100644 --- a/include/general.h +++ b/include/general.h @@ -13,6 +13,7 @@ input_terminator_t get_input_terminated_by(input_terminator_mask_t terminators, char *out, BYTE outlen); char *get_input(void); BYTE cgetn_input(char *s, BYTE len); +int16_t cget_number(int16_t default_val); void cget_return(void); char retry_or_quit(void); char *format_euro(char *s, int maxlen, int cent); diff --git a/src/credit_manager.c b/src/credit_manager.c index 6590f85..6dbae85 100644 --- a/src/credit_manager.c +++ b/src/credit_manager.c @@ -64,7 +64,6 @@ struct credits_t *find_credit(char *name) { void deposit_credit(char *nickname) { char *time = get_time(); - char *input; struct credits_t *credit; unsigned int deposit; @@ -72,8 +71,7 @@ void deposit_credit(char *nickname) { return; // cannot find named credit cprintf("\r\nEinzahlung in Cent:\r\n"); - if ((input = get_input()) == NULL || *input == '\0' || - (deposit = atoi(input)) == 0) + if (cget_number(0) == 0) return; credit->credit += deposit; @@ -84,7 +82,7 @@ void deposit_credit(char *nickname) { } static void new_credit(void) { - char *input, *name; + char name[NICKNAME_MAX_LEN + 1]; char *time; int credit; @@ -97,12 +95,11 @@ static void new_credit(void) { clrscr(); cprintf("\rNickname (max. 10 Zeichen):\r\n"); - if ((input = get_input()) == NULL || *input == '\0') + if (cgetn_input(name, sizeof(name)) == 0) return; - name = strdup(input); + cprintf("\r\nGuthaben in Cents:\r\n"); - if ((input = get_input()) == NULL || *input == '\0' || - (credit = atoi(input)) == 0) + if ((credit = cget_number(0)) == 0) return; strncpy(credits.credits[credits.num_items].nickname, name, NICKNAME_MAX_LEN); credits.credits[credits.num_items].credit = credit; diff --git a/src/general.c b/src/general.c index 91318aa..df0c2c6 100644 --- a/src/general.c +++ b/src/general.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "general.h" #include "vdc_patch_charset.h" @@ -72,14 +73,62 @@ BYTE cgetn_input(char *s, BYTE len) { return strlen(s); } -void cget_return() { - BYTE c; +int16_t cget_number(int16_t default_val) { + char c; + int x, y; + uint8_t num_chars = 0; + char buf[6] = {0, 0, 0, 0, 0, 0}; + int i = 0; + x = wherex(); + y = wherey(); while (1) { c = cgetc(); - if (c == PETSCII_CR) { - return; + + /* Enter */ + if (c == PETSCII_CR) + break; + + /* Backspace */ + if (c == PETSCII_DEL) { + if (i == 0) + continue; + buf[--i] = '\0'; + cputcxy(x + i, y, ' '); + gotoxy(x + i, y); + continue; + } + + /* Abort */ + if (c == PETSCII_ESC) { + return default_val; } + + /* end of buffer? wait for user to press RETURN */ + if (i == (sizeof(buf) - 1)) + continue; + + /* match either numbers or iff it's the first entered char a minus sign */ + if ((c >= PETSCII_0 && c <= PETSCII_9) || (c == '-' && i == 0)) { + buf[i] = c; + ++i; + ++num_chars; + cputc(c); + } + } + + if (num_chars == 0) { + return default_val; + } else if ((num_chars == 1) && (c == '-')) { + return default_val; + } + + return atoi(buf); +} + +void cget_return() { + while (cgetc() != PETSCII_CR) { } + return; } char retry_or_quit(void) { diff --git a/src/itemz.c b/src/itemz.c index 7751b68..da2b7bd 100644 --- a/src/itemz.c +++ b/src/itemz.c @@ -22,7 +22,7 @@ static void itemz_print_screen(void) { char buffer[10]; clrscr(); - cprintf("itemz (phil_fry, sECuRE, sur5r) v:" GV "\r\n\r\n"); + cprintf("itemz (phil_fry, sECuRE, sur5r, mxf) v:" GV "\r\n\r\n"); cprintf("Datei: ITEMS\r\n\r\n"); for (i = 0; i < max(status.num_items, 15); i++) { if (format_euro(buffer, 10, status.status[i].price) != buffer) { @@ -37,35 +37,32 @@ static void itemz_print_screen(void) { } static void new_item(void) { - char *input, *name; - int price; + char name[MAX_ITEM_NAME_LENGTH + 1]; + int price, times_sold; if (status.num_items == MAX_ITEMS) { cprintf("\rEs ist bereits die maximale Anzahl an Eintraegen erreicht, " "druecke RETURN...\r\n"); - input = get_input(); + cget_return(); return; } cprintf("\rName des Eintrags:\r\n"); - if ((input = get_input()) == NULL || *input == '\0') + if (cgetn_input(name, sizeof(name)) == 0) return; - name = strdup(input); cprintf("\r\nPreis in Cents:\r\n"); - if ((input = get_input()) == NULL || *input == '\0' || - (price = atoi(input)) == 0) + if ((price = cget_number(0)) <= 0) return; cprintf("\r\nWie oft schon verkauft? [0] \r\n"); - if ((input = get_input()) == NULL) + if ((times_sold = cget_number(0)) < 0) return; memset(status.status[status.num_items].item_name, '\0', MAX_ITEM_NAME_LENGTH + 1); strncpy(status.status[status.num_items].item_name, name, MAX_ITEM_NAME_LENGTH); status.status[status.num_items].price = price; - status.status[status.num_items].times_sold = atoi(input); + status.status[status.num_items].times_sold = times_sold; status.num_items++; - free(name); } static void _delete_item(BYTE num) { @@ -75,17 +72,17 @@ static void _delete_item(BYTE num) { } static void delete_item(void) { - char *input; BYTE num, last; cprintf("\r Welcher Eintrag soll geloescht werden?\r\n"); - if ((input = get_input()) == NULL || *input == '\0') + if ((num = cget_number(-1)) < 0) return; - num = atoi(input); + if (status.num_items > 1) { /* Swap last item with this one and delete the last one to avoid holes */ last = (status.num_items - 1); - strcpy(status.status[num].item_name, status.status[last].item_name); + strncpy(status.status[num].item_name, status.status[last].item_name, + MAX_ITEM_NAME_LENGTH); status.status[num].price = status.status[last].price; status.status[num].times_sold = status.status[last].times_sold; _delete_item(last); diff --git a/src/kasse.c b/src/kasse.c index b59d44d..91b4268 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -141,10 +141,8 @@ static void print_log(char *name, int item_price, int einheiten, char *nickname, /* dialog which is called for each bought item */ 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, x, y, nickname_len; + BYTE matches = 0; + BYTE c, nickname_len; int einheiten; char nickname[NICKNAME_MAX_LEN + 1]; char rest[10]; @@ -155,42 +153,8 @@ static signed int buy(char *name, unsigned int price) { clrscr(); cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name); - x = wherex(); - y = wherey(); - while (1) { - /* Buffer-Ende erreicht? */ - if (i == (sizeof(entered) - 1)) - break; - c = cgetc(); - /* Enter */ - if (c == PETSCII_CR) - break; - /* Backspace */ - if (c == PETSCII_DEL) { - if (i == 0) - continue; - entered[--i] = '\0'; - cputcxy(x + i, y, ' '); - gotoxy(x + i, y); - continue; - } - if (c == PETSCII_ESC) { - cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n"); - cget_return(); - return 1; - } - if (c == '-' && i == 0) { - negative = -1; - 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; + einheiten = cget_number(1); if (einheiten > 100 || einheiten < -100 || einheiten == 0) { cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, dr" uUML "cke " @@ -345,9 +309,6 @@ void buy_stock(BYTE n) { } void buy_custom(void) { - BYTE c = 0, i = 0; - int negative = 1; - char entered[5] = {'1', 0, 0, 0, 0}; char name[MAX_ITEM_NAME_LENGTH + 1]; int price; @@ -357,23 +318,14 @@ void buy_custom(void) { return; cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name); - while (1) { - c = cgetc(); - if (c == PETSCII_CR) - break; - cputc(c); - if (c == PETSCII_ESC) { - cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n"); - cget_return(); - return; - } else if (c == '-' && i == 0) - negative = -1; - else if (c >= PETSCII_0 && c <= PETSCII_9) - entered[i++] = c; - } - price = atoi(entered) * negative; - cprintf("\r\n"); + price = cget_number(0); + + if (price == 0) { + cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n"); + cget_return(); + return; + } buy(name, price); } -- 2.39.5