GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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,
29  const struct Key_Value *kv)
30 {
31  FILE *fp = fopen(file, "w");
32  if (!fp)
33  G_fatal_error(_("Unable to open output file <%s>: %s"), file, strerror(errno));
34 
35  if (G_fwrite_key_value(fp, kv) != 0)
36  G_fatal_error(_("Error writing file <%s>: %s"), file, strerror(errno));
37 
38  if (fclose(fp) != 0)
39  G_fatal_error(_("Error closing output file <%s>: %s"), file, strerror(errno));
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  */
53 struct Key_Value *G_read_key_value_file(const char *file)
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, strerror(errno));
61 
62  kv = G_fread_key_value(fp);
63  if (!kv)
64  G_fatal_error(_("Error reading file <%s>: %s"), file, strerror(errno));
65 
66  if (fclose(fp) != 0)
67  G_fatal_error(_("Error closing input file <%s>: %s"), file, strerror(errno));
68 
69  return kv;
70 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
struct Key_Value * G_fread_key_value(FILE *)
Read key/values pairs from file.
Definition: key_value2.c:49
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:53
Definition: gis.h:512
int G_fwrite_key_value(FILE *, const struct Key_Value *)
Write key/value pairs to file.
Definition: key_value2.c:25
#define file
#define _(str)
Definition: glocale.h:10