@@ -1,11 +1,20 @@ | |||
#include "os.cpp" | |||
#include <math.h> | |||
#include <string.h> | |||
#include <unistd.h> | |||
#include "core.h" | |||
#include "os.cpp" | |||
#define STB_SPRINTF_IMPLEMENTATION | |||
#include "vendor/stb_sprintf.h" | |||
void *pushSizeFill(Arena *arena, size_t bytes, byte fill) { | |||
if (arena->capacity - arena->head >= bytes) { | |||
void *ptr = (char *)arena->memory + arena->head; | |||
arena->head += bytes; | |||
memset(ptr, fill, bytes); | |||
return ptr; | |||
} | |||
return 0; | |||
} | |||
void *pushSize(Arena *arena, size_t bytes) { | |||
if (arena->capacity - arena->head >= bytes) { | |||
void *ptr = (char *)arena->memory + arena->head; | |||
@@ -112,6 +121,18 @@ const char *cstring(Arena *arena, string str) { | |||
return arr; | |||
} | |||
bool strStartsWith(string str, string testStr) { | |||
if (str.length < testStr.length) { | |||
return false; | |||
} | |||
for (size_t i = 0; i < testStr.length; i++) { | |||
if (str.str[i] != testStr.str[i]) { | |||
return false; | |||
} | |||
} | |||
return true; | |||
} | |||
bool strEql(string s1, string s2) { | |||
if (s1.length != s2.length) { | |||
return false; | |||
@@ -307,81 +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"); | |||
struct stat st; | |||
stat((char *)filename.str, &st); | |||
size_t fsize = st.st_size; | |||
string readBuffer = PushString(arena, fsize); | |||
fread(readBuffer.str, sizeof(byte), readBuffer.length, input); | |||
fclose(input); | |||
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 | |||
Assert(false); | |||
#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 | |||
Assert(false); | |||
#endif | |||
return result; | |||
} | |||
list<string> getArgs(Arena *arena, int argc, char **argv) { | |||
list<string> args = PushList(arena, string, (size_t)argc); | |||
list<string> 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); | |||
@@ -389,8 +343,9 @@ UnixTimestamp getSystemUnixTime() { | |||
} | |||
Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp) { | |||
tm *timestamp = gmtime((time_t *)&time); | |||
return *timestamp; | |||
tm timestamp = {0}; | |||
gmtime_r((time_t *)unixTimestamp, ×tamp); | |||
return timestamp; | |||
} | |||
string formatTimeHms(Arena *arena, UnixTimestamp time) { | |||
@@ -423,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); | |||
} | |||
@@ -57,6 +57,7 @@ struct Scratch { | |||
}; | |||
void *pushSize(Arena *arena, size_t bytes); | |||
void *pushSizeFill(Arena *arena, size_t bytes, byte fill); | |||
Arena *arenaAlloc(size_t capacity); | |||
void arenaFree(Arena *arena); | |||
void arenaFreeFrom(Arena *arena, size_t pos); | |||
@@ -68,7 +69,9 @@ Scratch scratchStart(Arena **conflicts, size_t conflictCount); | |||
void scratchEnd(Scratch scratch); | |||
#define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size)) | |||
#define PushArrayZero(arena, type, size) (type *)pushSizeFill(arena, sizeof(type) * (size), 0) | |||
#define PushStruct(arena, type) (type *)pushSize(arena, sizeof(type)) | |||
#define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0) | |||
// ### Vectors ### | |||
template <typename T> | |||
@@ -134,7 +137,9 @@ struct list { | |||
}; | |||
#define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 }) | |||
#define PushListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, 0 }) | |||
#define PushFullList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, size }) | |||
#define PushFullListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, size }) | |||
template <typename T> T *appendList(list<T> *list, T element); | |||
template <typename T> void zeroList(list<T> *list); | |||
@@ -151,6 +156,7 @@ struct string { | |||
#define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1}) | |||
#define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) }) | |||
#define PushStringFill(arena, length, characterByte) (string{ (char *)pushSizeFill(arena, length, characterByte), (length) }) | |||
string operator""_s(const char *cstrLiteral, size_t length); | |||
// C Strings | |||
@@ -160,6 +166,7 @@ size_t calcStringLen(const char *str); | |||
string strFromCString(Arena *arena, const char *str); | |||
bool strEql(string s1, string s2); | |||
bool strStartsWith(string str, string testStr); | |||
bool stringContains(string str, char c); | |||
string strReverse(Arena *arena, string str); | |||
@@ -174,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<string> getArgs(Arena *arena, int argc, char **argv); | |||
@@ -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 |
@@ -5,6 +5,7 @@ | |||
#include <sys/mman.h> | |||
#include <sys/stat.h> | |||
#include <unistd.h> | |||
void *os_alloc(size_t capacity) { | |||
return mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |||
@@ -21,4 +22,77 @@ void os_free(void *ptr, size_t size) { | |||
Assert(err != -1); | |||
} | |||
string os_readEntireFile(Arena *arena, string filename) { | |||
Scratch temp = scratchStart(&arena, 1); | |||
FILE *input = fopen(cstring(temp.arena, filename), "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); | |||
} | |||
scratchEnd(temp); | |||
return readBuffer; | |||
} | |||
bool os_writeEntireFile(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), "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 temp = scratchStart(0, 0); | |||
string result = strPrintfv(temp.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(temp); | |||
} | |||
#endif |
@@ -1,8 +1,8 @@ | |||
#ifndef OS_IMPL_WIN32_CPP | |||
#define OS_IMPL_WIN32_CPP | |||
#include "os.h" | |||
#include "Windows.h" | |||
#include "os.h" | |||
void *os_alloc(size_t commitSize) { | |||
return VirtualAlloc(NULL, commitSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); | |||
@@ -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 |