]> git.sur5r.net Git - c128-kasse/blob - config.c
replace printf and file functions, warning: may be broken, not tested yet
[c128-kasse] / config.c
1 #include <stdlib.h>
2 #include <conio.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include "general.h"
6 #include "config.h"
7
8 unsigned long int money = 0;
9 unsigned long int num_items = 0;
10 BYTE num_credit_items = 0;
11 unsigned long int items_sold = 0;
12 BYTE printer_port = 4;
13 struct status_t status[MAX_ITEMS+1];
14 struct credits_t credits[MAX_CREDIT_ITEMS+1];
15
16 void load_config();
17
18 void load_items(){
19     char line[80];
20     char *sep;
21     BYTE lfn = 8;
22         BYTE rc;
23         int count=1;
24
25     rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "items,r");
26         if(rc!=0)
27         {
28                 cprintf("cannot open items\r\n");
29                 return;
30         }
31     for (num_items=0; num_items < MAX_ITEMS && count>0; num_items++) {
32                 count=cbm_read(lfn, line, 79);
33         //fgets(line, 79, f);
34         sep = strchr(line, '=');
35         strncpy(status[num_items].item_name, line, sep-line);
36         status[num_items].price = atoi(sep+1);
37         status[num_items].times_sold = 0; 
38     }
39     cbm_close(lfn);
40 }
41
42 /**
43  * must be called after load_items()
44  */
45 void load_state(){
46     char line[80];
47     char * sep;
48     char i;
49         BYTE lfn=8;
50         BYTE rc;
51         int count=1;
52
53         rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "state,r");
54     if (rc!=0){
55         cprintf("cannot open state\r\n");
56         return;
57     }
58     while (count>0) {
59         count=cbm_read(lfn,line,79);
60                 //fgets(line, 79, f);
61         sep = strchr(line, '=');
62         if (sep==NULL)
63                 continue;
64         *(line + (sep-line)) = 0;
65         for (i=0; i< MAX_ITEMS; i++) {
66                 if (strcmp(line, status[i].item_name)==0) {
67                         status[i].times_sold = atoi(sep+1);
68                         break;
69                 }
70         }
71     }
72     cbm_close(lfn);
73 }
74
75 void save_state(){
76         int i;
77         BYTE lfn=8;
78         BYTE rc;
79         int count=1;
80         int size=1;
81         char line[81];
82         
83         rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "state,w");
84     if (rc!=0){
85         c128_perror(23, "cannot open state file");
86         return;
87     }
88     for (i=0;i<num_items;i++)
89         {
90                 memset(line,0, 81);
91                 size=sprintf(line, "%s=%d\n", status[i].item_name, status[i].times_sold);
92                 cbm_write(lfn, line, size);
93         //fprintf(f, "%s=%d\n",status[i].item_name, status[i].times_sold);
94         }
95         cbm_close(lfn);
96 }
97 /*
98 void dump_state(){
99         FILE * f;
100         char buf[128];
101         int i, len;
102         memset(buf, 0, 128);
103         f = fopen("state", "r");
104         len = fread(buf, 1, 128, f);
105         cprintf("read %d bytes from state\n", len);
106         fclose(f);
107         for (i=0;i<len;i++)
108                 printf("%x ", buf[i]);
109         cprintf("\n");
110         
111 }
112 */
113 void load_config() {
114 }