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