namespace.h 4.1 KB

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