]> git.sur5r.net Git - c128-kasse/blob - src/itemz.c
itemz: fix int16_t -> uint8_t truncation
[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 #include "vdc_patch_charset.h"
20 #include "globals.h"
21
22 static void itemz_print_screen(void) {
23   BYTE i;
24   char buffer[EUR_FORMAT_MINLEN];
25
26   clrscr();
27   cprintf("itemz (phil_fry, sECuRE, sur5r, mxf) v:" GV "\r\n\r\n");
28   cprintf("Datei: ITEMS\r\n\r\n");
29   for (i = 0; i < max(status.num_items, 15); i++) {
30     if (format_euro(buffer, sizeof(buffer), 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 %2d: %s (%s, %d mal verkauft)\r\n", i,
35             status.status[i].item_name, buffer, status.status[i].times_sold);
36   }
37   cprintf("\r\nn) Neu d) L" oUML "schen s) Speichern m) Credit Modus q) "
38           "Beenden\r\nr) Reset des Verkauft-Z" aUML "hlers\r\n");
39 }
40
41 static void new_item(void) {
42   char name[MAX_ITEM_NAME_LENGTH + 1];
43   int price, times_sold;
44
45   if (status.num_items == MAX_ITEMS) {
46     cprintf("\rEs ist bereits die maximale Anzahl an Eintr" aUML
47             "gen erreicht, dr" uUML "cke RETURN...\r\n");
48     cget_return();
49     return;
50   }
51
52   cprintf("\rName des Eintrags:\r\n");
53   if (cgetn_input(name, sizeof(name)) == 0)
54     return;
55   cprintf("\r\nPreis in Cents:\r\n");
56   if ((price = cget_number(0)) <= 0)
57     return;
58   cprintf("\r\nWie oft schon verkauft? [0] \r\n");
59   if ((times_sold = cget_number(0)) < 0)
60     return;
61   memset(status.status[status.num_items].item_name, '\0',
62          MAX_ITEM_NAME_LENGTH + 1);
63   strncpy(status.status[status.num_items].item_name, name,
64           MAX_ITEM_NAME_LENGTH);
65   status.status[status.num_items].price = price;
66   status.status[status.num_items].times_sold = times_sold;
67   status.num_items++;
68 }
69
70 static void _delete_item(BYTE num) {
71   memset(status.status[num].item_name, '\0', MAX_ITEM_NAME_LENGTH);
72   status.status[num].price = 0;
73   status.status[num].times_sold = 0;
74 }
75
76 static void delete_item(void) {
77   int16_t num;
78   uint8_t last;
79   cprintf("\r Welcher Eintrag soll geloescht werden?\r\n");
80
81   num = cget_number(-1);
82   if (num < 0)
83     return;
84
85   if (status.num_items > 1) {
86     /* Swap last item with this one and delete the last one to avoid holes */
87     last = (status.num_items - 1);
88     strncpy(status.status[num].item_name, status.status[last].item_name,
89             MAX_ITEM_NAME_LENGTH);
90     status.status[num].price = status.status[last].price;
91     status.status[num].times_sold = status.status[last].times_sold;
92     _delete_item(last);
93   } else {
94     /* Just delete it */
95     _delete_item(num);
96   }
97   status.num_items--;
98 }
99
100 static void reset_counters(void) {
101   BYTE i;
102
103   for (i = 0; i < status.num_items; i++) {
104     status.status[i].times_sold = 0;
105   }
106 }
107
108 static void itemz_manager() {
109   char *c;
110   while (1) {
111     itemz_print_screen();
112     c = get_input();
113     switch (*c) {
114     case 'n':
115       new_item();
116       break;
117     case 'd':
118       delete_item();
119       break;
120     case 's':
121       save_items();
122       break;
123     case 'r':
124       reset_counters();
125       break;
126     case 'm':
127       return; // switch to credit mode
128     case 'q':
129       exit(0);
130     default:
131       cprintf("Unbekannter Befehl, druecke RETURN...\r\n");
132       cget_return();
133     }
134   }
135 }
136
137 int main(void) {
138   videomode(VIDEOMODE_80x25);
139
140   /*  clock CPU at double the speed (a whopping 2 Mhz!) */
141   fast();
142
143   credits.num_items = 0;
144   status.num_items = 0;
145   cprintf("itemz loading...\n");
146   load_config();
147   cprintf("itemz: loading ITEMS...\n");
148   load_items();
149   cprintf("itemz: loading CREDITS...\n");
150   load_credits();
151   while (1) {
152     itemz_manager();
153     credit_manager();
154   }
155   return 0;
156 }