GRASS GIS 8 Programmer's Manual 8.4.1(2025)-45ca3179ab
Loading...
Searching...
No Matches
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 */
23struct Key_Value *G_create_key_value(void)
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 */
39void 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 */
85const 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 */
104void 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}
void G_free(void *buf)
Free allocated memory.
Definition alloc.c:150
#define NULL
Definition ccmath.h:32
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
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition key_value1.c:85
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition key_value1.c:23
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
void G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition zero.c:23