key_value1.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*!
  2. \file key_value1.c
  3. \brief Key_Value management.
  4. (C) 2001-2008 by the GRASS Development Team
  5. This program is free software under the
  6. GNU General Public License (>=v2).
  7. Read the file COPYING that comes with GRASS
  8. for details.
  9. \author CERL
  10. */
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <grass/gis.h>
  14. /*!
  15. \brief Allocate and initialize Key_Value structure
  16. \return poiter to allocated Key_Value structure
  17. */
  18. struct Key_Value *G_create_key_value(void)
  19. {
  20. struct Key_Value *kv;
  21. kv = (struct Key_Value *)G_malloc(sizeof(struct Key_Value));
  22. if (kv == NULL)
  23. return NULL;
  24. kv->nitems = 0;
  25. kv->nalloc = 0;
  26. kv->key = (char **)NULL;
  27. kv->value = (char **)NULL;
  28. return kv;
  29. }
  30. /*!
  31. \brief Set value for given key
  32. If key has spaces in it, this will break the logic
  33. so rule is: NO SPACES IN key.
  34. \param[in] key key to be set up
  35. \param[in] value value for given key
  36. \param[in,out] kv Key_value structure to be modified
  37. \return 0 no memory
  38. \return 1 ok, but key was NULL or "" so ignored
  39. \return 2 ok
  40. */
  41. int G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
  42. {
  43. int n;
  44. int size;
  45. if (key == NULL || key == 0)
  46. return 1;
  47. for (n = 0; n < kv->nitems; n++)
  48. if (strcmp(key, kv->key[n]) == 0)
  49. break;
  50. if (n == kv->nitems) {
  51. if (n >= kv->nalloc) {
  52. if (kv->nalloc <= 0) {
  53. kv->nalloc = 8;
  54. size = kv->nalloc * sizeof(char *);
  55. kv->key = (char **)G_malloc(size);
  56. kv->value = (char **)G_malloc(size);
  57. }
  58. else {
  59. kv->nalloc *= 2;
  60. size = kv->nalloc * sizeof(char *);
  61. kv->key = (char **)G_realloc(kv->key, size);
  62. kv->value = (char **)G_realloc(kv->value, size);
  63. }
  64. if (kv->key == NULL || kv->value == NULL) {
  65. if (kv->key) {
  66. G_free(kv->key);
  67. kv->key = NULL;
  68. }
  69. if (kv->value) {
  70. G_free(kv->value);
  71. kv->value = NULL;
  72. }
  73. kv->nitems = kv->nalloc = 0;
  74. return 0;
  75. }
  76. }
  77. kv->value[n] = NULL;
  78. kv->key[n] = G_malloc(strlen(key) + 1);
  79. if (kv->key[n] == NULL)
  80. return 0;
  81. strcpy(kv->key[n], key);
  82. kv->nitems++;
  83. }
  84. if (value == NULL)
  85. size = 0;
  86. else
  87. size = strlen(value);
  88. if (kv->value[n] != NULL)
  89. G_free(kv->value[n]);
  90. if (size > 0) {
  91. kv->value[n] = G_malloc(size + 1);
  92. if (kv->value[n] == NULL)
  93. return 0;
  94. strcpy(kv->value[n], value);
  95. }
  96. else
  97. kv->value[n] = NULL;
  98. return 2;
  99. }
  100. /*!
  101. \brief Find given key
  102. \param[in] key key to be found
  103. \param[in] kv Key_value structure
  104. \return poiter to value of key
  105. \return NULL if no key found
  106. */
  107. char *G_find_key_value(const char *key, const struct Key_Value *kv)
  108. {
  109. int n;
  110. for (n = 0; n < kv->nitems; n++)
  111. if (strcmp(key, kv->key[n]) == 0)
  112. return kv->value[n][0] ? kv->value[n] : NULL;
  113. return NULL;
  114. }
  115. /*!
  116. \brief Free allocated Key_Value structure
  117. \param[in] kv Key_Value structure
  118. \return
  119. */
  120. void G_free_key_value(struct Key_Value *kv)
  121. {
  122. int n;
  123. for (n = 0; n < kv->nitems; n++) {
  124. G_free(kv->key[n]);
  125. G_free(kv->value[n]);
  126. }
  127. G_free(kv->key);
  128. G_free(kv->value);
  129. kv->nitems = 0; /* just for safe measure */
  130. kv->nalloc = 0;
  131. G_free(kv);
  132. }