GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72636
 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-2009
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  ncols = SEG->ncols - SEG->spill;
51  scols = SEG->scols;
52  size = scols * SEG->len;
53 
54  for (col = 0; col < ncols; col += scols) {
55  SEG->address(SEG, row, col, &n, &index);
56  SEG->seek(SEG, n, index);
57 
58  if (read(SEG->fd, buf, size) != size) {
59  G_warning("Segment_get_row: %s", strerror(errno));
60  return -1;
61  }
62 
63  /* The buf variable is a void pointer and thus points to anything. */
64  /* Therefore, it's size is unknown and thus, it cannot be used for */
65  /* pointer arithmetic (some compilers treat this as an error - SGI */
66  /* MIPSPro compiler for one). Since the read command is reading in */
67  /* "size" bytes, cast the buf variable to char * before incrementing */
68  buf = ((char *)buf) + size;
69  }
70  if ((size = SEG->spill * SEG->len)) {
71  SEG->address(SEG, row, col, &n, &index);
72  SEG->seek(SEG, n, index);
73 
74  if (read(SEG->fd, buf, size) != size) {
75  G_warning("Segment_get_row: %s", strerror(errno));
76  return -1;
77  }
78  }
79 
80  return 1;
81 }
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
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