core.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef _LSUP_CORE_H
  2. #define _LSUP_CORE_H
  3. #include <ctype.h>
  4. #include <dirent.h>
  5. #include <errno.h>
  6. #include <inttypes.h>
  7. #include <limits.h>
  8. #include <stdbool.h>
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <uuid/uuid.h>
  15. #include "log.h"
  16. // Logging and debugging.
  17. #ifdef DEBUG
  18. // GDB breakpoints.
  19. #include <signal.h>
  20. #define BREAKPOINT raise (SIGINT)
  21. #else
  22. #define BREAKPOINT
  23. #endif
  24. #define LIKELY(x) __builtin_expect(!!(x), true)
  25. #define UNLIKELY(x) __builtin_expect(!!(x), false)
  26. // TODO Cross-platform ramdisk path.
  27. #define TMPDIR "/tmp"
  28. #define KLEN sizeof(LSUP_Key)
  29. #define DBL_KLEN sizeof(LSUP_DoubleKey)
  30. #define TRP_KLEN sizeof(LSUP_TripleKey)
  31. #define QUAD_KLEN sizeof(LSUP_QuadKey)
  32. # define UUIDSTR_SIZE 37
  33. /* * * RETURN CODES * * */
  34. /**
  35. * 0 is success, positive integers (>88800) are warnings, and negative integers
  36. * (>-88900) are errors.
  37. */
  38. typedef int LSUP_rc;
  39. #define LSUP_OK 0
  40. #define LSUP_NOACTION 88801
  41. #define LSUP_NORESULT 88802
  42. #define LSUP_END 88803
  43. #define LSUP_CONFLICT 88804
  44. // NOTE When adding new warning codes, use a value larger than the last one
  45. // in the list. Also change LSUP_MAX_WARNING.
  46. #define LSUP_MIN_WARNING LSUP_NOACTION
  47. #define LSUP_MAX_WARNING LSUP_CONFLICT
  48. #define LSUP_ERROR -88899
  49. #define LSUP_PARSE_ERR -88898
  50. #define LSUP_VALUE_ERR -88897
  51. #define LSUP_TXN_ERR -88896
  52. #define LSUP_DB_ERR -88895
  53. #define LSUP_NOT_IMPL_ERR -88894
  54. #define LSUP_IO_ERR -88893
  55. #define LSUP_MEM_ERR -88892
  56. #define LSUP_CONFLICT_ERR -88891
  57. #define LSUP_ENV_ERR -88890
  58. // NOTE When adding new error codes, use a value larger than the last one
  59. // in the list. Also change LSUP_MAX_ERROR.
  60. #define LSUP_MIN_ERROR LSUP_ERROR
  61. #define LSUP_MAX_ERROR LSUP_ENV_ERR
  62. extern char *warning_msg[], *error_msg[];
  63. extern char *LSUP_root_path;
  64. typedef enum {
  65. LSUP_BOOL_UNION,
  66. LSUP_BOOL_SUBTRACTION,
  67. LSUP_BOOL_INTERSECTION,
  68. LSUP_BOOL_XOR,
  69. } LSUP_bool_op;
  70. typedef size_t LSUP_Key;
  71. typedef LSUP_Key LSUP_DoubleKey[2];
  72. typedef LSUP_Key LSUP_TripleKey[3];
  73. typedef LSUP_Key LSUP_QuadKey[4];
  74. typedef char uuid_str_t[UUIDSTR_SIZE];
  75. /** @brief Make recursive directories.
  76. *
  77. * from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  78. */
  79. LSUP_rc mkdir_p(const char *path, mode_t mode);
  80. /** @brief Remove a directory recursively, as in Unix "rm -r".
  81. *
  82. * @param path[in] Path of directory to remove.
  83. */
  84. LSUP_rc
  85. rm_r (const char *path);
  86. /** @brief Return an error message for a return code.
  87. */
  88. const char *
  89. LSUP_strerror (LSUP_rc rc);
  90. /**
  91. * Encode a code point using UTF-8
  92. *
  93. * @author Ondřej Hruška <ondra@ondrovo.com>
  94. * @license MIT
  95. * https://gist.github.com/MightyPork/52eda3e5677b4b03524e40c9f0ab1da5
  96. *
  97. * @param out - output buffer (min 5 characters), will be 0-terminated
  98. * @param utf - code point 0-0x10FFFF
  99. * @return number of bytes on success, 0 on failure (also produces U+FFFD,
  100. * which uses 3 bytes)
  101. */
  102. inline int utf8_encode(const uint32_t utf, unsigned char *out)
  103. {
  104. if (utf <= 0x7F) {
  105. // Plain ASCII
  106. out[0] = (char) utf;
  107. out[1] = 0;
  108. return 1;
  109. }
  110. else if (utf <= 0x07FF) {
  111. // 2-byte unicode
  112. out[0] = (char) (((utf >> 6) & 0x1F) | 0xC0);
  113. out[1] = (char) (((utf >> 0) & 0x3F) | 0x80);
  114. out[2] = 0;
  115. return 2;
  116. }
  117. else if (utf <= 0xFFFF) {
  118. // 3-byte unicode
  119. out[0] = (char) (((utf >> 12) & 0x0F) | 0xE0);
  120. out[1] = (char) (((utf >> 6) & 0x3F) | 0x80);
  121. out[2] = (char) (((utf >> 0) & 0x3F) | 0x80);
  122. out[3] = 0;
  123. return 3;
  124. }
  125. else if (utf <= 0x10FFFF) {
  126. // 4-byte unicode
  127. out[0] = (char) (((utf >> 18) & 0x07) | 0xF0);
  128. out[1] = (char) (((utf >> 12) & 0x3F) | 0x80);
  129. out[2] = (char) (((utf >> 6) & 0x3F) | 0x80);
  130. out[3] = (char) (((utf >> 0) & 0x3F) | 0x80);
  131. out[4] = 0;
  132. return 4;
  133. }
  134. else {
  135. // error - use replacement character
  136. out[0] = (char) 0xEF;
  137. out[1] = (char) 0xBF;
  138. out[2] = (char) 0xBD;
  139. out[3] = 0;
  140. return 0;
  141. }
  142. }
  143. // Error handling via goto.
  144. #define CHECK(exp, rc, marker) (rc) = (exp); if ((rc) != LSUP_OK) goto marker
  145. // Jump if rc is negative (skip warnings).
  146. #define PCHECK(exp, rc, marker) (rc) = (exp); if ((rc) < LSUP_OK) goto marker
  147. // Return rc if it is of LSUP_rc type and is negative (=error)
  148. #define RCCK(exp) LSUP_rc _rc = (exp); if (UNLIKELY (_rc < 0)) return _rc
  149. // Return NULL if it is of LSUP_rc type and is negative (=error)
  150. #define RCNL(exp) if (UNLIKELY ((exp) < 0)) return NULL
  151. #define MALLOC_GUARD(var, rc) do { \
  152. (var) = malloc (sizeof *(var)); \
  153. if (UNLIKELY (var == NULL)) return (rc); \
  154. } while (0);
  155. #define CALLOC_GUARD(var, rc) do { \
  156. (var) = calloc (1, sizeof *(var)); \
  157. if (UNLIKELY (var == NULL)) return (rc); \
  158. } while (0);
  159. /*
  160. #define MALLOC_GUARD_ME(var) MALLOC_GUARD((var), LSUP_MEM_ERR) \
  161. #define CALLOC_GUARD_ME(var) CALLOC_GUARD((var), LSUP_MEM_ERR) \
  162. #define MALLOC_GUARD_NL(var) MALLOC_GUARD((var), NULL) \
  163. #define CALLOC_GUARD_NL(var) CALLOC_GUARD((var), NULL) \
  164. */
  165. #endif