]> git.sur5r.net Git - c128-kasse/blob - src/itemz.c
fix prototypes
[c128-kasse] / src / itemz.c
1 /*
2  * RGB2R-C128-Kassenprogramm
3  * © 2007-2009 phil_fry, sECuRE, sur5r
4  * See LICENSE for license information
5  *
6  * itemz.c: Verwaltet die ITEMS- und CREDITS-datei
7  *
8  */
9 #include <c128.h>
10 #include <conio.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "general.h"
16 #include "config.h"
17 #include "credit_manager.h"
18 #include "version.h"
19
20 static void itemz_print_screen(void) {
21         BYTE i;
22         char buffer[10];
23
24         clrscr();
25         cprintf("itemz (phil_fry, sECuRE, sur5r) v:" GV "\r\n\r\n");
26         cprintf("Datei: ITEMS\r\n\r\n");
27         for (i = 0; i < max(status.num_items, 15); i++) {
28                 if (format_euro(buffer, 10, status.status[i].price) != buffer) {
29                         cprintf("Error: Could not format price %d\r\n", status.status[i].price);
30                         exit(1);
31                 }
32                 cprintf("Eintrag %2d: %s (%s, %d mal verkauft)\r\n",
33                         i, status.status[i].item_name, buffer, status.status[i].times_sold);
34         }
35         cprintf("\r\nn) Neu d) Loeschen s) Speichern m) Credit Modus q) Beenden\r\nr) Reset des Verkauft-Zaehlers\r\n");
36 }
37
38 static void new_item(void) {
39         char *input, *name;
40         int price;
41
42         if (status.num_items == MAX_ITEMS) {
43                 cprintf("\rEs ist bereits die maximale Anzahl an Eintraegen erreicht, druecke RETURN...\r\n");
44                 input = get_input();
45                 return;
46         }
47
48         cprintf("\rName des Eintrags:\r\n");
49         if ((input = get_input()) == NULL || *input == '\0')
50                 return;
51         name = strdup(input);
52         cprintf("\r\nPreis in Cents:\r\n");
53         if ((input = get_input()) == NULL || *input == '\0' || (price = atoi(input)) == 0)
54                 return;
55         cprintf("\r\nWie oft schon verkauft? [0] \r\n");
56         if ((input = get_input()) == NULL)
57                 return;
58         memset(status.status[status.num_items].item_name, '\0', MAX_ITEM_NAME_LENGTH+1);
59         strncpy(status.status[status.num_items].item_name, name, MAX_ITEM_NAME_LENGTH);
60         status.status[status.num_items].price = price;
61         status.status[status.num_items].times_sold = atoi(input);
62         status.num_items++;
63         free(name);
64 }
65
66 static void _delete_item(BYTE num) {
67         memset(status.status[num].item_name, '\0', MAX_ITEM_NAME_LENGTH);
68         status.status[num].price = 0;
69         status.status[num].times_sold = 0;
70 }
71
72 static void delete_item(void) {
73         char *input;
74         BYTE num, last;
75
76         cprintf("\r Welcher Eintrag soll geloescht werden?\r\n");
77         if ((input = get_input()) == NULL || *input == '\0')
78                 return;
79         num = atoi(input);
80         if (status.num_items > 1) {
81                 /* Swap last item with this one and delete the last one to avoid holes */
82                 last = (status.num_items - 1);
83                 strcpy(status.status[num].item_name, status.status[last].item_name);
84                 status.status[num].price = status.status[last].price;
85                 status.status[num].times_sold = status.status[last].times_sold;
86                 _delete_item(last);
87         } else {
88                 /* Just delete it */
89                 _delete_item(num);
90         }
91         status.num_items--;
92 }
93
94 static void reset_counters(void) {
95         BYTE i;
96
97         for (i = 0; i < status.num_items; i++) {
98                 status.status[i].times_sold = 0;
99         }
100 }
101
102 static void itemz_manager(){
103         char *c;
104         while(1){
105                 itemz_print_screen();
106                 c = get_input();
107                 switch (*c) {
108                         case 'n':
109                                 new_item(); break;
110                         case 'd':
111                                 delete_item(); break;
112                         case 's':
113                                 save_items(); break;
114                         case 'r':
115                                 reset_counters(); break;
116                         case 'm':
117                                 return; // switch to credit mode
118                         case 'q':
119                                 exit(0);
120                         default:
121                                 cprintf("Unbekannter Befehl, druecke RETURN...\r\n");
122                                 get_input(); 
123                 }
124         }
125 }
126
127 int main(void) {
128         if (VIDEOMODE == 40)
129                 toggle_videomode();
130         credits.num_items = 0;
131         status.num_items = 0;
132         cprintf("itemz loading...\n");
133         load_config();
134         cprintf("itemz: loading ITEMS...\n");
135         load_items();
136         cprintf("itemz: loading CREDITS...\n");
137         load_credits();
138         while (1) {
139                 itemz_manager();
140                 credit_manager();
141         }
142         return 0;
143 }