summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpreston <preston@openbsd-preston.prestonpan.tech>2023-03-07 23:12:25 -0800
committerpreston <preston@openbsd-preston.prestonpan.tech>2023-03-07 23:12:25 -0800
commitb844282a592b5a51000107edfffee49eba560854 (patch)
tree973ef81261c7b739375dd808c7532403937ff2b1
parent43c54b0fb79daca7d7fc2b4575f7729eceddce78 (diff)
leaderboard save done theoretically
-rw-r--r--src/leaderboard.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/leaderboard.c b/src/leaderboard.c
index b198d9f..850fdf0 100644
--- a/src/leaderboard.c
+++ b/src/leaderboard.c
@@ -53,14 +53,22 @@ leaderboard_t read_leaderboard(char* path, unsigned int capacity) {
return lb;
}
void save_leaderboard(leaderboard_t leaderboard, char* path) {
- FILE* file = fopen(path, "w");
+ FILE* file = fopen(path, "a"); /* a appends to file */
int res = flock(fileno(file), LOCK_EX);
+ int i;
+ leaderscore_t cur;
if (res != 0) {
fclose(file);
return;
}
/* got lock */
/* TODO: actually write the leaderboard */
+ for (i = 0; i < leaderboard.count; i ++) {
+ cur = leaderboard.scores[i];
+ /* TODO: realloc again and then append int in string form to str */
+ /* append to file: name,score\n */
+ fprintf(file, "%s,%ld\n", cur.name, cur.score); /* prints to file stream */
+ }
flock(fileno(file), LOCK_UN);
fclose(file);
}