jim1.h 9.5 KB

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