xdrdouble.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*!
  2. \file lib/db/dbmi_base/xdrdouble.c
  3. \brief DBMI Library (base) - external data representation (double)
  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, Brad Douglas, Markus Neteler
  8. \author Doxygenized by Martin Landa <landa.martin gmail.com> (2011)
  9. */
  10. #include "xdr.h"
  11. /*!
  12. \brief Send double
  13. \param d
  14. \return
  15. */
  16. int db__send_double(double d)
  17. {
  18. int stat = DB_OK;
  19. if (!db__send(&d, sizeof(d)))
  20. stat = DB_PROTOCOL_ERR;
  21. if (stat == DB_PROTOCOL_ERR)
  22. db_protocol_error();
  23. return stat;
  24. }
  25. /*!
  26. \brief Receive double
  27. \param d
  28. */
  29. int db__recv_double(double *d)
  30. {
  31. int stat = DB_OK;
  32. if (!db__recv(d, sizeof(*d)))
  33. stat = DB_PROTOCOL_ERR;
  34. if (stat == DB_PROTOCOL_ERR)
  35. db_protocol_error();
  36. return stat;
  37. }
  38. /*!
  39. \brief Send double array
  40. \param x
  41. \param n
  42. \return
  43. */ \
  44. int db__send_double_array(const double *x, int n)
  45. {
  46. int stat = DB_OK;
  47. if (!db__send(&n, sizeof(n)))
  48. stat = DB_PROTOCOL_ERR;
  49. if (!db__send(x, n * sizeof(*x)))
  50. stat = DB_PROTOCOL_ERR;
  51. if (stat == DB_PROTOCOL_ERR)
  52. db_protocol_error();
  53. return stat;
  54. }
  55. /*!
  56. \brief Receive double array
  57. Returns an allocated array of doubles
  58. Caller is responsible for free()
  59. \param x
  60. \param n
  61. \return
  62. */
  63. int db__recv_double_array(double **x, int *n)
  64. {
  65. int stat = DB_OK;
  66. int count = 0;
  67. double *a = NULL;
  68. if (!db__recv(&count, sizeof(count)))
  69. stat = DB_PROTOCOL_ERR;
  70. *n = count;
  71. *x = a = (double *)db_calloc(count, sizeof(*a));
  72. if (!db__recv(a, count * sizeof(*a)))
  73. stat = DB_PROTOCOL_ERR;
  74. if (stat == DB_PROTOCOL_ERR)
  75. db_protocol_error();
  76. return stat;
  77. }