GRASS Programmer's Manual  6.4.2(2012)
g3dkeys.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include "G3d_intern.h"
00004 
00005 /*---------------------------------------------------------------------------*/
00006 
00007 int G3d_keyGetInt(struct Key_Value *keys, const char *key, int *i)
00008 {
00009     char *str;
00010 
00011     if ((str = G_find_key_value(key, keys)) == NULL) {
00012         G3d_error("G3d_keyGetInt: cannot find field %s in key structure",
00013                   key);
00014         return 0;
00015     }
00016 
00017     if (sscanf(str, "%d", i) == 1)
00018         return 1;
00019 
00020     G3d_error("G3d_keyGetInt: invalid value: field %s in key structure", key);
00021     return 0;
00022 }
00023 
00024 /*---------------------------------------------------------------------------*/
00025 
00026 int G3d_keyGetDouble(struct Key_Value *keys, const char *key, double *d)
00027 {
00028     char *str;
00029 
00030     if ((str = G_find_key_value(key, keys)) == NULL) {
00031         G3d_error("G3d_keyGetDouble: cannot find field %s in key structure",
00032                   key);
00033         return 0;
00034     }
00035 
00036     if (sscanf(str, "%lf", d) == 1)
00037         return 1;
00038 
00039     G3d_error("G3d_keyGetDouble: invalid value: field %s in key structure",
00040               key);
00041     return 0;
00042 }
00043 
00044 /*---------------------------------------------------------------------------*/
00045 
00046 int
00047 G3d_keyGetString(struct Key_Value *keys, const char *key, char **returnStr)
00048 {
00049     char *str;
00050 
00051     if ((str = G_find_key_value(key, keys)) == NULL) {
00052         G3d_error("G3d_keyGetString: cannot find field %s in key structure",
00053                   key);
00054         return 0;
00055     }
00056 
00057     G_strip(str);
00058     *returnStr = G_store(str);
00059     return 1;
00060 }
00061 
00062 /*---------------------------------------------------------------------------*/
00063 
00064 int
00065 G3d_keyGetValue(struct Key_Value *keys, const char *key, char *val1,
00066                 char *val2, int result1, int result2, int *resultVar)
00067 {
00068     char *str;
00069 
00070     if ((str = G_find_key_value(key, keys)) == NULL) {
00071         G3d_error("G3d_keyGetValue: cannot find field %s in key structure",
00072                   key);
00073         return 0;
00074     }
00075 
00076     G_strip(str);
00077     if (strcmp(str, val1) == 0) {
00078         *resultVar = result1;
00079         return 1;
00080     }
00081     if (strcmp(str, val2) == 0) {
00082         *resultVar = result2;
00083         return 1;
00084     }
00085 
00086     G3d_error("G3d_keyGetValue: invalid type: field %s in key structure",
00087               key);
00088     return 0;
00089 }
00090 
00091 /*---------------------------------------------------------------------------*/
00092 
00093 int G3d_keySetInt(struct Key_Value *keys, const char *key, const int *i)
00094 {
00095     char keyValStr[200];
00096 
00097     sprintf(keyValStr, "%d", *i);
00098     return (G_set_key_value(key, keyValStr, keys) != 0);
00099 }
00100 
00101 /*---------------------------------------------------------------------------*/
00102 
00103 int G3d_keySetDouble(struct Key_Value *keys, const char *key, const double *d)
00104 {
00105     char keyValStr[200];
00106 
00107     sprintf(keyValStr, "%.50f", *d);
00108     return (G_set_key_value(key, keyValStr, keys) != 0);
00109 }
00110 
00111 /*---------------------------------------------------------------------------*/
00112 
00113 int
00114 G3d_keySetString(struct Key_Value *keys, const char *key,
00115                  char *const *keyValStr)
00116 {
00117     return (G_set_key_value(key, *keyValStr, keys) != 0);
00118 }
00119 
00120 /*---------------------------------------------------------------------------*/
00121 
00122 int
00123 G3d_keySetValue(struct Key_Value *keys, const char *key, const char *val1,
00124                 const char *val2, int keyval1, int keyval2,
00125                 const int *keyvalVar)
00126 {
00127     if (*keyvalVar == keyval1)
00128         return (G_set_key_value(key, val1, keys) != 0);
00129     if (*keyvalVar == keyval2)
00130         return (G_set_key_value(key, val2, keys) != 0);
00131 
00132     G3d_error("G3d_keySetValue: wrong key value");
00133     return 0;
00134 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines