put_row.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * \file lib/segment/put_row.c
  3. *
  4. * \brief Write segment row 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 <string.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <grass/gis.h>
  18. #include "local_proto.h"
  19. /* buf is CELL * WRAT code */
  20. /* int Segment_put_row (SEGMENT *SEG, CELL *buf,int row) */
  21. /**
  22. * \brief Write row to segment file.
  23. *
  24. * Transfers non-segmented matrix data, row by row, into a segment
  25. * file. <b>seg</b> is the segment structure that was configured from a
  26. * call to <i>Segment_init()</i>. <b>buf</b> should contain
  27. * <em>ncols*len</em> bytes of data to be transferred to the segment
  28. * file. <b>row</b> specifies the row from the data matrix being
  29. * transferred.
  30. *
  31. * \param[in,out] SEG segment
  32. * \param[in] buf data to write to segment
  33. * \param[in] row
  34. * \return 1 if successful
  35. * \return -1 if unable to seek or write segment file
  36. */
  37. int Segment_put_row(const SEGMENT * SEG, const void *buf, off_t row)
  38. {
  39. int size;
  40. off_t ncols;
  41. int scols;
  42. int n, index;
  43. int result;
  44. off_t col;
  45. ncols = SEG->ncols - SEG->spill;
  46. scols = SEG->scols;
  47. size = scols * SEG->len;
  48. /* printf("Segment_put_row ncols: %d, scols %d, size: %d, col %d, row: %d, SEG->fd: %d\n",ncols,scols,size,col,row, SEG->fd); */
  49. for (col = 0; col < ncols; col += scols) {
  50. SEG->address(SEG, row, col, &n, &index);
  51. SEG->seek(SEG, n, index);
  52. if ((result = write(SEG->fd, buf, size)) != size) {
  53. G_warning("Segment_put_row write error %s", strerror(errno));
  54. /* printf("Segment_put_row result = %d. ncols: %d, scols %d, size: %d, col %d, row: %d, SEG->fd: %d\n",result,ncols,scols,size,col,row, SEG->fd); */
  55. return -1;
  56. }
  57. /* The buf variable is a void pointer and thus points to anything. */
  58. /* Therefore, it's size is unknown and thus, it cannot be used for */
  59. /* pointer arithmetic (some compilers treat this as an error - SGI */
  60. /* MIPSPro compiler for one). Since the read command is reading in */
  61. /* "size" bytes, cast the buf variable to char * before incrementing */
  62. buf = ((const char *)buf) + size;
  63. }
  64. if ((size = SEG->spill * SEG->len)) {
  65. SEG->address(SEG, row, col, &n, &index);
  66. SEG->seek(SEG, n, index);
  67. if (write(SEG->fd, buf, size) != size) {
  68. G_warning("Segment_put_row final write error: %s",
  69. strerror(errno));
  70. return -1;
  71. }
  72. }
  73. return 1;
  74. }