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