]> git.sur5r.net Git - c128-kasse/blob - src/credit_manager.c
Clear screen before asking questions
[c128-kasse] / src / credit_manager.c
1 /* 
2  * RGB2R-C128-Kassenprogramm
3  * (c) 2007-2008 phil_fry, sECuRE, sur5r
4  * See LICENSE for license information
5  *
6  */
7 #include <conio.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "config.h"
13 #include "general.h"
14
15 static char *filter = NULL;
16 static BYTE filter_len;
17
18 static BYTE current_credits_page = 0;
19
20 static void credit_print_screen() {
21         BYTE i, pages;
22         char buffer[10];
23
24         clrscr();
25         cprintf("itemz (phil_fry, sECuRE, sur5r)\r\n\r\n");
26         pages = (credits.num_items / CREDITS_PER_PAGE);
27         if (current_credits_page > pages)
28                 current_credits_page = pages;
29         cprintf("Datei: CREDITS (Seite %d von %d)\r\n\r\n", current_credits_page, pages);
30         for (i = (current_credits_page * CREDITS_PER_PAGE); i < credits.num_items && i < ((current_credits_page+1) * CREDITS_PER_PAGE); i++) {
31                 if (filter == NULL || strncmp(credits.credits[i].nickname, filter, filter_len) == 0) {
32                         if (format_euro(buffer, 10, credits.credits[i].credit) != buffer) {
33                                 cprintf("Error: Could not format credit %d\r\n", credits.credits[i].credit);
34                                 exit(1);
35                         }
36         
37                         cprintf("%d: %s: %s\r\n", i, credits.credits[i].nickname, buffer);
38                 }
39         }
40         cprintf("\r\nn) Neu d) Loeschen p) Einzahlen b) Seite hoch f) Seite runter\r\ng) Filtern e) Aendern s) Speichern z) Zurueck\r\n");
41 }
42
43 struct credits_t *find_credit(char *name){
44         int i;
45         for (i = 0; i < credits.num_items; i++)
46                 if (strncmp(name, credits.credits[i].nickname, NICKNAME_MAX_LEN + 1) == 0)
47                         return &credits.credits[i];
48         return NULL;
49 }
50
51 /*
52  * when depositing money with this and returning to the main menu, the program
53  * will crash with a message like the following:
54  * 
55  */
56 static void deposit_credit() {
57         char *input;
58         struct credits_t *credit;
59         unsigned int deposit;
60
61         cprintf("\r\nName:\r\n");
62         if ((input = get_input()) == NULL || *input == '\0')
63                 return; // no name given
64                 
65         if ((credit = find_credit(input)) == NULL)
66                 return; // cannot find named credit
67         
68         cprintf("\r\nEinzahlung in Cent:\r\n");
69         if ((input = get_input()) == NULL || *input == '\0' || (deposit = atoi(input)) == 0)
70                 return;
71
72         credit->credit += deposit;
73         
74         toggle_videomode();
75         cprintf("%d Cent eingezahlt fuer %s.\r\nRestguthaben: %d\r\n", deposit, credit->nickname, credit->credit);
76         toggle_videomode();
77         cprintf("\r\nEinzahlung durchgefuehrt, drucke RETURN...\r\n");
78         input = get_input();
79         toggle_videomode();
80         clrscr();
81         toggle_videomode();
82 }
83
84 static void new_credit() {
85         char *input, *name;
86         int credit;
87
88         if (credits.num_items == 75) {
89                 cprintf("\rEs ist bereits die maximale Anzahl an Eintraegen erreicht, druecke RETURN...\r\n");
90                 input = get_input();
91                 return;
92         }
93
94         clrscr();
95         cprintf("\rNickname:\r\n");
96         if ((input = get_input()) == NULL || *input == '\0')
97                 return;
98         name = strdup(input);
99         cprintf("\r\nGuthaben in Cents:\r\n");
100         if ((input = get_input()) == NULL || *input == '\0' || (credit = atoi(input)) == 0)
101                 return;
102         strcpy(credits.credits[credits.num_items].nickname, name);
103         credits.credits[credits.num_items].credit = credit;
104         credits.num_items++;
105         free(name);
106 }
107
108 static void _delete_credit(BYTE num) {
109         memset(credits.credits[num].nickname, '\0', 11);
110         credits.credits[num].credit = 0;
111 }
112
113 static void delete_credit() {
114         char *input;
115         BYTE num, last;
116
117         cprintf("\r Welcher Eintrag soll geloescht werden?\r\n");
118         if ((input = get_input()) == NULL || *input == '\0')
119                 return;
120         num = atoi(input);
121         if (credits.num_items > 1) {
122                 /* Swap last item with this one and delete the last one to avoid holes */
123                 last = (credits.num_items - 1);
124                 strcpy(credits.credits[num].nickname, credits.credits[last].nickname);
125                 credits.credits[num].credit = credits.credits[last].credit;
126                 _delete_credit(last);
127         } else {
128                 /* Just delete it */
129                 _delete_credit(num);
130         }
131         credits.num_items--;
132 }
133
134 void credit_manager(){
135         char *c;
136         while(1){
137                 credit_print_screen();
138                 c = get_input();
139                 switch (*c) {
140                         case 'n':
141                                 new_credit(); break;
142                         case 'd':
143                                 delete_credit(); break;
144                         case 's':
145                                 save_credits(); break;
146                         case 'f':
147                                 if (current_credits_page < (credits.num_items / CREDITS_PER_PAGE))
148                                                 current_credits_page++;
149                                 break;
150                         case 'b':
151                                 if (current_credits_page > 0)
152                                         current_credits_page--;
153                                 break;
154                         case 'p':
155                                 deposit_credit(); break;
156                         case 'g':
157                                 cprintf("Filter eingeben:\r\n");
158                                 filter = get_input();
159                                 if (filter == NULL || *filter == 32 || (filter_len = strlen(filter)) == 0)
160                                         filter = NULL;
161                                 break;
162                         case 'z':
163                                 return; 
164                         default:
165                                 cprintf("Unbekannter Befehl, druecke RETURN...\r\n");
166                                 get_input(); 
167                 }
168         }       
169 }