diff --git a/.gitignore b/.gitignore index 05c7d92..35f0e66 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,11 @@ # git ls-files --others --exclude-from=.git/info/exclude # Lines that start with '#' are comments. -# For a project mostly in C, the following would be a good set of -# exclude patterns (uncomment them if you want to use them): *.depend *.cbp *.layout sdl2-config *.exclude *.o -highscoretest.c *.psd *.exe !bhi.exe diff --git a/highscores.c b/highscores.c new file mode 100644 index 0000000..7a56e16 --- /dev/null +++ b/highscores.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "highscores.h" + +User * HIGHSCORES_GetList(int * usercount){ + printf("Call BHI interface:\n"); + system("bhi top output.txt"); + printf("BHI interface quit!\nBHI output handling...\n"); + + FILE * fp; + char * line = NULL; + size_t len = 0; + ssize_t read; + char * name, * scorestring; + int nameCharCount = 0, scoreCharCount = 0; + bool switchread = false; + User * ul = malloc(10 * sizeof(User)); + fp = fopen("output.txt", "r"); + if (fp == NULL) + exit(EXIT_FAILURE); + if ((read = getline(&line, &len, fp)) != -1) + if (line[0] == 0) + return EXIT_FAILURE; + int counter = 0; + while ((read = getline(&line, &len, fp)) != -1) { + name = malloc(read * sizeof(char)); + scorestring = malloc(read * sizeof(char)); + nameCharCount = 0; + scoreCharCount = 0; + switchread = false; + #ifdef DEBUG + printf("Retrieved line of length %u:\n", read); + printf("%s", line); + #endif + for (ssize_t i = 0; i < read; i++) { + if (line[i] == '+') { + switchread = true; + continue; + } + if (switchread) { + if (line[i] == '\n') break; + scorestring[scoreCharCount++] = line[i]; + } else + name[nameCharCount++] = line[i]; + } + name[nameCharCount] = '\0'; + scorestring[scoreCharCount] = '\0'; + User tmp; + tmp.Username = name; + tmp.Score = scorestring; + ul[counter++] = tmp; + } + + fclose(fp); + if (line) + free(line); + for (size_t i = 0; i < counter; i++) { + printf("User: %s -> Score: %s\n", ul[i].Username, ul[i].Score); + } + + *usercount = counter; + printf("BHI Interface successfully quit!\n"); + return ul; +} /* main */ diff --git a/highscores.h b/highscores.h new file mode 100644 index 0000000..d8f0660 --- /dev/null +++ b/highscores.h @@ -0,0 +1,10 @@ +#ifndef __highscores_h__ +#define __highscores_h__ + +// Structs +typedef struct userStruct { + char * Username, * Score; +} User; +// End Structs + +#endif