namespace.h 4.2 KB

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