From 2ccfb653128efe83af2f791328e7096ae456147b Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Wed, 15 Jan 2025 13:29:10 +0100 Subject: [PATCH] dumb fixes --- app.cpp | 31 ++++++++++++++++--------------- djstdlib/os_linux.cpp | 28 +++++++++++++++++++++------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/app.cpp b/app.cpp index 07d8729..aa362c5 100644 --- a/app.cpp +++ b/app.cpp @@ -41,11 +41,10 @@ struct GymLogEntry { GymLogDbParsed *parseDb(Arena *arena, string database) { GymLogDbParsed *dbParsed = PushStruct(arena, GymLogDbParsed); - GymLogDbHeader *header = (GymLogDbHeader *)database.str; - dbParsed->header = *header; + dbParsed->header = *((GymLogDbHeader *)database.str); size_t head = sizeof(GymLogDbHeader); - uint32 entriesLeft = header->nextId - 1; + uint32 entriesLeft = dbParsed->header.nextId - 1; dbParsed->entries = PushList(arena, GymLogDbParsedEntry, entriesLeft); while (entriesLeft > 0 && head < database.length) { @@ -133,12 +132,12 @@ int gymTrackerStatus(Arena *arena, list args) { int statusCode = 0; string file = os_readEntireFile(arena, LOG_FILE_LOCATION); - GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION)); - if (file.length % sizeof(GymLogEntry) != 0) { puts("Log file corrupted."); statusCode = 1; } else { + GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION)); + Timestamp startTs = {0}; int numDays = 1; bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s); @@ -285,12 +284,12 @@ int gymTrackerDeleteEntries(Arena *arena, list args) { // Syntax: do weightKg reps int gymTrackerDo(Arena *arena, list args) { int statusCode = 0; - string newExerciseName = {0}; + string exerciseName = {0}; if (args.length < 3 || args.data[0].length == 0) { log("Invalid exercise name and/or number of arguments.\n"); statusCode = 1; } else { - newExerciseName = args.data[0]; + exerciseName = args.data[0]; } GymLogDbParsedEntry *existingEntry = 0; @@ -299,14 +298,17 @@ int gymTrackerDo(Arena *arena, list args) { GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION)); for (EachIn(db->entries, i)) { GymLogDbParsedEntry entry = db->entries.data[i]; - if (strStartsWith(entry.name, newExerciseName)) { + if (strStartsWith(entry.name, exerciseName)) { existingEntry = &entry; - log("Assuming exercise \"%S\".\n\n", entry.name); + if (entry.name.length != exerciseName.length) { + exerciseName = entry.name; + log("Assuming exercise \"%S\".\n\n", entry.name); + } break; } } if (!existingEntry) { - log("The exercise \"%S\" hasn't been registered.", newExerciseName); + log("The exercise \"%S\" hasn't been registered.", exerciseName); statusCode = 1; } } @@ -328,7 +330,7 @@ int gymTrackerDo(Arena *arena, list args) { }; os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry)); - statusCode = gymTrackerWorkToday(arena, exerciseId, newExerciseName); + statusCode = gymTrackerWorkToday(arena, exerciseId, exerciseName); } } @@ -353,7 +355,6 @@ int gymTrackerListExercises(Arena *arena, list args) { int gymTrackerAddExercise(Arena *arena, list args) { int statusCode = 0; - GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION)); string newExerciseName = args.data[0]; if (newExerciseName.length == 0) { @@ -362,8 +363,7 @@ int gymTrackerAddExercise(Arena *arena, list args) { } if (statusCode != 1) { - string databaseLocation = DB_FILE_LOCATION; - string database = os_readEntireFile(arena, databaseLocation); + string database = os_readEntireFile(arena, DB_FILE_LOCATION); byte *buf = 0; size_t newEntryStartIndex = 0; @@ -411,7 +411,8 @@ int gymTrackerAddExercise(Arena *arena, list args) { byte *newExerciseNameDb = buf + newEntryStartIndex + sizeof(GymLogDbEntry); memcpy(newExerciseNameDb, newExerciseName.str, newExerciseName.length); size_t bufSize = newEntryStartIndex + sizeof(GymLogDbEntry) + newExerciseName.length; - os_writeEntireFile(arena, databaseLocation, buf, bufSize); + log("%i\n", bufSize); + os_writeEntireFile(arena, DB_FILE_LOCATION, buf, bufSize); } } diff --git a/djstdlib/os_linux.cpp b/djstdlib/os_linux.cpp index 3add835..69817d7 100644 --- a/djstdlib/os_linux.cpp +++ b/djstdlib/os_linux.cpp @@ -23,7 +23,9 @@ void os_free(void *ptr, size_t size) { } string os_readEntireFile(Arena *arena, string filename) { - FILE *input = fopen((char *)filename.str, "r"); + Scratch temp = scratchStart(&arena, 1); + + FILE *input = fopen(cstring(temp.arena, filename), "r"); string readBuffer; if (input) { struct stat st; @@ -35,34 +37,45 @@ string os_readEntireFile(Arena *arena, string filename) { } else { readBuffer = PushString(arena, 0); } + + scratchEnd(temp); return readBuffer; } bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) { + Scratch temp = scratchStart(&arena, 1); + bool result = false; - FILE *output = fopen((char *)filename.str, "w"); + FILE *output = fopen(cstring(temp.arena, filename), "w"); if (output) { - fwrite(contents, contentsLength, contentsLength, output); + fwrite(contents, sizeof(byte), contentsLength, output); fclose(output); result = true; } + + scratchEnd(temp); return result; } bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) { + Scratch temp = scratchStart(&arena, 1); + bool result = false; - FILE *output = fopen((char *)filename.str, "a"); + FILE *output = fopen(cstring(temp.arena, filename), "a"); if (output) { fwrite(contents, sizeof(byte), contentsLength, output); fclose(output); result = true; } + + scratchEnd(temp); return result; } void os_log(LogTarget target, const char *fmt, va_list argList) { - Scratch scratch = scratchStart(0, 0); - string result = strPrintfv(scratch.arena, fmt, argList); + Scratch temp = scratchStart(0, 0); + + string result = strPrintfv(temp.arena, fmt, argList); // TODO(djledda): finish implementation without cstdlib switch (target) { case LogTarget_stdin: @@ -78,7 +91,8 @@ void os_log(LogTarget target, const char *fmt, va_list argList) { write(1, (const void *)result.str, result.length); break; } - scratchEnd(scratch); + + scratchEnd(temp); } #endif