GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
intio.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_ints(int fd, int useXdr, const int *i, int nofNum)
10 {
11  char xdrIntBuf[RASTER3D_XDR_INT_LENGTH * 1024];
12  unsigned int n;
13 
14  if (nofNum <= 0)
15  Rast3d_fatal_error("Rast3d_write_ints: nofNum out of range");
16 
17  if (useXdr == RASTER3D_NO_XDR) {
18  if (write(fd, i, sizeof(int) * nofNum) != sizeof(int) * nofNum) {
19  Rast3d_error("Rast3d_write_ints: 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_int(&xdrIntBuf[RASTER3D_XDR_INT_LENGTH * j], i);
35 
36  if (write(fd, xdrIntBuf, RASTER3D_XDR_INT_LENGTH * n) !=
38  Rast3d_error("Rast3d_write_ints: 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_ints(int fd, int useXdr, int *i, int nofNum)
52 {
53  char xdrIntBuf[RASTER3D_XDR_INT_LENGTH * 1024];
54  unsigned int n;
55 
56  if (nofNum <= 0)
57  Rast3d_fatal_error("Rast3d_read_ints: nofNum out of range");
58 
59  if (useXdr == RASTER3D_NO_XDR) {
60  if (read(fd, i, sizeof(int) * nofNum) != sizeof(int) * nofNum) {
61  Rast3d_error("Rast3d_read_ints: 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, xdrIntBuf, RASTER3D_XDR_INT_LENGTH * n) !=
77  Rast3d_error("Rast3d_read_ints: reading xdr from file failed");
78  return 0;
79  }
80 
81  for (j = 0; j < n; j++)
82  G_xdr_get_int(i, &xdrIntBuf[RASTER3D_XDR_INT_LENGTH * j]);
83 
84  nofNum -= n;
85  i += n;
86  } while (nofNum);
87 
88  return 1;
89 }
void G_xdr_get_int(int *, const void *)
Definition: gis/xdr.c:63
void G_xdr_put_int(void *, const int *)
Definition: gis/xdr.c:68
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void Rast3d_error(const char *,...) __attribute__((format(printf
#define RASTER3D_NO_XDR
int Rast3d_write_ints(int fd, int useXdr, const int *i, int nofNum)
Definition: intio.c:9
#define RASTER3D_XDR_INT_LENGTH
int Rast3d_read_ints(int fd, int useXdr, int *i, int nofNum)
Definition: intio.c:51