put_row.c 2.6 KB

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