valuefmt.c
Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <grass/gis.h>
00004 #include <grass/dbmi.h>
00005
00012 int
00013 db_convert_Cstring_to_value(const char *Cstring, int sqltype, dbValue * value)
00014 {
00015 int i;
00016 double d;
00017
00018 switch (db_sqltype_to_Ctype(sqltype)) {
00019 case DB_C_TYPE_STRING:
00020 return db_set_value_string(value, Cstring);
00021 case DB_C_TYPE_INT:
00022 i = 0;
00023 sscanf(Cstring, "%d", &i);
00024 db_set_value_int(value, i);
00025 break;
00026 case DB_C_TYPE_DOUBLE:
00027 d = 0.0;
00028 sscanf(Cstring, "%lf", &d);
00029 db_set_value_double(value, d);
00030 break;
00031 case DB_C_TYPE_DATETIME:
00032 return db_convert_Cstring_to_value_datetime(Cstring, sqltype, value);
00033 default:
00034 db_error("db_convert_Cstring_to_value(): unrecognized sqltype");
00035 return DB_FAILED;
00036 }
00037 return DB_OK;
00038 }
00039
00046 int
00047 db_convert_value_to_string(dbValue * value, int sqltype, dbString * string)
00048 {
00049 char buf[64];
00050 const char *bp = buf;
00051
00052 if (db_test_value_isnull(value)) {
00053 *buf = 0;
00054 }
00055 else {
00056 switch (db_sqltype_to_Ctype(sqltype)) {
00057 case DB_C_TYPE_INT:
00058 sprintf(buf, "%d", db_get_value_int(value));
00059 break;
00060 case DB_C_TYPE_DOUBLE:
00061 sprintf(buf, "%.15g", db_get_value_double(value));
00062 G_trim_decimal(buf);
00063 break;
00064 case DB_C_TYPE_STRING:
00065 bp = db_get_value_string(value);
00066 break;
00067 case DB_C_TYPE_DATETIME:
00068 return db_convert_value_datetime_into_string(value, sqltype,
00069 string);
00070 default:
00071 db_error
00072 ("db_convert_value_into_string(): unrecongized sqltype-type");
00073 return DB_FAILED;
00074 }
00075 }
00076 return db_set_string(string, bp);
00077 }