get_row.c 2.1 KB

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