GRASS 8 Programmer's Manual 8.6.0dev(2026)-ddeab64dbf
Loading...
Searching...
No Matches
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 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
19#include <grass/gis.h>
20#include <grass/glocale.h>
21
22#include "local_proto.h"
23
24/**
25 * \fn int Segment_get_row (SEGMENT *SEG, void *buf, int row)
26 *
27 * \brief Read row from segment file.
28 *
29 * Transfers data from a segment file, row by row, into memory
30 * (which can then be written to a regular matrix file). <b>Seg</b> is the
31 * segment structure that was configured from a call to
32 * <i>Segment_init()</i>.
33 *
34 * <b>Buf</b> will be filled with <em>ncols*len</em> bytes of data
35 * corresponding to the <b>row</b> in the data matrix.
36 *
37 * \param[in] seg segment
38 * \param[in,out] buf
39 * \param[in] row
40 * \return 1 if successful
41 * \return -1 if unable to seek or read segment file
42 */
43int 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,
52 SEG->len * SEG->ncols);
53
54 return 1;
55 }
56
57 ncols = SEG->ncols - SEG->spill;
58 scols = SEG->scols;
59 size = scols * SEG->len;
60
61 for (col = 0; col < ncols; col += scols) {
62 SEG->address(SEG, row, col, &n, &index);
63 if (SEG->seek(SEG, n, index) == -1) {
64 int err = errno;
65 G_warning(_("File read/write operation failed: %s (%d)"),
66 strerror(err), err);
67 return -1;
68 }
69
70 if (read(SEG->fd, buf, size) != size) {
71 G_warning("Segment_get_row: %s", strerror(errno));
72 return -1;
73 }
74
75 /* The buf variable is a void pointer and thus points to anything. */
76 /* Therefore, it's size is unknown and thus, it cannot be used for */
77 /* pointer arithmetic (some compilers treat this as an error - SGI */
78 /* MIPSPro compiler for one). Since the read command is reading in */
79 /* "size" bytes, cast the buf variable to char * before incrementing */
80 buf = ((char *)buf) + size;
81 }
82 if ((size = SEG->spill * SEG->len)) {
83 SEG->address(SEG, row, col, &n, &index);
84 if (SEG->seek(SEG, n, index) == -1) {
85 int err = errno;
86 G_warning(_("File read/write operation failed: %s (%d)"),
87 strerror(err), err);
88 return -1;
89 }
90
91 if (read(SEG->fd, buf, size) != size) {
92 G_warning("Segment_get_row: %s", strerror(errno));
93 return -1;
94 }
95 }
96
97 return 1;
98}
AMI_err seek(off_t offset)
Definition ami_stream.h:445
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition glocale.h:10
int Segment_get_row(const SEGMENT *SEG, void *buf, off_t row)
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
#define read
Definition unistd.h:5