jim.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #ifndef JIM_H_
  2. #define JIM_H_
  3. #ifndef JIM_SCOPES_CAPACITY
  4. #define JIM_SCOPES_CAPACITY 128
  5. #endif // JIM_SCOPES_CAPACITY
  6. typedef void* Jim_Sink;
  7. typedef size_t (*Jim_Write)(const void *ptr, size_t size, size_t nmemb, Jim_Sink sink);
  8. typedef enum {
  9. JIM_OK = 0,
  10. JIM_WRITE_ERROR,
  11. JIM_SCOPES_OVERFLOW,
  12. JIM_SCOPES_UNDERFLOW,
  13. JIM_OUT_OF_SCOPE_KEY,
  14. JIM_DOUBLE_KEY
  15. } Jim_Error;
  16. const char *jim_error_string(Jim_Error error);
  17. typedef enum {
  18. JIM_ARRAY_SCOPE,
  19. JIM_OBJECT_SCOPE,
  20. } Jim_Scope_Kind;
  21. typedef struct {
  22. Jim_Scope_Kind kind;
  23. int tail;
  24. int key;
  25. } Jim_Scope;
  26. typedef struct {
  27. Jim_Sink sink;
  28. Jim_Write write;
  29. Jim_Error error;
  30. Jim_Scope scopes[JIM_SCOPES_CAPACITY];
  31. size_t scopes_size;
  32. } Jim;
  33. void jim_null(Jim *jim);
  34. void jim_bool(Jim *jim, int boolean);
  35. void jim_integer(Jim *jim, long long int x);
  36. void jim_float(Jim *jim, double x, int precision);
  37. void jim_string(Jim *jim, const char *str);
  38. void jim_string_sized(Jim *jim, const char *str, size_t size);
  39. void jim_element_begin(Jim *jim);
  40. void jim_element_end(Jim *jim);
  41. void jim_array_begin(Jim *jim);
  42. void jim_array_end(Jim *jim);
  43. void jim_object_begin(Jim *jim);
  44. void jim_member_key(Jim *jim, const char *str);
  45. void jim_member_key_sized(Jim *jim, const char *str, size_t size);
  46. void jim_object_end(Jim *jim);
  47. #endif // JIM_H_
  48. #ifdef JIM_IMPLEMENTATION
  49. static size_t jim_strlen(const char *s)
  50. {
  51. size_t count = 0;
  52. while (*(s + count)) {
  53. count += 1;
  54. }
  55. return count;
  56. }
  57. static void jim_scope_push(Jim *jim, Jim_Scope_Kind kind)
  58. {
  59. if (jim->error == JIM_OK) {
  60. if (jim->scopes_size < JIM_SCOPES_CAPACITY) {
  61. jim->scopes[jim->scopes_size].kind = kind;
  62. jim->scopes[jim->scopes_size].tail = 0;
  63. jim->scopes[jim->scopes_size].key = 0;
  64. jim->scopes_size += 1;
  65. } else {
  66. jim->error = JIM_SCOPES_OVERFLOW;
  67. }
  68. }
  69. }
  70. static void jim_scope_pop(Jim *jim)
  71. {
  72. if (jim->error == JIM_OK) {
  73. if (jim->scopes_size > 0) {
  74. jim->scopes_size--;
  75. } else {
  76. jim->error = JIM_SCOPES_UNDERFLOW;
  77. }
  78. }
  79. }
  80. static Jim_Scope *jim_current_scope(Jim *jim)
  81. {
  82. if (jim->error == JIM_OK) {
  83. if (jim->scopes_size > 0) {
  84. return &jim->scopes[jim->scopes_size - 1];
  85. }
  86. }
  87. return NULL;
  88. }
  89. static void jim_write(Jim *jim, const char *buffer, size_t size)
  90. {
  91. if (jim->error == JIM_OK) {
  92. if (jim->write(buffer, 1, size, jim->sink) < size) {
  93. jim->error = 1;
  94. }
  95. }
  96. }
  97. static void jim_write_cstr(Jim *jim, const char *cstr)
  98. {
  99. if (jim->error == JIM_OK) {
  100. jim_write(jim, cstr, jim_strlen(cstr));
  101. }
  102. }
  103. static int jim_get_utf8_char_len(unsigned char ch)
  104. {
  105. if ((ch & 0x80) == 0) return 1;
  106. switch (ch & 0xf0) {
  107. case 0xf0:
  108. return 4;
  109. case 0xe0:
  110. return 3;
  111. default:
  112. return 2;
  113. }
  114. }
  115. void jim_element_begin(Jim *jim)
  116. {
  117. if (jim->error == JIM_OK) {
  118. Jim_Scope *scope = jim_current_scope(jim);
  119. if (scope && scope->tail && !scope->key) {
  120. jim_write_cstr(jim, ",");
  121. }
  122. }
  123. }
  124. void jim_element_end(Jim *jim)
  125. {
  126. if (jim->error == JIM_OK) {
  127. Jim_Scope *scope = jim_current_scope(jim);
  128. if (scope) {
  129. scope->tail = 1;
  130. scope->key = 0;
  131. }
  132. }
  133. }
  134. const char *jim_error_string(Jim_Error error)
  135. {
  136. // TODO(#1): error strings are not particularly useful
  137. switch (error) {
  138. case JIM_OK:
  139. return "There is no error. The developer of this software just had a case of \"Task failed successfully\" https://i.imgur.com/Bdb3rkq.jpg - Please contact the developer and tell them that they are very lazy for not checking errors properly.";
  140. case JIM_WRITE_ERROR:
  141. return "Write error";
  142. case JIM_SCOPES_OVERFLOW:
  143. return "Stack of Scopes Overflow";
  144. case JIM_SCOPES_UNDERFLOW:
  145. return "Stack of Scopes Underflow";
  146. case JIM_OUT_OF_SCOPE_KEY:
  147. return "Out of Scope key";
  148. case JIM_DOUBLE_KEY:
  149. return "Tried to set the member key twice";
  150. default:
  151. return NULL;
  152. }
  153. }
  154. void jim_null(Jim *jim)
  155. {
  156. if (jim->error == JIM_OK) {
  157. jim_element_begin(jim);
  158. jim_write_cstr(jim, "null");
  159. jim_element_end(jim);
  160. }
  161. }
  162. void jim_bool(Jim *jim, int boolean)
  163. {
  164. if (jim->error == JIM_OK) {
  165. jim_element_begin(jim);
  166. if (boolean) {
  167. jim_write_cstr(jim, "true");
  168. } else {
  169. jim_write_cstr(jim, "false");
  170. }
  171. jim_element_end(jim);
  172. }
  173. }
  174. static void jim_integer_no_element(Jim *jim, long long int x)
  175. {
  176. if (jim->error == JIM_OK) {
  177. if (x < 0) {
  178. jim_write_cstr(jim, "-");
  179. x = -x;
  180. }
  181. if (x == 0) {
  182. jim_write_cstr(jim, "0");
  183. } else {
  184. char buffer[64];
  185. size_t count = 0;
  186. while (x > 0) {
  187. buffer[count++] = (x % 10) + '0';
  188. x /= 10;
  189. }
  190. for (size_t i = 0; i < count / 2; ++i) {
  191. char t = buffer[i];
  192. buffer[i] = buffer[count - i - 1];
  193. buffer[count - i - 1] = t;
  194. }
  195. jim_write(jim, buffer, count);
  196. }
  197. }
  198. }
  199. void jim_integer(Jim *jim, long long int x)
  200. {
  201. if (jim->error == JIM_OK) {
  202. jim_element_begin(jim);
  203. jim_integer_no_element(jim, x);
  204. jim_element_end(jim);
  205. }
  206. }
  207. static int is_nan_or_inf(double x)
  208. {
  209. unsigned long long int mask = (1ULL << 11ULL) - 1ULL;
  210. return (((*(unsigned long long int*) &x) >> 52ULL) & mask) == mask;
  211. }
  212. void jim_float(Jim *jim, double x, int precision)
  213. {
  214. if (jim->error == JIM_OK) {
  215. if (is_nan_or_inf(x)) {
  216. jim_null(jim);
  217. } else {
  218. jim_element_begin(jim);
  219. jim_integer_no_element(jim, (long long int) x);
  220. x -= (double) (long long int) x;
  221. while (precision-- > 0) {
  222. x *= 10.0;
  223. }
  224. jim_write_cstr(jim, ".");
  225. long long int y = (long long int) x;
  226. if (y < 0) {
  227. y = -y;
  228. }
  229. jim_integer_no_element(jim, y);
  230. jim_element_end(jim);
  231. }
  232. }
  233. }
  234. static void jim_string_sized_no_element(Jim *jim, const char *str, size_t size)
  235. {
  236. if (jim->error == JIM_OK) {
  237. const char *hex_digits = "0123456789abcdef";
  238. const char *specials = "btnvfr";
  239. const char *p = str;
  240. size_t len = size;
  241. jim_write_cstr(jim, "\"");
  242. size_t cl;
  243. for (size_t i = 0; i < len; i++) {
  244. unsigned char ch = ((unsigned char *) p)[i];
  245. if (ch == '"' || ch == '\\') {
  246. jim_write(jim, "\\", 1);
  247. jim_write(jim, p + i, 1);
  248. } else if (ch >= '\b' && ch <= '\r') {
  249. jim_write(jim, "\\", 1);
  250. jim_write(jim, &specials[ch - '\b'], 1);
  251. } else if (0x20 <= ch && ch <= 0x7F) { // is printable
  252. jim_write(jim, p + i, 1);
  253. } else if ((cl = jim_get_utf8_char_len(ch)) == 1) {
  254. jim_write(jim, "\\u00", 4);
  255. jim_write(jim, &hex_digits[(ch >> 4) % 0xf], 1);
  256. jim_write(jim, &hex_digits[ch % 0xf], 1);
  257. } else {
  258. jim_write(jim, p + i, cl);
  259. i += cl - 1;
  260. }
  261. }
  262. jim_write_cstr(jim, "\"");
  263. }
  264. }
  265. void jim_string_sized(Jim *jim, const char *str, size_t size)
  266. {
  267. if (jim->error == JIM_OK) {
  268. jim_element_begin(jim);
  269. jim_string_sized_no_element(jim, str, size);
  270. jim_element_end(jim);
  271. }
  272. }
  273. void jim_string(Jim *jim, const char *str)
  274. {
  275. if (jim->error == JIM_OK) {
  276. jim_string_sized(jim, str, jim_strlen(str));
  277. }
  278. }
  279. void jim_array_begin(Jim *jim)
  280. {
  281. if (jim->error == JIM_OK) {
  282. jim_element_begin(jim);
  283. jim_write_cstr(jim, "[");
  284. jim_scope_push(jim, JIM_ARRAY_SCOPE);
  285. }
  286. }
  287. void jim_array_end(Jim *jim)
  288. {
  289. if (jim->error == JIM_OK) {
  290. jim_write_cstr(jim, "]");
  291. jim_scope_pop(jim);
  292. jim_element_end(jim);
  293. }
  294. }
  295. void jim_object_begin(Jim *jim)
  296. {
  297. if (jim->error == JIM_OK) {
  298. jim_element_begin(jim);
  299. jim_write_cstr(jim, "{");
  300. jim_scope_push(jim, JIM_OBJECT_SCOPE);
  301. }
  302. }
  303. void jim_member_key(Jim *jim, const char *str)
  304. {
  305. if (jim->error == JIM_OK) {
  306. jim_member_key_sized(jim, str, jim_strlen(str));
  307. }
  308. }
  309. void jim_member_key_sized(Jim *jim, const char *str, size_t size)
  310. {
  311. if (jim->error == JIM_OK) {
  312. jim_element_begin(jim);
  313. Jim_Scope *scope = jim_current_scope(jim);
  314. if (scope && scope->kind == JIM_OBJECT_SCOPE) {
  315. if (!scope->key) {
  316. jim_string_sized_no_element(jim, str, size);
  317. jim_write_cstr(jim, ":");
  318. scope->key = 1;
  319. } else {
  320. jim->error = JIM_DOUBLE_KEY;
  321. }
  322. } else {
  323. jim->error = JIM_OUT_OF_SCOPE_KEY;
  324. }
  325. }
  326. }
  327. void jim_object_end(Jim *jim)
  328. {
  329. if (jim->error == JIM_OK) {
  330. jim_write_cstr(jim, "}");
  331. jim_scope_pop(jim);
  332. jim_element_end(jim);
  333. }
  334. }
  335. #endif // JIM_IMPLEMENTATION