core.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifdef DEBUG
  16. #define DEBUG_TEST 1
  17. #else
  18. #define DEBUG_TEST 0
  19. #endif
  20. #define STR "%s\n"
  21. #define TRACE(fmt, ...) \
  22. do {\
  23. if (DEBUG_TEST) \
  24. fprintf(stderr, "%s:%d:%s(): " fmt "\n", \
  25. __FILE__, __LINE__, __func__, __VA_ARGS__); \
  26. } while (0)
  27. #define LIKELY(x) __builtin_expect(!!(x), true)
  28. #define UNLIKELY(x) __builtin_expect(!!(x), false)
  29. // TODO Cross-platform ramdisk path.
  30. #define TMPDIR "/tmp"
  31. #define KLEN sizeof(LSUP_Key)
  32. #define DBL_KLEN sizeof(LSUP_DoubleKey)
  33. #define TRP_KLEN sizeof(LSUP_TripleKey)
  34. #define QUAD_KLEN sizeof(LSUP_QuadKey)
  35. # define UUIDSTR_SIZE 37
  36. // Handy flags operations.
  37. #define SET_FLAG(n, f) ((n) |= (f))
  38. #define CLR_FLAG(n, f) ((n) &= ~(f))
  39. #define TGL_FLAG(n, f) ((n) ^= (f))
  40. #define CHK_FLAG(n, f) ((n) & (f))
  41. /* * * RETURN CODES * * */
  42. /**
  43. * 0 is success, positive integers (>88800) are warnings, and negative integers
  44. * (<-88800) are errors.
  45. */
  46. typedef enum {
  47. LSUP_OK = 0,
  48. LSUP_NOACTION = 88801,
  49. LSUP_NORESULT = 88802,
  50. LSUP_END = 88803,
  51. LSUP_ERROR = -88801,
  52. LSUP_PARSE_ERR = -88802,
  53. LSUP_VALUE_ERR = -88803,
  54. LSUP_TXN_ERR = -88804,
  55. LSUP_DB_ERR = -88805,
  56. LSUP_NOT_IMPL_ERR = -88806,
  57. LSUP_IO_ERR = -88807,
  58. LSUP_MEM_ERR = -88808,
  59. } LSUP_rc;
  60. typedef enum {
  61. LSUP_BOOL_UNION,
  62. LSUP_BOOL_SUBTRACTION,
  63. LSUP_BOOL_INTERSECTION,
  64. LSUP_BOOL_XOR,
  65. } LSUP_bool_op;
  66. typedef size_t LSUP_Key;
  67. typedef LSUP_Key LSUP_DoubleKey[2];
  68. typedef LSUP_Key LSUP_TripleKey[3];
  69. typedef LSUP_Key LSUP_QuadKey[4];
  70. typedef char uuid_str_t[UUIDSTR_SIZE];
  71. // Yes, a textbook mistake; but writing min and max for all int types is crazy.
  72. #define min(x, y) (x) < (y) ? (x) : (y)
  73. #define max(x, y) (x) > (y) ? (x) : (y)
  74. /** @brief Make recursive directories.
  75. *
  76. * from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950
  77. */
  78. LSUP_rc mkdir_p(const char *path, mode_t mode);
  79. /** @brief Remove a directory recursively, as in Unix "rm -r".
  80. *
  81. * @param path[in] Path of directory to remove.
  82. */
  83. LSUP_rc
  84. rm_r (const char *path);
  85. // Error handling via goto.
  86. #define CHECK(exp, rc, marker) (rc) = (exp); if ((rc) != LSUP_OK) goto marker
  87. // Jump if rc is negative (skip warnings).
  88. #define PCHECK(exp, rc, marker) (rc) = (exp); if ((rc) < LSUP_OK) goto marker
  89. // Return rc if it is of LSUP_rc type and is negative (=error)
  90. #define RCCK(exp) LSUP_rc _rc = (exp); if (_rc < 0) return _rc
  91. // Return NULL if it is of LSUP_rc type and is negative (=error)
  92. #define RCNL(exp) if((exp) < 0) return NULL
  93. #endif