GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
segment/get.c
Go to the documentation of this file.
1 /**
2  * \file lib/segment/get.c
3  *
4  * \brief Get segment 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 <string.h>
15 #include "local_proto.h"
16 
17 /*bugfix: buf: char* vs int* -> wrong pointer arithmetics!!!. Pierre de Mouveaux
18  * - 09 april 2000 */
19 /* int Segment_get (SEGMENT *SEG, register int *buf,int row,int col) */
20 
21 /**
22  * \brief Get value from segment file.
23  *
24  * Provides random read access to the segmented data. It gets
25  * <i>len</i> bytes of data into <b>buf</b> from the segment file
26  * <b>seg</b> for the corresponding <b>row</b> and <b>col</b> in the
27  * original data matrix.
28  *
29  * \param[in] SEG segment
30  * \param[in,out] buf value return buffer
31  * \param[in] row
32  * \param[in] col
33  * \return 1 of successful
34  * \return -1 if unable to seek or read segment file
35  */
36 int Segment_get(SEGMENT *SEG, void *buf, off_t row, off_t col)
37 {
38  int index, n, i;
39 
40  if (SEG->cache) {
41  memcpy(buf, SEG->cache + ((size_t)row * SEG->ncols + col) * SEG->len,
42  SEG->len);
43 
44  return 1;
45  }
46 
47  SEG->address(SEG, row, col, &n, &index);
48  if ((i = seg_pagein(SEG, n)) < 0)
49  return -1;
50 
51  memcpy(buf, &SEG->scb[i].buf[index], SEG->len);
52 
53  return 1;
54 }
int seg_pagein(SEGMENT *SEG, int n)
Internal use only.
Definition: pagein.c:34
int Segment_get(SEGMENT *SEG, void *buf, off_t row, off_t col)
Get value from segment file.
Definition: segment/get.c:36
char * buf
Definition: segment.h:46
int len
Definition: segment.h:23
int(* address)(const struct SEGMENT *, off_t, off_t, int *, int *)
Definition: segment.h:39
struct SEGMENT::scb * scb
off_t ncols
Definition: segment.h:22
char * cache
Definition: segment.h:61