intio.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <rpc/types.h>
  6. #include <rpc/xdr.h>
  7. #include "G3d_intern.h"
  8. /*---------------------------------------------------------------------------*/
  9. int G3d_writeInts(int fd, int useXdr, const int *i, int nofNum)
  10. {
  11. int firstTime = 1;
  12. XDR xdrEncodeStream;
  13. char xdrIntBuf[G3D_XDR_INT_LENGTH * 1024];
  14. u_int n;
  15. if (nofNum <= 0)
  16. G3d_fatalError("G3d_writeInts: nofNum out of range");
  17. if (useXdr == G3D_NO_XDR) {
  18. if (write(fd, i, sizeof(int) * nofNum) != sizeof(int) * nofNum) {
  19. G3d_error("G3d_writeInts: writing to file failed");
  20. return 0;
  21. }
  22. else {
  23. return 1;
  24. }
  25. }
  26. if (firstTime) {
  27. xdrmem_create(&xdrEncodeStream, xdrIntBuf, G3D_XDR_INT_LENGTH * 1024,
  28. XDR_ENCODE);
  29. firstTime = 1;
  30. }
  31. do {
  32. n = nofNum % 1024;
  33. if (n == 0)
  34. n = 1024;
  35. if (!xdr_setpos(&xdrEncodeStream, 0)) {
  36. G3d_error("G3d_writeInts: positioning xdr failed");
  37. return 0;
  38. }
  39. if (!xdr_vector(&xdrEncodeStream, (char *)i, n, sizeof(int),
  40. (xdrproc_t) xdr_int)) {
  41. G3d_error("G3d_writeInts: writing xdr failed");
  42. return 0;
  43. }
  44. if (write(fd, xdrIntBuf, G3D_XDR_INT_LENGTH * n) !=
  45. G3D_XDR_INT_LENGTH * n) {
  46. G3d_error("G3d_writeInts: writing xdr to file failed");
  47. return 0;
  48. }
  49. nofNum -= n;
  50. i += n;
  51. } while (nofNum);
  52. return 1;
  53. }
  54. /*---------------------------------------------------------------------------*/
  55. int G3d_readInts(int fd, int useXdr, int *i, int nofNum)
  56. {
  57. int firstTime = 1;
  58. XDR xdrDecodeStream;
  59. char xdrIntBuf[G3D_XDR_INT_LENGTH * 1024];
  60. u_int n;
  61. if (nofNum <= 0)
  62. G3d_fatalError("G3d_readInts: nofNum out of range");
  63. if (useXdr == G3D_NO_XDR) {
  64. if (read(fd, i, sizeof(int) * nofNum) != sizeof(int) * nofNum) {
  65. G3d_error("G3d_readInts: reading from file failed");
  66. return 0;
  67. }
  68. else {
  69. return 1;
  70. }
  71. }
  72. if (firstTime) {
  73. xdrmem_create(&xdrDecodeStream, xdrIntBuf, G3D_XDR_INT_LENGTH * 1024,
  74. XDR_DECODE);
  75. firstTime = 1;
  76. }
  77. do {
  78. n = nofNum % 1024;
  79. if (n == 0)
  80. n = 1024;
  81. if (read(fd, xdrIntBuf, G3D_XDR_INT_LENGTH * n) !=
  82. G3D_XDR_INT_LENGTH * n) {
  83. G3d_error("G3d_readInts: reading xdr from file failed");
  84. return 0;
  85. }
  86. if (!xdr_setpos(&xdrDecodeStream, 0)) {
  87. G3d_error("G3d_readInts: positioning xdr failed");
  88. return 0;
  89. }
  90. if (!xdr_vector(&xdrDecodeStream, (char *)i, n, sizeof(int),
  91. (xdrproc_t) xdr_int)) {
  92. G3d_error("G3d_readInts: reading xdr failed");
  93. return 0;
  94. }
  95. nofNum -= n;
  96. i += n;
  97. } while (nofNum);
  98. return 1;
  99. }