update
This commit is contained in:
93
app.cpp
93
app.cpp
@@ -1,9 +1,10 @@
|
||||
#include <math.h>
|
||||
#include <memory.h>
|
||||
#include <time.h>
|
||||
#include "core.hpp"
|
||||
#include "./djstdlib/core.cpp"
|
||||
|
||||
const string LOG_FILE_LOCATION = strlit(".\\log.gtl");
|
||||
const string DB_FILE_LOCATION = strlit(".\\db.gtd");
|
||||
const string LOG_FILE_LOCATION = "./log.gtl"_s;
|
||||
const string DB_FILE_LOCATION = "./db.gtd"_s;
|
||||
|
||||
struct GymLogDbHeader {
|
||||
uint32 nextId;
|
||||
@@ -24,14 +25,16 @@ struct GymLogDbParsed {
|
||||
list<GymLogDbParsedEntry> entries;
|
||||
};
|
||||
|
||||
struct WeightRepsInfo {
|
||||
uint8 reps;
|
||||
real32 weight;
|
||||
};
|
||||
|
||||
struct GymLogEntry {
|
||||
uint64 timestamp;
|
||||
uint32 exerciseId;
|
||||
union {
|
||||
struct WeightReps {
|
||||
uint8 reps;
|
||||
real32 weight;
|
||||
} weightRepsInfo;
|
||||
WeightRepsInfo weightRepsInfo;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -62,7 +65,7 @@ int gymTrackerWorkForExercise(Arena *arena, uint32 exerciseId) {
|
||||
string logfile = readEntireFile(arena, LOG_FILE_LOCATION);
|
||||
|
||||
if (logfile.length % sizeof(GymLogEntry) != 0) {
|
||||
puts("Log file corrupted.");
|
||||
log("Log file corrupted.\n");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
size_t entryCount = logfile.length / sizeof(GymLogEntry);
|
||||
@@ -70,21 +73,19 @@ int gymTrackerWorkForExercise(Arena *arena, uint32 exerciseId) {
|
||||
int currentYear = 0;
|
||||
list<GymLogEntry> logEntries = { (GymLogEntry *)logfile.str, entryCount, entryCount };
|
||||
|
||||
tm todayTs = {0};
|
||||
time_t todayUnix = getSystemUnixTime();
|
||||
gmtime_s(&todayTs, (time_t *)&todayUnix);
|
||||
UnixTimestamp todayUnix = getSystemUnixTime();
|
||||
Timestamp todayTs = timestampFromUnixTime(&todayUnix);
|
||||
|
||||
real32 work = 0;
|
||||
for (EachIn(logEntries, i)) {
|
||||
GymLogEntry logEntry = logEntries.data[i];
|
||||
tm logTs = {0};
|
||||
gmtime_s(&logTs, (time_t *)&logEntry.timestamp);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Total work for this exercise today:\n%.2f J * m/ex\n", work);
|
||||
log("Total work for this exercise today:\n%.2f J * m/ex\n", work);
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
@@ -100,11 +101,11 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
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"))) {
|
||||
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) {
|
||||
@@ -112,8 +113,8 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
statusCode = 1;
|
||||
} else {
|
||||
uint64 todayUnix = getSystemUnixTime();
|
||||
time_t stopUnix = (time_t)(todayUnix - numDays * 24 * 60 * 60);
|
||||
gmtime_s(&stopTs, (time_t *)&stopUnix);
|
||||
UnixTimestamp stopUnix = todayUnix - numDays * 24 * 60 * 60;
|
||||
stopTs = timestampFromUnixTime(&stopUnix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,9 +131,9 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
|
||||
int dayCount = 0;
|
||||
|
||||
tm timestamp = {0};
|
||||
Timestamp timestamp = {0};
|
||||
if (logEntries.length > 0) {
|
||||
gmtime_s(×tamp, (time_t *)&logEntries.data[logEntries.length - 1].timestamp);
|
||||
timestamp = timestampFromUnixTime(&logEntries.data[logEntries.length - 1].timestamp);
|
||||
}
|
||||
|
||||
for (EachInReversed(logEntries, i)) {
|
||||
@@ -142,9 +143,9 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
break;
|
||||
}
|
||||
if (dayCount > 0) {
|
||||
puts("");
|
||||
log("\n");
|
||||
}
|
||||
printf("--- %s ---\n", cstring(arena, formatTimeYmd(arena, ×tamp)));
|
||||
log("--- %S ---\n", formatTimeYmd(arena, ×tamp));
|
||||
currentDay = timestamp.tm_yday;
|
||||
currentYear = timestamp.tm_year;
|
||||
dayCount++;
|
||||
@@ -152,11 +153,11 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
|
||||
workPerExerciseByDay.data[entry.exerciseId] += entry.weightRepsInfo.reps * entry.weightRepsInfo.weight;
|
||||
|
||||
char *format;
|
||||
const char *format;
|
||||
if (entry.weightRepsInfo.weight == (int32)entry.weightRepsInfo.weight) {
|
||||
format = "%s: %s, %.0fkg X %i\n";
|
||||
format = "%S: %S, %.0fkg X %i\n";
|
||||
} else {
|
||||
format = "%s: %s, %.2fkg X %i\n";
|
||||
format = "%S: %S, %.2fkg X %i\n";
|
||||
}
|
||||
|
||||
string *exerciseName = &(nameByExercise.data[entry.exerciseId]);
|
||||
@@ -169,22 +170,22 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
}
|
||||
}
|
||||
|
||||
printf(format,
|
||||
cstring(arena, formatTimeHms(arena, ×tamp)),
|
||||
exerciseName->str ? cstring(arena, *exerciseName) : "unknown-exercise",
|
||||
log(format,
|
||||
formatTimeHms(arena, ×tamp),
|
||||
exerciseName->str ? *exerciseName : "unknown-exercise"_s,
|
||||
entry.weightRepsInfo.weight,
|
||||
entry.weightRepsInfo.reps);
|
||||
|
||||
if (i > 0) {
|
||||
gmtime_s(×tamp, (time_t *)&logEntries.data[i - 1].timestamp);
|
||||
timestamp = timestampFromUnixTime(&logEntries.data[i - 1].timestamp);
|
||||
}
|
||||
|
||||
if (i == 0 || timestamp.tm_yday != currentDay || timestamp.tm_year != currentYear) {
|
||||
puts("");
|
||||
puts("Work summary:");
|
||||
log("\n");
|
||||
log("Work summary:\n");
|
||||
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]);
|
||||
log("%S: %.2f J * m/ex\n", nameByExercise.data[j], workPerExerciseByDay.data[j]);
|
||||
}
|
||||
}
|
||||
zeroListFull(&workPerExerciseByDay);
|
||||
@@ -201,7 +202,7 @@ 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.");
|
||||
log("Invalid exercise name and/or number of arguments.\n");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
newExerciseName = args.data[0];
|
||||
@@ -226,10 +227,10 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
||||
if (statusCode == 0) {
|
||||
uint32 exerciseId = existingEntry->id;
|
||||
size_t parsedCount = 0;
|
||||
real32 kg = parsePositiveReal32(arena, args.data[1], &parsedCount);
|
||||
real32 kg = parsePositiveReal32(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.");
|
||||
log("Invalid reps or weight input.\n");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
GymLogEntry entry = {
|
||||
@@ -251,7 +252,7 @@ int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||
int statusCode = 0;
|
||||
string newExerciseName = args.data[0];
|
||||
if (newExerciseName.length == 0) {
|
||||
printf("No exercise name provided.");
|
||||
log("No exercise name provided.\n");
|
||||
statusCode = 1;
|
||||
}
|
||||
|
||||
@@ -280,7 +281,7 @@ int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||
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);
|
||||
log("Exercise \"%S\" already registered (entry #%i)\n", entryName, currentEntry->id);
|
||||
break;
|
||||
}
|
||||
head += currentEntry->nameLength;
|
||||
@@ -313,25 +314,25 @@ int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||
}
|
||||
|
||||
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) {
|
||||
puts("At least one arg is required.");
|
||||
log("At least one arg is required.\n");
|
||||
statusCode = 1;
|
||||
}
|
||||
|
||||
if (statusCode == 0) {
|
||||
if (strEql(args.data[0], strlit("status"))) {
|
||||
if (strEql(args.data[0], "status"_s)) {
|
||||
statusCode = gymTrackerStatus(arena, listSlice(args, 1));
|
||||
} else if (strEql(args.data[0], strlit("do"))) {
|
||||
} else if (strEql(args.data[0], "do"_s)) {
|
||||
statusCode = gymTrackerDo(arena, listSlice(args, 1));
|
||||
} else if (strEql(args.data[0], strlit("add"))) {
|
||||
} else if (strEql(args.data[0], "add"_s)) {
|
||||
statusCode = gymTrackerAddExercise(arena, listSlice(args, 1));
|
||||
} else {
|
||||
printf("Unknown command \"%s\"", cstring(arena, args.data[0]));
|
||||
log("Unknown command \"%S\"\n", args.data[0]);
|
||||
statusCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user