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 *parseDb(Arena *arena, string database) {
|
||||||
GymLogDbParsed *dbParsed = PushStruct(arena, GymLogDbParsed);
|
GymLogDbParsed *dbParsed = PushStruct(arena, GymLogDbParsed);
|
||||||
|
|
||||||
GymLogDbHeader *header = (GymLogDbHeader *)database.str;
|
dbParsed->header = *((GymLogDbHeader *)database.str);
|
||||||
dbParsed->header = *header;
|
|
||||||
|
|
||||||
size_t head = sizeof(GymLogDbHeader);
|
size_t head = sizeof(GymLogDbHeader);
|
||||||
uint32 entriesLeft = header->nextId - 1;
|
uint32 entriesLeft = dbParsed->header.nextId - 1;
|
||||||
dbParsed->entries = PushList(arena, GymLogDbParsedEntry, entriesLeft);
|
dbParsed->entries = PushList(arena, GymLogDbParsedEntry, entriesLeft);
|
||||||
|
|
||||||
while (entriesLeft > 0 && head < database.length) {
|
while (entriesLeft > 0 && head < database.length) {
|
||||||
@@ -133,12 +132,12 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
|||||||
int statusCode = 0;
|
int statusCode = 0;
|
||||||
string file = os_readEntireFile(arena, LOG_FILE_LOCATION);
|
string file = os_readEntireFile(arena, LOG_FILE_LOCATION);
|
||||||
|
|
||||||
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
|
|
||||||
|
|
||||||
if (file.length % sizeof(GymLogEntry) != 0) {
|
if (file.length % sizeof(GymLogEntry) != 0) {
|
||||||
puts("Log file corrupted.");
|
puts("Log file corrupted.");
|
||||||
statusCode = 1;
|
statusCode = 1;
|
||||||
} else {
|
} else {
|
||||||
|
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
|
||||||
|
|
||||||
Timestamp startTs = {0};
|
Timestamp startTs = {0};
|
||||||
int numDays = 1;
|
int numDays = 1;
|
||||||
bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s);
|
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
|
// Syntax: do <exercise-name> weightKg reps
|
||||||
int gymTrackerDo(Arena *arena, list<string> args) {
|
int gymTrackerDo(Arena *arena, list<string> args) {
|
||||||
int statusCode = 0;
|
int statusCode = 0;
|
||||||
string newExerciseName = {0};
|
string exerciseName = {0};
|
||||||
if (args.length < 3 || args.data[0].length == 0) {
|
if (args.length < 3 || args.data[0].length == 0) {
|
||||||
log("Invalid exercise name and/or number of arguments.\n");
|
log("Invalid exercise name and/or number of arguments.\n");
|
||||||
statusCode = 1;
|
statusCode = 1;
|
||||||
} else {
|
} else {
|
||||||
newExerciseName = args.data[0];
|
exerciseName = args.data[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
GymLogDbParsedEntry *existingEntry = 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));
|
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
|
||||||
for (EachIn(db->entries, i)) {
|
for (EachIn(db->entries, i)) {
|
||||||
GymLogDbParsedEntry entry = db->entries.data[i];
|
GymLogDbParsedEntry entry = db->entries.data[i];
|
||||||
if (strStartsWith(entry.name, newExerciseName)) {
|
if (strStartsWith(entry.name, exerciseName)) {
|
||||||
existingEntry = &entry;
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!existingEntry) {
|
if (!existingEntry) {
|
||||||
log("The exercise \"%S\" hasn't been registered.", newExerciseName);
|
log("The exercise \"%S\" hasn't been registered.", exerciseName);
|
||||||
statusCode = 1;
|
statusCode = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -328,7 +330,7 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));
|
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 gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||||
int statusCode = 0;
|
int statusCode = 0;
|
||||||
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));
|
|
||||||
|
|
||||||
string newExerciseName = args.data[0];
|
string newExerciseName = args.data[0];
|
||||||
if (newExerciseName.length == 0) {
|
if (newExerciseName.length == 0) {
|
||||||
@@ -362,8 +363,7 @@ int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (statusCode != 1) {
|
if (statusCode != 1) {
|
||||||
string databaseLocation = DB_FILE_LOCATION;
|
string database = os_readEntireFile(arena, DB_FILE_LOCATION);
|
||||||
string database = os_readEntireFile(arena, databaseLocation);
|
|
||||||
|
|
||||||
byte *buf = 0;
|
byte *buf = 0;
|
||||||
size_t newEntryStartIndex = 0;
|
size_t newEntryStartIndex = 0;
|
||||||
@@ -411,7 +411,8 @@ int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
|||||||
byte *newExerciseNameDb = buf + newEntryStartIndex + sizeof(GymLogDbEntry);
|
byte *newExerciseNameDb = buf + newEntryStartIndex + sizeof(GymLogDbEntry);
|
||||||
memcpy(newExerciseNameDb, newExerciseName.str, newExerciseName.length);
|
memcpy(newExerciseNameDb, newExerciseName.str, newExerciseName.length);
|
||||||
size_t bufSize = newEntryStartIndex + sizeof(GymLogDbEntry) + 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) {
|
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;
|
string readBuffer;
|
||||||
if (input) {
|
if (input) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
@@ -35,34 +37,45 @@ string os_readEntireFile(Arena *arena, string filename) {
|
|||||||
} else {
|
} else {
|
||||||
readBuffer = PushString(arena, 0);
|
readBuffer = PushString(arena, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scratchEnd(temp);
|
||||||
return readBuffer;
|
return readBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
|
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
|
||||||
bool result = false;
|
Scratch temp = scratchStart(&arena, 1);
|
||||||
FILE *output = fopen((char *)filename.str, "w");
|
|
||||||
if (output) {
|
|
||||||
fwrite(contents, contentsLength, contentsLength, output);
|
|
||||||
fclose(output);
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) {
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
FILE *output = fopen((char *)filename.str, "a");
|
FILE *output = fopen(cstring(temp.arena, filename), "w");
|
||||||
if (output) {
|
if (output) {
|
||||||
fwrite(contents, sizeof(byte), contentsLength, output);
|
fwrite(contents, sizeof(byte), contentsLength, output);
|
||||||
fclose(output);
|
fclose(output);
|
||||||
result = true;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void os_log(LogTarget target, const char *fmt, va_list argList) {
|
void os_log(LogTarget target, const char *fmt, va_list argList) {
|
||||||
Scratch scratch = scratchStart(0, 0);
|
Scratch temp = scratchStart(0, 0);
|
||||||
string result = strPrintfv(scratch.arena, fmt, argList);
|
|
||||||
|
string result = strPrintfv(temp.arena, fmt, argList);
|
||||||
// TODO(djledda): finish implementation without cstdlib
|
// TODO(djledda): finish implementation without cstdlib
|
||||||
switch (target) {
|
switch (target) {
|
||||||
case LogTarget_stdin:
|
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);
|
write(1, (const void *)result.str, result.length);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
scratchEnd(scratch);
|
|
||||||
|
scratchEnd(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user