]> git.sur5r.net Git - c128-kasse/blob - src/general.c
kasse: use cgetn_input() to simplify logic
[c128-kasse] / src / general.c
1 /*
2  * RGB2R-C128-Kassenprogramm
3  * © 2007-2009 phil_fry, sECuRE, sur5r
4  * See LICENSE for license information
5  *
6  */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <conio.h>
11
12 #include "general.h"
13 #include "vdc_patch_charset.h"
14
15 /*
16  * get_input_terminated_by() reads input (handling backspace correctly) until
17  * a terminator of |terminators| is encountered or |out| is full (outlen-1
18  * characters were read).
19  *
20  * get_input_terminated_by() returns the terminator it encountered.
21  *
22  */
23 input_terminator_t get_input_terminated_by(input_terminator_mask_t terminators,
24                                            char *out, BYTE outlen) {
25   BYTE i = strlen(out);
26   BYTE c, x, y;
27   x = wherex() - i;
28   y = wherey();
29   while (1) {
30     c = cgetc();
31     if (((terminators & INPUT_TERMINATOR_RETURN) == INPUT_TERMINATOR_RETURN) &&
32         (c == PETSCII_CR)) {
33       return INPUT_TERMINATOR_RETURN;
34     } else if (((terminators & INPUT_TERMINATOR_SPACE) ==
35                 INPUT_TERMINATOR_SPACE) &&
36                (c == PETSCII_SP)) {
37       return INPUT_TERMINATOR_SPACE;
38     } else if (c == PETSCII_DEL) {
39       /* If you are at the left-most position, do nothing */
40       if (i == 0)
41         continue;
42       out[--i] = '\0';
43       cputcxy(x + i, y, ' ');
44       gotoxy(x + i, y);
45       continue;
46     }
47     if (i == (outlen - 1)) {
48       continue;
49     }
50     cputc(c);
51     out[i++] = c;
52   }
53 }
54
55 /*
56  * Liest (maximal 31) Zeichen ein, bis Enter gedrückt wird.
57  * Vorsicht: Es wird ein statischer Buffer benutzt, sodass man
58  * das Ergebnis via strdup() retten muss, bevor man get_input()
59  * erneut aufruft
60  *
61  */
62 char *get_input(void) {
63   static char output[32];
64   memset(output, '\0', sizeof(output));
65   get_input_terminated_by(INPUT_TERMINATOR_RETURN, output, sizeof(output));
66   return output;
67 }
68
69 BYTE cgetn_input(char *s, BYTE len) {
70   memset(s, '\0', len);
71   get_input_terminated_by(INPUT_TERMINATOR_RETURN, s, len);
72   return strlen(s);
73 }
74
75 void cget_return() {
76   BYTE c;
77   while (1) {
78     c = cgetc();
79     if (c == PETSCII_CR) {
80       return;
81     }
82   }
83 }
84
85 char retry_or_quit(void) {
86   char *c;
87   do {
88     cprintf("\r\nr)etry or q)uit?\r\n");
89     c = get_input();
90   } while ((*c != 'r') && (*c != 'q'));
91   return *c;
92 }
93
94 char *format_euro(char *s, int maxlen, int cent) {
95   if (snprintf(s, maxlen, "%3d,%02d" EURSYM, cent / 100, cent % 100) > maxlen)
96     return NULL;
97   return s;
98 }
99
100 void c128_perror(BYTE c, char *msg) {
101   cprintf("\r\nError (Code %d) while: %s\r\nOS Error = %d\r\n", c, msg,
102           _oserror);
103 }