put_row.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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-2018
  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. if (SEG->cache) {
  46. memcpy(SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, buf, SEG->len * SEG->ncols);
  47. return 1;
  48. }
  49. ncols = SEG->ncols - SEG->spill;
  50. scols = SEG->scols;
  51. size = scols * SEG->len;
  52. /* 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); */
  53. for (col = 0; col < ncols; col += scols) {
  54. SEG->address(SEG, row, col, &n, &index);
  55. SEG->seek(SEG, n, index);
  56. if ((result = write(SEG->fd, buf, size)) != size) {
  57. G_warning("Segment_put_row write error %s", strerror(errno));
  58. /* 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); */
  59. return -1;
  60. }
  61. /* The buf variable is a void pointer and thus points to anything. */
  62. /* Therefore, it's size is unknown and thus, it cannot be used for */
  63. /* pointer arithmetic (some compilers treat this as an error - SGI */
  64. /* MIPSPro compiler for one). Since the read command is reading in */
  65. /* "size" bytes, cast the buf variable to char * before incrementing */
  66. buf = ((const char *)buf) + size;
  67. }
  68. if ((size = SEG->spill * SEG->len)) {
  69. SEG->address(SEG, row, col, &n, &index);
  70. SEG->seek(SEG, n, index);
  71. if (write(SEG->fd, buf, size) != size) {
  72. G_warning("Segment_put_row final write error: %s",
  73. strerror(errno));
  74. return -1;
  75. }
  76. }
  77. return 1;
  78. }