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