xdrint.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*!
  2. \file lib/db/dbmi_base/xdrint.c
  3. \brief DBMI Library (base) - external data representation (integer)
  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 integer
  13. \param n
  14. \return
  15. */
  16. int db__send_int(int n)
  17. {
  18. int stat = DB_OK;
  19. if (!db__send(&n, sizeof(n)))
  20. stat = DB_PROTOCOL_ERR;
  21. if (stat == DB_PROTOCOL_ERR)
  22. db_protocol_error();
  23. return stat;
  24. }
  25. /*!
  26. \brief Receive integer
  27. \param n
  28. \return
  29. */
  30. int db__recv_int(int *n)
  31. {
  32. int stat = DB_OK;
  33. if (!db__recv(n, sizeof(*n)))
  34. stat = DB_PROTOCOL_ERR;
  35. if (stat == DB_PROTOCOL_ERR)
  36. db_protocol_error();
  37. return stat;
  38. }
  39. /*!
  40. \brief Send integer array
  41. \param x
  42. \param n
  43. \return
  44. */
  45. int db__send_int_array(const int *x, int n)
  46. {
  47. int stat = DB_OK;
  48. if (!db__send(&n, sizeof(n)))
  49. stat = DB_PROTOCOL_ERR;
  50. if (!db__send(x, n * sizeof(*x)))
  51. stat = DB_PROTOCOL_ERR;
  52. if (stat == DB_PROTOCOL_ERR)
  53. db_protocol_error();
  54. return stat;
  55. }
  56. /*!
  57. \brief Receive integer array
  58. Returns an allocated array of ints
  59. Caller is responsible for free()
  60. \param x
  61. \param n
  62. \return
  63. */
  64. int db__recv_int_array(int **x, int *n)
  65. {
  66. int stat = DB_OK;
  67. int count = 0;
  68. int *a = NULL;
  69. if (!db__recv(&count, sizeof(count)))
  70. stat = DB_PROTOCOL_ERR;
  71. *n = count;
  72. *x = a = (int *)db_calloc(count, sizeof(*a));
  73. if (!db__recv(a, count * sizeof(*a)))
  74. stat = DB_PROTOCOL_ERR;
  75. if (stat == DB_PROTOCOL_ERR)
  76. db_protocol_error();
  77. return stat;
  78. }