Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

220 satır
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 initialiseCore();
  54. Scratch scratchStart(Arena **conflicts, size_t conflictCount);
  55. void scratchEnd(Scratch scratch);
  56. #define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
  57. #define PushStruct(arena, type) (type *)pushSize(arena, sizeof(type))
  58. // ### Vectors ###
  59. template <typename T>
  60. union Vector2 {
  61. struct {
  62. T x;
  63. T y;
  64. };
  65. T vec[2];
  66. };
  67. template <typename T>
  68. inline function Vector2<T> vec2(T x, T y) {
  69. Vector2<T> result = {0};
  70. result.x = x;
  71. result.y = y;
  72. return result;
  73. }
  74. template <typename T>
  75. union Vector3 {
  76. struct {
  77. T x;
  78. T y;
  79. T z;
  80. };
  81. T vec[3];
  82. };
  83. template <typename T>
  84. inline function Vector3<T> vec3(T x, T y, T z) {
  85. Vector3<T> result = {0};
  86. result.x = x;
  87. result.y = y;
  88. result.z = z;
  89. return result;
  90. }
  91. template <typename T>
  92. union Vector4 {
  93. struct {
  94. T x;
  95. T y;
  96. T z;
  97. T w;
  98. };
  99. T vec[4];
  100. };
  101. template <typename T>
  102. inline function Vector4<T> vec4(T x, T y, T z, T w) {
  103. Vector4<T> result = {0};
  104. result.x = x;
  105. result.y = y;
  106. result.z = z;
  107. result.w = w;
  108. return result;
  109. }
  110. // ### Lists ###
  111. template <typename T>
  112. struct list {
  113. T* data;
  114. size_t length;
  115. size_t head;
  116. };
  117. #define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 })
  118. #define PushFullList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, size })
  119. template <typename T> T *appendList(list<T> *list, T element);
  120. template <typename T> void zeroList(list<T> *list);
  121. template <typename T> void zeroListFull(list<T> *list);
  122. template <typename T> list<T> listSlice(list<T> l, size_t start, size_t stop = 0);
  123. // ### Strings ###
  124. struct string {
  125. char *str;
  126. size_t length;
  127. };
  128. #define STB_SPRINTF_DECORATE(name) stb_##name // define this before including if you want to change the names
  129. #include "vendor/stb_sprintf.h"
  130. #define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1})
  131. #define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) })
  132. string operator""_s(const char *cstrLiteral, unsigned long length);
  133. // C Strings
  134. const char *cstring(Arena *arena, list<char> buf);
  135. const char *cstring(Arena *arena, string str);
  136. size_t calcStringLen(const char *str);
  137. string strFromCString(Arena *arena, const char *str);
  138. bool strEql(string s1, string s2);
  139. bool stringContains(string str, char c);
  140. string strReverse(Arena *arena, string str);
  141. string strSlice(string str, size_t start, size_t stop = 0);
  142. string strSlice(char *data, size_t start, size_t stop = 0);
  143. list<string> strSplit(Arena *arena, string splitStr, string inputStr);
  144. string strPrintfv(Arena *arena, const char *fmt, va_list args);
  145. string strPrintf(Arena *arena, const char *fmt, ...);
  146. int8 parsePositiveInt(string str, size_t *lengthPointer);
  147. real32 parsePositiveReal32(Arena *arena, string str, size_t *lengthPointer);
  148. inline function bool isNumeric(char c);
  149. // ### File IO ###
  150. string readEntireFile(Arena *arena, string filename);
  151. bool writeEntireFile(Arena *arena, string filename, const byte *contents, size_t contentsLength);
  152. bool fileAppend(Arena *arena, string filename, const byte *contents, size_t contentsLength);
  153. // ### Cmdline ###
  154. list<string> getArgs(Arena *arena, int argc, char **argv);
  155. // ### Time ###
  156. typedef uint64 UnixTimestamp;
  157. typedef tm Timestamp;
  158. UnixTimestamp getSystemUnixTime();
  159. Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp);
  160. string formatTimeHms(Arena *arena, UnixTimestamp time);
  161. string formatTimeHms(Arena *arena, Timestamp *time);
  162. string formatTimeYmd(Arena *arena, UnixTimestamp time);
  163. string formatTimeYmd(Arena *arena, Timestamp *time);
  164. // ### Linked Lists ###
  165. // TODO(djledda): implement basic linked lists (based on arenas?)
  166. // ### Logging ###
  167. enum LogTarget {
  168. LogTarget_stdout,
  169. LogTarget_stdin,
  170. LogTarget_stderr,
  171. LogTarget_count,
  172. };
  173. void log(list<int> l, LogTarget target = LogTarget_stdout);
  174. void log(list<string> l, LogTarget target = LogTarget_stdout);
  175. void log(const char *fmt, ...);
  176. void logError(const char *fmt, ...);
  177. // ### Loops ###
  178. #define EachIn(list, it) size_t it = 0; it < list.length; it++
  179. #define EachInReversed(list, it) size_t it = list.length - 1; it >= 0 && it < list.length; it--
  180. #define EachInArray(arr, it) size_t it = 0; it < ArrayCount(arr); ++it
  181. // ### Misc ###
  182. int intCompare(const void *a, const void *b);
  183. #endif