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