GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-df949ca623
keys.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include "raster3d_intern.h"
4 
5 /*---------------------------------------------------------------------------*/
6 
7 int Rast3d_key_get_int(struct Key_Value *keys, const char *key, int *i)
8 {
9  const char *str;
10 
11  if ((str = G_find_key_value(key, keys)) == NULL) {
13  "Rast3d_key_get_int: cannot find field %s in key structure", key);
14  return 0;
15  }
16 
17  if (sscanf(str, "%d", i) == 1)
18  return 1;
19 
20  Rast3d_error("Rast3d_key_get_int: invalid value: field %s in key structure",
21  key);
22  return 0;
23 }
24 
25 /*---------------------------------------------------------------------------*/
26 
27 int Rast3d_key_get_double(struct Key_Value *keys, const char *key, double *d)
28 {
29  const char *str;
30 
31  if ((str = G_find_key_value(key, keys)) == NULL) {
33  "Rast3d_key_get_double: cannot find field %s in key structure",
34  key);
35  return 0;
36  }
37 
38  if (sscanf(str, "%lf", d) == 1)
39  return 1;
40 
42  "Rast3d_key_get_double: invalid value: field %s in key structure", key);
43  return 0;
44 }
45 
46 /*---------------------------------------------------------------------------*/
47 
48 int Rast3d_key_get_string(struct Key_Value *keys, const char *key,
49  char **returnStr)
50 {
51  const char *str;
52 
53  if ((str = G_find_key_value(key, keys)) == NULL) {
55  "Rast3d_key_get_string: cannot find field %s in key structure",
56  key);
57  return 0;
58  }
59 
60  *returnStr = G_store(str);
61  return 1;
62 }
63 
64 /*---------------------------------------------------------------------------*/
65 
66 int Rast3d_key_get_value(struct Key_Value *keys, const char *key, char *val1,
67  char *val2, int result1, int result2, int *resultVar)
68 {
69  const char *str;
70 
71  if ((str = G_find_key_value(key, keys)) == NULL) {
73  "Rast3d_key_get_value: cannot find field %s in key structure", key);
74  return 0;
75  }
76 
77  if (strcmp(str, val1) == 0) {
78  *resultVar = result1;
79  return 1;
80  }
81  if (strcmp(str, val2) == 0) {
82  *resultVar = result2;
83  return 1;
84  }
85 
87  "Rast3d_key_get_value: invalid type: field %s in key structure", key);
88  return 0;
89 }
90 
91 /*---------------------------------------------------------------------------*/
92 
93 int Rast3d_key_set_int(struct Key_Value *keys, const char *key, const int *i)
94 {
95  char keyValStr[200];
96 
97  sprintf(keyValStr, "%d", *i);
98  G_set_key_value(key, keyValStr, keys);
99  return 1;
100 }
101 
102 /*---------------------------------------------------------------------------*/
103 
104 int Rast3d_key_set_double(struct Key_Value *keys, const char *key,
105  const double *d)
106 {
107  char keyValStr[200];
108 
109  sprintf(keyValStr, "%.50f", *d);
110  G_set_key_value(key, keyValStr, keys);
111  return 1;
112 }
113 
114 /*---------------------------------------------------------------------------*/
115 
116 int Rast3d_key_set_string(struct Key_Value *keys, const char *key,
117  char *const *keyValStr)
118 {
119  G_set_key_value(key, *keyValStr, keys);
120  return 1;
121 }
122 
123 /*---------------------------------------------------------------------------*/
124 
125 int Rast3d_key_set_value(struct Key_Value *keys, const char *key,
126  const char *val1, const char *val2, int keyval1,
127  int keyval2, const int *keyvalVar)
128 {
129  if (*keyvalVar == keyval1) {
130  G_set_key_value(key, val1, keys);
131  return 1;
132  }
133 
134  if (*keyvalVar == keyval2) {
135  G_set_key_value(key, val2, keys);
136  return 1;
137  }
138 
139  Rast3d_error("Rast3d_key_set_value: wrong key value");
140  return 0;
141 }
#define NULL
Definition: ccmath.h:32
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
Definition: key_value1.c:85
void G_set_key_value(const char *, const char *, struct Key_Value *)
Set value for given key.
Definition: key_value1.c:39
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
void Rast3d_error(const char *,...) __attribute__((format(printf
int Rast3d_key_get_string(struct Key_Value *keys, const char *key, char **returnStr)
Definition: keys.c:48
int Rast3d_key_set_value(struct Key_Value *keys, const char *key, const char *val1, const char *val2, int keyval1, int keyval2, const int *keyvalVar)
Definition: keys.c:125
int Rast3d_key_set_double(struct Key_Value *keys, const char *key, const double *d)
Definition: keys.c:104
int Rast3d_key_get_int(struct Key_Value *keys, const char *key, int *i)
Definition: keys.c:7
int Rast3d_key_get_double(struct Key_Value *keys, const char *key, double *d)
Definition: keys.c:27
int Rast3d_key_get_value(struct Key_Value *keys, const char *key, char *val1, char *val2, int result1, int result2, int *resultVar)
Definition: keys.c:66
int Rast3d_key_set_int(struct Key_Value *keys, const char *key, const int *i)
Definition: keys.c:93
int Rast3d_key_set_string(struct Key_Value *keys, const char *key, char *const *keyValStr)
Definition: keys.c:116
Definition: gis.h:527