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