GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
valuefmt.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <grass/gis.h>
4 #include <grass/dbmi.h>
5 
12 int
13 db_convert_Cstring_to_value(const char *Cstring, int sqltype, dbValue * value)
14 {
15  int i;
16  double d;
17 
18  switch (db_sqltype_to_Ctype(sqltype)) {
19  case DB_C_TYPE_STRING:
20  return db_set_value_string(value, Cstring);
21  case DB_C_TYPE_INT:
22  i = 0;
23  sscanf(Cstring, "%d", &i);
24  db_set_value_int(value, i);
25  break;
26  case DB_C_TYPE_DOUBLE:
27  d = 0.0;
28  sscanf(Cstring, "%lf", &d);
29  db_set_value_double(value, d);
30  break;
31  case DB_C_TYPE_DATETIME:
32  return db_convert_Cstring_to_value_datetime(Cstring, sqltype, value);
33  default:
34  db_error("db_convert_Cstring_to_value(): unrecognized sqltype");
35  return DB_FAILED;
36  }
37  return DB_OK;
38 }
39 
46 int
47 db_convert_value_to_string(dbValue * value, int sqltype, dbString * string)
48 {
49  char buf[64];
50  const char *bp = buf;
51 
52  if (db_test_value_isnull(value)) {
53  *buf = 0;
54  }
55  else {
56  switch (db_sqltype_to_Ctype(sqltype)) {
57  case DB_C_TYPE_INT:
58  sprintf(buf, "%d", db_get_value_int(value));
59  break;
60  case DB_C_TYPE_DOUBLE:
61  sprintf(buf, "%.15g", db_get_value_double(value));
62  G_trim_decimal(buf);
63  break;
64  case DB_C_TYPE_STRING:
65  bp = db_get_value_string(value);
66  break;
67  case DB_C_TYPE_DATETIME:
68  return db_convert_value_datetime_into_string(value, sqltype,
69  string);
70  default:
71  db_error
72  ("db_convert_value_into_string(): unrecongized sqltype-type");
73  return DB_FAILED;
74  }
75  }
76  return db_set_string(string, bp);
77 }
int db_test_value_isnull(dbValue *value)
Definition: value.c:10
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void db_set_value_int(dbValue *value, int i)
Definition: value.c:169
int G_trim_decimal(char *buf)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:30
void db_error(const char *s)
int db_convert_value_to_string(dbValue *value, int sqltype, dbString *string)
Definition: valuefmt.c:47
int db_convert_value_datetime_into_string(dbValue *value, int sqltype, dbString *string)
Definition: datetime.c:14
int db_sqltype_to_Ctype(int sqltype)
Definition: sqlCtype.c:9
void db_set_value_double(dbValue *value, double d)
Definition: value.c:181
double db_get_value_double(dbValue *value)
Definition: value.c:32
char * value
Definition: env.c:30
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int db_set_value_string(dbValue *value, const char *s)
Definition: value.c:193
int db_set_string(dbString *x, const char *s)
Definition: string.c:33
int db_get_value_int(dbValue *value)
Definition: value.c:21
int db_convert_Cstring_to_value(const char *Cstring, int sqltype, dbValue *value)
Definition: valuefmt.c:13
const char * db_get_value_string(dbValue *value)
Definition: value.c:70
int db_convert_Cstring_to_value_datetime(const char *buf, int sqltype, dbValue *value)
Definition: datetime.c:182