From bf838ea04649d03c4eb4980ba7b46b33eeef1c44 Mon Sep 17 00:00:00 2001 From: Cestmir Houska Date: Fri, 22 Apr 2011 02:38:57 +0200 Subject: [PATCH 1/2] Separated I/O operations into itemio.c Signed-off-by: Cestmir Houska --- Makefile | 12 ++++++++++-- brmbar.c | 18 +----------------- itemio.c | 15 +++++++++++++++ itemio.h | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 itemio.c create mode 100644 itemio.h diff --git a/Makefile b/Makefile index 340c194..eb46e52 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,19 @@ CC=bcc CFLAGS=-Md -W +LDFLAGS=-Md -W -brmbar.com: brmbar.c defines.h - $(CC) $(CFLAGS) brmbar.c -o brmbar.com +brmbar.com: brmbar.o itemio.o + $(CC) $(LDFLAGS) brmbar.o itemio.o -o brmbar.com + +brmbar.o: brmbar.c defines.h itemio.h + $(CC) $(CFLAGS) brmbar.c -c -o brmbar.o + +itemio.o: itemio.c defines.h itemio.h + $(CC) $(CFLAGS) itemio.c -c -o itemio.o clean: rm -f brmbar.com + rm -f *.o run: dosbox brmbar.com diff --git a/brmbar.c b/brmbar.c index 8d6b80c..f4e5b4d 100644 --- a/brmbar.c +++ b/brmbar.c @@ -1,21 +1,5 @@ -#include #include "defines.h" - -struct item { - char ean[EAN_MAXLEN]; - char name[NAME_MAXLEN]; - int price; -} items[ITEM_MAXNUM]; - -void fill_items() -{ - char buf[128]; - FILE *f = fopen("items.txt", "r"); - while (fgets(buf, 128, f)) { - printf("%s\n", buf); - } - fclose(f); -} +#include "itemio.h" int main() { diff --git a/itemio.c b/itemio.c new file mode 100644 index 0000000..f6eb696 --- /dev/null +++ b/itemio.c @@ -0,0 +1,15 @@ +#include +#include "itemio.h" + +struct item items[ITEM_MAXNUM]; + +void fill_items() +{ + char buf[128]; + FILE *f = fopen("items.txt", "r"); + while (fgets(buf, 128, f)) { + printf("%s\n", buf); + } + fclose(f); +} + diff --git a/itemio.h b/itemio.h new file mode 100644 index 0000000..6f304b9 --- /dev/null +++ b/itemio.h @@ -0,0 +1,17 @@ +#ifndef _BRMBAR_ITEMIO_H_ +#define _BRMBAR_ITEMIO_H_ + +#include "defines.h" + +struct item { + char ean[EAN_MAXLEN]; + char name[NAME_MAXLEN]; + int price; +}; + +extern struct item items[ITEM_MAXNUM]; + +extern void fill_items(); + +#endif + From 8b49a62f9eb149f9e1ab3ef87bdb503ccdcdc395 Mon Sep 17 00:00:00 2001 From: Cestmir Houska Date: Fri, 22 Apr 2011 03:09:31 +0200 Subject: [PATCH 2/2] Itemio: Items can be loaded from the data file Signed-off-by: Cestmir Houska --- itemio.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- itemio.h | 2 ++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/itemio.c b/itemio.c index f6eb696..d698cf6 100644 --- a/itemio.c +++ b/itemio.c @@ -1,14 +1,61 @@ #include +#include #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); } diff --git a/itemio.h b/itemio.h index 6f304b9..940ee94 100644 --- a/itemio.h +++ b/itemio.h @@ -9,6 +9,8 @@ struct item { int price; }; +extern int items_count; + extern struct item items[ITEM_MAXNUM]; extern void fill_items();