band_reference.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <grass/gis.h>
  15. #include <grass/glocale.h>
  16. /*!
  17. \brief Read band reference identifier from file (internal use only).
  18. \param fd file descriptor
  19. \param[out] key_val key/value pairs (filename, identifier)
  20. \return 1 on success
  21. \return -1 error - unable fetch key/value pairs
  22. \return -2 error - invalid band reference
  23. */
  24. int G__read_band_reference(FILE *fd, struct Key_Value **key_val)
  25. {
  26. const char *filename, *band_ref;
  27. *key_val = G_fread_key_value(fd);
  28. if (*key_val) {
  29. G_debug(1, "No band reference detected");
  30. return -1;
  31. }
  32. filename = G_find_key_value("file", *key_val);
  33. band_ref = G_find_key_value("identifier", *key_val);
  34. if (!filename || !band_ref) {
  35. G_debug(1, "Invalid band reference identifier");
  36. return -2;
  37. }
  38. G_debug(1, "Band reference <%s> (%s)", band_ref, filename);
  39. return 1;
  40. }
  41. /*!
  42. \brief Write band reference identifier to file (internal use only).
  43. \param fd file descriptor
  44. \param filename filename JSON reference
  45. \param band_reference band reference identifier
  46. \return 1 on success
  47. \return -1 error - unable to write key/value pairs into fileo
  48. */
  49. int G__write_band_reference(FILE *fd,
  50. const char *filename, const char *band_reference)
  51. {
  52. struct Key_Value *key_val;
  53. key_val = G_create_key_value();
  54. G_set_key_value("file", filename, key_val);
  55. G_set_key_value("identifier", band_reference, key_val);
  56. if (G_fwrite_key_value(fd, key_val) < 0) {
  57. G_debug(1, "Error writing band reference file");
  58. G_free_key_value(key_val);
  59. return -1;
  60. }
  61. G_free_key_value(key_val);
  62. return 1;
  63. }