mirror of
https://github.com/brmlab/brmbar.git
synced 2025-06-07 21:04:00 +02:00
Itemio: Items can be loaded from the data file
Signed-off-by: Cestmir Houska <czestmyr@gmail.com>
This commit is contained in:
parent
bf838ea046
commit
8b49a62f9e
2 changed files with 52 additions and 3 deletions
53
itemio.c
53
itemio.c
|
@ -1,14 +1,61 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "itemio.h"
|
||||
|
||||
struct item items[ITEM_MAXNUM];
|
||||
int items_count;
|
||||
|
||||
void fill_items()
|
||||
{
|
||||
char buf[128];
|
||||
//Add 32 characters for a safe margin
|
||||
#define BUFSIZE EAN_MAXLEN + NAME_MAXLEN + 32
|
||||
char buf[BUFSIZE];
|
||||
FILE *f = fopen("items.txt", "r");
|
||||
while (fgets(buf, 128, f)) {
|
||||
printf("%s\n", buf);
|
||||
char c;
|
||||
|
||||
items_count = 0;
|
||||
|
||||
while (fgets(buf, BUFSIZE, f)) {
|
||||
int i = 0;
|
||||
int begin = 0;
|
||||
|
||||
//Initialize the item
|
||||
items[items_count].ean[0] = 0;
|
||||
items[items_count].name[0] = 0;
|
||||
items[items_count].price = 0;
|
||||
|
||||
//Read the item EAN
|
||||
while (i < BUFSIZE) {
|
||||
if (buf[i] == '\t') {
|
||||
buf[i] = 0;
|
||||
strcpy(items[items_count].ean, &buf[begin]);
|
||||
buf[i] = '\t';
|
||||
begin = i+1;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
//Read the item name
|
||||
i = begin;
|
||||
while (i < BUFSIZE) {
|
||||
if (buf[i] == '\t') {
|
||||
buf[i] = 0;
|
||||
strcpy(items[items_count].name, &buf[begin]);
|
||||
buf[i] = '\t';
|
||||
begin = i+1;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
//Use scanf to read the item price
|
||||
i = begin;
|
||||
if (i < BUFSIZE) {
|
||||
sscanf(&buf[i], "%d", &items[items_count].price);
|
||||
}
|
||||
|
||||
items_count++;
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
|
2
itemio.h
2
itemio.h
|
@ -9,6 +9,8 @@ struct item {
|
|||
int price;
|
||||
};
|
||||
|
||||
extern int items_count;
|
||||
|
||||
extern struct item items[ITEM_MAXNUM];
|
||||
|
||||
extern void fill_items();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue