key_value3.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <errno.h>
  10. #include <string.h>
  11. #include <grass/gis.h>
  12. #include <grass/glocale.h>
  13. /*!
  14. \brief Write key/value pairs to file
  15. \param file filename for writing
  16. \param kv pointer Key_Value structure
  17. \return 0 success
  18. \return 1 error writing
  19. */
  20. void G_write_key_value_file(const char *file,
  21. const struct Key_Value *kv)
  22. {
  23. FILE *fp = fopen(file, "w");
  24. if (!fp)
  25. G_fatal_error(_("Unable to open output file <%s>: %s"), file, strerror(errno));
  26. if (G_fwrite_key_value(fp, kv) != 0)
  27. G_fatal_error(_("Error writing file <%s>: %s"), file, strerror(errno));
  28. if (fclose(fp) != 0)
  29. G_fatal_error(_("Error closing output file <%s>: %s"), file, strerror(errno));
  30. }
  31. /*!
  32. \brief Read key/values pairs from file
  33. Allocated memory must be freed G_free_key_value(). Call
  34. G_fatal_error() when unable to read key/value items from the file.
  35. \param[in] file filename for reading
  36. \return pointer to allocated Key_Value structure
  37. \return NULL on error
  38. */
  39. struct Key_Value *G_read_key_value_file(const char *file)
  40. {
  41. FILE *fp;
  42. struct Key_Value *kv;
  43. fp = fopen(file, "r");
  44. if (!fp)
  45. G_fatal_error(_("Unable to open input file <%s>: %s"), file, strerror(errno));
  46. kv = G_fread_key_value(fp);
  47. if (!kv)
  48. G_fatal_error(_("Error reading file <%s>: %s"), file, strerror(errno));
  49. if (fclose(fp) != 0)
  50. G_fatal_error(_("Error closing input file <%s>: %s"), file, strerror(errno));
  51. return kv;
  52. }