Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

221 рядки
5.7 KiB

  1. #ifndef CORE_H
  2. #define CORE_H
  3. // cstdlib includes
  4. #include <math.h>
  5. #include <stdint.h> // necessary for int type sizes
  6. #include <stdio.h>
  7. #include <time.h> // TODO(djledda): try not to depend on this one
  8. // ### Misc macros ###
  9. #if ENABLE_ASSERT
  10. #define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;}
  11. #else
  12. #define Assert(expression)
  13. #endif
  14. #define function static
  15. #define global static
  16. #define local_persist static
  17. // ### Types ###
  18. typedef int8_t int8;
  19. typedef int16_t int16;
  20. typedef int32_t int32;
  21. typedef int64_t int64;
  22. typedef uint8_t uint8;
  23. typedef uint16_t uint16;
  24. typedef uint32_t uint32;
  25. typedef uint64_t uint64;
  26. typedef uint8_t byte;
  27. typedef float real32;
  28. typedef double real64;
  29. // ### Sizes and Numbers ###
  30. #define Bytes(n) (n)
  31. #define Kilobytes(n) (n << 10)
  32. #define Megabytes(n) (n << 20)
  33. #define Gigabytes(n) (((uint64)n) << 30)
  34. #define Terabytes(n) (((uint64)n) << 40)
  35. #define Thousand(n) ((n)*1000)
  36. #define Million(n) ((n)*1000000)
  37. #define Billion(n) ((n)*1000000000LL)
  38. #define ArrayCount(arr) (sizeof(arr) / sizeof((arr)[0]))
  39. // ### Arenas ###
  40. struct Arena {
  41. void *memory;
  42. size_t capacity;
  43. size_t head;
  44. };
  45. struct Scratch {
  46. Arena *arena;
  47. size_t start;
  48. };
  49. void *pushSize(Arena *arena, size_t bytes);
  50. Arena *arenaAlloc(size_t capacity);
  51. void arenaFree(Arena *arena);
  52. void arenaFreeFrom(Arena *arena, size_t pos);
  53. void arenaPopTo(Arena *arena, void *pos);
  54. void initialiseCore();
  55. Scratch scratchStart(Arena **conflicts, size_t conflictCount);
  56. void scratchEnd(Scratch scratch);
  57. #define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
  58. #define PushStruct(arena, type) (type *)pushSize(arena, sizeof(type))
  59. // ### Vectors ###
  60. template <typename T>
  61. union Vector2 {
  62. struct {
  63. T x;
  64. T y;
  65. };
  66. T vec[2];
  67. };
  68. template <typename T>
  69. inline function Vector2<T> vec2(T x, T y) {
  70. Vector2<T> result = {0};
  71. result.x = x;
  72. result.y = y;
  73. return result;
  74. }
  75. template <typename T>
  76. union Vector3 {
  77. struct {
  78. T x;
  79. T y;
  80. T z;
  81. };
  82. T vec[3];
  83. };
  84. template <typename T>
  85. inline function Vector3<T> vec3(T x, T y, T z) {
  86. Vector3<T> result = {0};
  87. result.x = x;
  88. result.y = y;
  89. result.z = z;
  90. return result;
  91. }
  92. template <typename T>
  93. union Vector4 {
  94. struct {
  95. T x;
  96. T y;
  97. T z;
  98. T w;
  99. };
  100. T vec[4];
  101. };
  102. template <typename T>
  103. inline function Vector4<T> vec4(T x, T y, T z, T w) {
  104. Vector4<T> result = {0};
  105. result.x = x;
  106. result.y = y;
  107. result.z = z;
  108. result.w = w;
  109. return result;
  110. }
  111. // ### Lists ###
  112. template <typename T>
  113. struct list {
  114. T* data;
  115. size_t length;
  116. size_t head;
  117. };
  118. #define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 })
  119. #define PushFullList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, size })
  120. template <typename T> T *appendList(list<T> *list, T element);
  121. template <typename T> void zeroList(list<T> *list);
  122. template <typename T> void zeroListFull(list<T> *list);
  123. template <typename T> list<T> listSlice(list<T> l, size_t start, size_t stop = 0);
  124. // ### Strings ###
  125. struct string {
  126. char *str;
  127. size_t length;
  128. };
  129. #define STB_SPRINTF_DECORATE(name) stb_##name // define this before including if you want to change the names
  130. #include "vendor/stb_sprintf.h"
  131. #define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1})
  132. #define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) })
  133. string operator""_s(const char *cstrLiteral, size_t length);
  134. // C Strings
  135. const char *cstring(Arena *arena, list<char> buf);
  136. const char *cstring(Arena *arena, string str);
  137. size_t calcStringLen(const char *str);
  138. string strFromCString(Arena *arena, const char *str);
  139. bool strEql(string s1, string s2);
  140. bool stringContains(string str, char c);
  141. string strReverse(Arena *arena, string str);
  142. string strSlice(string str, size_t start, size_t stop = 0);
  143. string strSlice(char *data, size_t start, size_t stop = 0);
  144. list<string> strSplit(Arena *arena, string splitStr, string inputStr);
  145. string strPrintfv(Arena *arena, const char *fmt, va_list args);
  146. string strPrintf(Arena *arena, const char *fmt, ...);
  147. int8 parsePositiveInt(string str, size_t *lengthPointer);
  148. real32 parsePositiveReal32(Arena *arena, string str, size_t *lengthPointer);
  149. inline function bool isNumeric(char c);
  150. // ### File IO ###
  151. string readEntireFile(Arena *arena, string filename);
  152. bool writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength);
  153. bool fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength);
  154. // ### Cmdline ###
  155. list<string> getArgs(Arena *arena, int argc, char **argv);
  156. // ### Time ###
  157. typedef uint64 UnixTimestamp;
  158. typedef tm Timestamp;
  159. UnixTimestamp getSystemUnixTime();
  160. Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp);
  161. string formatTimeHms(Arena *arena, UnixTimestamp time);
  162. string formatTimeHms(Arena *arena, Timestamp *time);
  163. string formatTimeYmd(Arena *arena, UnixTimestamp time);
  164. string formatTimeYmd(Arena *arena, Timestamp *time);
  165. // ### Linked Lists ###
  166. // TODO(djledda): implement basic linked lists (based on arenas?)
  167. // ### Logging ###
  168. enum LogTarget {
  169. LogTarget_stdout,
  170. LogTarget_stdin,
  171. LogTarget_stderr,
  172. LogTarget_count,
  173. };
  174. void log(list<int> l, LogTarget target = LogTarget_stdout);
  175. void log(list<string> l, LogTarget target = LogTarget_stdout);
  176. void log(const char *fmt, ...);
  177. void logError(const char *fmt, ...);
  178. // ### Loops ###
  179. #define EachIn(list, it) size_t it = 0; it < (list).head; it++
  180. #define EachInReversed(list, it) size_t it = (list).head - 1; it >= 0 && it < (list).head; it--
  181. #define EachInArray(arr, it) size_t it = 0; it < ArrayCount(arr); ++it
  182. // ### Misc ###
  183. int intCompare(const void *a, const void *b);
  184. #endif