put_row.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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-2009
  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. segment_address(SEG, row, col, &n, &index);
  52. if (segment_seek(SEG, n, index) < 0) {
  53. G_warning
  54. ("Failed seek in segment file for index = %d n = %d at col:row %d:%d",
  55. index, n, col, row);
  56. return -1;
  57. }
  58. if ((result = write(SEG->fd, buf, size)) != size) {
  59. G_warning("segment_put_row write error %s", strerror(errno));
  60. /* 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); */
  61. return -1;
  62. }
  63. /* The buf variable is a void pointer and thus points to anything. */
  64. /* Therefore, it's size is unknown and thus, it cannot be used for */
  65. /* pointer arithmetic (some compilers treat this as an error - SGI */
  66. /* MIPSPro compiler for one). Since the read command is reading in */
  67. /* "size" bytes, cast the buf variable to char * before incrementing */
  68. buf = ((const char *)buf) + size;
  69. }
  70. if ((size = SEG->spill * SEG->len)) {
  71. segment_address(SEG, row, col, &n, &index);
  72. if (segment_seek(SEG, n, index) < 0) {
  73. G_warning
  74. ("Failed seek in segment file for index = %d n = %d at col:row %d:%d",
  75. index, n, col, row);
  76. return -1;
  77. }
  78. if (write(SEG->fd, buf, size) != size) {
  79. G_warning("segment_put_row final write error: %s",
  80. strerror(errno));
  81. return -1;
  82. }
  83. }
  84. return 1;
  85. }