key_value3.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /*!
  13. \brief Write key/value pairs to file
  14. \param[in] file filename for writing
  15. \param[in] kv Key_Value structure
  16. \param[out] stat status (0 ok, -3 cannot open file, -4 error writing key/value)
  17. \return 0 success
  18. \return 1 error writing
  19. */
  20. int G_write_key_value_file(const char *file,
  21. const struct Key_Value *kv, int *stat)
  22. {
  23. FILE *fd;
  24. *stat = 0;
  25. fd = fopen(file, "w");
  26. if (fd == NULL)
  27. *stat = -3;
  28. else if (G_fwrite_key_value(fd, kv) != 0 || fclose(fd) == EOF)
  29. *stat = -4;
  30. return (*stat != 0);
  31. }
  32. /*!
  33. \brief Read key/values pairs from file
  34. Allocated memory must be freed G_free_key_value().
  35. \param[in] file filename for reading
  36. \param[out] stat status (0 ok, -1 cannot open file, -2 error writing key/value)
  37. \return poiter to allocated Key_Value structure
  38. \return NULL on error
  39. */
  40. struct Key_Value *G_read_key_value_file(const char *file, int *stat)
  41. {
  42. FILE *fd;
  43. struct Key_Value *kv;
  44. *stat = 0;
  45. fd = fopen(file, "r");
  46. if (fd == NULL) {
  47. *stat = -1;
  48. return NULL;
  49. }
  50. kv = G_fread_key_value(fd);
  51. fclose(fd);
  52. if (kv == NULL)
  53. *stat = -2;
  54. return kv;
  55. }