put_row.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * \file 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-2006
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <grass/segment.h>
  18. #include <grass/gis.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,int row)
  40. {
  41. int size;
  42. int ncols;
  43. int scols;
  44. int n, index, col;
  45. int result;
  46. ncols = SEG->ncols - SEG->spill ;
  47. scols = SEG->scols ;
  48. size = scols * SEG->len;
  49. /* 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); */
  50. for (col = 0; col < ncols; col += scols)
  51. {
  52. segment_address (SEG, row, col, &n, &index) ;
  53. if(segment_seek (SEG, n, index) < 0) {
  54. G_warning (
  55. "Failed seek in segment file for index = %d n = %d at col:row %d:%d",
  56. index,n,col,row);
  57. return -1;
  58. }
  59. if((result = write (SEG->fd, buf, size)) != size)
  60. {
  61. G_warning ("segment_put_row write error %s",strerror(errno));
  62. /* 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); */
  63. return -1;
  64. }
  65. /* The buf variable is a void pointer and thus points to anything. */
  66. /* Therefore, it's size is unknown and thus, it cannot be used for */
  67. /* pointer arithmetic (some compilers treat this as an error - SGI */
  68. /* MIPSPro compiler for one). Since the read command is reading in */
  69. /* "size" bytes, cast the buf variable to char * before incrementing */
  70. buf = ((const char *) buf) + size;
  71. }
  72. if ((size = SEG->spill * SEG->len))
  73. {
  74. segment_address (SEG, row, col, &n, &index) ;
  75. if(segment_seek (SEG, n, index) < 0) {
  76. G_warning (
  77. "Failed seek in segment file for index = %d n = %d at col:row %d:%d",
  78. index,n,col,row);
  79. return -1;
  80. }
  81. if(write (SEG->fd, buf, size) != size)
  82. {
  83. G_warning ("segment_put_row final write error: %s",strerror(errno));
  84. return -1;
  85. }
  86. }
  87. return 1;
  88. }