valuefmt.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*!
  2. \file lib/db/dbmi_base/valuefmt.c
  3. \brief DBMI Library (base) - value formatting
  4. (C) 1999-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Joel Jones (CERL/UIUC), Radim Blazek
  8. \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/glocale.h>
  15. /*!
  16. \brief Convert string to value
  17. \param Cstring string buffer
  18. \param sqltype SQL data type
  19. \param[out] value pointer to dbValue
  20. \return DB_OK on success
  21. \return DB_FAILED on error
  22. */
  23. int db_convert_Cstring_to_value(const char *Cstring, int sqltype, dbValue * value)
  24. {
  25. int i;
  26. double d;
  27. switch (db_sqltype_to_Ctype(sqltype)) {
  28. case DB_C_TYPE_STRING:
  29. return db_set_value_string(value, Cstring);
  30. case DB_C_TYPE_INT:
  31. i = 0;
  32. sscanf(Cstring, "%d", &i);
  33. db_set_value_int(value, i);
  34. break;
  35. case DB_C_TYPE_DOUBLE:
  36. d = 0.0;
  37. sscanf(Cstring, "%lf", &d);
  38. db_set_value_double(value, d);
  39. break;
  40. case DB_C_TYPE_DATETIME:
  41. return db_convert_Cstring_to_value_datetime(Cstring, sqltype, value);
  42. default:
  43. db_error(_("db_convert_Cstring_to_value(): unrecognized sqltype"));
  44. return DB_FAILED;
  45. }
  46. return DB_OK;
  47. }
  48. /*!
  49. \brief Convert value to string
  50. \param value pointer to dbValue
  51. \param sqltype SQL data type
  52. \param[out] string pointer to dbString
  53. \return DB_OK on success
  54. */
  55. int db_convert_value_to_string(dbValue * value, int sqltype, dbString * string)
  56. {
  57. char buf[64];
  58. const char *bp = buf;
  59. if (db_test_value_isnull(value)) {
  60. *buf = 0;
  61. }
  62. else {
  63. switch (db_sqltype_to_Ctype(sqltype)) {
  64. case DB_C_TYPE_INT:
  65. sprintf(buf, "%d", db_get_value_int(value));
  66. break;
  67. case DB_C_TYPE_DOUBLE:
  68. sprintf(buf, "%.15g", db_get_value_double(value));
  69. G_trim_decimal(buf);
  70. break;
  71. case DB_C_TYPE_STRING:
  72. bp = db_get_value_string(value);
  73. break;
  74. case DB_C_TYPE_DATETIME:
  75. return db_convert_value_datetime_into_string(value, sqltype,
  76. string);
  77. default:
  78. db_error(_("db_convert_value_into_string(): unrecongized sqltype-type"));
  79. return DB_FAILED;
  80. }
  81. }
  82. return db_set_string(string, bp);
  83. }