|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- #include <math.h>
- #include <memory.h>
- #include <time.h>
- #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<GymLogDbParsedEntry> 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<GymLogEntry> 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<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 {
- 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<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;
-
- 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 <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) {
- 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<string> 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<string> 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;
- }
|