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