seek.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. * \file lib/gis/seek.c
  3. *
  4. * \brief GIS Library - file seek routines
  5. *
  6. * (C) 2009-2010 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Glynn Clements
  12. */
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. \brief Get the current file position of the stream.
  19. \param fp file descriptor
  20. \return file position
  21. \return -1 on failure
  22. */
  23. off_t G_ftell(FILE *fp)
  24. {
  25. #ifdef HAVE_FSEEKO
  26. return ftello(fp);
  27. #else
  28. return (off_t) ftell(fp);
  29. #endif
  30. }
  31. /*!
  32. \brief Change the file position of the stream.
  33. The value of <i>whence</i> must be one of the constants `SEEK_SET',
  34. `SEEK_CUR', or `SEEK_END', to indicate whether the <i>offset</i> is
  35. relative to the beginning of the file, the current file position, or
  36. the end of the file, respectively.
  37. \param fp file descriptor
  38. \param offset offset
  39. \param whence
  40. */
  41. void G_fseek(FILE *fp, off_t offset, int whence)
  42. {
  43. #ifdef HAVE_FSEEKO
  44. if (fseeko(fp, offset, whence) != 0)
  45. G_fatal_error(_("Unable to seek"));
  46. #else
  47. long loff = (long) offset;
  48. if ((off_t) loff != offset)
  49. G_fatal_error(_("Seek offset out of range"));
  50. if (fseek(fp, loff, whence) != 0)
  51. G_fatal_error(_("Unable to seek"));
  52. #endif
  53. }