diff --git a/app.cpp b/app.cpp index e93df66..07d8729 100644 --- a/app.cpp +++ b/app.cpp @@ -62,7 +62,7 @@ GymLogDbParsed *parseDb(Arena *arena, string database) { list loadEntryLog(Arena *arena, string fileLocation) { list result = {0}; - string logfile = readEntireFile(arena, LOG_FILE_LOCATION); + string logfile = os_readEntireFile(arena, LOG_FILE_LOCATION); if (logfile.length % sizeof(GymLogEntry) != 0) { log("Log file corrupted.\n"); @@ -96,7 +96,7 @@ WorkSummary workSummaryForExercise(list entries) { int gymTrackerWorkToday(Arena *arena, uint32 exerciseId, string exerciseName) { int statusCode = 0; - string logfile = readEntireFile(arena, LOG_FILE_LOCATION); + string logfile = os_readEntireFile(arena, LOG_FILE_LOCATION); if (logfile.length % sizeof(GymLogEntry) != 0) { log("Log file corrupted.\n"); @@ -114,7 +114,7 @@ int gymTrackerWorkToday(Arena *arena, uint32 exerciseId, string exerciseName) { Timestamp logTs = timestampFromUnixTime(&logEntry.timestamp); if (logTs.tm_yday == todayTs.tm_yday && todayTs.tm_year == logTs.tm_year) { todaysEntries.head += 1; - todaysEntries.data = &logEntries.data[i + 1]; + todaysEntries.data = &logEntries.data[i]; } } @@ -131,9 +131,9 @@ int gymTrackerWorkToday(Arena *arena, uint32 exerciseId, string exerciseName) { int gymTrackerStatus(Arena *arena, list args) { int statusCode = 0; - string file = readEntireFile(arena, LOG_FILE_LOCATION); + string file = os_readEntireFile(arena, LOG_FILE_LOCATION); - GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION)); + GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION)); if (file.length % sizeof(GymLogEntry) != 0) { puts("Log file corrupted."); @@ -271,7 +271,7 @@ int gymTrackerDeleteEntries(Arena *arena, list args) { log("%i is more than the current number of log entries (%i). Aborting.", numToDelete, logEntries.length); statusCode = 1; } else { - writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDelete) * sizeof(GymLogEntry)); + os_writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDelete) * sizeof(GymLogEntry)); } } else { log("Invalid number to delete.\n"); @@ -296,7 +296,7 @@ int gymTrackerDo(Arena *arena, list args) { GymLogDbParsedEntry *existingEntry = 0; if (statusCode == 0) { - GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION)); + 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)) { @@ -327,7 +327,7 @@ int gymTrackerDo(Arena *arena, list args) { kg, }; - fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry)); + os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry)); statusCode = gymTrackerWorkToday(arena, exerciseId, newExerciseName); } } @@ -337,7 +337,7 @@ int gymTrackerDo(Arena *arena, list args) { int gymTrackerListExercises(Arena *arena, list args) { int statusCode = 0; - GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION)); + GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION)); if (db->entries.length == 0) { log("No entries currently registered in the exercise database."); @@ -353,7 +353,7 @@ int gymTrackerListExercises(Arena *arena, list args) { int gymTrackerAddExercise(Arena *arena, list args) { int statusCode = 0; - GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION)); + GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION)); string newExerciseName = args.data[0]; if (newExerciseName.length == 0) { @@ -363,7 +363,7 @@ int gymTrackerAddExercise(Arena *arena, list args) { if (statusCode != 1) { string databaseLocation = DB_FILE_LOCATION; - string database = readEntireFile(arena, databaseLocation); + string database = os_readEntireFile(arena, databaseLocation); byte *buf = 0; size_t newEntryStartIndex = 0; @@ -411,7 +411,7 @@ int gymTrackerAddExercise(Arena *arena, list args) { 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); + os_writeEntireFile(arena, databaseLocation, buf, bufSize); } } @@ -430,16 +430,19 @@ int main(int argc, char **argv) { } 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], "delete"_s)) { - statusCode = gymTrackerDeleteEntries(arena, listSlice(args, 1)); - } else if (strEql(args.data[0], "list"_s)) { - statusCode = gymTrackerListExercises(arena, listSlice(args, 1)); - } else if (strEql(args.data[0], "add"_s)) { - statusCode = gymTrackerAddExercise(arena, listSlice(args, 1)); + string cmd = args.data[0]; + list argsRest = listSlice(args, 1); + + if (strEql("status"_s, cmd)) { + statusCode = gymTrackerStatus(arena, argsRest); + } else if (strEql("do"_s, cmd)) { + statusCode = gymTrackerDo(arena, argsRest); + } else if (strEql("delete"_s, cmd)) { + statusCode = gymTrackerDeleteEntries(arena, argsRest); + } else if (strEql("list"_s, cmd)) { + statusCode = gymTrackerListExercises(arena, argsRest); + } else if (strEql("add"_s, cmd)) { + statusCode = gymTrackerAddExercise(arena, argsRest); } else { log("Unknown command \"%S\"\n", args.data[0]); statusCode = 1; diff --git a/build b/build index 2c9057c..876340a 100755 --- a/build +++ b/build @@ -1,3 +1,3 @@ #!/bin/bash -g++ -g -g3 -DOS_LINUX ./app.cpp -o ./target/app +g++ -Wall -g -g3 -DOS_LINUX ./app.cpp -o ./target/app diff --git a/djstdlib/core.cpp b/djstdlib/core.cpp index 12956e9..c08b51b 100644 --- a/djstdlib/core.cpp +++ b/djstdlib/core.cpp @@ -328,96 +328,14 @@ real32 parsePositiveReal32(string str, size_t *lengthPointer) { return result; } -string readEntireFile(Arena *arena, string filename) { -#if OS_WINDOWS - string result = {0}; - HANDLE fileHandle = CreateFileA(cstring(arena, filename), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); - if (fileHandle != INVALID_HANDLE_VALUE) { - LARGE_INTEGER fileSize; - if (GetFileSizeEx(fileHandle, &fileSize)) { - string readfile = PushString(arena, (size_t)fileSize.QuadPart); - if (readfile.str) { - DWORD bytesRead; - if (ReadFile(fileHandle, readfile.str, (DWORD)fileSize.QuadPart, &bytesRead, NULL) && (fileSize.QuadPart == bytesRead)) { - result = readfile; - } - } - } - CloseHandle(fileHandle); - } - return result; -#elif OS_LINUX - FILE *input = fopen((char *)filename.str, "r"); - string readBuffer; - if (input) { - struct stat st; - stat((char *)filename.str, &st); - size_t fsize = st.st_size; - readBuffer = PushString(arena, fsize); - fread(readBuffer.str, sizeof(byte), readBuffer.length, input); - fclose(input); - } else { - readBuffer = PushString(arena, 0); - } - return readBuffer; -#endif -} - -bool writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) { - bool result = false; -#if OS_WINDOWS - HANDLE fileHandle = CreateFileA(cstring(arena, filename), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL); - if (fileHandle != INVALID_HANDLE_VALUE) { - DWORD bytesWritten; - if (WriteFile(fileHandle, contents, (DWORD)contentsLength, &bytesWritten, NULL)) { - // file written successfully - result = bytesWritten == contentsLength; - } - CloseHandle(fileHandle); - } -#elif OS_LINUX - FILE *output = fopen((char *)filename.str, "w"); - if (output) { - fwrite(contents, contentsLength, contentsLength, output); - fclose(output); - result = true; - } -#endif - return result; -} - -bool fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) { - bool result = false; -#if OS_WINDOWS - HANDLE fileHandle = CreateFileA(cstring(arena, filename), FILE_APPEND_DATA | FILE_GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (fileHandle != INVALID_HANDLE_VALUE) { - DWORD bytesWritten; - DWORD position = SetFilePointer(fileHandle, 0, NULL, FILE_END); - if (WriteFile(fileHandle, contents, (DWORD)contentsLength, &bytesWritten, NULL)) { - // file written successfully - result = bytesWritten == contentsLength; - } - CloseHandle(fileHandle); - } -#elif OS_LINUX - FILE *output = fopen((char *)filename.str, "a"); - if (output) { - fwrite(contents, contentsLength, contentsLength, output); - fclose(output); - result = true; - } -#endif - return result; -} - list getArgs(Arena *arena, int argc, char **argv) { - list args = PushList(arena, string, (size_t)argc); + list args = PushList(arena, string, (size_t)argc - 1); for (int i = 1; i < argc; i++) { appendList(&args, strFromCString(arena, argv[i])); } return args; } - + UnixTimestamp getSystemUnixTime() { time_t now; time(&now); @@ -425,8 +343,9 @@ UnixTimestamp getSystemUnixTime() { } Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp) { - tm *timestamp = gmtime((time_t *)unixTimestamp); - return *timestamp; + tm timestamp = {0}; + gmtime_r((time_t *)unixTimestamp, ×tamp); + return timestamp; } string formatTimeHms(Arena *arena, UnixTimestamp time) { @@ -459,65 +378,24 @@ string formatTimeYmd(Arena *arena, Timestamp *time) { return buf; } -function void __core_log(LogTarget target, const char *fmt, va_list argList) { - Scratch scratch = scratchStart(0, 0); - string result = strPrintfv(scratch.arena, fmt, argList); -#if OS_WINDOWS - DWORD done; - HANDLE stdHandle; - switch (target) { - case LogTarget_stdin: - stdHandle = GetStdHandle(STD_INPUT_HANDLE); - break; - case LogTarget_stdout: - stdHandle = GetStdHandle(STD_ERROR_HANDLE); - break; - case LogTarget_stderr: - stdHandle = GetStdHandle(STD_OUTPUT_HANDLE); - break; - default: - stdHandle = GetStdHandle(STD_OUTPUT_HANDLE); - break; - } - WriteFile(stdHandle, result.str, (DWORD)result.length, &done, 0); -#elif OS_LINUX - // TODO(djledda): finish implementation without cstdlib - switch (target) { - case LogTarget_stdin: - write(0, (const void *)result.str, result.length); - break; - case LogTarget_stderr: - fflush(stderr); - write(2, (const void *)result.str, result.length); - break; - case LogTarget_stdout: - default: - fflush(stdout); - write(1, (const void *)result.str, result.length); - break; - } -#endif - scratchEnd(scratch); -} - void logErr(const char *fmt, ...) { va_list argList; va_start(argList, fmt); - __core_log(LogTarget_stdout, fmt, argList); + os_log(LogTarget_stdout, fmt, argList); va_end(argList); } function void logStdout(const char *fmt, ...) { va_list argList; va_start(argList, fmt); - __core_log(LogTarget_stdout, fmt, argList); + os_log(LogTarget_stdout, fmt, argList); va_end(argList); } void log(const char *fmt, ...) { va_list argList; va_start(argList, fmt); - __core_log(LogTarget_stdout, fmt, argList); + os_log(LogTarget_stdout, fmt, argList); va_end(argList); } diff --git a/djstdlib/core.h b/djstdlib/core.h index 3654df1..2889d13 100644 --- a/djstdlib/core.h +++ b/djstdlib/core.h @@ -181,11 +181,6 @@ real32 parsePositiveReal32(Arena *arena, string str, size_t *lengthPointer); inline function bool isNumeric(char c); -// ### File IO ### -string readEntireFile(Arena *arena, string filename); -bool writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength); -bool fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength); - // ### Cmdline ### list getArgs(Arena *arena, int argc, char **argv); diff --git a/djstdlib/os.h b/djstdlib/os.h index 44aac28..07b654e 100644 --- a/djstdlib/os.h +++ b/djstdlib/os.h @@ -9,4 +9,12 @@ void os_reserve(void *ptr); void os_decommit(void *ptr); void os_free(void *ptr, size_t freeSize); +// ### File IO ### +string os_readEntireFile(Arena *arena, string filename); +bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength); +bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength); + +// ### Standard IO ### +void os_log(LogTarget target, const char *fmt, va_list argList); + #endif diff --git a/djstdlib/os_linux.cpp b/djstdlib/os_linux.cpp index 01f9330..3add835 100644 --- a/djstdlib/os_linux.cpp +++ b/djstdlib/os_linux.cpp @@ -22,4 +22,63 @@ void os_free(void *ptr, size_t size) { Assert(err != -1); } +string os_readEntireFile(Arena *arena, string filename) { + FILE *input = fopen((char *)filename.str, "r"); + string readBuffer; + if (input) { + struct stat st; + stat((char *)filename.str, &st); + size_t fsize = st.st_size; + readBuffer = PushString(arena, fsize); + fread(readBuffer.str, sizeof(byte), readBuffer.length, input); + fclose(input); + } else { + readBuffer = PushString(arena, 0); + } + 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; +} + +bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) { + bool result = false; + FILE *output = fopen((char *)filename.str, "a"); + if (output) { + fwrite(contents, sizeof(byte), contentsLength, output); + fclose(output); + result = true; + } + 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); + // TODO(djledda): finish implementation without cstdlib + switch (target) { + case LogTarget_stdin: + write(0, (const void *)result.str, result.length); + break; + case LogTarget_stderr: + fflush(stderr); + write(2, (const void *)result.str, result.length); + break; + case LogTarget_stdout: + default: + fflush(stdout); + write(1, (const void *)result.str, result.length); + break; + } + scratchEnd(scratch); +} + #endif diff --git a/djstdlib/os_win32.cpp b/djstdlib/os_win32.cpp index a255c8a..70ba555 100644 --- a/djstdlib/os_win32.cpp +++ b/djstdlib/os_win32.cpp @@ -18,4 +18,75 @@ void os_free(void *ptr, size_t size) { VirtualFree(ptr, NULL, MEM_RELEASE); } +string os_readEntireFile(Arena *arena, string filename) { + string result = {0}; + HANDLE fileHandle = CreateFileA(cstring(arena, filename), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); + if (fileHandle != INVALID_HANDLE_VALUE) { + LARGE_INTEGER fileSize; + if (GetFileSizeEx(fileHandle, &fileSize)) { + string readfile = PushString(arena, (size_t)fileSize.QuadPart); + if (readfile.str) { + DWORD bytesRead; + if (ReadFile(fileHandle, readfile.str, (DWORD)fileSize.QuadPart, &bytesRead, NULL) && (fileSize.QuadPart == bytesRead)) { + result = readfile; + } + } + } + CloseHandle(fileHandle); + } + return result; +} + +bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength) { + bool result = false; + HANDLE fileHandle = CreateFileA(cstring(arena, filename), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, NULL, NULL); + if (fileHandle != INVALID_HANDLE_VALUE) { + DWORD bytesWritten; + if (WriteFile(fileHandle, contents, (DWORD)contentsLength, &bytesWritten, NULL)) { + // file written successfully + result = bytesWritten == contentsLength; + } + CloseHandle(fileHandle); + } + return result; +} + +bool os_fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength) { + bool result = false; + HANDLE fileHandle = CreateFileA(cstring(arena, filename), FILE_APPEND_DATA | FILE_GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (fileHandle != INVALID_HANDLE_VALUE) { + DWORD bytesWritten; + DWORD position = SetFilePointer(fileHandle, 0, NULL, FILE_END); + if (WriteFile(fileHandle, contents, (DWORD)contentsLength, &bytesWritten, NULL)) { + // file written successfully + result = bytesWritten == contentsLength; + } + CloseHandle(fileHandle); + } + return result; +} + +function void os_log(LogTarget target, const char *fmt, va_list argList) { + Scratch scratch = scratchStart(0, 0); + string result = strPrintfv(scratch.arena, fmt, argList); + DWORD done; + HANDLE stdHandle; + switch (target) { + case LogTarget_stdin: + stdHandle = GetStdHandle(STD_INPUT_HANDLE); + break; + case LogTarget_stdout: + stdHandle = GetStdHandle(STD_ERROR_HANDLE); + break; + case LogTarget_stderr: + stdHandle = GetStdHandle(STD_OUTPUT_HANDLE); + break; + default: + stdHandle = GetStdHandle(STD_OUTPUT_HANDLE); + break; + } + WriteFile(stdHandle, result.str, (DWORD)result.length, &done, 0); + scratchEnd(scratch); +} + #endif