GRASS 8 Programmer's Manual 8.6.0dev(2026)-0f6a7341fc
Loading...
Searching...
No Matches
key_value3.c
Go to the documentation of this file.
1/*!
2 \file lib/gis/key_value3.c
3
4 \brief Key_Value management.
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 <errno.h>
13#include <string.h>
14#include <grass/gis.h>
15#include <grass/glocale.h>
16
17/*!
18 \brief Write key/value pairs to file
19
20 \param file filename for writing
21 \param kv pointer Key_Value structure
22
23 \return 0 success
24 \return 1 error writing
25 */
26void G_write_key_value_file(const char *file, const struct Key_Value *kv)
27{
28 FILE *fp = fopen(file, "w");
29
30 if (!fp)
31 G_fatal_error(_("Unable to open output file <%s>: %s"), file,
33
34 if (G_fwrite_key_value(fp, kv) != 0)
35 G_fatal_error(_("Error writing file <%s>: %s"), file, strerror(errno));
36
37 if (fclose(fp) != 0)
38 G_fatal_error(_("Error closing output file <%s>: %s"), file,
40}
41
42/*!
43 \brief Read key/values pairs from file
44
45 Allocated memory must be freed G_free_key_value(). Call
46 G_fatal_error() when unable to read key/value items from the file.
47
48 \param[in] file filename for reading
49
50 \return pointer to allocated Key_Value structure
51 \return NULL on error
52 */
54{
55 FILE *fp;
56 struct Key_Value *kv;
57
58 fp = fopen(file, "r");
59 if (!fp)
60 G_fatal_error(_("Unable to open input file <%s>: %s"), file,
62
64 if (!kv)
65 G_fatal_error(_("Error reading file <%s>: %s"), file, strerror(errno));
66
67 if (fclose(fp) != 0)
68 G_fatal_error(_("Error closing input file <%s>: %s"), file,
70
71 return kv;
72}
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int G_fwrite_key_value(FILE *, const struct Key_Value *)
Write key/value pairs to file.
Definition key_value2.c:23
struct Key_Value * G_fread_key_value(FILE *)
Read key/values pairs from file.
Definition key_value2.c:47
#define _(str)
Definition glocale.h:10
void G_write_key_value_file(const char *file, const struct Key_Value *kv)
Write key/value pairs to file.
Definition key_value3.c:26
struct Key_Value * G_read_key_value_file(const char *file)
Read key/values pairs from file.
Definition key_value3.c:53
#define file