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