Browse Source

refactoring: added function to read files

remotes/origin/fdai7372-main-patch-93194
KRUGSON 2 years ago
parent
commit
8f42003b7d
  1. 7
      src/c/items.c
  2. 7
      src/c/main.c
  3. 7
      src/c/map.c
  4. 17
      src/c/nav_helper.c
  5. 3
      src/c/nav_helper.h

7
src/c/items.c

@ -15,12 +15,7 @@ Item *getItems(char *itemsMapFile)
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
}
stream = getStream(itemsMapFile);
char delimiter[] = ";";

7
src/c/main.c

@ -73,12 +73,7 @@ void printInit()
size_t len = 0;
ssize_t read;
stream = fopen(gameInstructionsFile, "r");
if (stream == NULL)
{
printf("ERROR: couldn't open or find file: INSTRUCTIONS !\n");
exit(EXIT_FAILURE); // exit
}
stream = getStream(gameInstructionsFile);
/* print line by line from file */
while ((read = getline(&line, &len, stream)) != -1)

7
src/c/map.c

@ -10,12 +10,7 @@ Room *getMap(char *gameMapFile)
size_t len = 0;
ssize_t read;
stream = fopen(gameMapFile, "r");
if (stream == NULL)
{
printf("ERROR: couldn't open or find file: MAP!\n");
exit(EXIT_FAILURE); // exit
}
stream = getStream(gameMapFile);
char delimiter[] = ";";

17
src/c/nav_helper.c

@ -1,4 +1,6 @@
//bibs
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//headers
@ -9,4 +11,17 @@ bool startsWith(const char *a, const char *b)
if (strncmp(a, b, strlen(b)) == 0)
return 1;
return 0;
};
};
FILE *getStream(char *filePath)
{
FILE *stream;
stream = fopen(filePath, "r");
if (stream == NULL)
{
printf("ERROR: couldn't open or find file: %s !\n", filePath);
exit(EXIT_FAILURE); // exit
}
return stream;
}

3
src/c/nav_helper.h

@ -1,9 +1,12 @@
#ifndef NAV_HELPER_H
#define NAV_HELPER_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
bool startsWith(const char *a, const char *b);
FILE *getStream(char *filePath);
#endif
Loading…
Cancel
Save