seek.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Seek into a segment.
  22. *
  23. * \param[in,out] SEG segment
  24. * \param[in] n
  25. * \param[in] index
  26. * \return 0 on success
  27. * \return -1 if unable to seek
  28. */
  29. #define SEG_SEEK_FAST(SEG, n, index) \
  30. ((((off_t) (n)) << (SEG)->sizebits) + (index) + (SEG)->offset)
  31. #define SEG_SEEK_SLOW(SEG, n, index) \
  32. ((off_t) (n) * (SEG)->size + (index) + (SEG)->offset)
  33. int segment_seek_fast(const SEGMENT * SEG, int n, int index)
  34. {
  35. if (lseek((SEG)->fd, SEG_SEEK_FAST(SEG, n, index),
  36. SEEK_SET) == (off_t) -1) {
  37. G_fatal_error("segment_seek: %s", strerror(errno));
  38. }
  39. return 0;
  40. }
  41. int segment_seek_slow(const SEGMENT * SEG, int n, int index)
  42. {
  43. if (lseek((SEG)->fd, SEG_SEEK_SLOW(SEG, n, index),
  44. SEEK_SET) == (off_t) -1) {
  45. G_fatal_error("segment_seek: %s", strerror(errno));
  46. }
  47. return 0;
  48. }
  49. int segment_seek(const SEGMENT * SEG, int n, int index)
  50. {
  51. return SEG->segment_seek(SEG, n, index);
  52. }