GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
segment/get_row.c
Go to the documentation of this file.
1 /**
2  * \file lib/segment/get_row.c
3  *
4  * \brief Segment row retrieval routines.
5  *
6  * This program is free software under the GNU General Public License
7  * (>=v2). Read the file COPYING that comes with GRASS for details.
8  *
9  * \author GRASS GIS Development Team
10  *
11  * \date 2005-2018
12  */
13 
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <grass/gis.h>
19 #include "local_proto.h"
20 
21 /**
22  * \fn int Segment_get_row (SEGMENT *SEG, void *buf, int row)
23  *
24  * \brief Read row from segment file.
25  *
26  * Transfers data from a segment file, row by row, into memory
27  * (which can then be written to a regular matrix file). <b>Seg</b> is the
28  * segment structure that was configured from a call to
29  * <i>Segment_init()</i>.
30  *
31  * <b>Buf</b> will be filled with <em>ncols*len</em> bytes of data
32  * corresponding to the <b>row</b> in the data matrix.
33  *
34  * \param[in] seg segment
35  * \param[in,out] buf
36  * \param[in] row
37  * \return 1 if successful
38  * \return -1 if unable to seek or read segment file
39  */
40 
41 int Segment_get_row(const SEGMENT *SEG, void *buf, off_t row)
42 {
43  int size;
44  off_t ncols, col;
45  int scols;
46  int n, index;
47 
48  if (SEG->cache) {
49  memcpy(buf, SEG->cache + ((size_t)row * SEG->ncols) * SEG->len,
50  SEG->len * SEG->ncols);
51 
52  return 1;
53  }
54 
55  ncols = SEG->ncols - SEG->spill;
56  scols = SEG->scols;
57  size = scols * SEG->len;
58 
59  for (col = 0; col < ncols; col += scols) {
60  SEG->address(SEG, row, col, &n, &index);
61  SEG->seek(SEG, n, index);
62 
63  if (read(SEG->fd, buf, size) != size) {
64  G_warning("Segment_get_row: %s", strerror(errno));
65  return -1;
66  }
67 
68  /* The buf variable is a void pointer and thus points to anything. */
69  /* Therefore, it's size is unknown and thus, it cannot be used for */
70  /* pointer arithmetic (some compilers treat this as an error - SGI */
71  /* MIPSPro compiler for one). Since the read command is reading in */
72  /* "size" bytes, cast the buf variable to char * before incrementing */
73  buf = ((char *)buf) + size;
74  }
75  if ((size = SEG->spill * SEG->len)) {
76  SEG->address(SEG, row, col, &n, &index);
77  SEG->seek(SEG, n, index);
78 
79  if (read(SEG->fd, buf, size) != size) {
80  G_warning("Segment_get_row: %s", strerror(errno));
81  return -1;
82  }
83  }
84 
85  return 1;
86 }
void G_warning(const char *,...) __attribute__((format(printf
int Segment_get_row(const SEGMENT *SEG, void *buf, off_t row)
int len
Definition: segment.h:23
int spill
Definition: segment.h:29
int(* address)(const struct SEGMENT *, off_t, off_t, int *, int *)
Definition: segment.h:39
off_t ncols
Definition: segment.h:22
char * cache
Definition: segment.h:61
int fd
Definition: segment.h:43
int(* seek)(const struct SEGMENT *S, int, int)
Definition: segment.h:40
int scols
Definition: segment.h:25