GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
valuefmt.c
Go to the documentation of this file.
1 /*!
2  \file lib/db/dbmi_base/valuefmt.c
3 
4  \brief DBMI Library (base) - value formatting
5 
6  (C) 1999-2009, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Joel Jones (CERL/UIUC), Radim Blazek
12  \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
13  */
14 
15 #include <stdio.h>
16 #include <string.h>
17 #include <grass/gis.h>
18 #include <grass/dbmi.h>
19 #include <grass/glocale.h>
20 
21 /*!
22  \brief Convert string to value
23 
24  \param Cstring string buffer
25  \param sqltype SQL data type
26  \param[out] value pointer to dbValue
27 
28  \return DB_OK on success
29  \return DB_FAILED on error
30  */
31 int db_convert_Cstring_to_value(const char *Cstring, int sqltype,
32  dbValue *value)
33 {
34  int i;
35  double d;
36 
37  switch (db_sqltype_to_Ctype(sqltype)) {
38  case DB_C_TYPE_STRING:
39  return db_set_value_string(value, Cstring);
40  case DB_C_TYPE_INT:
41  i = 0;
42  sscanf(Cstring, "%d", &i);
43  db_set_value_int(value, i);
44  break;
45  case DB_C_TYPE_DOUBLE:
46  d = 0.0;
47  sscanf(Cstring, "%lf", &d);
48  db_set_value_double(value, d);
49  break;
50  case DB_C_TYPE_DATETIME:
51  return db_convert_Cstring_to_value_datetime(Cstring, sqltype, value);
52  default:
53  db_error(_("db_convert_Cstring_to_value(): unrecognized sqltype"));
54  return DB_FAILED;
55  }
56  return DB_OK;
57 }
58 
59 /*!
60  \brief Convert value to string
61 
62  \param value pointer to dbValue
63  \param sqltype SQL data type
64  \param[out] string pointer to dbString
65 
66  \return DB_OK on success
67  */
68 int db_convert_value_to_string(dbValue *value, int sqltype, dbString *string)
69 {
70  char buf[64];
71  const char *bp = buf;
72 
73  if (db_test_value_isnull(value)) {
74  *buf = 0;
75  }
76  else {
77  switch (db_sqltype_to_Ctype(sqltype)) {
78  case DB_C_TYPE_INT:
79  sprintf(buf, "%d", db_get_value_int(value));
80  break;
81  case DB_C_TYPE_DOUBLE:
82  sprintf(buf, "%.15g", db_get_value_double(value));
83  G_trim_decimal(buf);
84  break;
85  case DB_C_TYPE_STRING:
86  bp = db_get_value_string(value);
87  break;
88  case DB_C_TYPE_DATETIME:
89  return db_convert_value_datetime_into_string(value, sqltype,
90  string);
91  default:
92  db_error(
93  _("db_convert_value_into_string(): unrecongized sqltype-type"));
94  return DB_FAILED;
95  }
96  }
97  return db_set_string(string, bp);
98 }
#define DB_C_TYPE_INT
Definition: dbmi.h:108
#define DB_FAILED
Definition: dbmi.h:72
#define DB_C_TYPE_STRING
Definition: dbmi.h:107
#define DB_C_TYPE_DOUBLE
Definition: dbmi.h:109
#define DB_OK
Definition: dbmi.h:71
#define DB_C_TYPE_DATETIME
Definition: dbmi.h:110
int db_test_value_isnull(dbValue *)
Check of value is null.
Definition: value.c:26
const char * db_get_value_string(dbValue *)
Get string value.
Definition: value.c:92
double db_get_value_double(dbValue *)
Get double precision value.
Definition: value.c:50
int db_set_value_string(dbValue *, const char *)
Set string value.
Definition: value.c:219
int db_sqltype_to_Ctype(int)
Get C data type based on given SQL data type.
Definition: sqlCtype.c:24
int db_convert_value_datetime_into_string(dbValue *, int, dbString *)
Convert datetime value into string.
Definition: datetime.c:31
void db_set_value_double(dbValue *, double)
Set double precision value.
Definition: value.c:207
int db_convert_Cstring_to_value_datetime(const char *, int, dbValue *)
Convert datetime string to value.
Definition: datetime.c:196
void db_set_value_int(dbValue *, int)
Set integer value.
Definition: value.c:195
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
int db_get_value_int(dbValue *)
Get integer value.
Definition: value.c:38
void db_error(const char *)
Report error message.
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:24
#define _(str)
Definition: glocale.h:10
int db_convert_value_to_string(dbValue *value, int sqltype, dbString *string)
Convert value to string.
Definition: valuefmt.c:68
int db_convert_Cstring_to_value(const char *Cstring, int sqltype, dbValue *value)
Convert string to value.
Definition: valuefmt.c:31