-
-
Notifications
You must be signed in to change notification settings - Fork 291
/
valuefmt.c
96 lines (83 loc) · 2.28 KB
/
valuefmt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*!
\file lib/db/dbmi_base/valuefmt.c
\brief DBMI Library (base) - value formatting
(C) 1999-2009, 2011 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
\author Joel Jones (CERL/UIUC), Radim Blazek
\author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
*/
#include <stdio.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
/*!
\brief Convert string to value
\param Cstring string buffer
\param sqltype SQL data type
\param[out] value pointer to dbValue
\return DB_OK on success
\return DB_FAILED on error
*/
int db_convert_Cstring_to_value(const char *Cstring, int sqltype, dbValue * value)
{
int i;
double d;
switch (db_sqltype_to_Ctype(sqltype)) {
case DB_C_TYPE_STRING:
return db_set_value_string(value, Cstring);
case DB_C_TYPE_INT:
i = 0;
sscanf(Cstring, "%d", &i);
db_set_value_int(value, i);
break;
case DB_C_TYPE_DOUBLE:
d = 0.0;
sscanf(Cstring, "%lf", &d);
db_set_value_double(value, d);
break;
case DB_C_TYPE_DATETIME:
return db_convert_Cstring_to_value_datetime(Cstring, sqltype, value);
default:
db_error(_("db_convert_Cstring_to_value(): unrecognized sqltype"));
return DB_FAILED;
}
return DB_OK;
}
/*!
\brief Convert value to string
\param value pointer to dbValue
\param sqltype SQL data type
\param[out] string pointer to dbString
\return DB_OK on success
*/
int db_convert_value_to_string(dbValue * value, int sqltype, dbString * string)
{
char buf[64];
const char *bp = buf;
if (db_test_value_isnull(value)) {
*buf = 0;
}
else {
switch (db_sqltype_to_Ctype(sqltype)) {
case DB_C_TYPE_INT:
sprintf(buf, "%d", db_get_value_int(value));
break;
case DB_C_TYPE_DOUBLE:
sprintf(buf, "%.15g", db_get_value_double(value));
G_trim_decimal(buf);
break;
case DB_C_TYPE_STRING:
bp = db_get_value_string(value);
break;
case DB_C_TYPE_DATETIME:
return db_convert_value_datetime_into_string(value, sqltype,
string);
default:
db_error(_("db_convert_value_into_string(): unrecongized sqltype-type"));
return DB_FAILED;
}
}
return db_set_string(string, bp);
}