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