GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-e07a2894ef
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) != (int)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  unsigned int j;
29 
30  n = nofNum % 1024;
31  if (n == 0)
32  n = 1024;
33 
34  for (j = 0; j < n; j++)
35  G_xdr_put_int(&xdrIntBuf[RASTER3D_XDR_INT_LENGTH * j], i);
36 
37  if (write(fd, xdrIntBuf, RASTER3D_XDR_INT_LENGTH * n) !=
39  Rast3d_error("Rast3d_write_ints: writing xdr to file failed");
40  return 0;
41  }
42 
43  nofNum -= n;
44  i += n;
45  } while (nofNum);
46 
47  return 1;
48 }
49 
50 /*---------------------------------------------------------------------------*/
51 
52 int Rast3d_read_ints(int fd, int useXdr, int *i, int nofNum)
53 {
54  char xdrIntBuf[RASTER3D_XDR_INT_LENGTH * 1024];
55  unsigned int n;
56 
57  if (nofNum <= 0)
58  Rast3d_fatal_error("Rast3d_read_ints: nofNum out of range");
59 
60  if (useXdr == RASTER3D_NO_XDR) {
61  if (read(fd, i, sizeof(int) * nofNum) != (int)sizeof(int) * nofNum) {
62  Rast3d_error("Rast3d_read_ints: reading from file failed");
63  return 0;
64  }
65  else {
66  return 1;
67  }
68  }
69 
70  do {
71  unsigned int j;
72 
73  n = nofNum % 1024;
74  if (n == 0)
75  n = 1024;
76 
77  if (read(fd, xdrIntBuf, RASTER3D_XDR_INT_LENGTH * n) !=
79  Rast3d_error("Rast3d_read_ints: reading xdr from file failed");
80  return 0;
81  }
82 
83  for (j = 0; j < n; j++)
84  G_xdr_get_int(i, &xdrIntBuf[RASTER3D_XDR_INT_LENGTH * j]);
85 
86  nofNum -= n;
87  i += n;
88  } while (nofNum);
89 
90  return 1;
91 }
void G_xdr_get_int(int *, const void *)
Definition: gis/xdr.c:69
void G_xdr_put_int(void *, const int *)
Definition: gis/xdr.c:74
void Rast3d_error(const char *,...) __attribute__((format(printf
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
int Rast3d_read_ints(int fd, int useXdr, int *i, int nofNum)
Definition: intio.c:52
int Rast3d_write_ints(int fd, int useXdr, const int *i, int nofNum)
Definition: intio.c:9
#define RASTER3D_NO_XDR
#define RASTER3D_XDR_INT_LENGTH