GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
key_value1.c
Go to the documentation of this file.
1 
16 #include <string.h>
17 #include <stdlib.h>
18 #include <grass/gis.h>
19 
25 struct Key_Value *G_create_key_value(void)
26 {
27  struct Key_Value *kv;
28 
29  kv = (struct Key_Value *)G_malloc(sizeof(struct Key_Value));
30  if (kv == NULL)
31  return NULL;
32 
33  kv->nitems = 0;
34  kv->nalloc = 0;
35  kv->key = (char **)NULL;
36  kv->value = (char **)NULL;
37 
38  return kv;
39 }
40 
55 int G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
56 {
57  int n;
58  int size;
59 
60  if (key == NULL || key == 0)
61  return 1;
62 
63  for (n = 0; n < kv->nitems; n++)
64  if (strcmp(key, kv->key[n]) == 0)
65  break;
66 
67  if (n == kv->nitems) {
68  if (n >= kv->nalloc) {
69  if (kv->nalloc <= 0) {
70  kv->nalloc = 8;
71  size = kv->nalloc * sizeof(char *);
72  kv->key = (char **)G_malloc(size);
73  kv->value = (char **)G_malloc(size);
74  }
75  else {
76  kv->nalloc *= 2;
77  size = kv->nalloc * sizeof(char *);
78  kv->key = (char **)G_realloc(kv->key, size);
79  kv->value = (char **)G_realloc(kv->value, size);
80  }
81 
82  if (kv->key == NULL || kv->value == NULL) {
83  if (kv->key) {
84  G_free(kv->key);
85  kv->key = NULL;
86  }
87  if (kv->value) {
88  G_free(kv->value);
89  kv->value = NULL;
90  }
91  kv->nitems = kv->nalloc = 0;
92  return 0;
93  }
94  }
95  kv->value[n] = NULL;
96  kv->key[n] = G_malloc(strlen(key) + 1);
97  if (kv->key[n] == NULL)
98  return 0;
99  strcpy(kv->key[n], key);
100  kv->nitems++;
101  }
102  if (value == NULL)
103  size = 0;
104  else
105  size = strlen(value);
106  if (kv->value[n] != NULL)
107  G_free(kv->value[n]);
108  if (size > 0) {
109  kv->value[n] = G_malloc(size + 1);
110  if (kv->value[n] == NULL)
111  return 0;
112  strcpy(kv->value[n], value);
113  }
114  else
115  kv->value[n] = NULL;
116  return 2;
117 }
118 
128 char *G_find_key_value(const char *key, const struct Key_Value *kv)
129 {
130  int n;
131 
132  for (n = 0; n < kv->nitems; n++)
133  if (strcmp(key, kv->key[n]) == 0)
134  return kv->value[n][0] ? kv->value[n] : NULL;
135  return NULL;
136 }
137 
145 int G_free_key_value(struct Key_Value *kv)
146 {
147  int n;
148 
149  if (!kv)
150  return 0;
151 
152  for (n = 0; n < kv->nitems; n++) {
153  G_free(kv->key[n]);
154  G_free(kv->value[n]);
155  }
156  G_free(kv->key);
157  G_free(kv->value);
158  kv->nitems = 0; /* just for safe measure */
159  kv->nalloc = 0;
160  G_free(kv);
161 
162  return 0;
163 }
char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key.
Definition: key_value1.c:128
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:145
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
char * value
Definition: env.c:30
return NULL
Definition: dbfopen.c:1394
int n
Definition: dataquad.c:291
int G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
Definition: key_value1.c:55
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition: key_value1.c:25