]> git.sur5r.net Git - c128-kasse/blob - src/credit_manager.c
Check if read < 0
[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         cprintf("\rNickname:\r\n");
95         if ((input = get_input()) == NULL || *input == '\0')
96                 return;
97         name = strdup(input);
98         cprintf("\r\nGuthaben in Cents:\r\n");
99         if ((input = get_input()) == NULL || *input == '\0' || (credit = atoi(input)) == 0)
100                 return;
101         strcpy(credits.credits[credits.num_items].nickname, name);
102         credits.credits[credits.num_items].credit = credit;
103         credits.num_items++;
104         free(name);
105 }
106
107 static void _delete_credit(BYTE num) {
108         memset(credits.credits[num].nickname, '\0', 11);
109         credits.credits[num].credit = 0;
110 }
111
112 static void delete_credit() {
113         char *input;
114         BYTE num, last;
115
116         cprintf("\r Welcher Eintrag soll geloescht werden?\r\n");
117         if ((input = get_input()) == NULL || *input == '\0')
118                 return;
119         num = atoi(input);
120         if (credits.num_items > 1) {
121                 /* Swap last item with this one and delete the last one to avoid holes */
122                 last = (credits.num_items - 1);
123                 strcpy(credits.credits[num].nickname, credits.credits[last].nickname);
124                 credits.credits[num].credit = credits.credits[last].credit;
125                 _delete_credit(last);
126         } else {
127                 /* Just delete it */
128                 _delete_credit(num);
129         }
130         credits.num_items--;
131 }
132
133 void credit_manager(){
134         char *c;
135         while(1){
136                 credit_print_screen();
137                 c = get_input();
138                 switch (*c) {
139                         case 'n':
140                                 new_credit(); break;
141                         case 'd':
142                                 delete_credit(); break;
143                         case 's':
144                                 save_credits(); break;
145                         case 'f':
146                                 if (current_credits_page < (credits.num_items / CREDITS_PER_PAGE))
147                                                 current_credits_page++;
148                                 break;
149                         case 'b':
150                                 if (current_credits_page > 0)
151                                         current_credits_page--;
152                                 break;
153                         case 'p':
154                                 deposit_credit(); break;
155                         case 'g':
156                                 cprintf("Filter eingeben:\r\n");
157                                 filter = get_input();
158                                 if (filter == NULL || *filter == 32 || (filter_len = strlen(filter)) == 0)
159                                         filter = NULL;
160                                 break;
161                         case 'z':
162                                 return; 
163                         default:
164                                 cprintf("Unbekannter Befehl, druecke RETURN...\r\n");
165                                 get_input(); 
166                 }
167         }       
168 }