From 12d6f1143a7038d56d2d1ad1365d836b2cfd25a3 Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Sun, 5 Feb 2023 03:31:04 +0100 Subject: [PATCH] added items with test and items.map file --- src/c/items.c | 57 +++++++++++++++++++++++++++++++++++++++++++ src/c/items.h | 20 +++++++++++++++ src/content/items.map | 8 ++++++ test/c/test_items.c | 38 +++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 src/c/items.c create mode 100644 src/c/items.h create mode 100644 src/content/items.map create mode 100644 test/c/test_items.c diff --git a/src/c/items.c b/src/c/items.c new file mode 100644 index 0000000..191051a --- /dev/null +++ b/src/c/items.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +#include "nav_helper.h" +#include "items.h" + +Item *getItems(char *itemsMapFile) +{ + static Item getItems[maxItems]; + + FILE *stream; + char *line = NULL; + size_t len = 0; + ssize_t read; + + stream = fopen(itemsMapFile, "r"); + if (stream == NULL) + { + printf("ERROR: couldn't open or find file: ITEMS!\n"); + exit(EXIT_FAILURE); // exit + } + + char delimiter[] = ";"; + + /* print line by line from file */ + int lineCounter = 0; + while ((read = getline(&line, &len, stream)) != -1) + { + // printf("Retrieved line of length %u :\n", read); + if (startsWith(line, "#") == 0) + { + char *arr[itemAttributesMax]; + char *token = strtok(line, delimiter); + int countToken = 0; + while (token != NULL) + { + arr[countToken] = token; + token = strtok(NULL, ";"); + + countToken += 1; + } + free(token); + + Item i; + i.id = atoi(arr[0]); + strcpy(i.itemName, arr[1]); + //printf(arr[2]); + i.price = atoi(arr[3]); + + getItems[lineCounter] = i; + lineCounter += 1; + } + } + return getItems; +} diff --git a/src/c/items.h b/src/c/items.h new file mode 100644 index 0000000..ed50446 --- /dev/null +++ b/src/c/items.h @@ -0,0 +1,20 @@ +#ifndef ITEMS_H +#define ITEMS_H + +#define itemAttributesMax 4 // for items +#define maxItems 100 // for inventory/item list + +#include + +typedef struct +{ + int id; + char itemName[50]; + bool inShopAvailable; + int price; +} Item; + +Item *getItems(char *itemsMapFile); + + +#endif \ No newline at end of file diff --git a/src/content/items.map b/src/content/items.map new file mode 100644 index 0000000..0dcc59c --- /dev/null +++ b/src/content/items.map @@ -0,0 +1,8 @@ +# LINES WITH '#' AT BEGINNING GET IGNORED +#ID;NAME;inShopAvailable;price +1;healing potion;1;20 +2;shield;1;50 +3;UV flashlight;1;80 +4;key for police station;0;9999 +5;acid;1;9999 +6;gun;0;9999 \ No newline at end of file diff --git a/test/c/test_items.c b/test/c/test_items.c new file mode 100644 index 0000000..9fc3ce3 --- /dev/null +++ b/test/c/test_items.c @@ -0,0 +1,38 @@ +#ifdef TEST + +#include "unity.h" +#include "nav_helper.h" +#include "items.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_items(void) +{ + /* arrange */ + // Hier die Werte eingeben/deklarieren + Item* items; + int expectedItemID = 1; + int expectedSecondItemID = 2; + + /* act */ + // Die Funktion wird ausgeführt + items = getItems("./src/content/items.map"); + + /* make visible OUTPUT */ + for(int i=0; i<4;i++){ + printf("%s\n", items[i].itemName); + } + + /* assert */ + // Vergleichen mit Inhalt von game.Map File + TEST_ASSERT_EQUAL_INT(expectedItemID, items[0].id); + TEST_ASSERT_EQUAL_INT(expectedSecondItemID, items[1].id); +} + +#endif // TEST