]> git.sur5r.net Git - c128-kasse/blob - src/general.c
df0c2c6d70ef58373ed74fa63a955b9ebb943c22
[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 #include <stdint.h>
12
13 #include "general.h"
14 #include "vdc_patch_charset.h"
15
16 /*
17  * get_input_terminated_by() reads input (handling backspace correctly) until
18  * a terminator of |terminators| is encountered or |out| is full (outlen-1
19  * characters were read).
20  *
21  * get_input_terminated_by() returns the terminator it encountered.
22  *
23  */
24 input_terminator_t get_input_terminated_by(input_terminator_mask_t terminators,
25                                            char *out, BYTE outlen) {
26   BYTE i = strlen(out);
27   BYTE c, x, y;
28   x = wherex() - i;
29   y = wherey();
30   while (1) {
31     c = cgetc();
32     if (((terminators & INPUT_TERMINATOR_RETURN) == INPUT_TERMINATOR_RETURN) &&
33         (c == PETSCII_CR)) {
34       return INPUT_TERMINATOR_RETURN;
35     } else if (((terminators & INPUT_TERMINATOR_SPACE) ==
36                 INPUT_TERMINATOR_SPACE) &&
37                (c == PETSCII_SP)) {
38       return INPUT_TERMINATOR_SPACE;
39     } else if (c == PETSCII_DEL) {
40       /* If you are at the left-most position, do nothing */
41       if (i == 0)
42         continue;
43       out[--i] = '\0';
44       cputcxy(x + i, y, ' ');
45       gotoxy(x + i, y);
46       continue;
47     }
48     if (i == (outlen - 1)) {
49       continue;
50     }
51     cputc(c);
52     out[i++] = c;
53   }
54 }
55
56 /*
57  * Liest (maximal 31) Zeichen ein, bis Enter gedrückt wird.
58  * Vorsicht: Es wird ein statischer Buffer benutzt, sodass man
59  * das Ergebnis via strdup() retten muss, bevor man get_input()
60  * erneut aufruft
61  *
62  */
63 char *get_input(void) {
64   static char output[32];
65   memset(output, '\0', sizeof(output));
66   get_input_terminated_by(INPUT_TERMINATOR_RETURN, output, sizeof(output));
67   return output;
68 }
69
70 BYTE cgetn_input(char *s, BYTE len) {
71   memset(s, '\0', len);
72   get_input_terminated_by(INPUT_TERMINATOR_RETURN, s, len);
73   return strlen(s);
74 }
75
76 int16_t cget_number(int16_t default_val) {
77   char c;
78   int x, y;
79   uint8_t num_chars = 0;
80   char buf[6] = {0, 0, 0, 0, 0, 0};
81   int i = 0;
82   x = wherex();
83   y = wherey();
84   while (1) {
85     c = cgetc();
86
87     /* Enter */
88     if (c == PETSCII_CR)
89       break;
90
91     /* Backspace */
92     if (c == PETSCII_DEL) {
93       if (i == 0)
94         continue;
95       buf[--i] = '\0';
96       cputcxy(x + i, y, ' ');
97       gotoxy(x + i, y);
98       continue;
99     }
100
101     /* Abort */
102     if (c == PETSCII_ESC) {
103       return default_val;
104     }
105
106     /* end of buffer? wait for user to press RETURN */
107     if (i == (sizeof(buf) - 1))
108       continue;
109
110     /* match either numbers or iff it's the first entered char a minus sign */
111     if ((c >= PETSCII_0 && c <= PETSCII_9) || (c == '-' && i == 0)) {
112       buf[i] = c;
113       ++i;
114       ++num_chars;
115       cputc(c);
116     }
117   }
118
119   if (num_chars == 0) {
120     return default_val;
121   } else if ((num_chars == 1) && (c == '-')) {
122     return default_val;
123   }
124
125   return atoi(buf);
126 }
127
128 void cget_return() {
129   while (cgetc() != PETSCII_CR) {
130   }
131   return;
132 }
133
134 char retry_or_quit(void) {
135   char *c;
136   do {
137     cprintf("\r\nr)etry or q)uit?\r\n");
138     c = get_input();
139   } while ((*c != 'r') && (*c != 'q'));
140   return *c;
141 }
142
143 char *format_euro(char *s, int maxlen, int cent) {
144   if (snprintf(s, maxlen, "%3d,%02d" EURSYM, cent / 100, cent % 100) > maxlen)
145     return NULL;
146   return s;
147 }
148
149 void c128_perror(BYTE c, char *msg) {
150   cprintf("\r\nError (Code %d) while: %s\r\nOS Error = %d\r\n", c, msg,
151           _oserror);
152 }