GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
doubleio.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 "raster3d_intern.h"
6 
7 /*---------------------------------------------------------------------------*/
8 
9 int Rast3d_write_doubles(int fd, int useXdr, const double *i, int nofNum)
10 {
11  char xdrDoubleBuf[RASTER3D_XDR_DOUBLE_LENGTH * 1024];
12  unsigned int n;
13 
14  if (nofNum <= 0)
15  Rast3d_fatal_error("Rast3d_write_doubles: nofNum out of range");
16 
17  if (useXdr == RASTER3D_NO_XDR) {
18  if (write(fd, i, sizeof(double) * nofNum) != sizeof(double) * nofNum) {
19  Rast3d_error("Rast3d_write_doubles: writing to file failed");
20  return 0;
21  }
22  else {
23  return 1;
24  }
25  }
26 
27  do {
28  int j;
29  n = nofNum % 1024;
30  if (n == 0)
31  n = 1024;
32 
33  for (j = 0; j < n; j++)
34  G_xdr_put_double(&xdrDoubleBuf[RASTER3D_XDR_DOUBLE_LENGTH * j], i);
35 
36  if (write(fd, xdrDoubleBuf, RASTER3D_XDR_DOUBLE_LENGTH * n) !=
38  Rast3d_error("Rast3d_write_doubles: writing xdr to file failed");
39  return 0;
40  }
41 
42  nofNum -= n;
43  i += n;
44  } while (nofNum);
45 
46  return 1;
47 }
48 
49 /*---------------------------------------------------------------------------*/
50 
51 int Rast3d_read_doubles(int fd, int useXdr, double *i, int nofNum)
52 {
53  char xdrDoubleBuf[RASTER3D_XDR_DOUBLE_LENGTH * 1024];
54  unsigned int n;
55 
56  if (nofNum <= 0)
57  Rast3d_fatal_error("Rast3d_read_doubles: nofNum out of range");
58 
59  if (useXdr == RASTER3D_NO_XDR) {
60  if (read(fd, i, sizeof(double) * nofNum) != sizeof(double) * nofNum) {
61  Rast3d_error("Rast3d_read_doubles: reading from file failed");
62  return 0;
63  }
64  else {
65  return 1;
66  }
67  }
68 
69  do {
70  int j;
71  n = nofNum % 1024;
72  if (n == 0)
73  n = 1024;
74 
75  if (read(fd, xdrDoubleBuf, RASTER3D_XDR_DOUBLE_LENGTH * n) !=
77  Rast3d_error("Rast3d_read_doubles: reading xdr from file failed");
78  return 0;
79  }
80 
81  for (j = 0; j < n; j++)
82  G_xdr_get_double(i, &xdrDoubleBuf[RASTER3D_XDR_DOUBLE_LENGTH * j]);
83 
84  nofNum -= n;
85  i += n;
86  } while (nofNum);
87 
88  return 1;
89 }
#define RASTER3D_XDR_DOUBLE_LENGTH
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
int Rast3d_write_doubles(int fd, int useXdr, const double *i, int nofNum)
Definition: doubleio.c:9
void Rast3d_error(const char *,...) __attribute__((format(printf
#define RASTER3D_NO_XDR
int Rast3d_read_doubles(int fd, int useXdr, double *i, int nofNum)
Definition: doubleio.c:51
void G_xdr_put_double(void *, const double *)
Definition: gis/xdr.c:88
void G_xdr_get_double(double *, const void *)
Definition: gis/xdr.c:83