xdrfloat.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "xdr.h"
  2. int db__send_float(float d)
  3. {
  4. int stat = DB_OK;
  5. if (!db__send(&d, sizeof(d)))
  6. stat = DB_PROTOCOL_ERR;
  7. if (stat == DB_PROTOCOL_ERR)
  8. db_protocol_error();
  9. return stat;
  10. }
  11. int db__recv_float(float *d)
  12. {
  13. int stat = DB_OK;
  14. if (!db__recv(d, sizeof(*d)))
  15. stat = DB_PROTOCOL_ERR;
  16. if (stat == DB_PROTOCOL_ERR)
  17. db_protocol_error();
  18. return stat;
  19. }
  20. int db__send_float_array(const float *x, int n)
  21. {
  22. int stat = DB_OK;
  23. if (!db__send(&n, sizeof(n)))
  24. stat = DB_PROTOCOL_ERR;
  25. if (!db__send(x, n * sizeof(*x)))
  26. stat = DB_PROTOCOL_ERR;
  27. if (stat == DB_PROTOCOL_ERR)
  28. db_protocol_error();
  29. return stat;
  30. }
  31. /* returns an allocated array of floats */
  32. /* caller is responsible for free() */
  33. int db__recv_float_array(float **x, int *n)
  34. {
  35. int stat = DB_OK;
  36. int count = 0;
  37. float *a = NULL;
  38. if (!db__recv(&count, sizeof(count)))
  39. stat = DB_PROTOCOL_ERR;
  40. *n = count;
  41. *x = a = (float *)db_calloc(count, sizeof(*a));
  42. if (!db__recv(a, count * sizeof(*a)))
  43. stat = DB_PROTOCOL_ERR;
  44. if (stat == DB_PROTOCOL_ERR)
  45. db_protocol_error();
  46. return stat;
  47. }