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