Compare commits
3 Commits
0fcbb4154b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d676c50961 | ||
| 49ce94a6ab | |||
| 6932a7a142 |
18
core.c
18
core.c
@@ -221,7 +221,7 @@ bool strContains(string str, string search) {
|
||||
}
|
||||
|
||||
string NUMERIC_CHARS = s("0123456789");
|
||||
inline function bool isNumeric(char c) {
|
||||
inline static bool isNumeric(char c) {
|
||||
return strContainsChar(NUMERIC_CHARS, c);
|
||||
}
|
||||
|
||||
@@ -352,7 +352,7 @@ Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp) {
|
||||
}
|
||||
|
||||
string formatTimeHmsUnix(Arena *arena, UnixTimestamp time) {
|
||||
local_persist const string format = s("HH-MM-SS");
|
||||
static const string format = s("HH-MM-SS");
|
||||
string buf = PushString(arena, format.length);
|
||||
struct tm *timestamp = gmtime((time_t *)&time);
|
||||
strftime(buf.str, buf.length + 1, "%T", timestamp);
|
||||
@@ -360,14 +360,14 @@ string formatTimeHmsUnix(Arena *arena, UnixTimestamp time) {
|
||||
}
|
||||
|
||||
string formatTimeHms(Arena *arena, Timestamp *time) {
|
||||
local_persist const string format = s("HH-MM-SS");
|
||||
static const string format = s("HH-MM-SS");
|
||||
string buf = PushString(arena, format.length);
|
||||
strftime(buf.str, buf.length + 1, "%T", (struct tm *)time);
|
||||
return buf;
|
||||
}
|
||||
|
||||
string formatTimeYmdUnix(Arena *arena, UnixTimestamp time) {
|
||||
local_persist const string format = s("YYYY-mm-dd");
|
||||
static const string format = s("YYYY-mm-dd");
|
||||
string buf = PushString(arena, format.length);
|
||||
struct tm *timestamp = gmtime((time_t *)&time);
|
||||
strftime(buf.str, buf.length + 1, "%Y-%m-%d", timestamp);
|
||||
@@ -375,34 +375,34 @@ string formatTimeYmdUnix(Arena *arena, UnixTimestamp time) {
|
||||
}
|
||||
|
||||
string formatTimeYmd(Arena *arena, Timestamp *time) {
|
||||
local_persist const string format = s("YYYY-mm-dd");
|
||||
static const string format = s("YYYY-mm-dd");
|
||||
string buf = PushString(arena, format.length);
|
||||
strftime(buf.str, buf.length + 1, "%Y-%m-%d", (struct tm *)time);
|
||||
return buf;
|
||||
}
|
||||
|
||||
function void printStderr(const char *fmt, ...) {
|
||||
static void printStderr(const char *fmt, ...) {
|
||||
va_list argList;
|
||||
va_start(argList, fmt);
|
||||
os_print(StdStream_stdout, fmt, argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
function void printlnStderr(const char *fmt, ...) {
|
||||
static void printlnStderr(const char *fmt, ...) {
|
||||
va_list argList;
|
||||
va_start(argList, fmt);
|
||||
os_println(StdStream_stdout, fmt, argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
function void printStdout(const char *fmt, ...) {
|
||||
static void printStdout(const char *fmt, ...) {
|
||||
va_list argList;
|
||||
va_start(argList, fmt);
|
||||
os_print(StdStream_stdout, fmt, argList);
|
||||
va_end(argList);
|
||||
}
|
||||
|
||||
function void printlnStdout(const char *fmt, ...) {
|
||||
static void printlnStdout(const char *fmt, ...) {
|
||||
va_list argList;
|
||||
va_start(argList, fmt);
|
||||
os_println(StdStream_stdout, fmt, argList);
|
||||
|
||||
21
core.h
21
core.h
@@ -14,9 +14,6 @@
|
||||
#define Assert(expression)
|
||||
#endif
|
||||
|
||||
#define function static
|
||||
#define global static
|
||||
#define local_persist static
|
||||
#define Forever for (;;)
|
||||
|
||||
#define DeferLoop(begin_stmnt, end_stmnt) for(int __defer_i = ((begin_stmnt), 0); __defer_i < 1; (++__defer_i, (end_stmnt)))
|
||||
@@ -91,7 +88,7 @@ union Vec2 {
|
||||
};
|
||||
real32 vec[2];
|
||||
};
|
||||
inline function Vec2 vec2(real32 x, real32 y) {
|
||||
inline Vec2 vec2(real32 x, real32 y) {
|
||||
Vec2 result = {0};
|
||||
result.x = x;
|
||||
result.y = y;
|
||||
@@ -107,7 +104,7 @@ union Vec3 {
|
||||
};
|
||||
real32 vec[3];
|
||||
};
|
||||
inline function Vec3 vec3(real32 x, real32 y, real32 z) {
|
||||
inline Vec3 vec3(real32 x, real32 y, real32 z) {
|
||||
Vec3 result = {0};
|
||||
result.x = x;
|
||||
result.y = y;
|
||||
@@ -131,7 +128,7 @@ union Vec4 {
|
||||
};
|
||||
real32 vec[4];
|
||||
};
|
||||
inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
|
||||
inline Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
|
||||
Vec4 result = {0};
|
||||
result.x = x;
|
||||
result.y = y;
|
||||
@@ -169,11 +166,11 @@ void *__listcopyhelper__;
|
||||
if ((list).length < (list).capacity) { \
|
||||
(list).data[(list).length++] = (element); \
|
||||
}
|
||||
#define ZeroListFull(list) memset((list)->data, 0, (list)->length * sizeof((list)->data[0]))
|
||||
#define ZeroList(list) (list)->length = 0; \
|
||||
memset((list)->data, 0, (list)->length * sizeof((list)->data[0]))
|
||||
/** 1 indicates last element, 2 second last, etc. */
|
||||
#define ListGetFromEnd(list, count) ((list).data[(list).length - (count)])
|
||||
|
||||
|
||||
#define ZeroList(list) /** Keeps the list's length and sets all memory to 0 */ memset((list).data, 0, (list).length * sizeof((list).data[0]))
|
||||
#define ClearList(list) /** Clears the list by setting all memory and the length to 0 */ (list).length = 0; memset((list).data, 0, (list).length * sizeof((list).data[0]))
|
||||
#define ListGetFromEnd(list, count) /** 1 indicates last element, 2 second last, etc. */ ((list).data[(list).length - (count)])
|
||||
#define ListLast(list) ListGetFromEnd(list, 1)
|
||||
|
||||
inline VoidList __cloneList(Arena *arena, VoidList list, size_t underlyingSize) {
|
||||
@@ -233,6 +230,7 @@ string strReverse(Arena *arena, string str);
|
||||
string strSlice(string str, size_t start, size_t stop);
|
||||
string strChopStart(string str, size_t start);
|
||||
string strSliceCStr(char *data, size_t start, size_t stop);
|
||||
inline string strChopStart(string str, size_t start);
|
||||
StringList strSplit(Arena *arena, string splitStr, string inputStr);
|
||||
string strPrintfv(Arena *arena, const char *fmt, va_list args);
|
||||
string strPrintf(Arena *arena, const char *fmt, ...);
|
||||
@@ -339,6 +337,7 @@ extern void (*println)(const char *fmt, ...);
|
||||
#define EachIn(list, it) size_t it = 0; it < (list).length; it++
|
||||
#define EachEl(list, type, element) type *element = (list).data; element < (list).data + (list).length; element += 1
|
||||
#define EachInReversed(list, it) size_t it = (list).length - 1; it >= 0 && it < (list).length; it--
|
||||
#define EachElReversed(list, type, element) type *element = (list).data + (list).length - 1; element >= (list).data; element -= 1
|
||||
#define EachInArray(arr, it) size_t it = 0; it < ArrayCount(arr); ++it
|
||||
|
||||
// ### Misc ###
|
||||
|
||||
4
os.h
4
os.h
@@ -11,8 +11,8 @@ void os_free(void *ptr, uint64 freeSize);
|
||||
|
||||
// ### File IO ###
|
||||
string os_readEntireFile(Arena *arena, string filename);
|
||||
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
|
||||
bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 contentsLength);
|
||||
bool os_writeEntireFile(string filename, const byte *contents, uint64 contentsLength);
|
||||
bool os_fileAppend(string filename, const byte *contents, uint64 contentsLength);
|
||||
|
||||
// ### Standard IO ###
|
||||
void os_print(StdStream target, const char *fmt, va_list argList);
|
||||
|
||||
20
os_linux.c
20
os_linux.c
@@ -46,7 +46,7 @@ string os_readEntireFile(Arena *arena, string filename) {
|
||||
close(input);
|
||||
if (bytesRead == -1) {
|
||||
arenaPopTo(arena, readBuffer.str);
|
||||
return s("");
|
||||
readBuffer = s("");
|
||||
}
|
||||
} else {
|
||||
readBuffer = s("");
|
||||
@@ -56,35 +56,37 @@ string os_readEntireFile(Arena *arena, string filename) {
|
||||
return readBuffer;
|
||||
}
|
||||
|
||||
bool os_writeEntireFile(Arena *arena, string filename, const byte *contents, uint64 contentsLength) {
|
||||
Scratch temp = scratchStart(&arena, 1);
|
||||
bool os_writeEntireFile(string filename, const byte *contents, uint64 contentsLength) {
|
||||
Scratch temp = scratchStart(NULL, 0);
|
||||
|
||||
bool result = false;
|
||||
int output = open(cstring(temp.arena, filename), O_WRONLY);
|
||||
struct stat st;
|
||||
stat((char *)filename.str, &st);
|
||||
int output = open(cstring(temp.arena, filename), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
||||
if (output) {
|
||||
int64 bytesWritten = write(output, contents, contentsLength);
|
||||
if (bytesWritten != -1) {
|
||||
result = true;
|
||||
}
|
||||
close(output);
|
||||
}
|
||||
close(output);
|
||||
|
||||
scratchEnd(temp);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool os_fileAppend(Arena *arena, string filename, const byte *contents, uint64 contentsLength) {
|
||||
Scratch temp = scratchStart(&arena, 1);
|
||||
bool os_fileAppend(string filename, const byte *contents, uint64 contentsLength) {
|
||||
Scratch temp = scratchStart(NULL, 0);
|
||||
|
||||
bool result = false;
|
||||
int output = open(cstring(temp.arena, filename), O_APPEND);
|
||||
int output = open(cstring(temp.arena, filename), O_WRONLY | O_APPEND);
|
||||
if (output) {
|
||||
int bytesWritten = write(output, contents, contentsLength);
|
||||
if (bytesWritten != -1) {
|
||||
result = true;
|
||||
}
|
||||
close(output);
|
||||
}
|
||||
close(output);
|
||||
|
||||
scratchEnd(temp);
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user