]> git.sur5r.net Git - c128-kasse/blob - src/credit_manager.c
put filter back in again.
[c128-kasse] / src / credit_manager.c
1 /* 
2  * RGB2R-C128-Kassenprogramm
3  * (c) 2007 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         /* 16 entries per page */
27         pages = (credits.num_items / 16);
28         if (current_credits_page > pages)
29                 current_credits_page = pages;
30         cprintf("Datei: CREDITS (Seite %d von %d)\r\n\r\n", current_credits_page, pages);
31         for (i = (current_credits_page * 16); i < credits.num_items && i < ((current_credits_page+1) * 16); i++) {
32                 if (filter == NULL || strncmp(credits.credits[i].nickname, filter, filter_len) == 0) {
33                         if (format_euro(buffer, 10, credits.credits[i].credit) != buffer) {
34                                 cprintf("Error: Could not format credit %d\r\n", credits.credits[i].credit);
35                                 exit(1);
36                         }
37         
38                         cprintf("%d: %s: %s\r\n", i, credits.credits[i].nickname, buffer);
39                 }
40         }
41         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");
42 }
43
44 static struct credits_t * find_credit(char * name){
45         int i;
46         for (i=0;i<credits.num_items;i++)
47                 if (strncmp(name, credits.credits[i].nickname, 11) == 0) {
48                         return &credits.credits[i];
49                 }
50         return NULL;
51 }
52
53 /* this is currently broken and should not be used
54  *
55  * when depositing money with this and returning to the main menu, the program
56  * will crash with a message like the following:
57  * 
58  * break
59  * pc    sr ac xr yr sp
60  * e180b 31 27 0a 00 e8 ....
61  */
62 static void deposit_credit() {
63         char * input;
64         struct credits_t * credit;
65         int deposit;
66
67         cprintf("\r\nName:\r\n");
68         if ((input = get_input()) == NULL || *input == '\0')
69                 return; // no name given
70                 
71         if (!(credit = find_credit(input)))
72                 return; // cannot find named credit
73         
74         cprintf("\r\nEinzahlung in Cent:\r\n");
75         if ((input = get_input()) == NULL || *input == '\0' || (deposit = atoi(input)) == 0)
76                 return;
77
78         credit->credit += deposit;
79         
80         toggle_videomode();
81         cprintf("%d Cent eingezahlt fuer %s. Restguthaben: %d\r\n", deposit, credit->nickname, credit->credit);
82         sleep(1);
83         clrscr();
84         toggle_videomode();
85 }
86
87 static void new_credit() {
88         char *input, *name;
89         int credit;
90
91         if (credits.num_items == 75) {
92                 cprintf("\rEs ist bereits die maximale Anzahl an Eintraegen erreicht, druecke RETURN...\r\n");
93                 input = get_input();
94                 return;
95         }
96
97         cprintf("\rNickname:\r\n");
98         if ((input = get_input()) == NULL || *input == '\0')
99                 return;
100         name = strdup(input);
101         cprintf("\r\nGuthaben in Cents:\r\n");
102         if ((input = get_input()) == NULL || *input == '\0' || (credit = atoi(input)) == 0)
103                 return;
104         strcpy(credits.credits[credits.num_items].nickname, name);
105         credits.credits[credits.num_items].credit = credit;
106         credits.num_items++;
107         free(name);
108 }
109
110 static void _delete_credit(BYTE num) {
111         memset(credits.credits[num].nickname, '\0', 11);
112         credits.credits[num].credit = 0;
113 }
114
115 static void delete_credit() {
116         char *input;
117         BYTE num, last;
118
119         cprintf("\r Welcher Eintrag soll geloescht werden?\r\n");
120         if ((input = get_input()) == NULL || *input == '\0')
121                 return;
122         num = atoi(input);
123         if (credits.num_items > 1) {
124                 /* Swap last item with this one and delete the last one to avoid holes */
125                 last = (credits.num_items - 1);
126                 strcpy(credits.credits[num].nickname, credits.credits[last].nickname);
127                 credits.credits[num].credit = credits.credits[last].credit;
128                 _delete_credit(last);
129         } else {
130                 /* Just delete it */
131                 _delete_credit(num);
132         }
133         credits.num_items--;
134 }
135
136 void credit_manager(){
137         char *c;
138         while(1){
139                 credit_print_screen();
140                 c = get_input();
141                 switch (*c) {
142                         case 'n':
143                                 new_credit(); break;
144                         case 'd':
145                                 delete_credit(); break;
146                         case 's':
147                                 save_credits(); break;
148                         case 'f':
149                                 if (current_credits_page < (credits.num_items / 16))
150                                                 current_credits_page++;
151                                 break;
152                         case 'b':
153                                 if (current_credits_page > 0)
154                                         current_credits_page--;
155                                 break;
156                         case 'p':
157                                 deposit_credit(); break;
158                         case 'g':
159                                 cprintf("Filter eingeben:\r\n");
160                                 filter = get_input();
161                                 if (filter == NULL || *filter == 32 || (filter_len = strlen(filter)) == 0)
162                                         filter = NULL;
163                                 break;
164                         case 'z':
165                                 return; 
166                         default:
167                                 cprintf("Unbekannter Befehl, druecke RETURN...\r\n");
168                                 get_input(); 
169                 }
170         }       
171 }