migrate to c
This commit is contained in:
729
app.c
Normal file
729
app.c
Normal file
@@ -0,0 +1,729 @@
|
||||
#include "time.h"
|
||||
#include "djstdlib/core.c"
|
||||
|
||||
typedef enum {
|
||||
CmdArgType_BOOL,
|
||||
CmdArgType_STRING,
|
||||
CmdArgType_INT,
|
||||
CmdArgType_FLOAT,
|
||||
// --
|
||||
CmdArgType_Count,
|
||||
} CmdArgType;
|
||||
|
||||
string cmd_argTypeFmt(CmdArgType type) {
|
||||
switch (type) {
|
||||
case CmdArgType_FLOAT:
|
||||
return s("float");
|
||||
case CmdArgType_BOOL:
|
||||
return s("boolean flag");
|
||||
case CmdArgType_INT:
|
||||
return s("integer");
|
||||
case CmdArgType_STRING:
|
||||
return s("string");
|
||||
default:
|
||||
return s("invalid command argument type");
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
* The zero byte '\0' means no char name.
|
||||
*/
|
||||
char charName;
|
||||
string name;
|
||||
string description;
|
||||
CmdArgType type;
|
||||
} CmdOptionArg;
|
||||
|
||||
typedef struct {
|
||||
string name;
|
||||
string description;
|
||||
CmdArgType type;
|
||||
} CmdPositionalArg;
|
||||
|
||||
typedef struct {
|
||||
string name;
|
||||
string content;
|
||||
CmdArgType type;
|
||||
} CmdParsedOptionArg;
|
||||
|
||||
typedef struct {
|
||||
int index;
|
||||
CmdArgType type;
|
||||
void *content;
|
||||
} CmdParsedPositionalArg;
|
||||
|
||||
DefineList(CmdParsedPositionalArg, CmdParsedPositionalArg);
|
||||
DefineList(CmdParsedOptionArg, CmdParsedOptionArg);
|
||||
typedef struct {
|
||||
CmdParsedPositionalArgList posArgs;
|
||||
CmdParsedOptionArgList optArgs;
|
||||
} ParsedCmd;
|
||||
|
||||
DefineList(CmdPositionalArg, CmdPositionalArg);
|
||||
DefineList(CmdOptionArg, CmdOptionArg);
|
||||
typedef struct {
|
||||
string name;
|
||||
string description;
|
||||
CmdPositionalArgList posArgs;
|
||||
CmdOptionArgList optArgs;
|
||||
/**
|
||||
* @returns The status code of the command
|
||||
*/
|
||||
int32 (*command)(Arena *arena, StringList args);
|
||||
} BasicCommand;
|
||||
|
||||
void cmd_printSyntax(BasicCommand *cmd) {
|
||||
print("%S", cmd->name);
|
||||
for (EachIn(cmd->posArgs, j)) {
|
||||
print(" [%S]", cmd->posArgs.data[j].name);
|
||||
}
|
||||
}
|
||||
|
||||
DefineList(BasicCommand, BasicCommand);
|
||||
void cmd_printHelp(Arena *arena, BasicCommandList commands, string *helpCmd) {
|
||||
if (helpCmd) {
|
||||
for (EachIn(commands, i)) {
|
||||
BasicCommand *icmd = &commands.data[i];
|
||||
if (strEql(*helpCmd, icmd->name)) {
|
||||
print("Syntax: "); cmd_printSyntax(icmd); print("\n\n");
|
||||
print("%S\n", icmd->description);
|
||||
print("\n");
|
||||
if (icmd->posArgs.length > 0) {
|
||||
print("Arguments:\n");
|
||||
for (EachIn(icmd->posArgs, j)) {
|
||||
CmdPositionalArg *posArg = &icmd->posArgs.data[j];
|
||||
print("%S (%S) - %S\n", posArg->name, cmd_argTypeFmt(posArg->type), posArg->description);
|
||||
}
|
||||
}
|
||||
if (icmd->optArgs.length > 0) {
|
||||
print("Options:\n");
|
||||
for (EachIn(icmd->optArgs, j)) {
|
||||
CmdOptionArg *optArg = &icmd->optArgs.data[j];
|
||||
string charNameStr = optArg->charName != '\0' ? strPrintf(arena, "-%c, ", optArg->charName) : s("");
|
||||
print("%S--%S (%S) - %S\n", charNameStr, optArg->name, cmd_argTypeFmt(optArg->type), optArg->description);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print("Available Commands:\n");
|
||||
for (EachIn(commands, i)) {
|
||||
print("- "); cmd_printSyntax(&commands.data[i]); print("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const string LOG_FILE_LOCATION = s("./log.gtl");
|
||||
const string DB_FILE_LOCATION = s("./db.gtd");
|
||||
|
||||
typedef struct {
|
||||
uint32 nextId;
|
||||
} GymLogDbHeader;
|
||||
|
||||
typedef struct {
|
||||
uint32 id;
|
||||
uint32 nameLength;
|
||||
} GymLogDbEntry;
|
||||
|
||||
typedef struct {
|
||||
uint32 id;
|
||||
string name;
|
||||
} GymLogDbParsedEntry;
|
||||
|
||||
typedef GymLogDbParsedEntry Exercise;
|
||||
|
||||
DefineList(GymLogDbParsedEntry, GymLogDbParsedEntry);
|
||||
typedef struct {
|
||||
GymLogDbHeader header;
|
||||
GymLogDbParsedEntryList entries;
|
||||
} GymLogDbParsed;
|
||||
|
||||
typedef struct {
|
||||
uint8 reps;
|
||||
real32 weight;
|
||||
} WeightRepsInfo;
|
||||
|
||||
typedef struct {
|
||||
uint64 timestamp;
|
||||
uint32 exerciseId;
|
||||
union {
|
||||
WeightRepsInfo weightRepsInfo;
|
||||
};
|
||||
} GymLogEntry;
|
||||
|
||||
typedef struct {
|
||||
real32 totalWork;
|
||||
uint32 restTime;
|
||||
} WorkSummary;
|
||||
|
||||
GymLogDbParsed *parseDb(Arena *arena, string database) {
|
||||
GymLogDbParsed *dbParsed = PushStruct(arena, GymLogDbParsed);
|
||||
|
||||
dbParsed->header = *((GymLogDbHeader *)database.str);
|
||||
|
||||
size_t head = sizeof(GymLogDbHeader);
|
||||
uint32 entriesLeft = dbParsed->header.nextId - 1;
|
||||
dbParsed->entries = PushList(arena, GymLogDbParsedEntryList, 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;
|
||||
}
|
||||
|
||||
DefineList(GymLogEntry, GymLogEntry);
|
||||
GymLogEntryList loadEntryLog(Arena *arena, string fileLocation) {
|
||||
GymLogEntryList result = {0};
|
||||
string logfile = os_readEntireFile(arena, LOG_FILE_LOCATION);
|
||||
|
||||
if (logfile.length % sizeof(GymLogEntry) != 0) {
|
||||
print("Log file corrupted.\n");
|
||||
} else {
|
||||
size_t entryCount = logfile.length / sizeof(GymLogEntry);
|
||||
result = (GymLogEntryList){ (GymLogEntry *)logfile.str, entryCount, entryCount };
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
WorkSummary workSummaryForExercise(GymLogEntryList entries, Exercise exercise) {
|
||||
WorkSummary result = {0};
|
||||
UnixTimestamp lastTimestamp = 0;
|
||||
for (EachInReversed(entries, i)) {
|
||||
if (entries.data[i].exerciseId == exercise.id) {
|
||||
GymLogEntry logEntry = entries.data[i];
|
||||
result.totalWork += logEntry.weightRepsInfo.weight * logEntry.weightRepsInfo.reps;
|
||||
if (lastTimestamp > 0) {
|
||||
result.restTime += (uint32)(lastTimestamp - logEntry.timestamp);
|
||||
}
|
||||
lastTimestamp = logEntry.timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int gymTrackerLogWorkToday(Arena *arena, Exercise exercise) {
|
||||
int statusCode = 0;
|
||||
string logfile = os_readEntireFile(arena, LOG_FILE_LOCATION);
|
||||
|
||||
if (logfile.length % sizeof(GymLogEntry) != 0) {
|
||||
print("Log file corrupted.\n");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
size_t entryCount = logfile.length / sizeof(GymLogEntry);
|
||||
GymLogEntryList logEntries = { (GymLogEntry *)logfile.str, entryCount, entryCount };
|
||||
|
||||
UnixTimestamp todayUnix = getSystemUnixTime();
|
||||
Timestamp todayTs = timestampFromUnixTime(&todayUnix);
|
||||
GymLogEntryList todaysEntries = {0};
|
||||
todaysEntries.data = logEntries.data;
|
||||
for (EachInReversed(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) {
|
||||
todaysEntries.length += 1;
|
||||
todaysEntries.data = &logEntries.data[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (todaysEntries.data) {
|
||||
todaysEntries.capacity = todaysEntries.length;
|
||||
WorkSummary summary = workSummaryForExercise(todaysEntries, exercise);
|
||||
print("Total work today for %S:\n%.2fkg in ~%.2fmin.\n", exercise.name, summary.totalWork, (real32)summary.restTime / 60.0f);
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
DefineList(real32, Real32);
|
||||
DefineList(uint32, Uint32);
|
||||
int32 gymTrackerStatus(Arena *arena, StringList args) {
|
||||
int32 statusCode = 0;
|
||||
string file = os_readEntireFile(arena, LOG_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};
|
||||
ParsePositiveIntResult numDays = {1, true};
|
||||
bool showAll = args.length == 1 && strEql(args.data[0], s("--all"));
|
||||
if (!showAll) {
|
||||
if (args.length == 2 && (strEql(args.data[0], s("--days")) || strEql(args.data[0], s("-d")))) {
|
||||
size_t l;
|
||||
numDays = parsePositiveInt(args.data[1], &l);
|
||||
}
|
||||
if (!numDays.valid) {
|
||||
puts("Bad argument for --days (-d) parameter.");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
uint64 todayUnix = getSystemUnixTime();
|
||||
UnixTimestamp startUnix = todayUnix - numDays.result * 24 * 60 * 60;
|
||||
startTs = timestampFromUnixTime(&startUnix);
|
||||
}
|
||||
}
|
||||
|
||||
if (statusCode == 0) {
|
||||
int lastDay = -1;
|
||||
int lastYear = -1;
|
||||
size_t entryCount = file.length / sizeof(GymLogEntry);
|
||||
GymLogEntryList logEntries = { (GymLogEntry *)file.str, entryCount, entryCount };
|
||||
|
||||
StringList nameByExercise = PushFullListZero(arena, StringList, db->header.nextId);
|
||||
|
||||
Real32List workPerExerciseByDay = PushFullListZero(arena, Real32List, db->header.nextId);
|
||||
Real32List workPerExerciseByPrevDay = PushFullListZero(arena, Real32List, db->header.nextId);
|
||||
Uint32List restPerExerciseByDay = PushFullListZero(arena, Uint32List, db->header.nextId);
|
||||
Uint32List lastTsPerExerciseByDay = PushFullListZero(arena, Uint32List, db->header.nextId);
|
||||
|
||||
int dayCount = 0;
|
||||
|
||||
Timestamp timestamp = {0};
|
||||
|
||||
GymLogEntry *prevEntry = 0;
|
||||
GymLogEntry *entry = 0;
|
||||
for (EachIn(logEntries, i)) {
|
||||
prevEntry = entry;
|
||||
entry = &logEntries.data[i];
|
||||
|
||||
timestamp = timestampFromUnixTime(&entry->timestamp);
|
||||
|
||||
if (timestamp.tm_year < startTs.tm_year || timestamp.tm_yday < startTs.tm_yday) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (timestamp.tm_yday != lastDay || timestamp.tm_year != lastYear) {
|
||||
if (dayCount > 0) {
|
||||
print("\n");
|
||||
}
|
||||
print("================ %S ===================\n", formatTimeYmd(arena, ×tamp));
|
||||
lastDay = timestamp.tm_yday;
|
||||
lastYear = timestamp.tm_year;
|
||||
dayCount++;
|
||||
}
|
||||
|
||||
workPerExerciseByDay.data[entry->exerciseId] += entry->weightRepsInfo.reps * entry->weightRepsInfo.weight;
|
||||
uint32 lastTsForExercise = lastTsPerExerciseByDay.data[entry->exerciseId];
|
||||
if (lastTsForExercise > 0) {
|
||||
restPerExerciseByDay.data[entry->exerciseId] += (uint32)(entry->timestamp - lastTsForExercise);
|
||||
}
|
||||
lastTsPerExerciseByDay.data[entry->exerciseId] = (uint32)entry->timestamp;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string nameToPrint = {0};
|
||||
if (prevEntry && entry->exerciseId == prevEntry->exerciseId) {
|
||||
nameToPrint = PushStringFill(arena, exerciseName->length, '.');
|
||||
} else {
|
||||
nameToPrint = exerciseName->str ? *exerciseName : s("unknown-exercise");
|
||||
print("\n");
|
||||
}
|
||||
print(format,
|
||||
formatTimeHms(arena, ×tamp),
|
||||
nameToPrint,
|
||||
entry->weightRepsInfo.weight,
|
||||
entry->weightRepsInfo.reps);
|
||||
|
||||
Timestamp nextTimestamp = {0};
|
||||
if (i < logEntries.length - 1) {
|
||||
nextTimestamp = timestampFromUnixTime(&logEntries.data[i + 1].timestamp);
|
||||
}
|
||||
|
||||
if (i == logEntries.length + 1 || nextTimestamp.tm_yday != lastDay || nextTimestamp.tm_year != lastYear) {
|
||||
print("\n");
|
||||
print("Work summary:\n");
|
||||
for (size_t j = 0; j < workPerExerciseByDay.length; j++) {
|
||||
if (workPerExerciseByDay.data[j] != 0.0f) {
|
||||
const char *fmtString;
|
||||
real32 improvement = 0;
|
||||
real32 workToday = workPerExerciseByDay.data[j];
|
||||
real32 workLastTime = workPerExerciseByPrevDay.data[j];
|
||||
if (workPerExerciseByPrevDay.data[j] == 0) {
|
||||
fmtString = COLOR_TEXT("%S", ANSI_fg_cyan) ": %.2fkg in %.2fmin\n";
|
||||
} else {
|
||||
improvement = workToday - workLastTime;
|
||||
if (improvement > 0) {
|
||||
fmtString = COLOR_TEXT("%S", ANSI_fg_cyan) ": %.2fkg in %.2fmin " COLOR_TEXT("+%.2fkg (+%.2f%%)\n", ANSI_fg_green);
|
||||
} else if (improvement < 0) {
|
||||
fmtString = COLOR_TEXT("%S", ANSI_fg_cyan) ": %.2fkg in %.2fmin " COLOR_TEXT("%.2fkg (%.2f%%)\n", ANSI_fg_red);
|
||||
} else {
|
||||
fmtString = COLOR_TEXT("%S", ANSI_fg_cyan) ": %.2fkg in %.2fmin " COLOR_TEXT("(no change)\n", ANSI_fg_yellow);
|
||||
}
|
||||
}
|
||||
|
||||
print(fmtString, nameByExercise.data[j], workToday, (real32)restPerExerciseByDay.data[j] / 60.0f, improvement, improvement / workLastTime * 100);
|
||||
|
||||
workPerExerciseByPrevDay.data[j] = workToday;
|
||||
}
|
||||
}
|
||||
ZeroListFull(&workPerExerciseByDay);
|
||||
ZeroListFull(&restPerExerciseByDay);
|
||||
ZeroListFull(&lastTsPerExerciseByDay);
|
||||
prevEntry = 0;
|
||||
entry = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
int gymTrackerDeleteEntries(Arena *arena, StringList args) {
|
||||
int statusCode = 0;
|
||||
|
||||
if (args.length == 0) {
|
||||
print("Please pass the number of entries to delete starting from the most recent.");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
size_t position = 0;
|
||||
ParsePositiveIntResult numToDeleteParsed = parsePositiveInt(args.data[0], &position);
|
||||
if (numToDeleteParsed.valid) {
|
||||
GymLogEntryList logEntries = loadEntryLog(arena, LOG_FILE_LOCATION);
|
||||
if (numToDeleteParsed.result > logEntries.length) {
|
||||
print("%i is more than the current number of log entries (%i). Aborting.", numToDeleteParsed, logEntries.length);
|
||||
statusCode = 1;
|
||||
} else {
|
||||
os_writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDeleteParsed.result) * sizeof(GymLogEntry));
|
||||
}
|
||||
} else {
|
||||
print("Invalid number to delete.\n");
|
||||
statusCode = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
// Syntax: do <exercise-name> weightKg reps
|
||||
int32 gymTrackerDo(Arena *arena, StringList args) {
|
||||
int32 statusCode = 0;
|
||||
Exercise exercise = {};
|
||||
|
||||
if (args.length < 3 || args.data[0].length == 0) {
|
||||
print("Invalid exercise name and/or number of arguments.\n");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
exercise.name = args.data[0];
|
||||
}
|
||||
|
||||
GymLogDbParsedEntry *existingEntry = 0;
|
||||
|
||||
if (statusCode == 0) {
|
||||
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, exercise.name)) {
|
||||
existingEntry = &entry;
|
||||
if (entry.name.length != exercise.name.length) {
|
||||
exercise.name = entry.name;
|
||||
print("Assuming exercise \"%S\".\n\n", entry.name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!existingEntry) {
|
||||
print("The exercise \"%S\" hasn't been registered.", exercise.name);
|
||||
statusCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (statusCode == 0) {
|
||||
exercise.id = existingEntry->id;
|
||||
size_t parsedCount = 0;
|
||||
ParsePositiveReal32Result kg = parsePositiveReal32(args.data[1], &parsedCount);
|
||||
ParsePositiveIntResult reps = parsePositiveInt(args.data[2], &parsedCount);
|
||||
if (!kg.valid || !reps.valid) {
|
||||
print("%zu, %f, %\n", parsedCount, kg, reps);
|
||||
print("Invalid reps or weight input.\n");
|
||||
statusCode = 1;
|
||||
} else {
|
||||
GymLogEntry entry = {
|
||||
.timestamp=getSystemUnixTime(),
|
||||
.exerciseId=exercise.id,
|
||||
.weightRepsInfo={
|
||||
.reps=reps.result,
|
||||
.weight=kg.result,
|
||||
},
|
||||
};
|
||||
|
||||
os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));
|
||||
statusCode = gymTrackerLogWorkToday(arena, exercise);
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
int gymTrackerListExercises(Arena *arena, StringList args) {
|
||||
int statusCode = 0;
|
||||
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
|
||||
|
||||
if (db->entries.length == 0) {
|
||||
print("No entries currently registered in the exercise database.");
|
||||
} else {
|
||||
print("%i entries currently registered in the exercise database:\n\n", db->entries.length);
|
||||
for (EachIn(db->entries, i)) {
|
||||
print("#%i: %S\n", i + 1, db->entries.data[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
int gymTrackerAddExercise(Arena *arena, StringList args) {
|
||||
int statusCode = 0;
|
||||
|
||||
string newExerciseName = args.data[0];
|
||||
if (newExerciseName.length == 0) {
|
||||
print("No exercise name provided.\n");
|
||||
statusCode = 1;
|
||||
}
|
||||
|
||||
if (statusCode != 1) {
|
||||
string database = os_readEntireFile(arena, DB_FILE_LOCATION);
|
||||
|
||||
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;
|
||||
print("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;
|
||||
os_writeEntireFile(arena, DB_FILE_LOCATION, buf, bufSize);
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
int32 cmd_dispatch(Arena *arena, StringList args, BasicCommandList cmds) {
|
||||
int32 result = 0;
|
||||
|
||||
if (args.length < 1) {
|
||||
print("At least one arg is required.\n");
|
||||
result = 1;
|
||||
} else if (strEql(args.data[0], s("--help"))) {
|
||||
cmd_printHelp(arena, cmds, NULL);
|
||||
} else if (args.length > 1 && strEql(args.data[1], s("--help"))) {
|
||||
cmd_printHelp(arena, cmds, &args.data[0]);
|
||||
} else {
|
||||
string userCmd = args.data[0];
|
||||
for (EachIn(cmds, i)) {
|
||||
if (strEql(cmds.data[i].name, userCmd)) {
|
||||
StringList argsRest = ListTail(args, 1);
|
||||
cmds.data[i].command(arena, argsRest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
initialiseDjStdCore();
|
||||
Arena *arena = arenaAlloc(Megabytes(64));
|
||||
StringList args = getArgs(arena, argc, argv);
|
||||
|
||||
CmdOptionArg cmdStatusOptArgs[] = {
|
||||
{
|
||||
.charName = '\0',
|
||||
.name = s("all"),
|
||||
.description = s("Displays the full recorded history since day zero."),
|
||||
.type = CmdArgType_BOOL
|
||||
},
|
||||
{
|
||||
.charName = 'd',
|
||||
.name = s("days"),
|
||||
.description = s("Displays the history for a previous number of days."),
|
||||
.type = CmdArgType_INT,
|
||||
}
|
||||
};
|
||||
BasicCommand cmdStatus = {
|
||||
.name = s("status"),
|
||||
.description = s("Shows the currently recorded exercises. Default displays the current day."),
|
||||
.posArgs = EmptyList(CmdPositionalArgList),
|
||||
.optArgs = ArrayAsList(CmdOptionArgList, cmdStatusOptArgs),
|
||||
.command = gymTrackerStatus,
|
||||
};
|
||||
|
||||
CmdPositionalArg cmdDoPosArgs[] = {
|
||||
{
|
||||
.name = s("exercise"),
|
||||
.type = CmdArgType_STRING,
|
||||
},
|
||||
{
|
||||
.name = s("weight"),
|
||||
.description = s("Weight moved for one repetition"),
|
||||
.type = CmdArgType_FLOAT,
|
||||
},
|
||||
{
|
||||
.name = s("reps"),
|
||||
.description = s("Number of repetitions performed"),
|
||||
.type = CmdArgType_INT,
|
||||
}
|
||||
};
|
||||
BasicCommand cmdDo = {
|
||||
.name = s("do"),
|
||||
.description = s("Records an exercise with weight and reps"),
|
||||
.posArgs = ArrayAsList(CmdPositionalArgList, cmdDoPosArgs),
|
||||
.optArgs = EmptyList(CmdOptionArgList),
|
||||
.command = gymTrackerDo,
|
||||
};
|
||||
|
||||
CmdPositionalArg cmdDeletePosArgs[] = {
|
||||
{
|
||||
.name = s("count"),
|
||||
.description = s("The number of entries to pop off the end of the record."),
|
||||
.type = CmdArgType_INT,
|
||||
}
|
||||
};
|
||||
BasicCommand cmdDelete = {
|
||||
.name = s("delete"),
|
||||
.description = s("Deletes the last given number of entries."),
|
||||
.posArgs = ArrayAsList(CmdPositionalArgList, cmdDeletePosArgs),
|
||||
.optArgs = EmptyList(CmdOptionArgList),
|
||||
.command = gymTrackerDeleteEntries,
|
||||
};
|
||||
|
||||
BasicCommand cmdList = {
|
||||
.name = s("list"),
|
||||
.description = s("Lists all available exercises in the database."),
|
||||
.posArgs = EmptyList(CmdPositionalArgList),
|
||||
.optArgs = EmptyList(CmdOptionArgList),
|
||||
.command = gymTrackerListExercises,
|
||||
};
|
||||
|
||||
CmdPositionalArg cmdAddPosArgs[] = {
|
||||
{
|
||||
.name = s("name"),
|
||||
.description = s("The name of the exercise to be added."),
|
||||
.type = CmdArgType_STRING,
|
||||
},
|
||||
};
|
||||
BasicCommand cmdAdd = {
|
||||
.name = s("add"),
|
||||
.description = s("Adds a new exercise name to the database."),
|
||||
.posArgs = ArrayAsList(CmdPositionalArgList, cmdAddPosArgs),
|
||||
.optArgs = EmptyList(CmdOptionArgList),
|
||||
.command = gymTrackerAddExercise,
|
||||
};
|
||||
|
||||
BasicCommand commands[] = {
|
||||
cmdStatus,
|
||||
cmdDo,
|
||||
cmdDelete,
|
||||
cmdList,
|
||||
cmdAdd,
|
||||
};
|
||||
|
||||
return cmd_dispatch(arena, args, ArrayAsList(BasicCommandList, commands));
|
||||
|
||||
/*
|
||||
if (icmd.posArgs.length > 0) {
|
||||
print("\tPositional arguments:\n");
|
||||
for (EachIn(icmd.posArgs, j)) {
|
||||
CmdPositionalArg arg = icmd.posArgs.data[j];
|
||||
print("\t- %S: (%S) %S\n",
|
||||
arg.name,
|
||||
cmdArgTypeFmt(arg.type),
|
||||
arg.description
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (icmd.optArgs.length > 0) {
|
||||
print("\tOptions:\n");
|
||||
}
|
||||
|
||||
int statusCode = 0;
|
||||
|
||||
if (statusCode == 0) {
|
||||
string cmd = args.data[0];
|
||||
list<string> argsRest = ListSlice(args, 1);
|
||||
|
||||
if (strEql(s("status"), cmd)) {
|
||||
statusCode = gymTrackerStatus(arena, argsRest);
|
||||
} else if (strEql(s("do"), cmd)) {
|
||||
statusCode = gymTrackerDo(arena, argsRest);
|
||||
} else if (strEql(s("delete"), cmd)) {
|
||||
statusCode = gymTrackerDeleteEntries(arena, argsRest);
|
||||
} else if (strEql(s("list"), cmd)) {
|
||||
statusCode = gymTrackerListExercises(arena, argsRest);
|
||||
} else if (strEql(s("add"), cmd)) {
|
||||
statusCode = gymTrackerAddExercise(arena, argsRest);
|
||||
} else {
|
||||
print("Unknown command \"%S\"\n", args.data[0]);
|
||||
statusCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return statusCode;
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user