namespace.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef _LSUP_NAMESPACE_H
  2. #define _LSUP_NAMESPACE_H
  3. #include "core.h"
  4. /** @brief Namespace prefix length, including terminator.
  5. */
  6. #define PFX_LEN 8
  7. /** @brief Default namespace. Only available upon environment initialization.
  8. */
  9. #define LSUP_DEF_NSM LSUP_default_env->nsm
  10. /** @brief Namespace map structure.
  11. *
  12. * It contains a double hash map of pfx->ns and ns->pfx for fast 2-way lookup.
  13. *
  14. * Prefixes are fixed PFX_LEN-size strings, namespaces are arbitrary sized
  15. * strings.
  16. */
  17. typedef struct hashmap LSUP_NSMap;
  18. /** @brief Namespace prefix type.
  19. */
  20. typedef char ns_pfx[PFX_LEN];
  21. /** @brief Create a new namespace map.
  22. *
  23. * @return A pointer to an empty map. It must be freed with #LSUP_nsmap_free().
  24. */
  25. LSUP_NSMap *
  26. LSUP_nsmap_new (void);
  27. /** @brief Free a namespace map and its internal structures.
  28. *
  29. * @param[in] map The map to free.
  30. */
  31. void
  32. LSUP_nsmap_free (LSUP_NSMap *map);
  33. /** @brief Add a prefix -> namespace pair to the map or update it.
  34. *
  35. * If the prefix already exists, it is updated with the new value, if
  36. * different.
  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_NOACTION if the
  45. * record already existed with the same value; LSUP_MEM_ERR if an allocation
  46. * error occurred.
  47. */
  48. LSUP_rc
  49. LSUP_nsmap_add (LSUP_NSMap *map, const ns_pfx 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 ns_pfx 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 (LSUP_NSMap *map, const ns_pfx 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] pfx 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 (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] 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.
  89. * The caller is in charge of freeing the memory.
  90. *
  91. * @return LSUP_OK on success, LSUP_NORESULT 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. 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. * @param[in] map Namespace map to look up.
  100. *
  101. * @param[in] uri URI string to normalize.
  102. *
  103. * @param[out] String pointer to be filled with the prefixed URI. If the
  104. * namespace is not in the map or an error occurred, this will be NULL.
  105. * The caller is in charge of freeing the memory.
  106. *
  107. * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
  108. * LSUP_MEM_ERR if a memory allocation error ocurred.
  109. */
  110. LSUP_rc
  111. LSUP_nsmap_denormalize_uri (
  112. const LSUP_NSMap *map, const char *fq_uri, char **pfx_uri);
  113. /** @brief Dump all entries of a namespace map.
  114. *
  115. * @param[in] map Map to dump.
  116. *
  117. * @return 2-dimensional array of strings, with as many rows as namespace
  118. * entries, and two columns: the first for the namespace prefix, the second
  119. * for the namespace.
  120. */
  121. const char ***
  122. LSUP_nsmap_dump (const LSUP_NSMap *map);
  123. #endif