GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
segment/seek.c
Go to the documentation of this file.
1 /**
2  * \file lib/segment/seek.c
3  *
4  * \brief Segment seek 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-2009
12  */
13 
14 #include <stdio.h>
15 #include <sys/types.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  * \brief Internal use only
24  *
25  * Seek into a segment.
26  *
27  * \param[in,out] SEG segment
28  * \param[in] n
29  * \param[in] index
30  * \return 0 on success
31  * \return -1 if unable to seek
32  */
33 
34 #define SEG_SEEK_FAST(SEG, n, index) \
35  ((((off_t)(n)) << (SEG)->sizebits) + (index) + (SEG)->offset)
36 
37 #define SEG_SEEK_SLOW(SEG, n, index) \
38  ((off_t)(n) * (SEG)->size + (index) + (SEG)->offset)
39 
40 int seg_seek_fast(const SEGMENT *SEG, int n, int index)
41 {
42  if (lseek((SEG)->fd, SEG_SEEK_FAST(SEG, n, index), SEEK_SET) == (off_t)-1) {
43  G_fatal_error("Segment seek: %s", strerror(errno));
44  }
45 
46  return 0;
47 }
48 
49 int seg_seek_slow(const SEGMENT *SEG, int n, int index)
50 {
51  if (lseek((SEG)->fd, SEG_SEEK_SLOW(SEG, n, index), SEEK_SET) == (off_t)-1) {
52  G_fatal_error("Segment seek: %s", strerror(errno));
53  }
54 
55  return 0;
56 }
57 
58 int seg_seek(const SEGMENT *SEG, int n, int index)
59 {
60  return SEG->seek(SEG, n, index);
61 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int seg_seek(const SEGMENT *SEG, int n, int index)
Definition: segment/seek.c:58
int seg_seek_fast(const SEGMENT *SEG, int n, int index)
Definition: segment/seek.c:40
int seg_seek_slow(const SEGMENT *SEG, int n, int index)
Definition: segment/seek.c:49
#define SEG_SEEK_FAST(SEG, n, index)
Internal use only.
Definition: segment/seek.c:34
#define SEG_SEEK_SLOW(SEG, n, index)
Definition: segment/seek.c:37
int(* seek)(const struct SEGMENT *S, int, int)
Definition: segment.h:40