GRASS GIS 7 Programmer's Manual  7.0.2(2015)-r00000
segment/seek.c
Go to the documentation of this file.
1 
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 
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 G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
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
#define SEG_SEEK_FAST(SEG, n, index)
Internal use only.
Definition: segment/seek.c:36