keys.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "raster3d_intern.h"
  4. /*---------------------------------------------------------------------------*/
  5. int Rast3d_key_get_int(struct Key_Value *keys, const char *key, int *i)
  6. {
  7. const char *str;
  8. if ((str = G_find_key_value(key, keys)) == NULL) {
  9. Rast3d_error("Rast3d_key_get_int: cannot find field %s in key structure",
  10. key);
  11. return 0;
  12. }
  13. if (sscanf(str, "%d", i) == 1)
  14. return 1;
  15. Rast3d_error("Rast3d_key_get_int: invalid value: field %s in key structure", key);
  16. return 0;
  17. }
  18. /*---------------------------------------------------------------------------*/
  19. int Rast3d_key_get_double(struct Key_Value *keys, const char *key, double *d)
  20. {
  21. const char *str;
  22. if ((str = G_find_key_value(key, keys)) == NULL) {
  23. Rast3d_error("Rast3d_key_get_double: cannot find field %s in key structure",
  24. key);
  25. return 0;
  26. }
  27. if (sscanf(str, "%lf", d) == 1)
  28. return 1;
  29. Rast3d_error("Rast3d_key_get_double: invalid value: field %s in key structure",
  30. key);
  31. return 0;
  32. }
  33. /*---------------------------------------------------------------------------*/
  34. int
  35. Rast3d_key_get_string(struct Key_Value *keys, const char *key, char **returnStr)
  36. {
  37. const char *str;
  38. if ((str = G_find_key_value(key, keys)) == NULL) {
  39. Rast3d_error("Rast3d_key_get_string: cannot find field %s in key structure",
  40. key);
  41. return 0;
  42. }
  43. *returnStr = G_store(str);
  44. return 1;
  45. }
  46. /*---------------------------------------------------------------------------*/
  47. int
  48. Rast3d_key_get_value(struct Key_Value *keys, const char *key, char *val1,
  49. char *val2, int result1, int result2, int *resultVar)
  50. {
  51. const char *str;
  52. if ((str = G_find_key_value(key, keys)) == NULL) {
  53. Rast3d_error("Rast3d_key_get_value: cannot find field %s in key structure",
  54. key);
  55. return 0;
  56. }
  57. if (strcmp(str, val1) == 0) {
  58. *resultVar = result1;
  59. return 1;
  60. }
  61. if (strcmp(str, val2) == 0) {
  62. *resultVar = result2;
  63. return 1;
  64. }
  65. Rast3d_error("Rast3d_key_get_value: invalid type: field %s in key structure",
  66. key);
  67. return 0;
  68. }
  69. /*---------------------------------------------------------------------------*/
  70. int Rast3d_key_set_int(struct Key_Value *keys, const char *key, const int *i)
  71. {
  72. char keyValStr[200];
  73. sprintf(keyValStr, "%d", *i);
  74. G_set_key_value(key, keyValStr, keys);
  75. return 1;
  76. }
  77. /*---------------------------------------------------------------------------*/
  78. int Rast3d_key_set_double(struct Key_Value *keys, const char *key, const double *d)
  79. {
  80. char keyValStr[200];
  81. sprintf(keyValStr, "%.50f", *d);
  82. G_set_key_value(key, keyValStr, keys);
  83. return 1;
  84. }
  85. /*---------------------------------------------------------------------------*/
  86. int
  87. Rast3d_key_set_string(struct Key_Value *keys, const char *key,
  88. char *const *keyValStr)
  89. {
  90. G_set_key_value(key, *keyValStr, keys);
  91. return 1;
  92. }
  93. /*---------------------------------------------------------------------------*/
  94. int
  95. Rast3d_key_set_value(struct Key_Value *keys, const char *key, const char *val1,
  96. const char *val2, int keyval1, int keyval2,
  97. const int *keyvalVar)
  98. {
  99. if (*keyvalVar == keyval1) {
  100. G_set_key_value(key, val1, keys);
  101. return 1;
  102. }
  103. if (*keyvalVar == keyval2) {
  104. G_set_key_value(key, val2, keys);
  105. return 1;
  106. }
  107. Rast3d_error("Rast3d_key_set_value: wrong key value");
  108. return 0;
  109. }