GRASS Programmer's Manual  6.4.2(2012)
 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 }