#include <stdlib.h>
-#include <stdio.h>
+#include <conio.h>
#include <string.h>
+#include <stdio.h>
#include "general.h"
#include "config.h"
void load_config();
void load_items(){
- FILE* f;
char line[80];
- char * sep;
- BYTE lfn = 219;
-// cbm_open(lfn, (BYTE)8, (BYTE)0, "items,r");
- f = fopen("items","r");
- for (num_items=0; num_items < MAX_ITEMS && !feof(f); num_items++) {
- fgets(line, 79, f);
+ char *sep;
+ BYTE lfn = 8;
+ BYTE rc;
+ int count=1;
+
+ rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "items,r");
+ if(rc!=0)
+ {
+ cprintf("cannot open items\r\n");
+ return;
+ }
+ for (num_items=0; num_items < MAX_ITEMS && count>0; num_items++) {
+ count=cbm_read(lfn, line, 79);
+ //fgets(line, 79, f);
sep = strchr(line, '=');
strncpy(status[num_items].item_name, line, sep-line);
status[num_items].price = atoi(sep+1);
status[num_items].times_sold = 0;
}
- fclose(f);
+ cbm_close(lfn);
}
/**
* must be called after load_items()
*/
void load_state(){
- FILE * f;
char line[80];
char * sep;
- char i, j;
- f = fopen("state", "r");
- if (f==NULL){
+ char i;
+ BYTE lfn=8;
+ BYTE rc;
+ int count=1;
+
+ rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "state,r");
+ if (rc!=0){
cprintf("cannot open state\r\n");
return;
}
- while (!feof(f)) {
- fgets(line, 79, f);
+ while (count>0) {
+ count=cbm_read(lfn,line,79);
+ //fgets(line, 79, f);
sep = strchr(line, '=');
if (sep==NULL)
continue;
}
}
}
- fclose(f);
+ cbm_close(lfn);
}
void save_state(){
- FILE * f;
int i;
+ BYTE lfn=8;
+ BYTE rc;
+ int count=1;
+ int size=1;
+ char line[81];
- f = fopen("state", "w");
- if (f==NULL){
+ rc=cbm_open(lfn, (BYTE)8, (BYTE)0, "state,w");
+ if (rc!=0){
c128_perror(23, "cannot open state file");
return;
}
for (i=0;i<num_items;i++)
- fprintf(f, "%s=%d\n",status[i].item_name, status[i].times_sold);
- fclose(f);
+ {
+ memset(line,0, 81);
+ size=sprintf(line, "%s=%d\n", status[i].item_name, status[i].times_sold);
+ cbm_write(lfn, line, size);
+ //fprintf(f, "%s=%d\n",status[i].item_name, status[i].times_sold);
+ }
+ cbm_close(lfn);
}
/*
void dump_state(){
memset(buf, 0, 128);
f = fopen("state", "r");
len = fread(buf, 1, 128, f);
- printf("read %d bytes from state\n", len);
+ cprintf("read %d bytes from state\n", len);
fclose(f);
for (i=0;i<len;i++)
printf("%x ", buf[i]);
- printf("\n");
+ cprintf("\n");
}
*/
load_items();
load_state();
for (i=0; i < num_items; ++i) {
- printf("%x: %s (%s, %d mal)\n", i, status[i].item_name, format_euro(euro, 9, status[i].price), status[i].times_sold);
+ cprintf("%x: %s (%s, %d mal)\n", i, status[i].item_name, format_euro(euro, 9, status[i].price), status[i].times_sold);
status[i].times_sold+=10;
}
load_items();
load_state();
for (i=0; i < num_items; ++i) {
- printf("%x: %s (%s, %d mal)\n", i, status[i].item_name, format_euro(euro, 9, status[i].price), status[i].times_sold);
+ cprintf("%x: %s (%s, %d mal)\n", i, status[i].item_name, format_euro(euro, 9, status[i].price), status[i].times_sold);
}
- printf("strlen(%s)==%d\n", status[1].item_name, strlen(status[1].item_name));
- printf("%d %d %d\n", status[1].item_name[0], '\r', '\n');
-}
\ No newline at end of file
+ cprintf("strlen(%s)==%d\n", status[1].item_name, strlen(status[1].item_name));
+ cprintf("%d %d %d\n", status[1].item_name[0], '\r', '\n');
+}