xdrshort.c 1.8 KB

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