g3dkeys.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "G3d_intern.h"
  4. /*---------------------------------------------------------------------------*/
  5. int G3d_keyGetInt(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. G3d_error("G3d_keyGetInt: cannot find field %s in key structure",
  10. key);
  11. return 0;
  12. }
  13. if (sscanf(str, "%d", i) == 1)
  14. return 1;
  15. G3d_error("G3d_keyGetInt: invalid value: field %s in key structure", key);
  16. return 0;
  17. }
  18. /*---------------------------------------------------------------------------*/
  19. int G3d_keyGetDouble(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. G3d_error("G3d_keyGetDouble: cannot find field %s in key structure",
  24. key);
  25. return 0;
  26. }
  27. if (sscanf(str, "%lf", d) == 1)
  28. return 1;
  29. G3d_error("G3d_keyGetDouble: invalid value: field %s in key structure",
  30. key);
  31. return 0;
  32. }
  33. /*---------------------------------------------------------------------------*/
  34. int
  35. G3d_keyGetString(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. G3d_error("G3d_keyGetString: 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. G3d_keyGetValue(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. G3d_error("G3d_keyGetValue: 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. G3d_error("G3d_keyGetValue: invalid type: field %s in key structure",
  66. key);
  67. return 0;
  68. }
  69. /*---------------------------------------------------------------------------*/
  70. int G3d_keySetInt(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 G3d_keySetDouble(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. G3d_keySetString(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. G3d_keySetValue(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. G3d_error("G3d_keySetValue: wrong key value");
  108. return 0;
  109. }