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