breakout/highscores.c
2018-01-15 22:01:58 +01:00

69 lines
1.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdbool.h>
#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");
*usercount = 0;
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)
return ul;
if ((read = getline(&line, &len, fp)) != -1)
if (line[0] == 0)
return ul;
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 */