GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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, dbValue * value)
32 {
33  int i;
34  double d;
35 
36  switch (db_sqltype_to_Ctype(sqltype)) {
37  case DB_C_TYPE_STRING:
38  return db_set_value_string(value, Cstring);
39  case DB_C_TYPE_INT:
40  i = 0;
41  sscanf(Cstring, "%d", &i);
42  db_set_value_int(value, i);
43  break;
44  case DB_C_TYPE_DOUBLE:
45  d = 0.0;
46  sscanf(Cstring, "%lf", &d);
47  db_set_value_double(value, d);
48  break;
49  case DB_C_TYPE_DATETIME:
50  return db_convert_Cstring_to_value_datetime(Cstring, sqltype, value);
51  default:
52  db_error(_("db_convert_Cstring_to_value(): unrecognized sqltype"));
53  return DB_FAILED;
54  }
55  return DB_OK;
56 }
57 
58 /*!
59  \brief Convert value to string
60 
61  \param value pointer to dbValue
62  \param sqltype SQL data type
63  \param[out] string pointer to dbString
64 
65  \return DB_OK on success
66 */
67 int db_convert_value_to_string(dbValue * value, int sqltype, dbString * string)
68 {
69  char buf[64];
70  const char *bp = buf;
71 
72  if (db_test_value_isnull(value)) {
73  *buf = 0;
74  }
75  else {
76  switch (db_sqltype_to_Ctype(sqltype)) {
77  case DB_C_TYPE_INT:
78  sprintf(buf, "%d", db_get_value_int(value));
79  break;
80  case DB_C_TYPE_DOUBLE:
81  sprintf(buf, "%.15g", db_get_value_double(value));
82  G_trim_decimal(buf);
83  break;
84  case DB_C_TYPE_STRING:
85  bp = db_get_value_string(value);
86  break;
87  case DB_C_TYPE_DATETIME:
88  return db_convert_value_datetime_into_string(value, sqltype,
89  string);
90  default:
91  db_error(_("db_convert_value_into_string(): unrecongized sqltype-type"));
92  return DB_FAILED;
93  }
94  }
95  return db_set_string(string, bp);
96 }
#define DB_C_TYPE_DATETIME
Definition: dbmi.h:110
int db_get_value_int(dbValue *)
Get integer value.
Definition: value.c:38
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
int db_convert_value_to_string(dbValue *value, int sqltype, dbString *string)
Convert value to string.
Definition: valuefmt.c:67
int db_test_value_isnull(dbValue *)
Check of value is null.
Definition: value.c:26
#define DB_C_TYPE_STRING
Definition: dbmi.h:107
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
#define DB_C_TYPE_INT
Definition: dbmi.h:108
void db_set_value_double(dbValue *, double)
Set double precision value.
Definition: value.c:207
void db_error(const char *)
Report error message.
int db_convert_value_datetime_into_string(dbValue *, int, dbString *)
Convert datetime value into string.
Definition: datetime.c:31
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
#define DB_FAILED
Definition: dbmi.h:72
#define DB_C_TYPE_DOUBLE
Definition: dbmi.h:109
void db_set_value_int(dbValue *, int)
Set integer value.
Definition: value.c:195
#define _(str)
Definition: glocale.h:10
int db_convert_Cstring_to_value_datetime(const char *, int, dbValue *)
Convert datetime string to value.
Definition: datetime.c:197
int db_convert_Cstring_to_value(const char *Cstring, int sqltype, dbValue *value)
Convert string to value.
Definition: valuefmt.c:31
#define DB_OK
Definition: dbmi.h:71
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:24