GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
key_value1.c
Go to the documentation of this file.
1 /*!
2  \file lib/gis/key_value1.c
3 
4  \brief Subroutines for Key/Value management.
5 
6  (C) 2001-2008, 2012 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author CERL
12  */
13 
14 #include <string.h>
15 #include <stdlib.h>
16 #include <grass/gis.h>
17 
18 /*!
19  \brief Allocate and initialize Key_Value structure
20 
21  \return pointer to allocated Key_Value structure
22  */
24 {
25  struct Key_Value *kv = G_malloc(sizeof(struct Key_Value));
26 
27  G_zero(kv, sizeof(struct Key_Value));
28 
29  return kv;
30 }
31 
32 /*!
33  \brief Set value for given key
34 
35  \param key key to be set up
36  \param value value for given key
37  \param[in,out] kv Key_value structure to be modified
38  */
39 void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
40 {
41  int n;
42 
43  if (!key)
44  return;
45 
46  for (n = 0; n < kv->nitems; n++)
47  if (strcmp(key, kv->key[n]) == 0)
48  break;
49 
50  if (n == kv->nitems) {
51  if (n >= kv->nalloc) {
52  size_t size;
53 
54  if (kv->nalloc <= 0)
55  kv->nalloc = 8;
56  else
57  kv->nalloc *= 2;
58 
59  size = kv->nalloc * sizeof(char *);
60  kv->key = G_realloc(kv->key, size);
61  kv->value = G_realloc(kv->value, size);
62  }
63 
64  kv->key[n] = G_store(key);
65  kv->value[n] = G_store(value);
66  kv->nitems++;
67  return;
68  }
69 
70  if (kv->value[n])
71  G_free(kv->value[n]);
72 
73  kv->value[n] = value ? G_store(value) : NULL;
74 }
75 
76 /*!
77  \brief Find given key (case sensitive)
78 
79  \param key key to be found
80  \param kv pointer to Key_value structure
81 
82  \return pointer to value of key
83  \return NULL if no key found
84  */
85 const char *G_find_key_value(const char *key, const struct Key_Value *kv)
86 {
87  int n;
88 
89  if (!kv)
90  return NULL;
91 
92  for (n = 0; n < kv->nitems; n++)
93  if (strcmp(key, kv->key[n]) == 0)
94  return kv->value[n][0] ? kv->value[n] : NULL;
95 
96  return NULL;
97 }
98 
99 /*!
100  \brief Free allocated Key_Value structure
101 
102  \param[in] kv Key_Value structure to be freed
103  */
104 void G_free_key_value(struct Key_Value *kv)
105 {
106  int n;
107 
108  if (!kv)
109  return;
110 
111  for (n = 0; n < kv->nitems; n++) {
112  G_free(kv->key[n]);
113  G_free(kv->value[n]);
114  }
115  G_free(kv->key);
116  G_free(kv->value);
117  kv->nitems = 0; /* just for safe measure */
118  kv->nalloc = 0;
119  G_free(kv);
120 }
#define NULL
Definition: ccmath.h:32
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_realloc(p, n)
Definition: defs/gis.h:96
#define G_malloc(n)
Definition: defs/gis.h:94
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
void G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:104
void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
Definition: key_value1.c:39
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition: key_value1.c:23
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:85
Definition: gis.h:528
char ** value
Definition: gis.h:532
int nitems
Definition: gis.h:529
int nalloc
Definition: gis.h:530
char ** key
Definition: gis.h:531