GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
gis/band_reference.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/band_reference.c
3  *
4  * \brief GIS Library - Band reference management (internal use only)
5  *
6  * (C) 2019 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author Martin Landa (with financial support by mundialis, Bonn, for openEO EU H2020 grant 776242, https://openeo.org)
13  */
14 
15 #include <grass/gis.h>
16 #include <grass/glocale.h>
17 
18 /*!
19  \brief Read band reference identifier from file (internal use only).
20 
21  \param fd file descriptor
22  \param[out] key_val key/value pairs (filename, identifier)
23 
24  \return 1 on success
25  \return -1 error - unable fetch key/value pairs
26  \return -2 error - invalid band reference
27  */
28 int G__read_band_reference(FILE *fd, struct Key_Value **key_val)
29 {
30  const char *filename, *band_ref;
31 
32  *key_val = G_fread_key_value(fd);
33  if (*key_val) {
34  G_debug(1, "No band reference detected");
35  return -1;
36  }
37 
38  filename = G_find_key_value("file", *key_val);
39  band_ref = G_find_key_value("identifier", *key_val);
40  if (!filename || !band_ref) {
41  G_debug(1, "Invalid band reference identifier");
42  return -2;
43  }
44 
45  G_debug(1, "Band reference <%s> (%s)", band_ref, filename);
46 
47  return 1;
48 }
49 
50 /*!
51  \brief Write band reference identifier to file (internal use only).
52 
53  \param fd file descriptor
54  \param filename filename JSON reference
55  \param band_reference band reference identifier
56 
57  \return 1 on success
58  \return -1 error - unable to write key/value pairs into fileo
59 */
61  const char *filename, const char *band_reference)
62 {
63  struct Key_Value *key_val;
64 
65  key_val = G_create_key_value();
66  G_set_key_value("file", filename, key_val);
67  G_set_key_value("identifier", band_reference, key_val);
68 
69  if (G_fwrite_key_value(fd, key_val) < 0) {
70  G_debug(1, "Error writing band reference file");
71  G_free_key_value(key_val);
72  return -1;
73  }
74  G_free_key_value(key_val);
75 
76  return 1;
77 }
78 
struct Key_Value * G_fread_key_value(FILE *)
Read key/values pairs from file.
Definition: key_value2.c:49
int G__read_band_reference(FILE *fd, struct Key_Value **key_val)
Read band reference identifier from file (internal use only).
void G_set_key_value(const char *, const char *, struct Key_Value *)
Set value for given key.
Definition: key_value1.c:38
Definition: gis.h:501
int G_fwrite_key_value(FILE *, const struct Key_Value *)
Write key/value pairs to file.
Definition: key_value2.c:25
int G__write_band_reference(FILE *fd, const char *filename, const char *band_reference)
Write band reference identifier to file (internal use only).
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition: key_value1.c:23
int G_debug(int, const char *,...) __attribute__((format(printf
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
Definition: key_value1.c:103
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
Definition: key_value1.c:84