Browse Source

update

c-only
Daniel Ledda 3 weeks ago
parent
commit
768424e199
2 changed files with 68 additions and 98 deletions
  1. +10
    -39
      core.cpp
  2. +58
    -59
      core.h

+ 10
- 39
core.cpp View File

@@ -78,36 +78,7 @@ void scratchEnd(Scratch scratch) {
arenaFreeFrom(scratch.arena, scratch.start);
}

template <typename T>
T *appendList(list<T> *list, T element) {
if (list->head < list->length) {
list->data[list->head] = element;
list->head++;
return &(list->data[list->head - 1]);
} else {
return 0;
}
}

template <typename T>
void zeroListFull(list<T> *list) {
memset(list->data, 0, list->head * sizeof(T));
}

template <typename T>
void zeroList(list<T> *list) {
list->head = 0;
memset(list->data, 0, list->head * sizeof(T));
}

inline string operator""_s(const char *cstrLiteral, size_t length) {
return {
(char *)cstrLiteral,
length,
};
}

const char *cstring(Arena *arena, list<char> buf) {
const char *cstring(Arena *arena, CharList buf) {
char *arr = PushArray(arena, char, buf.length + 1);
memmove(arr, buf.data, buf.length);
arr[buf.length] = '\0';
@@ -245,8 +216,8 @@ inline bool isNumeric(char c) {
return stringContains(NUMERIC_CHARS, c);
}

list<string> strSplit(Arena *arena, string splitStr, string inputStr) {
list<string> result = {0};
StringList strSplit(Arena *arena, string splitStr, string inputStr) {
StringList result = {0};
if (inputStr.length > 0) {
size_t splitCount = 0;
size_t c = 0;
@@ -333,10 +304,10 @@ ParsePositiveReal32Result parsePositiveReal32(string str, size_t *lengthPointer)
return result;
}

list<string> getArgs(Arena *arena, int argc, char **argv) {
list<string> args = PushList(arena, string, (size_t)argc - 1);
StringList getArgs(Arena *arena, int argc, char **argv) {
StringList args = PushList(arena, StringList, (size_t)argc - 1);
for (int i = 1; i < argc; i++) {
appendList(&args, strFromCString(arena, argv[i]));
AppendList(string, &args, strFromCString(arena, argv[i]));
}
return args;
}
@@ -354,7 +325,7 @@ Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp) {
}

string formatTimeHms(Arena *arena, UnixTimestamp time) {
local_persist const string format = "HH-MM-SS"_s;
local_persist const string format = strlit("HH-MM-SS");
string buf = PushString(arena, format.length);
tm *timestamp = gmtime((time_t *)&time);
strftime(buf.str, buf.length + 1, "%T", timestamp);
@@ -362,14 +333,14 @@ string formatTimeHms(Arena *arena, UnixTimestamp time) {
}

string formatTimeHms(Arena *arena, Timestamp *time) {
local_persist const string format = "HH-MM-SS"_s;
local_persist const string format = s("HH-MM-SS");
string buf = PushString(arena, format.length);
strftime(buf.str, buf.length + 1, "%T", (tm *)time);
return buf;
}

string formatTimeYmd(Arena *arena, UnixTimestamp time) {
local_persist const string format = "YYYY-mm-dd"_s;
local_persist const string format = s("YYYY-mm-dd");
string buf = PushString(arena, format.length);
tm *timestamp = gmtime((time_t *)&time);
strftime(buf.str, buf.length + 1, "%Y-%m-%d", timestamp);
@@ -377,7 +348,7 @@ string formatTimeYmd(Arena *arena, UnixTimestamp time) {
}

string formatTimeYmd(Arena *arena, Timestamp *time) {
local_persist const string format = "YYYY-mm-dd"_s;
local_persist const string format = s("YYYY-mm-dd");
string buf = PushString(arena, format.length);
strftime(buf.str, buf.length + 1, "%Y-%m-%d", (tm *)time);
return buf;


+ 58
- 59
core.h View File

@@ -2,6 +2,7 @@
#define CORE_H

// cstdlib includes
#include <cwchar>
#include <math.h>
#include <stdint.h> // necessary for int type sizes
#include <stdio.h>
@@ -30,6 +31,7 @@ typedef uint64_t uint64;
typedef uint8_t byte;
typedef float real32;
typedef double real64;
typedef struct string string;

// ### Sizes and Numbers ###
#define Bytes(n) (n)
@@ -74,59 +76,53 @@ void scratchEnd(Scratch scratch);
#define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0)

// ### Vectors ###
template <typename T>
union Vector2 {
union Vec2 {
struct {
T x;
T y;
real32 x;
real32 y;
};
T vec[2];
real32 vec[2];
};
template <typename T>
inline function Vector2<T> vec2(T x, T y) {
Vector2<T> result = {0};
inline function Vec2 vec2(real32 x, real32 y) {
Vec2 result = {0};
result.x = x;
result.y = y;
return result;
}

template <typename T>
union Vector3 {
union Vec3 {
struct {
T x;
T y;
T z;
real32 x;
real32 y;
real32 z;
};
T vec[3];
real32 vec[3];
};
template <typename T>
inline function Vector3<T> vec3(T x, T y, T z) {
Vector3<T> result = {0};
inline function Vec3 vec3(real32 x, real32 y, real32 z) {
Vec3 result = {0};
result.x = x;
result.y = y;
result.z = z;
return result;
}

template <typename T>
union Vector4 {
union Vec4 {
struct {
T r;
T g;
T b;
T a;
real32 r;
real32 g;
real32 b;
real32 a;
};
struct {
T x;
T y;
T z;
T w;
real32 x;
real32 y;
real32 z;
real32 w;
};
T vec[4];
real32 vec[4];
};
template <typename T>
inline function Vector4<T> vec4(T x, T y, T z, T w) {
Vector4<T> result = {0};
inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
Vec4 result = {0};
result.x = x;
result.y = y;
result.z = z;
@@ -135,24 +131,29 @@ inline function Vector4<T> vec4(T x, T y, T z, T w) {
}

// ### Lists ###
template <typename T>
struct list {
T* data;
size_t length;
size_t head;
};

#define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 })
#define EmptyList(type) (list<type>{ NULL, 0, 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 })
#define ArrayAsList(type, array) (list<type>{ array, ArrayCount(array), ArrayCount(array) })

template <typename T> T *appendList(list<T> *list, T element);
template <typename T> void zeroList(list<T> *list);
template <typename T> void zeroListFull(list<T> *list);
template <typename T> list<T> listSlice(list<T> l, size_t start, size_t stop = 0);
#define DefineList(type, prefix) \
typedef struct {\
type* data;\
size_t length;\
size_t head;\
} prefix ## List

DefineList(string, String);

#define PushList(arena, type, size) ((type){ PushArray(arena, type, size), size, 0 })
#define EmptyList(type) ((type){ NULL, 0, 0 })
#define PushListZero(arena, type, size) ((type){ PushArrayZero(arena, type, size), size, 0 })
#define PushFullList(arena, type, size) ((type){ PushArray(arena, type, size), size, size })
#define PushFullListZero(arena, type, size) ((type){ PushArrayZero(arena, type, size), size, size })
#define ArrayAsList(type, array) ((type){ array, ArrayCount(array), ArrayCount(array) })

#define AppendList(type, list, element) \
if ((list)->head < (list)->length) { \
(list)->data[(list)->head++] = (element); \
}
#define ZeroListFull(list) memset((list)->data, 0, (list)->head * sizeof(T))
#define ZeroList(list) (list)->head = 0; \
memset((list)->data, 0, (list)->head * sizeof(T));

// ### Strings ###
struct string {
@@ -162,13 +163,13 @@ struct string {
#define STB_SPRINTF_DECORATE(name) stb_##name // define this before including if you want to change the names
#include "vendor/stb_sprintf.h"

#define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1})
#define s(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
const char *cstring(Arena *arena, list<char> buf);
DefineList(char, Char);
const char *cstringFromCharList(Arena *arena, CharList buf);
const char *cstring(Arena *arena, string str);
size_t calcStringLen(const char *str);
string strFromCString(Arena *arena, const char *str);
@@ -180,7 +181,7 @@ bool stringContains(string str, char c);
string strReverse(Arena *arena, string str);
string strSlice(string str, size_t start, size_t stop = 0);
string strSlice(char *data, size_t start, size_t stop = 0);
list<string> strSplit(Arena *arena, string splitStr, string inputStr);
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, ...);

@@ -192,7 +193,7 @@ ParsePositiveReal32Result parsePositiveReal32(Arena *arena, string str, size_t *
inline function bool isNumeric(char c);

// ### Cmdline ###
list<string> getArgs(Arena *arena, int argc, char **argv);
StringList getArgs(Arena *arena, int argc, char **argv);

// ### Time ###
typedef uint64 UnixTimestamp;
@@ -261,11 +262,9 @@ enum StdStream {
#define COLOR_TEXT_FG_BG(text, foregroundcolor, backgroundcolor) ANSI_INSTRUCTION_FROM_ENUM(foregroundcolor) ANSI_INSTRUCTION_FROM_ENUM(backgroundcolor) text ANSI_RESET
#define COLOR_TEXT_RGB(text, red, green, blue) ANSI_INSTRUCTION_STR("38;2;" #red ";" #green ";" #blue) text ANSI_RESET

void print(list<int> l, StdStream target = StdStream_stdout);
void print(list<string> l, StdStream target = StdStream_stdout);
void print(list<Vector2<real32>> l, StdStream target = StdStream_stdout);
void print(list<Vector3<real32>> l, StdStream target = StdStream_stdout);
void print(list<Vector4<real32>> l, StdStream target = StdStream_stdout);
DefineList(int, Int);
void printIntList(IntList l, StdStream target = StdStream_stdout);
void printStrList(StringList l, StdStream target = StdStream_stdout);
void print(const char *fmt, ...);
void printErr(const char *fmt, ...);



Loading…
Cancel
Save