#include #include #include #include "./djstdlib/core.cpp" const string LOG_FILE_LOCATION = "./log.gtl"_s; const string DB_FILE_LOCATION = "./db.gtd"_s; struct GymLogDbHeader { uint32 nextId; }; struct GymLogDbEntry { uint32 id; uint32 nameLength; }; struct GymLogDbParsedEntry { uint32 id; string name; }; struct GymLogDbParsed { GymLogDbHeader header; list entries; }; struct WeightRepsInfo { uint8 reps; real32 weight; }; struct GymLogEntry { uint64 timestamp; uint32 exerciseId; union { WeightRepsInfo weightRepsInfo; }; }; GymLogDbParsed *parseDb(Arena *arena, string database) { GymLogDbParsed *dbParsed = PushStruct(arena, GymLogDbParsed); GymLogDbHeader *header = (GymLogDbHeader *)database.str; dbParsed->header = *header; size_t head = sizeof(GymLogDbHeader); uint32 entriesLeft = header->nextId - 1; dbParsed->entries = PushList(arena, GymLogDbParsedEntry, entriesLeft); while (entriesLeft > 0 && head < database.length) { GymLogDbEntry *currentEntry = (GymLogDbEntry *)((byte *)database.str + head); GymLogDbParsedEntry parsedEntry = { currentEntry->id, PushString(arena, currentEntry->nameLength) }; head += sizeof(GymLogDbEntry); memcpy(parsedEntry.name.str, database.str + head, currentEntry->nameLength); appendList(&dbParsed->entries, parsedEntry); head += currentEntry->nameLength; } return dbParsed; } int gymTrackerWorkForExercise(Arena *arena, uint32 exerciseId) { int statusCode = 0; string logfile = readEntireFile(arena, LOG_FILE_LOCATION); if (logfile.length % sizeof(GymLogEntry) != 0) { log("Log file corrupted.\n"); statusCode = 1; } else { size_t entryCount = logfile.length / sizeof(GymLogEntry); int currentDay = 0; int currentYear = 0; list logEntries = { (GymLogEntry *)logfile.str, entryCount, entryCount }; UnixTimestamp todayUnix = getSystemUnixTime(); Timestamp todayTs = timestampFromUnixTime(&todayUnix); real32 work = 0; for (EachIn(logEntries, i)) { GymLogEntry logEntry = logEntries.data[i]; Timestamp logTs = timestampFromUnixTime(&logEntry.timestamp); if (logTs.tm_yday == todayTs.tm_yday && todayTs.tm_year == logTs.tm_year) { work += logEntry.weightRepsInfo.weight * logEntry.weightRepsInfo.reps; } } log("Total work for this exercise today:\n%.2f J * m/ex\n", work); } return statusCode; } int gymTrackerStatus(Arena *arena, list args) { int statusCode = 0; string file = readEntireFile(arena, LOG_FILE_LOCATION); GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION)); if (file.length % sizeof(GymLogEntry) != 0) { puts("Log file corrupted."); statusCode = 1; } else { Timestamp stopTs = {0}; if (args.length > 0 && strEql(args.data[0], "--today"_s)) { UnixTimestamp nowUnix = getSystemUnixTime(); stopTs = timestampFromUnixTime(&nowUnix); } else if (args.length > 1 && strEql(args.data[0], "--days"_s) || strEql(args.data[0], "-d"_s)) { size_t l; int numDays = parsePositiveInt(args.data[1], &l); if (numDays == -1) { puts("Bad argument for --days parameter."); statusCode = 1; } else { uint64 todayUnix = getSystemUnixTime(); UnixTimestamp stopUnix = todayUnix - numDays * 24 * 60 * 60; stopTs = timestampFromUnixTime(&stopUnix); } } if (statusCode == 0) { size_t entryCount = file.length / sizeof(GymLogEntry); int currentDay = -1; int currentYear = -1; list logEntries = { (GymLogEntry *)file.str, entryCount, entryCount }; list workPerExerciseByDay = PushFullList(arena, real32, db->header.nextId); list nameByExercise = PushFullList(arena, string, db->header.nextId); zeroListFull(&workPerExerciseByDay); zeroListFull(&nameByExercise); int dayCount = 0; Timestamp timestamp = {0}; if (logEntries.length > 0) { timestamp = timestampFromUnixTime(&logEntries.data[logEntries.length - 1].timestamp); } for (EachInReversed(logEntries, i)) { GymLogEntry entry = logEntries.data[i]; if (timestamp.tm_yday != currentDay || timestamp.tm_year != currentYear) { if (timestamp.tm_year < stopTs.tm_year || timestamp.tm_yday < stopTs.tm_yday) { break; } if (dayCount > 0) { log("\n"); } log("--- %S ---\n", formatTimeYmd(arena, ×tamp)); currentDay = timestamp.tm_yday; currentYear = timestamp.tm_year; dayCount++; } workPerExerciseByDay.data[entry.exerciseId] += entry.weightRepsInfo.reps * entry.weightRepsInfo.weight; const char *format; if (entry.weightRepsInfo.weight == (int32)entry.weightRepsInfo.weight) { format = "%S: %S, %.0fkg X %i\n"; } else { format = "%S: %S, %.2fkg X %i\n"; } string *exerciseName = &(nameByExercise.data[entry.exerciseId]); if (exerciseName->str == 0) { for (EachIn(db->entries, j)) { GymLogDbParsedEntry dbEntry = db->entries.data[j]; if (dbEntry.id == entry.exerciseId) { *exerciseName = dbEntry.name; } } } log(format, formatTimeHms(arena, ×tamp), exerciseName->str ? *exerciseName : "unknown-exercise"_s, entry.weightRepsInfo.weight, entry.weightRepsInfo.reps); if (i > 0) { timestamp = timestampFromUnixTime(&logEntries.data[i - 1].timestamp); } if (i == 0 || timestamp.tm_yday != currentDay || timestamp.tm_year != currentYear) { log("\n"); log("Work summary:\n"); for (size_t j = 0; j < workPerExerciseByDay.length; j++) { if (workPerExerciseByDay.data[j] != 0.0f) { log("%S: %.2f J * m/ex\n", nameByExercise.data[j], workPerExerciseByDay.data[j]); } } zeroListFull(&workPerExerciseByDay); } } } } return statusCode; } // Syntax: do weightKg reps int gymTrackerDo(Arena *arena, list args) { int statusCode = 0; string newExerciseName = {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]; } GymLogDbParsedEntry *existingEntry = 0; if (statusCode == 0) { GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION)); for (EachIn(db->entries, i)) { GymLogDbParsedEntry entry = db->entries.data[i]; if (strEql(entry.name, newExerciseName)) { existingEntry = &entry; break; } } if (!existingEntry) { statusCode = 1; } } if (statusCode == 0) { uint32 exerciseId = existingEntry->id; size_t parsedCount = 0; real32 kg = parsePositiveReal32(args.data[1], &parsedCount); uint8 reps = parsePositiveInt(args.data[2], &parsedCount); if (parsedCount == 0 || kg == NAN || reps == 0 || kg == 0) { log("Invalid reps or weight input.\n"); statusCode = 1; } else { GymLogEntry entry = { getSystemUnixTime(), exerciseId, reps, kg, }; fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry)); statusCode = gymTrackerWorkForExercise(arena, exerciseId); } } return statusCode; } int gymTrackerAddExercise(Arena *arena, list args) { int statusCode = 0; string newExerciseName = args.data[0]; if (newExerciseName.length == 0) { log("No exercise name provided.\n"); statusCode = 1; } if (statusCode != 1) { string databaseLocation = DB_FILE_LOCATION; string database = readEntireFile(arena, databaseLocation); byte *buf = 0; size_t newEntryStartIndex = 0; if (database.length == 0) { // Initialise DB newEntryStartIndex = sizeof(GymLogDbHeader); buf = PushArray(arena, byte, sizeof(GymLogDbHeader) + sizeof(GymLogDbEntry) + newExerciseName.length); GymLogDbHeader *header = (GymLogDbHeader *)buf; header->nextId = 1; } else { // Validate entry not already present bool invalid = false; GymLogDbHeader *header = (GymLogDbHeader *)database.str; size_t head = sizeof(GymLogDbHeader); uint32 entriesLeft = header->nextId - 1; while (entriesLeft > 0 && head < database.length) { GymLogDbEntry *currentEntry = (GymLogDbEntry *)((byte *)database.str + head); head += sizeof(GymLogDbEntry); string entryName = {(char *)((byte *)database.str + head), currentEntry->nameLength }; if (strEql(entryName, newExerciseName)) { invalid = true; log("Exercise \"%S\" already registered (entry #%i)\n", entryName, currentEntry->id); break; } head += currentEntry->nameLength; } if (!invalid) { newEntryStartIndex = database.length; buf = PushArray(arena, byte, database.length + sizeof(GymLogDbEntry) + newExerciseName.length); memcpy(buf, database.str, database.length); } else { statusCode = 1; } } if (statusCode != 1) { // Add entry GymLogDbHeader *header = (GymLogDbHeader *)buf; GymLogDbEntry *entry = (GymLogDbEntry *)(buf + newEntryStartIndex); entry->id = header->nextId; entry->nameLength = (uint32)newExerciseName.length; header->nextId++; byte *newExerciseNameDb = buf + newEntryStartIndex + sizeof(GymLogDbEntry); memcpy(newExerciseNameDb, newExerciseName.str, newExerciseName.length); size_t bufSize = newEntryStartIndex + sizeof(GymLogDbEntry) + newExerciseName.length; writeEntireFile(arena, databaseLocation, buf, bufSize); } } return statusCode; } int main(int argc, char **argv) { initialiseCore(); Arena *arena = arenaAlloc(Megabytes(64)); list args = getArgs(arena, argc, argv); int statusCode = 0; if (args.length < 1) { log("At least one arg is required.\n"); statusCode = 1; } if (statusCode == 0) { if (strEql(args.data[0], "status"_s)) { statusCode = gymTrackerStatus(arena, listSlice(args, 1)); } else if (strEql(args.data[0], "do"_s)) { statusCode = gymTrackerDo(arena, listSlice(args, 1)); } else if (strEql(args.data[0], "add"_s)) { statusCode = gymTrackerAddExercise(arena, listSlice(args, 1)); } else { log("Unknown command \"%S\"\n", args.data[0]); statusCode = 1; } } return statusCode; }