GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71942
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 }
int db_test_value_isnull(dbValue *value)
Check of value is null.
Definition: value.c:26
void G_trim_decimal(char *buf)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:24
void db_set_value_int(dbValue *value, int i)
Set integer value.
Definition: value.c:195
#define DB_C_TYPE_DATETIME
Definition: dbmi.h:110
int db_convert_value_to_string(dbValue *value, int sqltype, dbString *string)
Convert value to string.
Definition: valuefmt.c:67
int db_convert_value_datetime_into_string(dbValue *value, int sqltype, dbString *string)
Convert datetime value into string.
Definition: datetime.c:31
int db_sqltype_to_Ctype(int sqltype)
Get C data type based on given SQL data type.
Definition: sqlCtype.c:24
void db_set_value_double(dbValue *value, double d)
Set double precision value.
Definition: value.c:207
double db_get_value_double(dbValue *value)
Get double precision value.
Definition: value.c:50
#define DB_C_TYPE_STRING
Definition: dbmi.h:107
#define DB_C_TYPE_INT
Definition: dbmi.h:108
#define DB_FAILED
Definition: dbmi.h:72
#define DB_C_TYPE_DOUBLE
Definition: dbmi.h:109
int db_set_value_string(dbValue *value, const char *s)
Set string value.
Definition: value.c:219
int db_set_string(dbString *x, const char *s)
Inserts string to dbString (enlarge string)
Definition: string.c:41
#define _(str)
Definition: glocale.h:13
int db_get_value_int(dbValue *value)
Get integer value.
Definition: value.c:38
int db_convert_Cstring_to_value(const char *Cstring, int sqltype, dbValue *value)
Convert string to value.
Definition: valuefmt.c:31
const char * db_get_value_string(dbValue *value)
Get string value.
Definition: value.c:92
int db_convert_Cstring_to_value_datetime(const char *buf, int sqltype, dbValue *value)
Convert datetime string to value.
Definition: datetime.c:197
void db_error(const char *s)
Report error message.
#define DB_OK
Definition: dbmi.h:71