key_value3.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*!
  2. \file lib/gis/key_value3.c
  3. \brief Key_Value management.
  4. (C) 2001-2008, 2012 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author CERL
  8. */
  9. #include <grass/gis.h>
  10. #include <grass/glocale.h>
  11. /*!
  12. \brief Write key/value pairs to file
  13. \param file filename for writing
  14. \param kv pointer Key_Value structure
  15. \return 0 success
  16. \return 1 error writing
  17. */
  18. void G_write_key_value_file(const char *file,
  19. const struct Key_Value *kv)
  20. {
  21. FILE *fp = fopen(file, "w");
  22. if (!fp)
  23. G_fatal_error(_("Unable to open output file <%s>"), file);
  24. if (G_fwrite_key_value(fp, kv) != 0)
  25. G_fatal_error(_("Error writing file <%s>"), file);
  26. if (fclose(fp) != 0)
  27. G_fatal_error(_("Error closing output file <%s>"), file);
  28. }
  29. /*!
  30. \brief Read key/values pairs from file
  31. Allocated memory must be freed G_free_key_value(). Call
  32. G_fatal_error() when unable to read key/value items from the file.
  33. \param[in] file filename for reading
  34. \return poiter to allocated Key_Value structure
  35. \return NULL on error
  36. */
  37. struct Key_Value *G_read_key_value_file(const char *file)
  38. {
  39. FILE *fp;
  40. struct Key_Value *kv;
  41. fp = fopen(file, "r");
  42. if (!fp)
  43. G_fatal_error(_("Unable to open input file <%s>"), file);
  44. kv = G_fread_key_value(fp);
  45. if (!kv)
  46. G_fatal_error(_("Error reading file <%s>"), file);
  47. if (fclose(fp) != 0)
  48. G_fatal_error(_("Error closing input file <%s>"), file);
  49. return kv;
  50. }