seek.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <grass/gis.h>
  19. #include "local_proto.h"
  20. /**
  21. * \brief Internal use only
  22. *
  23. * Seek into a segment.
  24. *
  25. * \param[in,out] SEG segment
  26. * \param[in] n
  27. * \param[in] index
  28. * \return 0 on success
  29. * \return -1 if unable to seek
  30. */
  31. #define SEG_SEEK_FAST(SEG, n, index) \
  32. ((((off_t) (n)) << (SEG)->sizebits) + (index) + (SEG)->offset)
  33. #define SEG_SEEK_SLOW(SEG, n, index) \
  34. ((off_t) (n) * (SEG)->size + (index) + (SEG)->offset)
  35. int seg_seek_fast(const SEGMENT * SEG, int n, int index)
  36. {
  37. if (lseek((SEG)->fd, SEG_SEEK_FAST(SEG, n, index),
  38. SEEK_SET) == (off_t) -1) {
  39. G_fatal_error("Segment seek: %s", strerror(errno));
  40. }
  41. return 0;
  42. }
  43. int seg_seek_slow(const SEGMENT * SEG, int n, int index)
  44. {
  45. if (lseek((SEG)->fd, SEG_SEEK_SLOW(SEG, n, index),
  46. SEEK_SET) == (off_t) -1) {
  47. G_fatal_error("Segment seek: %s", strerror(errno));
  48. }
  49. return 0;
  50. }
  51. int seg_seek(const SEGMENT * SEG, int n, int index)
  52. {
  53. return SEG->seek(SEG, n, index);
  54. }