get_row.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * \file get_row.c
  3. *
  4. * \brief Segment row retrieval 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 <unistd.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <grass/gis.h>
  18. #include <grass/segment.h>
  19. /**
  20. * \fn int segment_get_row (SEGMENT *SEG, void *buf, int row)
  21. *
  22. * \brief Read row from segment file.
  23. *
  24. * Transfers data from a segment file, row by row, into memory
  25. * (which can then be written to a regular matrix file). <b>Seg</b> is the
  26. * segment structure that was configured from a call to
  27. * <i>segment_init()</i>.
  28. *
  29. * <b>Buf</b> will be filled with <em>ncols*len</em> bytes of data
  30. * corresponding to the <b>row</b> in the data matrix.
  31. *
  32. * \param[in] seg segment
  33. * \param[in,out] buf
  34. * \param[in] row
  35. * \return 1 if successful
  36. * \return -1 if unable to seek or read segment file
  37. */
  38. int segment_get_row(const SEGMENT * SEG, void *buf, int row)
  39. {
  40. int size;
  41. int ncols;
  42. int scols;
  43. int n, index, col;
  44. ncols = SEG->ncols - SEG->spill;
  45. scols = SEG->scols;
  46. size = scols * SEG->len;
  47. for (col = 0; col < ncols; col += scols) {
  48. segment_address(SEG, row, col, &n, &index);
  49. if (segment_seek(SEG, n, index) < 0)
  50. return -1;
  51. if (read(SEG->fd, buf, size) != size) {
  52. G_warning("segment_get_row: %s", strerror(errno));
  53. return -1;
  54. }
  55. /* The buf variable is a void pointer and thus points to anything. */
  56. /* Therefore, it's size is unknown and thus, it cannot be used for */
  57. /* pointer arithmetic (some compilers treat this as an error - SGI */
  58. /* MIPSPro compiler for one). Since the read command is reading in */
  59. /* "size" bytes, cast the buf variable to char * before incrementing */
  60. buf = ((char *)buf) + size;
  61. }
  62. if ((size = SEG->spill * SEG->len)) {
  63. segment_address(SEG, row, col, &n, &index);
  64. if (segment_seek(SEG, n, index) < 0)
  65. return -1;
  66. if (read(SEG->fd, buf, size) != size) {
  67. G_warning("segment_get_row: %s", strerror(errno));
  68. return -1;
  69. }
  70. }
  71. return 1;
  72. }