GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dkeys.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include "G3d_intern.h"
4 
5 /*---------------------------------------------------------------------------*/
6 
7 int G3d_keyGetInt(struct Key_Value *keys, const char *key, int *i)
8 {
9  char *str;
10 
11  if ((str = G_find_key_value(key, keys)) == NULL) {
12  G3d_error("G3d_keyGetInt: 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  G3d_error("G3d_keyGetInt: invalid value: field %s in key structure", key);
21  return 0;
22 }
23 
24 /*---------------------------------------------------------------------------*/
25 
26 int G3d_keyGetDouble(struct Key_Value *keys, const char *key, double *d)
27 {
28  char *str;
29 
30  if ((str = G_find_key_value(key, keys)) == NULL) {
31  G3d_error("G3d_keyGetDouble: 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  G3d_error("G3d_keyGetDouble: invalid value: field %s in key structure",
40  key);
41  return 0;
42 }
43 
44 /*---------------------------------------------------------------------------*/
45 
46 int
47 G3d_keyGetString(struct Key_Value *keys, const char *key, char **returnStr)
48 {
49  char *str;
50 
51  if ((str = G_find_key_value(key, keys)) == NULL) {
52  G3d_error("G3d_keyGetString: cannot find field %s in key structure",
53  key);
54  return 0;
55  }
56 
57  G_strip(str);
58  *returnStr = G_store(str);
59  return 1;
60 }
61 
62 /*---------------------------------------------------------------------------*/
63 
64 int
65 G3d_keyGetValue(struct Key_Value *keys, const char *key, char *val1,
66  char *val2, int result1, int result2, int *resultVar)
67 {
68  char *str;
69 
70  if ((str = G_find_key_value(key, keys)) == NULL) {
71  G3d_error("G3d_keyGetValue: cannot find field %s in key structure",
72  key);
73  return 0;
74  }
75 
76  G_strip(str);
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 
86  G3d_error("G3d_keyGetValue: invalid type: field %s in key structure",
87  key);
88  return 0;
89 }
90 
91 /*---------------------------------------------------------------------------*/
92 
93 int G3d_keySetInt(struct Key_Value *keys, const char *key, const int *i)
94 {
95  char keyValStr[200];
96 
97  sprintf(keyValStr, "%d", *i);
98  return (G_set_key_value(key, keyValStr, keys) != 0);
99 }
100 
101 /*---------------------------------------------------------------------------*/
102 
103 int G3d_keySetDouble(struct Key_Value *keys, const char *key, const double *d)
104 {
105  char keyValStr[200];
106 
107  sprintf(keyValStr, "%.50f", *d);
108  return (G_set_key_value(key, keyValStr, keys) != 0);
109 }
110 
111 /*---------------------------------------------------------------------------*/
112 
113 int
114 G3d_keySetString(struct Key_Value *keys, const char *key,
115  char *const *keyValStr)
116 {
117  return (G_set_key_value(key, *keyValStr, keys) != 0);
118 }
119 
120 /*---------------------------------------------------------------------------*/
121 
122 int
123 G3d_keySetValue(struct Key_Value *keys, const char *key, const char *val1,
124  const char *val2, int keyval1, int keyval2,
125  const int *keyvalVar)
126 {
127  if (*keyvalVar == keyval1)
128  return (G_set_key_value(key, val1, keys) != 0);
129  if (*keyvalVar == keyval2)
130  return (G_set_key_value(key, val2, keys) != 0);
131 
132  G3d_error("G3d_keySetValue: wrong key value");
133  return 0;
134 }
char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key.
Definition: key_value1.c:128
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G3d_keyGetValue(struct Key_Value *keys, const char *key, char *val1, char *val2, int result1, int result2, int *resultVar)
Definition: g3dkeys.c:65
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
DCELL val1
Definition: g3dcats.c:91
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
int G3d_keyGetDouble(struct Key_Value *keys, const char *key, double *d)
Definition: g3dkeys.c:26
int G3d_keySetInt(struct Key_Value *keys, const char *key, const int *i)
Definition: g3dkeys.c:93
int G3d_keySetValue(struct Key_Value *keys, const char *key, const char *val1, const char *val2, int keyval1, int keyval2, const int *keyvalVar)
Definition: g3dkeys.c:123
DCELL val2
Definition: g3dcats.c:91
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
int G3d_keySetDouble(struct Key_Value *keys, const char *key, const double *d)
Definition: g3dkeys.c:103
int G3d_keyGetString(struct Key_Value *keys, const char *key, char **returnStr)
Definition: g3dkeys.c:47
return NULL
Definition: dbfopen.c:1394
int G3d_keySetString(struct Key_Value *keys, const char *key, char *const *keyValStr)
Definition: g3dkeys.c:114
int G3d_keyGetInt(struct Key_Value *keys, const char *key, int *i)
Definition: g3dkeys.c:7
int G_set_key_value(const char *key, const char *value, struct Key_Value *kv)
Set value for given key.
Definition: key_value1.c:55