dumb fixes
This commit is contained in:
31
app.cpp
31
app.cpp
@@ -41,11 +41,10 @@ struct GymLogEntry {
|
||||
GymLogDbParsed *parseDb(Arena *arena, string database) {
|
||||
GymLogDbParsed *dbParsed = PushStruct(arena, GymLogDbParsed);
|
||||
|
||||
GymLogDbHeader *header = (GymLogDbHeader *)database.str;
|
||||
dbParsed->header = *header;
|
||||
dbParsed->header = *((GymLogDbHeader *)database.str);
|
||||
|
||||
size_t head = sizeof(GymLogDbHeader);
|
||||
uint32 entriesLeft = header->nextId - 1;
|
||||
uint32 entriesLeft = dbParsed->header.nextId - 1;
|
||||
dbParsed->entries = PushList(arena, GymLogDbParsedEntry, entriesLeft);
|
||||
|
||||
while (entriesLeft > 0 && head < database.length) {
|
||||
@@ -133,12 +132,12 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
||||
int statusCode = 0;
|
||||
string file = os_readEntireFile(arena, LOG_FILE_LOCATION);
|
||||
|
||||
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_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};
|
||||
int numDays = 1;
|
||||
bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s);
|
||||
@@ -285,12 +284,12 @@ int gymTrackerDeleteEntries(Arena *arena, list<string> args) {
|
||||
// Syntax: do <exercise-name> weightKg reps
|
||||
int gymTrackerDo(Arena *arena, list<string> args) {
|
||||
int statusCode = 0;
|
||||
string newExerciseName = {0};
|
||||
string exerciseName = {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];
|
||||
exerciseName = args.data[0];
|
||||
}
|
||||
|
||||
GymLogDbParsedEntry *existingEntry = 0;
|
||||
@@ -299,14 +298,17 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
||||
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, newExerciseName)) {
|
||||
if (strStartsWith(entry.name, exerciseName)) {
|
||||
existingEntry = &entry;
|
||||
log("Assuming exercise \"%S\".\n\n", entry.name);
|
||||
if (entry.name.length != exerciseName.length) {
|
||||
exerciseName = entry.name;
|
||||
log("Assuming exercise \"%S\".\n\n", entry.name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!existingEntry) {
|
||||
log("The exercise \"%S\" hasn't been registered.", newExerciseName);
|
||||
log("The exercise \"%S\" hasn't been registered.", exerciseName);
|
||||
statusCode = 1;
|
||||
}
|
||||
}
|
||||
@@ -328,7 +330,7 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
||||
};
|
||||
|
||||
os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));
|
||||
statusCode = gymTrackerWorkToday(arena, exerciseId, newExerciseName);
|
||||
statusCode = gymTrackerWorkToday(arena, exerciseId, exerciseName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,7 +355,6 @@ int gymTrackerListExercises(Arena *arena, list<string> args) {
|
||||
|
||||
int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||
int statusCode = 0;
|
||||
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
|
||||
|
||||
string newExerciseName = args.data[0];
|
||||
if (newExerciseName.length == 0) {
|
||||
@@ -362,8 +363,7 @@ int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||
}
|
||||
|
||||
if (statusCode != 1) {
|
||||
string databaseLocation = DB_FILE_LOCATION;
|
||||
string database = os_readEntireFile(arena, databaseLocation);
|
||||
string database = os_readEntireFile(arena, DB_FILE_LOCATION);
|
||||
|
||||
byte *buf = 0;
|
||||
size_t newEntryStartIndex = 0;
|
||||
@@ -411,7 +411,8 @@ int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||
byte *newExerciseNameDb = buf + newEntryStartIndex + sizeof(GymLogDbEntry);
|
||||
memcpy(newExerciseNameDb, newExerciseName.str, newExerciseName.length);
|
||||
size_t bufSize = newEntryStartIndex + sizeof(GymLogDbEntry) + newExerciseName.length;
|
||||
os_writeEntireFile(arena, databaseLocation, buf, bufSize);
|
||||
log("%i\n", bufSize);
|
||||
os_writeEntireFile(arena, DB_FILE_LOCATION, buf, bufSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,9 @@ void os_free(void *ptr, size_t size) {
|
||||
}
|
||||
|
||||
string os_readEntireFile(Arena *arena, string filename) {
|
||||
FILE *input = fopen((char *)filename.str, "r");
|
||||
Scratch temp = scratchStart(&arena, 1);
|
||||
|
||||
FILE *input = fopen(cstring(temp.arena, filename), "r");
|
||||
string readBuffer;
|
||||
if (input) {
|
||||
struct stat st;
|
||||
@@ -35,34 +37,45 @@ string os_readEntireFile(Arena *arena, string filename) {
|
||||
} else {
|
||||
readBuffer = PushString(arena, 0);
|
||||
}
|
||||
|
||||
scratchEnd(temp);
|
||||
return readBuffer;
|
||||
}
|
||||
|
||||
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
|
||||
bool result = false;
|
||||
FILE *output = fopen((char *)filename.str, "w");
|
||||
if (output) {
|
||||
fwrite(contents, contentsLength, contentsLength, output);
|
||||
fclose(output);
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Scratch temp = scratchStart(&arena, 1);
|
||||
|
||||
bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
|
||||
bool result = false;
|
||||
FILE *output = fopen((char *)filename.str, "a");
|
||||
FILE *output = fopen(cstring(temp.arena, filename), "w");
|
||||
if (output) {
|
||||
fwrite(contents, sizeof(byte), contentsLength, output);
|
||||
fclose(output);
|
||||
result = true;
|
||||
}
|
||||
|
||||
scratchEnd(temp);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
|
||||
Scratch temp = scratchStart(&arena, 1);
|
||||
|
||||
bool result = false;
|
||||
FILE *output = fopen(cstring(temp.arena, filename), "a");
|
||||
if (output) {
|
||||
fwrite(contents, sizeof(byte), contentsLength, output);
|
||||
fclose(output);
|
||||
result = true;
|
||||
}
|
||||
|
||||
scratchEnd(temp);
|
||||
return result;
|
||||
}
|
||||
|
||||
void os_log(LogTarget target, const char *fmt, va_list argList) {
|
||||
Scratch scratch = scratchStart(0, 0);
|
||||
string result = strPrintfv(scratch.arena, fmt, argList);
|
||||
Scratch temp = scratchStart(0, 0);
|
||||
|
||||
string result = strPrintfv(temp.arena, fmt, argList);
|
||||
// TODO(djledda): finish implementation without cstdlib
|
||||
switch (target) {
|
||||
case LogTarget_stdin:
|
||||
@@ -78,7 +91,8 @@ void os_log(LogTarget target, const char *fmt, va_list argList) {
|
||||
write(1, (const void *)result.str, result.length);
|
||||
break;
|
||||
}
|
||||
scratchEnd(scratch);
|
||||
|
||||
scratchEnd(temp);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user