key_value3.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*!
  2. \file key_value3.c
  3. \brief Key_Value management.
  4. (C) 2001-2008 by the GRASS Development Team
  5. This program is free software under the
  6. GNU General Public License (>=v2).
  7. Read the file COPYING that comes with GRASS
  8. for details.
  9. \author CERL
  10. */
  11. #include <grass/gis.h>
  12. #include <grass/glocale.h>
  13. /*!
  14. \brief Write key/value pairs to file
  15. \param[in] file filename for writing
  16. \param[in] kv 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>"), file);
  26. if (G_fwrite_key_value(fp, kv) != 0)
  27. G_fatal_error(_("Error writing file <%s>"), file);
  28. if (fclose(fp) != 0)
  29. G_fatal_error(_("Error closing output file <%s>"), file);
  30. }
  31. /*!
  32. \brief Read key/values pairs from file
  33. Allocated memory must be freed G_free_key_value().
  34. \param[in] file filename for reading
  35. \param[out] stat status (0 ok, -1 cannot open file, -2 error writing key/value)
  36. \return poiter 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>"), file);
  46. kv = G_fread_key_value(fp);
  47. if (!kv)
  48. G_fatal_error(_("Error reading file <%s>"), file);
  49. if (fclose(fp) != 0)
  50. G_fatal_error(_("Error closing input file <%s>"), file);
  51. return kv;
  52. }