Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

283 lines
8.3 KiB

  1. #ifndef CORE_H
  2. #define CORE_H
  3. // cstdlib includes
  4. #include "math.h"
  5. #include "stdbool.h"
  6. #include "stdint.h" // necessary for int type sizes
  7. #include "stdio.h"
  8. #include "time.h" // TODO(djledda): try not to depend on this one
  9. // ### Misc macros ###
  10. #if ENABLE_ASSERT
  11. #define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;}
  12. #else
  13. #define Assert(expression)
  14. #endif
  15. #define function static
  16. #define global static
  17. #define local_persist static
  18. // ### Types ###
  19. typedef int8_t int8;
  20. typedef int16_t int16;
  21. typedef int32_t int32;
  22. typedef int64_t int64;
  23. typedef uint8_t uint8;
  24. typedef uint16_t uint16;
  25. typedef uint32_t uint32;
  26. typedef uint64_t uint64;
  27. typedef uint8_t byte;
  28. typedef float real32;
  29. typedef double real64;
  30. typedef struct string string;
  31. // ### Sizes and Numbers ###
  32. #define Bytes(n) (n)
  33. #define Kilobytes(n) (n << 10)
  34. #define Megabytes(n) (n << 20)
  35. #define Gigabytes(n) (((uint64)n) << 30)
  36. #define Terabytes(n) (((uint64)n) << 40)
  37. #define Thousand(n) ((n)*1000)
  38. #define Million(n) ((n)*1000000)
  39. #define Billion(n) ((n)*1000000000LL)
  40. #define ArrayCount(arr) (sizeof(arr) / sizeof((arr)[0]))
  41. #define MemberSize(type, memberName) (sizeof(((type*)0)->data))
  42. #define MemberSizeUnderlying(type, memberName) (sizeof(*((type*)0)->data))
  43. // ### Arenas ###
  44. typedef struct {
  45. void *memory;
  46. size_t capacity;
  47. size_t head;
  48. } Arena;
  49. typedef struct {
  50. Arena *arena;
  51. size_t start;
  52. } Scratch;
  53. void *pushSize(Arena *arena, size_t bytes);
  54. void *pushSizeFill(Arena *arena, size_t bytes, byte fill);
  55. Arena *arenaAlloc(size_t capacity);
  56. void arenaFree(Arena *arena);
  57. void arenaFreeFrom(Arena *arena, size_t pos);
  58. void arenaPopTo(Arena *arena, void *pos);
  59. void initialiseDjStdCore();
  60. Scratch scratchStart(Arena **conflicts, size_t conflictCount);
  61. void scratchEnd(Scratch scratch);
  62. #define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
  63. #define PushArrayZero(arena, type, size) (type *)pushSizeFill(arena, sizeof(type) * (size), 0)
  64. #define PushStruct(arena, type) (type *)pushSize(arena, sizeof(type))
  65. #define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0)
  66. // ### Vectors ###
  67. typedef union {
  68. struct {
  69. real32 x;
  70. real32 y;
  71. };
  72. real32 vec[2];
  73. } Vec2;
  74. inline function Vec2 vec2(real32 x, real32 y) {
  75. Vec2 result = {0};
  76. result.x = x;
  77. result.y = y;
  78. return result;
  79. }
  80. typedef union {
  81. struct {
  82. real32 x;
  83. real32 y;
  84. real32 z;
  85. };
  86. real32 vec[3];
  87. } Vec3;
  88. inline function Vec3 vec3(real32 x, real32 y, real32 z) {
  89. Vec3 result = {0};
  90. result.x = x;
  91. result.y = y;
  92. result.z = z;
  93. return result;
  94. }
  95. typedef union {
  96. struct {
  97. real32 r;
  98. real32 g;
  99. real32 b;
  100. real32 a;
  101. };
  102. struct {
  103. real32 x;
  104. real32 y;
  105. real32 z;
  106. real32 w;
  107. };
  108. real32 vec[4];
  109. } Vec4;
  110. inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
  111. Vec4 result = {0};
  112. result.x = x;
  113. result.y = y;
  114. result.z = z;
  115. result.w = w;
  116. return result;
  117. }
  118. // ### Lists ###
  119. #define DefineList(type, prefix) \
  120. typedef struct {\
  121. type* data;\
  122. size_t length;\
  123. size_t head;\
  124. } prefix ## List
  125. DefineList(string, String);
  126. #define PushList(arena, type, size) ((type){ pushSize(arena, MemberSizeUnderlying(type, data)), size, 0 })
  127. #define EmptyList(type) ((type){ NULL, 0, 0 })
  128. #define PushListZero(arena, type, size) ((type){ pushSizeFill(arena, MemberSizeUnderlying(type, data), 0), size, 0 })
  129. #define PushFullList(arena, type, size) ((type){ pushSize(arena, MemberSizeUnderlying(type, data)), size, size })
  130. #define PushFullListZero(arena, type, size) ((type){ pushSizeFill(arena, MemberSizeUnderlying(type, data), 0), size, size })
  131. #define ArrayAsList(type, array) ((type){ array, ArrayCount(array), ArrayCount(array) })
  132. #define AppendList(type, list, element) \
  133. if ((list)->head < (list)->length) { \
  134. (list)->data[(list)->head++] = (element); \
  135. }
  136. #define ZeroListFull(list) memset((list)->data, 0, (list)->head * sizeof(T))
  137. #define ZeroList(list) (list)->head = 0; \
  138. memset((list)->data, 0, (list)->head * sizeof(T));
  139. // ### Strings ###
  140. struct string {
  141. char *str;
  142. size_t length;
  143. };
  144. #define STB_SPRINTF_DECORATE(name) stb_##name // define this before including if you want to change the names
  145. #include "vendor/stb_sprintf.h"
  146. #define s(lit) ((string){(char *)(lit), sizeof(lit) - 1})
  147. #define PushString(arena, length) ((string){ (char *)pushSize(arena, length), (length) })
  148. #define PushStringFill(arena, length, characterByte) ((string){ (char *)pushSizeFill(arena, length, characterByte), (length) })
  149. // C Strings
  150. DefineList(char, Char);
  151. const char *cstringFromCharList(Arena *arena, CharList buf);
  152. const char *cstring(Arena *arena, string str);
  153. size_t calcStringLen(const char *str);
  154. string strFromCString(Arena *arena, const char *str);
  155. bool strEql(string s1, string s2);
  156. bool strStartsWith(string str, string testStr);
  157. bool stringContains(string str, char c);
  158. string strReverse(Arena *arena, string str);
  159. string strSlice(string str, size_t start, size_t stop);
  160. string strSliceCStr(char *data, size_t start, size_t stop);
  161. StringList strSplit(Arena *arena, string splitStr, string inputStr);
  162. string strPrintfv(Arena *arena, const char *fmt, va_list args);
  163. string strPrintf(Arena *arena, const char *fmt, ...);
  164. typedef struct { uint8 result; bool valid; } ParsePositiveIntResult;
  165. ParsePositiveIntResult parsePositiveInt(string str, size_t *lengthPointer);
  166. typedef struct { real32 result; bool valid; } ParsePositiveReal32Result;
  167. ParsePositiveReal32Result parsePositiveReal32(string str, size_t *lengthPointer);
  168. inline function bool isNumeric(char c);
  169. // ### Cmdline ###
  170. StringList getArgs(Arena *arena, int argc, char **argv);
  171. // ### Time ###
  172. typedef uint64 UnixTimestamp;
  173. typedef struct tm Timestamp;
  174. UnixTimestamp getSystemUnixTime();
  175. Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp);
  176. string formatTimeHmsUnix(Arena *arena, UnixTimestamp time);
  177. string formatTimeHms(Arena *arena, Timestamp *time);
  178. string formatTimeYmdUnix(Arena *arena, UnixTimestamp time);
  179. string formatTimeYmd(Arena *arena, Timestamp *time);
  180. // ### Linked Lists ###
  181. // TODO(djledda): implement basic linked lists (based on arenas?)
  182. // ### Logging ###
  183. typedef enum {
  184. StdStream_stdout,
  185. StdStream_stdin,
  186. StdStream_stderr,
  187. } StdStream;
  188. #define ANSI_INSTRUCTION_FROM_ENUM(ansiCodeEnum) ANSI_INSTRUCTION(ansiCodeEnum)
  189. #define ANSI_INSTRUCTION(ansiCode) "\u001b[" #ansiCode "m"
  190. #define ANSI_INSTRUCTION_STR(ansiCodeStr) "\u001b[" ansiCodeStr "m"
  191. #define ANSI_RESET ANSI_INSTRUCTION(0)
  192. #define ANSI_fg_black 30
  193. #define ANSI_fg_red 31
  194. #define ANSI_fg_green 32
  195. #define ANSI_fg_yellow 33
  196. #define ANSI_fg_blue 34
  197. #define ANSI_fg_magenta 35
  198. #define ANSI_fg_cyan 36
  199. #define ANSI_fg_white 37
  200. #define ANSI_fg_bblack 90
  201. #define ANSI_fg_bred 91
  202. #define ANSI_fg_bgreen 92
  203. #define ANSI_fg_byellow 93
  204. #define ANSI_fg_bblue 94
  205. #define ANSI_fg_bmagenta 95
  206. #define ANSI_fg_bcyan 96
  207. #define ANSI_fg_bwhite 97
  208. #define ANSI_bg_black 40
  209. #define ANSI_bg_red 41
  210. #define ANSI_bg_green 42
  211. #define ANSI_bg_yellow 43
  212. #define ANSI_bg_blue 44
  213. #define ANSI_bg_magenta 45
  214. #define ANSI_bg_cyan 46
  215. #define ANSI_bg_white 47
  216. #define ANSI_bg_bblack 100
  217. #define ANSI_bg_bred 101
  218. #define ANSI_bg_bgreen 102
  219. #define ANSI_bg_byellow 103
  220. #define ANSI_bg_bblue 104
  221. #define ANSI_bg_bmagenta 105
  222. #define ANSI_bg_bcyan 106
  223. #define ANSI_bg_bwhite 107
  224. #define COLOR_TEXT(text, foregroundcolor) ANSI_INSTRUCTION_FROM_ENUM(foregroundcolor) text ANSI_RESET
  225. #define COLOR_TEXT_BG(text, backgroundcolor) ANSI_INSTRUCTION_FROM_ENUM(backgroundcolor) text ANSI_RESET
  226. #define COLOR_TEXT_FG_BG(text, foregroundcolor, backgroundcolor) ANSI_INSTRUCTION_FROM_ENUM(foregroundcolor) ANSI_INSTRUCTION_FROM_ENUM(backgroundcolor) text ANSI_RESET
  227. #define COLOR_TEXT_RGB(text, red, green, blue) ANSI_INSTRUCTION_STR("38;2;" #red ";" #green ";" #blue) text ANSI_RESET
  228. #define SetStdErr() DeferLoop
  229. DefineList(int, Int);
  230. void printIntListErr(IntList l);
  231. void printStrListErr(StringList l);
  232. void print(const char *fmt, ...);
  233. void printErr(const char *fmt, ...);
  234. // ### Loops ###
  235. #define EachIn(list, it) size_t it = 0; it < (list).head; it++
  236. #define EachInReversed(list, it) size_t it = (list).head - 1; it >= 0 && it < (list).head; it--
  237. #define EachInArray(arr, it) size_t it = 0; it < ArrayCount(arr); ++it
  238. // ### Misc ###
  239. int intCompare(const void *a, const void *b);
  240. #endif