GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e384be2c9
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 
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:84
struct Key_Value * G_fread_key_value(FILE *fd)
Read key/values pairs from file.
Definition: key_value2.c:49
int G_fwrite_key_value(FILE *fd, const struct Key_Value *kv)
Write key/value pairs to file.
Definition: key_value2.c:25
void G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:103
int G__read_band_reference(FILE *fd, struct Key_Value **key_val)
Read band reference identifier from file (internal use only).
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
Definition: gis.h:498
int G__write_band_reference(FILE *fd, const char *filename, const char *band_reference)
Write band reference identifier to file (internal use only).
void G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
Definition: key_value1.c:38
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
Definition: key_value1.c:23