key_value4.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*!
  2. \file lib/gis/key_value4.c
  3. \brief Key_Value management.
  4. (C) 2001-2014 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 <string.h>
  13. /*!
  14. \brief Update file, set up value for given key
  15. \param[in] file filename to be updated
  16. \param[in] key key value
  17. \param[in] value value to be updated
  18. */
  19. void G_update_key_value_file(const char *file,
  20. const char *key, const char *value)
  21. {
  22. struct Key_Value *kv;
  23. kv = G_read_key_value_file(file);
  24. G_set_key_value(key, value, kv);
  25. G_write_key_value_file(file, kv);
  26. G_free_key_value(kv);
  27. }
  28. /*!
  29. \brief Look up for key in file
  30. \param[in] file filename
  31. \param[in] key key to be found in file
  32. \param[out] value value for key
  33. \param[in] n number of characters to be copied
  34. \return 0 not found
  35. \return 1 ok
  36. */
  37. int G_lookup_key_value_from_file(const char *file,
  38. const char *key, char value[], int n)
  39. {
  40. struct Key_Value *kv;
  41. const char *v;
  42. *value = '\0';
  43. kv = G_read_key_value_file(file);
  44. v = G_find_key_value(key, kv);
  45. if (v) {
  46. strncpy(value, v, n);
  47. value[n - 1] = '\0';
  48. }
  49. G_free_key_value(kv);
  50. return v ? 1 : 0;
  51. }