|
- #include <math.h>
- #include <time.h>
- #include "core.hpp"
-
- const string LOG_FILE_LOCATION = strlit(".\\log.gtl");
- const string DB_FILE_LOCATION = strlit(".\\db.gtd");
-
- struct GymLogDbHeader {
- uint32 nextId;
- };
-
- struct GymLogDbEntry {
- uint32 id;
- uint32 nameLength;
- };
-
- struct GymLogDbParsedEntry {
- uint32 id;
- string name;
- };
-
- struct GymLogDbParsed {
- GymLogDbHeader header;
- list<GymLogDbParsedEntry> entries;
- };
-
- struct GymLogEntry {
- uint64 timestamp;
- uint32 exerciseId;
- union {
- struct WeightReps {
- uint8 reps;
- real32 weight;
- } 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) {
- puts("Log file corrupted.");
- statusCode = 1;
- } else {
- size_t entryCount = logfile.length / sizeof(GymLogEntry);
- int currentDay = 0;
- int currentYear = 0;
- list<GymLogEntry> logEntries = { (GymLogEntry *)logfile.str, entryCount, entryCount };
-
- tm todayTs = {0};
- time_t todayUnix = getSystemUnixTime();
- gmtime_s(&todayTs, (time_t *)&todayUnix);
-
- real32 work = 0;
- for (EachIn(logEntries, i)) {
- GymLogEntry logEntry = logEntries.data[i];
- tm logTs = {0};
- gmtime_s(&logTs, (time_t *)&logEntry.timestamp);
- if (logTs.tm_yday == todayTs.tm_yday && todayTs.tm_year == logTs.tm_year) {
- work += logEntry.weightRepsInfo.weight * logEntry.weightRepsInfo.reps;
- }
- }
-
- printf("Total work for this exercise today:\n%.2f J * m/ex\n", work);
- }
-
- return statusCode;
- }
-
- int gymTrackerStatus(Arena *arena, list<string> 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 {
- tm stopTs = {0};
- if (args.length > 0 && strEql(args.data[0], strlit("--today"))) {
- time_t todayUnix = getSystemUnixTime();
- gmtime_s(&stopTs, (time_t *)&todayUnix);
- } else if (args.length > 1 && strEql(args.data[0], strlit("--days")) || strEql(args.data[0], strlit("-d"))) {
- 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();
- time_t stopUnix = (time_t)(todayUnix - numDays * 24 * 60 * 60);
- gmtime_s(&stopTs, (time_t *)&stopUnix);
- }
- }
-
- if (statusCode == 0) {
- size_t entryCount = file.length / sizeof(GymLogEntry);
- int currentDay = -1;
- int currentYear = -1;
- list<GymLogEntry> logEntries = { (GymLogEntry *)file.str, entryCount, entryCount };
-
- list<real32> workPerExerciseByDay = PushFullList(arena, real32, db->header.nextId);
- list<string> nameByExercise = PushFullList(arena, string, db->header.nextId);
- zeroListFull(&workPerExerciseByDay);
- zeroListFull(&nameByExercise);
-
- int dayCount = 0;
-
- tm timestamp = {0};
- if (logEntries.length > 0) {
- gmtime_s(×tamp, (time_t *)&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) {
- puts("");
- }
- printf("--- %s ---\n", cstring(arena, formatTimeYmd(arena, ×tamp)));
- currentDay = timestamp.tm_yday;
- currentYear = timestamp.tm_year;
- dayCount++;
- }
-
- workPerExerciseByDay.data[entry.exerciseId] += entry.weightRepsInfo.reps * entry.weightRepsInfo.weight;
-
- 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;
- }
- }
- }
-
- printf(format,
- cstring(arena, formatTimeHms(arena, ×tamp)),
- exerciseName->str ? cstring(arena, *exerciseName) : "unknown-exercise",
- entry.weightRepsInfo.weight,
- entry.weightRepsInfo.reps);
-
- if (i > 0) {
- gmtime_s(×tamp, (time_t *)&logEntries.data[i - 1].timestamp);
- }
-
- if (i == 0 || timestamp.tm_yday != currentDay || timestamp.tm_year != currentYear) {
- puts("");
- puts("Work summary:");
- for (size_t j = 0; j < workPerExerciseByDay.length; j++) {
- if (workPerExerciseByDay.data[j] != 0.0f) {
- printf("%s: %.2f J * m/ex\n", cstring(arena, nameByExercise.data[j]), workPerExerciseByDay.data[j]);
- }
- }
- zeroListFull(&workPerExerciseByDay);
- }
- }
- }
- }
-
- return statusCode;
- }
-
- // Syntax: do <exercise-name> weightKg reps
- int gymTrackerDo(Arena *arena, list<string> args) {
- int statusCode = 0;
- string newExerciseName = {0};
- if (args.length < 3 || args.data[0].length == 0) {
- printf("Invalid exercise name and/or number of arguments.");
- 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(arena, args.data[1], &parsedCount);
- uint8 reps = parsePositiveInt(args.data[2], &parsedCount);
- if (parsedCount == 0 || kg == NAN || reps == 0 || kg == 0) {
- printf("Invalid reps or weight input.");
- 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<string> args) {
- int statusCode = 0;
- string newExerciseName = args.data[0];
- if (newExerciseName.length == 0) {
- printf("No exercise name provided.");
- 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;
- printf("Exercise \"%s\" already registered (entry #%i)\n", cstring(arena, 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) {
- Arena *arena = arenaAlloc(Megabytes(64));
- list<string> args = getArgs(arena, argc, argv);
- int statusCode = 0;
-
-
- if (args.length < 1) {
- puts("At least one arg is required.");
- statusCode = 1;
- }
-
- if (statusCode == 0) {
- if (strEql(args.data[0], strlit("status"))) {
- statusCode = gymTrackerStatus(arena, listSlice(args, 1));
- } else if (strEql(args.data[0], strlit("do"))) {
- statusCode = gymTrackerDo(arena, listSlice(args, 1));
- } else if (strEql(args.data[0], strlit("add"))) {
- statusCode = gymTrackerAddExercise(arena, listSlice(args, 1));
- } else {
- printf("Unknown command \"%s\"", cstring(arena, args.data[0]));
- statusCode = 1;
- }
- }
-
- return statusCode;
- }
|