namespace.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 Namespace map structure.
  8. *
  9. * It contains a double hash map of pfx->ns and ns->pfx for fast 2-way lookup.
  10. *
  11. * Prefixes are fixed PFX_LEN-size strings, namespaces are arbitrary sized
  12. * strings.
  13. */
  14. typedef struct ns_map_t LSUP_NSMap;
  15. /** @brief Namespace prefix type.
  16. */
  17. typedef char ns_pfx[PFX_LEN];
  18. /** @brief Create a new namespace map.
  19. *
  20. * @return A pointer to an empty map. It must be freed with #LSUP_nsmap_free().
  21. */
  22. LSUP_NSMap *
  23. LSUP_nsmap_new (void);
  24. /** @brief Free a namespace map and its internal structures.
  25. *
  26. * @param[in] map The map to free.
  27. */
  28. void
  29. LSUP_nsmap_free (LSUP_NSMap *map);
  30. /** @brief Add a prefix -> namespace pair to the map or update it.
  31. *
  32. * If the prefix already exists, it is updated with the new value, if
  33. * different.
  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_NOACTION if the
  42. * record already existed with the same value; LSUP_MEM_ERR if an allocation
  43. * error occurred.
  44. */
  45. LSUP_rc
  46. LSUP_nsmap_add (LSUP_NSMap *map, const ns_pfx pfx, const char *nsstr);
  47. /** @brief Remove a prefix -> namespace pair from a map.
  48. *
  49. * @param[in] map The map to remove from.
  50. *
  51. * @param[in] pfx The namespace prefix to remove.
  52. *
  53. * @return LSUP_OK on successful delete; LSUP_NOACTION if no record was found.
  54. */
  55. LSUP_rc
  56. LSUP_nsmap_remove (LSUP_NSMap *map, const ns_pfx pfx);
  57. /** @brief Get the namespace for a prefix.
  58. *
  59. * @param[in] map The map to look up the namespace in.
  60. *
  61. * @param[in] pfx The prefix to look up.
  62. *
  63. * @return A pointer to the namespace string. Note that this is not a copy and
  64. * should not be modified directly.
  65. */
  66. const char *
  67. LSUP_nsmap_get (const LSUP_NSMap *map, const ns_pfx pfx);
  68. /** @brief Convert a FQ URI string to a prefixed string if the prefix is found.
  69. *
  70. * @param[in] map Namespace map to look up.
  71. *
  72. * @param[in] uri URI string to normalize.
  73. *
  74. * @param[out] String pointer to be filled with the prefixed URI. If the
  75. * namespace is not in the map or an error occurred, this will be NULL.
  76. * The caller is in charge of freeing the memory.
  77. *
  78. * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
  79. * LSUP_MEM_ERR if a memory allocation error ocurred.
  80. */
  81. LSUP_rc
  82. LSUP_nsmap_normalize_uri (
  83. const LSUP_NSMap *map, const char *uri, char **pfx_uri);
  84. /** @brief Convert a namespace-prefixed string to a FQ URI sring if mapped.
  85. *
  86. * @param[in] map Namespace map to look up.
  87. *
  88. * @param[in] uri URI string to denormalize.
  89. *
  90. * @param[out] String pointer to be filled with the FQ URI. If the
  91. * namespace is not in the map or an error occurred, this will be NULL.
  92. * The caller is in charge of freeing the memory.
  93. *
  94. * @return LSUP_OK on success, LSUP_NORESULT if no entry was found in the map,
  95. * LSUP_MEM_ERR if a memory allocation error ocurred.
  96. */
  97. LSUP_rc
  98. LSUP_nsmap_denormalize_uri (
  99. const LSUP_NSMap *map, const char *pfx_uri, char **uri);
  100. #endif