update
This commit is contained in:
148
core.cpp
148
core.cpp
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user