GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-55b2a2bccc
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 GIS 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 
37 int Segment_get(SEGMENT *SEG, void *buf, off_t row, off_t col)
38 {
39  int index, n, i;
40 
41  if (SEG->cache) {
42  memcpy(buf, SEG->cache + ((size_t)row * SEG->ncols + col) * SEG->len,
43  SEG->len);
44 
45  return 1;
46  }
47 
48  SEG->address(SEG, row, col, &n, &index);
49  if ((i = seg_pagein(SEG, n)) < 0)
50  return -1;
51 
52  memcpy(buf, &SEG->scb[i].buf[index], SEG->len);
53 
54  return 1;
55 }
int seg_pagein(SEGMENT *SEG, int n)
Internal use only.
Definition: pagein.c:35
int Segment_get(SEGMENT *SEG, void *buf, off_t row, off_t col)
Get value from segment file.
Definition: segment/get.c:37
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