namespace.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef _LSUP_NAMESPACE_H
  2. #define _LSUP_NAMESPACE_H
  3. #include "hashmap.h"
  4. #include "lsup/core.h"
  5. /** @defgroup namespace Namespace module
  6. * @ingroup public
  7. * @{
  8. */
  9. /** @brief Namespace prefix length, including terminator.
  10. */
  11. #define PFX_LEN 8
  12. /** @brief Namespace prefix type.
  13. */
  14. typedef char LSUP_ns_pfx[PFX_LEN];
  15. /** @brief Prefix / Namespace pair.
  16. */
  17. typedef struct ns_entry_t {
  18. LSUP_ns_pfx pfx; // Namespace prefix.
  19. char * ns; // Fully qualified NS.
  20. } NSEntry;
  21. /** @brief Default namespace prefix map.
  22. *
  23. * This is a singleton. It gets created with #LSUP_init() and freed with
  24. * #LSUP_done().
  25. */
  26. extern struct hashmap *LSUP_default_nsm;
  27. /** @brief Add a prefix -> namespace pair to the map or update it.
  28. *
  29. * If the prefix already exists, it is quietly updated with the new value.
  30. *
  31. * @param[in] map The map to add to.
  32. *
  33. * @param[in] pfx The namespace prefix.
  34. *
  35. * @param[in] nsstr Fully qualified namespace.
  36. *
  37. * @return LSUP_OK if the record was added or replaced; LSUP_MEM_ERR if an
  38. * allocation error occurred.
  39. */
  40. LSUP_rc
  41. LSUP_nsmap_add (const char *pfx, const char *nsstr);
  42. /** @brief Remove a prefix -> namespace pair from a map.
  43. *
  44. * @param[in] map The map to remove from.
  45. *
  46. * @param[in] pfx The namespace prefix to remove.
  47. *
  48. * @return LSUP_OK on successful delete; LSUP_NOACTION if no record was found.
  49. */
  50. LSUP_rc
  51. LSUP_nsmap_remove (const char *pfx);
  52. /** @brief Get the namespace for a prefix.
  53. *
  54. * @param[in] map The map to look up the namespace in.
  55. *
  56. * @param[in] pfx The prefix to look up.
  57. *
  58. * @return A pointer to the namespace string. Note that this is not a copy and
  59. * should not be modified directly.
  60. */
  61. const char *
  62. LSUP_nsmap_get_ns (const char *pfx);
  63. /** @brief Get the prefix for a namespace.
  64. *
  65. * @param[in] map The map to look up the prefix in.
  66. *
  67. * @param[in] ns The namespace to look up.
  68. *
  69. * @return Found prefix, or NULL if the namespace is not mapped.
  70. */
  71. const char *
  72. LSUP_nsmap_get_pfx (const char *ns);
  73. /** @brief Convert a namespace-prefixed string to a FQ URI sring if mapped.
  74. *
  75. * @param[in] pfx_uri URI string to denormalize.
  76. *
  77. * @param[out] fq_uri String pointer to be filled with the FQ URI. The caller
  78. * is in charge of freeing the memory. If the namespace is not in the map or
  79. * an error occurred, this will be NULL. This is to inform the caller that the
  80. * result is not normalized and a TERM_NS_IRIREF should not be construed with
  81. * it.
  82. *
  83. * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
  84. * LSUP_MEM_ERR if a memory allocation error ocurred.
  85. */
  86. LSUP_rc
  87. LSUP_nsmap_normalize_uri (const char *pfx_uri, char **fq_uri);
  88. /** @brief Convert a FQ URI string to a prefixed string if the prefix is found.
  89. *
  90. * @todo Note that this function does not attempt to find the longest or
  91. * shortest namespace prefix in case of conflicts (e.g. when both
  92. * `http://example.edu/` and `http://example.edu/data/` are mapped and
  93. * `http://example.edu/data/51937642` is being denormalized). In such
  94. * case, the first prefix that is found is assigned.
  95. *
  96. * @param[in] fq_uri URI string to normalize.
  97. *
  98. * @param[out] pfx_uri String pointer to be filled with the prefixed URI. The
  99. * caller is in charge of freeing the memory. If the namespace is not in the
  100. * map or an error occurred, this will be NULL. This is to inform the caller
  101. * that the result is not denormalized and a TERM_IRIREF should not be
  102. * construed with it.
  103. *
  104. * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
  105. * LSUP_MEM_ERR if a memory allocation error ocurred.
  106. */
  107. LSUP_rc
  108. LSUP_nsmap_denormalize_uri (const char *fq_uri, char **pfx_uri);
  109. /** @brief Dump all entries of the namespace map.
  110. *
  111. * @param[in] map Map to dump.
  112. *
  113. * @return 2-dimensional array of strings, with as many rows as namespace
  114. * entries, and two columns: the first for the namespace prefix, the second
  115. * for the namespace. The last entry is NULL. Prefix and namespace strings
  116. * pointed to by the array are owned by the namespace map, but the individual
  117. * pointers must be freed along with the parent array. E.g.:
  118. *
  119. * const char ***nsm_data = LSUP_nsmap_dump (nsm);
  120. * // [...]
  121. * for (size_t i = 0; nsm_data[i] != NULL; i++)
  122. * free (nsm_data[i]);
  123. * free (nsm_data);
  124. */
  125. const char ***
  126. LSUP_nsmap_dump (void);
  127. /// @} END defgroup namespace
  128. #endif