namespace.h 4.1 KB

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