GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dintio.c
Go to the documentation of this file.
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 /*---------------------------------------------------------------------------*/
10 
11 int G3d_writeInts(int fd, int useXdr, const int *i, int nofNum)
12 {
13  int firstTime = 1;
14  XDR xdrEncodeStream;
15  char xdrIntBuf[G3D_XDR_INT_LENGTH * 1024];
16  u_int n;
17 
18  if (nofNum <= 0)
19  G3d_fatalError("G3d_writeInts: nofNum out of range");
20 
21  if (useXdr == G3D_NO_XDR) {
22  if (write(fd, i, sizeof(int) * nofNum) != sizeof(int) * nofNum) {
23  G3d_error("G3d_writeInts: writing to file failed");
24  return 0;
25  }
26  else {
27  return 1;
28  }
29  }
30 
31  if (firstTime) {
32  xdrmem_create(&xdrEncodeStream, xdrIntBuf, G3D_XDR_INT_LENGTH * 1024,
33  XDR_ENCODE);
34  firstTime = 1;
35  }
36 
37  do {
38  n = nofNum % 1024;
39  if (n == 0)
40  n = 1024;
41 
42  if (!xdr_setpos(&xdrEncodeStream, 0)) {
43  G3d_error("G3d_writeInts: positioning xdr failed");
44  return 0;
45  }
46 
47  if (!xdr_vector(&xdrEncodeStream, (char *)i, n, sizeof(int),
48  (xdrproc_t) xdr_int)) {
49  G3d_error("G3d_writeInts: writing xdr failed");
50  return 0;
51  }
52 
53  if (write(fd, xdrIntBuf, G3D_XDR_INT_LENGTH * n) !=
54  G3D_XDR_INT_LENGTH * n) {
55  G3d_error("G3d_writeInts: writing xdr to file failed");
56  return 0;
57  }
58 
59  nofNum -= n;
60  i += n;
61  } while (nofNum);
62 
63  return 1;
64 }
65 
66 /*---------------------------------------------------------------------------*/
67 
68 int G3d_readInts(int fd, int useXdr, int *i, int nofNum)
69 {
70  int firstTime = 1;
71  XDR xdrDecodeStream;
72  char xdrIntBuf[G3D_XDR_INT_LENGTH * 1024];
73  u_int n;
74 
75  if (nofNum <= 0)
76  G3d_fatalError("G3d_readInts: nofNum out of range");
77 
78  if (useXdr == G3D_NO_XDR) {
79  if (read(fd, i, sizeof(int) * nofNum) != sizeof(int) * nofNum) {
80  G3d_error("G3d_readInts: reading from file failed");
81  return 0;
82  }
83  else {
84  return 1;
85  }
86  }
87 
88  if (firstTime) {
89  xdrmem_create(&xdrDecodeStream, xdrIntBuf, G3D_XDR_INT_LENGTH * 1024,
90  XDR_DECODE);
91  firstTime = 1;
92  }
93 
94  do {
95  n = nofNum % 1024;
96  if (n == 0)
97  n = 1024;
98 
99  if (read(fd, xdrIntBuf, G3D_XDR_INT_LENGTH * n) !=
100  G3D_XDR_INT_LENGTH * n) {
101  G3d_error("G3d_readInts: reading xdr from file failed");
102  return 0;
103  }
104 
105  if (!xdr_setpos(&xdrDecodeStream, 0)) {
106  G3d_error("G3d_readInts: positioning xdr failed");
107  return 0;
108  }
109 
110  if (!xdr_vector(&xdrDecodeStream, (char *)i, n, sizeof(int),
111  (xdrproc_t) xdr_int)) {
112  G3d_error("G3d_readInts: reading xdr failed");
113  return 0;
114  }
115 
116  nofNum -= n;
117  i += n;
118  } while (nofNum);
119 
120  return 1;
121 }
XDR xdrEncodeStream
Definition: g3dfpxdr.c:62
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
FILE * fd
Definition: g3dcolor.c:368
#define G3D_NO_XDR
Definition: G3d_intern.h:32
int G3d_writeInts(int fd, int useXdr, const int *i, int nofNum)
Definition: g3dintio.c:11
#define G3D_XDR_INT_LENGTH
Definition: G3d_intern.h:13
XDR xdrDecodeStream
Definition: g3dfpxdr.c:62
int n
Definition: dataquad.c:291
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58
int G3d_readInts(int fd, int useXdr, int *i, int nofNum)
Definition: g3dintio.c:68