namespace.h 4.8 KB

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