GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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  G_zero(kv, sizeof(struct Key_Value));
27 
28  return kv;
29 }
30 
31 /*!
32  \brief Set value for given key
33 
34  \param key key to be set up
35  \param value value for given key
36  \param[in,out] kv Key_value structure to be modified
37  */
38 void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
39 {
40  int n;
41 
42  if (!key)
43  return;
44 
45  for (n = 0; n < kv->nitems; n++)
46  if (strcmp(key, kv->key[n]) == 0)
47  break;
48 
49  if (n == kv->nitems) {
50  if (n >= kv->nalloc) {
51  size_t size;
52 
53  if (kv->nalloc <= 0)
54  kv->nalloc = 8;
55  else
56  kv->nalloc *= 2;
57 
58  size = kv->nalloc * sizeof(char *);
59  kv->key = G_realloc(kv->key, size);
60  kv->value = G_realloc(kv->value, size);
61  }
62 
63  kv->key[n] = G_store(key);
64  kv->value[n] = G_store(value);
65  kv->nitems++;
66  return;
67  }
68 
69  if (kv->value[n])
70  G_free(kv->value[n]);
71 
72  kv->value[n] = value ? G_store(value) : NULL;
73 }
74 
75 /*!
76  \brief Find given key (case sensitive)
77 
78  \param key key to be found
79  \param kv pointer to Key_value structure
80 
81  \return pointer to value of key
82  \return NULL if no key found
83  */
84 const char *G_find_key_value(const char *key, const struct Key_Value *kv)
85 {
86  int n;
87 
88  if (!kv)
89  return NULL;
90 
91  for (n = 0; n < kv->nitems; n++)
92  if (strcmp(key, kv->key[n]) == 0)
93  return kv->value[n][0] ? kv->value[n] : NULL;
94 
95  return NULL;
96 }
97 
98 /*!
99  \brief Free allocated Key_Value structure
100 
101  \param[in] kv Key_Value structure to be freed
102  */
103 void G_free_key_value(struct Key_Value *kv)
104 {
105  int n;
106 
107  if (!kv)
108  return;
109 
110  for (n = 0; n < kv->nitems; n++) {
111  G_free(kv->key[n]);
112  G_free(kv->value[n]);
113  }
114  G_free(kv->key);
115  G_free(kv->value);
116  kv->nitems = 0; /* just for safe measure */
117  kv->nalloc = 0;
118  G_free(kv);
119 }
#define G_malloc(n)
Definition: defs/gis.h:112
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:84
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
#define NULL
Definition: ccmath.h:32
void G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:103
char ** value
Definition: gis.h:506
char ** key
Definition: gis.h:505
Definition: gis.h:501
int nalloc
Definition: gis.h:504
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
#define G_realloc(p, n)
Definition: defs/gis.h:114
int nitems
Definition: gis.h:503
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
Definition: key_value1.c:38
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition: key_value1.c:23