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);
void deposit_credit(char *nickname) {
char *time = get_time();
- char *input;
struct credits_t *credit;
unsigned int deposit;
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;
}
static void new_credit(void) {
- char *input, *name;
+ char name[NICKNAME_MAX_LEN + 1];
char *time;
int credit;
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;
#include <stdio.h>
#include <string.h>
#include <conio.h>
+#include <stdint.h>
#include "general.h"
#include "vdc_patch_charset.h"
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) {
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) {
}
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) {
}
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);
/* 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];
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 "
}
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;
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);
}