core.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* @file core.h
  2. *
  3. * @brief LSUP_REPO core module.
  4. */
  5. #ifndef _LSR_CORE_H
  6. #define _LSR_CORE_H
  7. #include "lsup_rdf.h"
  8. /*
  9. * Defines.
  10. */
  11. /** @brief URI prefix for all managed resources.
  12. */
  13. #define LSR_RSRC_PFX "urn:lsres:"
  14. /** @brief Macro to determine if an IRI is an LSR resource.
  15. */
  16. #define LSR_IS_RSRC_IRI(term) \
  17. (LSUP_IS_IRI (term) && strstr ((term)->data, LSR_RSRC_PFX) == (term)->data)
  18. /*
  19. * Data types.
  20. */
  21. typedef char LSR_id[UUIDSTR_SIZE];
  22. typedef enum res_flags_t {
  23. LSR_RS_MANAGED = 1 << 0, // Managed by the repo.
  24. LSR_RS_DIRTY = 1 << 1, // Dirty buffer.
  25. } LSR_ResFlags;
  26. typedef struct term_hmap_t {
  27. LSUP_Term * term;
  28. LSUP_Key key;
  29. UT_hash_handle hh;
  30. } LSR_TermMap;
  31. /*
  32. * Global variables.
  33. */
  34. /** @brief Whether the environment is initialized.
  35. */
  36. extern bool LSR_is_init;
  37. /** @brief managed terms.
  38. */
  39. extern LSR_TermMap *LSR_managed_preds, *LSR_managed_types;
  40. /*
  41. * API.
  42. */
  43. /** @brief Initialize LSUP and LSR environments.
  44. *
  45. * This function is idempotent.
  46. */
  47. LSUP_rc LSR_init (void);
  48. /** @brief Tear down LSUP and LSR environments.
  49. *
  50. * This function is idempotent. It is also called automatically at the end of
  51. * a program (atexit()).
  52. */
  53. void LSR_done (void);
  54. /** @brief Create a new URN term from a resource ID and optional fragment.
  55. *
  56. * @param[in] id Resource ID.
  57. *
  58. * @param[in] frag Fragment to add to the URN. If not NULL, it is appended to
  59. * the URN and an additional `#` character.
  60. *
  61. * @return New term. The default namespace map is associated with the term.
  62. * It should be freed with #LSUP_term_free().
  63. */
  64. inline LSUP_Term *LSR_id_to_urn (const uuid_t id, const char *frag)
  65. {
  66. char id_str[UUID_STR_LEN];
  67. uuid_unparse_lower (id, id_str);
  68. char *urn_str;
  69. if (frag) {
  70. urn_str = malloc (
  71. strlen (LSR_RSRC_PFX) + UUID_STR_LEN + strlen (frag) + 2);
  72. if (! urn_str) return NULL;
  73. sprintf (urn_str, "%s%s#%s", LSR_RSRC_PFX, id_str, frag);
  74. } else {
  75. urn_str = malloc (strlen (LSR_RSRC_PFX) + UUID_STR_LEN + 1);
  76. if (! urn_str) return NULL;
  77. sprintf (urn_str, "%s%s", LSR_RSRC_PFX, id_str);
  78. }
  79. LSUP_Term *urn = LSUP_iriref_new (urn_str, LSUP_default_env->nsm);
  80. free (urn_str);
  81. return urn;
  82. }
  83. #endif /* _LSR_CODE_H */