]> git.sur5r.net Git - c128-kasse/blob - config.c
fgets-replacement implemented
[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, *newl = NULL, *lp;
21         BYTE lfn = 8;
22         BYTE rc;
23         int count = 1;
24
25         if ((rc = cbm_open(lfn, (BYTE)8, (BYTE)0, "items,r")) != 0) {
26                 cprintf("cannot open items\r\n");
27                 return;
28         }
29         for (num_items = 0; num_items < MAX_ITEMS && count > 0;) {
30                 count = cbm_read(lfn, line, 79);
31                 if (count < 0) {
32                         cprintf("read error\r\n");
33                         return;
34                 }
35                 line[count] = '\0';
36                 lp = line;
37                 do {
38                         if (newl)
39                                 if (newl < ((line+count)-1))
40                                         lp = newl+1;
41                                 else break;
42                         sep = strchr(lp, '=');
43                         strncpy(status[num_items].item_name, lp, sep-lp);
44                         if (newl)
45                                 *newl = '\0';
46                         status[num_items].price = atoi(sep+1);
47                         status[num_items].times_sold = 0; 
48                         num_items++;
49                 } while (newl = strchr(lp, '\n'));
50         }
51         cbm_close(lfn);
52 }
53
54 /**
55  * must be called after load_items()
56  */
57 void load_state(){
58     char line[80];
59     char * sep;
60     char i;
61         BYTE lfn=8;
62         BYTE rc;
63         int count=1;
64
65         rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "state,r");
66     if (rc!=0){
67         cprintf("cannot open state\r\n");
68         return;
69     }
70     while (count>0) {
71         count=cbm_read(lfn,line,79);
72                 //fgets(line, 79, f);
73         sep = strchr(line, '=');
74         if (sep==NULL)
75                 continue;
76         *(line + (sep-line)) = 0;
77         for (i=0; i< MAX_ITEMS; i++) {
78                 if (strcmp(line, status[i].item_name)==0) {
79                         status[i].times_sold = atoi(sep+1);
80                         break;
81                 }
82         }
83     }
84     cbm_close(lfn);
85 }
86
87 void save_state(){
88         int i;
89         BYTE lfn=8;
90         BYTE rc;
91         int count=1;
92         int size=1;
93         char line[81];
94         
95         rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "state,w");
96     if (rc!=0){
97         c128_perror(23, "cannot open state file");
98         return;
99     }
100     for (i=0;i<num_items;i++)
101         {
102                 memset(line,0, 81);
103                 size=sprintf(line, "%s=%d\n", status[i].item_name, status[i].times_sold);
104                 cbm_write(lfn, line, size);
105         //fprintf(f, "%s=%d\n",status[i].item_name, status[i].times_sold);
106         }
107         cbm_close(lfn);
108 }
109 /*
110 void dump_state(){
111         FILE * f;
112         char buf[128];
113         int i, len;
114         memset(buf, 0, 128);
115         f = fopen("state", "r");
116         len = fread(buf, 1, 128, f);
117         cprintf("read %d bytes from state\n", len);
118         fclose(f);
119         for (i=0;i<len;i++)
120                 printf("%x ", buf[i]);
121         cprintf("\n");
122         
123 }
124 */
125 void load_config() {
126 }