GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
key_value2.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/key_value2.c
3
4 \brief Read/write Key_Value from/to file.
5
6 SPDX-FileCopyrightText: 2001-2008, 2012 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author CERL
10 */
11
12#include <grass/gis.h>
13
14/*!
15 \brief Write key/value pairs to file
16
17 \param[in,out] fd file to write to
18 \param kv pointer Key_Value structure
19
20 \return 0 success
21 \return -1 error writing
22 */
23int G_fwrite_key_value(FILE *fd, const struct Key_Value *kv)
24{
25 int n;
26 int err;
27
28 err = 0;
29 for (n = 0; n < kv->nitems; n++)
30 if (kv->value[n][0]) {
31 if (EOF == fprintf(fd, "%s: %s\n", kv->key[n], kv->value[n]))
32 err = -1;
33 }
34 return err;
35}
36
37/*!
38 \brief Read key/values pairs from file
39
40 Allocated memory must be freed G_free_key_value().
41
42 \param fd file to read key/values from
43
44 \return pointer to allocated Key_Value structure
45 \return NULL on error
46 */
48{
49 struct Key_Value *kv;
50 char *key, *value;
51 char buf[1024];
52
54 if (kv == NULL)
55 return NULL;
56 while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
57 key = value = buf;
58 while (*value && *value != ':')
59 value++;
60 if (*value != ':')
61 continue;
62 *value++ = 0;
63 G_strip(key);
66 }
67 return kv;
68}
#define NULL
Definition ccmath.h:32
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition getl.c:58
void G_set_key_value(const char *, const char *, struct Key_Value *)
Set value for given key.
Definition key_value1.c:37
void G_strip(char *)
Removes all leading and trailing white space from string.
Definition strings.c:298
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition key_value1.c:21
struct Key_Value * G_fread_key_value(FILE *fd)
Read key/values pairs from file.
Definition key_value2.c:47
int G_fwrite_key_value(FILE *fd, const struct Key_Value *kv)
Write key/value pairs to file.
Definition key_value2.c:23
char ** value
Definition gis.h:535
char ** key
Definition gis.h:534
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)