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