value.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*!
  2. \file lib/db/dbmi_base/value.c
  3. \brief DBMI Library (base) - value management
  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 <stdlib.h>
  11. #include <grass/dbmi.h>
  12. /*!
  13. \brief Check of value is null
  14. \param value pointer to dbValue
  15. \return non-zero is null
  16. \return zero is not null
  17. */
  18. int db_test_value_isnull(dbValue * value)
  19. {
  20. return (value->isNull != 0);
  21. }
  22. /*!
  23. \brief Get integer value
  24. \param value pointer to dbValue
  25. \return value
  26. */
  27. int db_get_value_int(dbValue * value)
  28. {
  29. return (value->i);
  30. }
  31. /*!
  32. \brief Get double precision value
  33. \param value pointer to dbValue
  34. \return value
  35. */
  36. double db_get_value_double(dbValue * value)
  37. {
  38. return (value->d);
  39. }
  40. /*!
  41. \brief Get value as double
  42. For given value and C type of value returns double representation.
  43. \param value pointer to dbValue
  44. \param ctype C data type
  45. \return value
  46. */
  47. double db_get_value_as_double(dbValue * value, int ctype)
  48. {
  49. double val;
  50. switch (ctype) {
  51. case (DB_C_TYPE_INT):
  52. val = (double)db_get_value_int(value);
  53. break;
  54. case (DB_C_TYPE_STRING):
  55. val = atof(db_get_value_string(value));
  56. break;
  57. case (DB_C_TYPE_DOUBLE):
  58. val = db_get_value_double(value);
  59. break;
  60. default:
  61. val = 0;
  62. }
  63. return val;
  64. }
  65. /*!
  66. \brief Get string value
  67. \param value pointer to dbValue
  68. \return value
  69. */
  70. const char *db_get_value_string(dbValue * value)
  71. {
  72. return (db_get_string(&value->s));
  73. }
  74. /*!
  75. \brief Get year value
  76. \param value pointer to dbValue
  77. \return value
  78. */
  79. int db_get_value_year(dbValue * value)
  80. {
  81. return (value->t.year);
  82. }
  83. /*!
  84. \brief Get month value
  85. \param value pointer to dbValue
  86. \return value
  87. */
  88. int db_get_value_month(dbValue * value)
  89. {
  90. return (value->t.month);
  91. }
  92. /*!
  93. \brief Get day value
  94. \param value pointer to dbValue
  95. \return value
  96. */
  97. int db_get_value_day(dbValue * value)
  98. {
  99. return (value->t.day);
  100. }
  101. /*!
  102. \brief Get hour value
  103. \param value pointer to dbValue
  104. \return value
  105. */
  106. int db_get_value_hour(dbValue * value)
  107. {
  108. return (value->t.hour);
  109. }
  110. /*!
  111. \brief Get minute value
  112. \param value pointer to dbValue
  113. \return value
  114. */
  115. int db_get_value_minute(dbValue * value)
  116. {
  117. return (value->t.minute);
  118. }
  119. /*!
  120. \brief Get seconds value
  121. \param value pointer to dbValue
  122. \return value
  123. */
  124. double db_get_value_seconds(dbValue * value)
  125. {
  126. return (value->t.seconds);
  127. }
  128. /*!
  129. \brief Set value to null
  130. \param value pointer to dbValue
  131. */
  132. void db_set_value_null(dbValue * value)
  133. {
  134. value->isNull = 1;
  135. }
  136. /*!
  137. \brief Set value to not null
  138. \param value pointer to dbValue
  139. */
  140. void db_set_value_not_null(dbValue * value)
  141. {
  142. value->isNull = 0;
  143. }
  144. /*!
  145. \brief Set integer value
  146. \param value pointer to dbValue
  147. \param i integer value
  148. */
  149. void db_set_value_int(dbValue * value, int i)
  150. {
  151. value->i = i;
  152. db_set_value_not_null(value);
  153. }
  154. /*!
  155. \brief Set double precision value
  156. \param value pointer to dbValue
  157. \param d double value
  158. */
  159. void db_set_value_double(dbValue * value, double d)
  160. {
  161. value->d = d;
  162. db_set_value_not_null(value);
  163. }
  164. /*!
  165. \brief Set string value
  166. \param value pointer to dbValue
  167. \param s string value
  168. */
  169. int db_set_value_string(dbValue * value, const char *s)
  170. {
  171. db_set_value_not_null(value);
  172. return db_set_string(&value->s, s);
  173. }
  174. /*!
  175. \brief Set year value
  176. \param value pointer to dbValue
  177. \param year year value
  178. */
  179. void db_set_value_year(dbValue * value, int year)
  180. {
  181. value->t.year = year;
  182. db_set_value_datetime_not_current(value);
  183. }
  184. /*!
  185. \brief Set month value
  186. \param value pointer to dbValue
  187. \param month month value
  188. */
  189. void db_set_value_month(dbValue * value, int month)
  190. {
  191. value->t.month = month;
  192. db_set_value_datetime_not_current(value);
  193. }
  194. /*!
  195. \brief Set day value
  196. \param value pointer to dbValue
  197. \param day day value
  198. */
  199. void db_set_value_day(dbValue * value, int day)
  200. {
  201. value->t.day = day;
  202. db_set_value_datetime_not_current(value);
  203. }
  204. /*!
  205. \brief Set hour value
  206. \param value pointer to dbValue
  207. \param hour hour value
  208. */
  209. void db_set_value_hour(dbValue * value, int hour)
  210. {
  211. value->t.hour = hour;
  212. db_set_value_datetime_not_current(value);
  213. }
  214. /*!
  215. \brief Set minute value
  216. \param value pointer to dbValue
  217. \param minute minute value
  218. */
  219. void db_set_value_minute(dbValue * value, int minute)
  220. {
  221. value->t.minute = minute;
  222. db_set_value_datetime_not_current(value);
  223. }
  224. /*!
  225. \brief Set seconds value
  226. \param value pointer to dbValue
  227. \param seconds seconds value
  228. */
  229. void db_set_value_seconds(dbValue * value, double seconds)
  230. {
  231. value->t.seconds = seconds;
  232. db_set_value_datetime_not_current(value);
  233. }
  234. /*!
  235. \brief Check if datatime is current
  236. \param value pointer to dbValue
  237. \return non-zero for true
  238. \return zero for false
  239. */
  240. int db_test_value_datetime_current(dbValue * value)
  241. {
  242. return (value->t.current != 0);
  243. }
  244. /*!
  245. \brief Set datetime to current
  246. \param value pointer to dbValue
  247. */
  248. void db_set_value_datetime_current(dbValue * value)
  249. {
  250. value->t.current = 1;
  251. db_set_value_not_null(value);
  252. }
  253. /*!
  254. \brief Set value to non-current
  255. \param value pointer to dbValue
  256. */
  257. void db_set_value_datetime_not_current(dbValue * value)
  258. {
  259. value->t.current = 0;
  260. db_set_value_not_null(value);
  261. }
  262. /*!
  263. \brief Copy value
  264. Copy value from src to destination
  265. \param dst destination dbValue
  266. \param src source dbValue
  267. */
  268. void db_copy_value(dbValue * dst, dbValue * src)
  269. {
  270. dst->isNull = src->isNull;
  271. dst->i = src->i;
  272. dst->d = src->d;
  273. if (src->s.nalloc > 0)
  274. db_copy_string(&(dst->s), &(src->s));
  275. dst->t.current = src->t.current;
  276. dst->t.year = src->t.year;
  277. dst->t.month = src->t.month;
  278. dst->t.day = src->t.day;
  279. dst->t.hour = src->t.hour;
  280. dst->t.minute = src->t.minute;
  281. dst->t.seconds = src->t.seconds;
  282. }
  283. /*!
  284. \brief Initialize dbCatValArray
  285. \param arr pointer to dbCatValArray to be initialized
  286. */
  287. void db_CatValArray_init(dbCatValArray * arr)
  288. {
  289. arr->n_values = 0;
  290. arr->alloc = 0;
  291. arr->value = NULL;
  292. }
  293. /*!
  294. \brief Free allocated dbCatValArray
  295. \param arr pointer to dbCatValArray
  296. */
  297. void db_CatValArray_free(dbCatValArray * arr)
  298. {
  299. if (arr->ctype == DB_C_TYPE_STRING || arr->ctype == DB_C_TYPE_DATETIME) {
  300. int i;
  301. for (i = 0; i < arr->n_values; i++) {
  302. if (arr->ctype == DB_C_TYPE_STRING && arr->value[i].val.s) {
  303. db_free_string(arr->value[i].val.s);
  304. }
  305. if (arr->ctype == DB_C_TYPE_DATETIME && arr->value[i].val.t) {
  306. db_free(arr->value[i].val.t);
  307. }
  308. }
  309. }
  310. G_free(arr->value);
  311. }
  312. /*!
  313. \brief Allocate dbCatValArray
  314. \todo return type void?
  315. \param arr pointer to dbCatValArray
  316. \param n number of items
  317. \return DB_OK
  318. */
  319. int db_CatValArray_alloc(dbCatValArray * arr, int n)
  320. {
  321. arr->value = (dbCatVal *) G_calloc(n, sizeof(dbCatVal));
  322. arr->alloc = n;
  323. return DB_OK;
  324. }
  325. /*!
  326. \brief Realloc dbCatValArray
  327. \todo return code void?
  328. \param arr pointer to dbCatValArray
  329. \param n number of items
  330. \return DB_OK
  331. */
  332. int db_CatValArray_realloc(dbCatValArray * arr, int n)
  333. {
  334. arr->value = (dbCatVal *) G_realloc(arr->value, n * sizeof(dbCatVal));
  335. arr->alloc = n;
  336. return DB_OK;
  337. }