]> git.sur5r.net Git - c128-kasse/blob - src/general.c
53d7804864952b7ac38f265dd1fd4d06c14bade0
[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
14 /*
15  * get_input_terminated_by() reads input (handling backspace correctly) until
16  * a terminator of |terminators| is encountered or |out| is full (outlen-1
17  * characters were read).
18  *
19  * get_input_terminated_by() returns the terminator it encountered.
20  *
21  */
22 input_terminator_t get_input_terminated_by(input_terminator_mask_t terminators, char *out, BYTE outlen) {
23         BYTE i = strlen(out);
24         BYTE c, x, y;
25         x = wherex() - i;
26         y = wherey();
27         while (1) {
28                 c = cgetc();
29                 if (((terminators & INPUT_TERMINATOR_RETURN) == INPUT_TERMINATOR_RETURN) && (c == PETSCII_CR)) {
30                         return INPUT_TERMINATOR_RETURN;
31                 } else if (((terminators & INPUT_TERMINATOR_SPACE) == INPUT_TERMINATOR_SPACE) && (c == PETSCII_SP)) {
32                         return INPUT_TERMINATOR_SPACE;
33                 } else if (c == PETSCII_DEL) {
34                         /* If you are at the left-most position, do nothing */
35                         if (i == 0)
36                                 continue;
37                         out[--i] = '\0';
38                         cputcxy(x+i, y, ' ');
39                         gotoxy(x+i, y);
40                         continue;
41                 }
42                 if (i == (outlen-1)) {
43                         continue;
44                 }
45                 cputc(c);
46                 out[i++] = c;
47         }
48 }
49
50 /*
51  * Liest (maximal 31) Zeichen ein, bis Enter gedrückt wird.
52  * Vorsicht: Es wird ein statischer Buffer benutzt, sodass man
53  * das Ergebnis via strdup() retten muss, bevor man get_input()
54  * erneut aufruft
55  *
56  */
57 char *get_input(void) {
58         static char output[32];
59         memset(output, '\0', sizeof(output));
60         get_input_terminated_by(INPUT_TERMINATOR_RETURN, output, sizeof(output));
61         return output;
62 }
63
64 char retry_or_quit(void) {
65         char *c;
66         do {
67                 cprintf("\r\nr)etry or q)uit?\r\n");
68                 c = get_input();
69         } while ((*c != 'r') && (*c != 'q'));
70         return *c;
71 }
72
73 char *format_euro(char *s, int maxlen, int cent){
74         int tmp = cent;
75         int len = strlen(",EUR");
76         while ((tmp /= 10) > 0)
77                 ++len;
78         if (len >= maxlen)
79                 return NULL;
80         // workaround to produce a leading zero for cents.. %0.2d won't work 
81         sprintf(s, "%3d,%s%dEUR", cent / 100, ((cent%100)<10?"0":""), cent % 100);
82         return s;
83 }
84
85 void c128_perror(BYTE c, char *msg) {
86         cprintf("\r\nError (Code %d) while: %s\r\nOS Error = %d\r\n", c, msg, _oserror);
87 }