GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
segment/put_row.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <grass/segment.h>
20 #include <grass/gis.h>
21 
22 
23 /* buf is CELL * WRAT code */
24 /* int segment_put_row (SEGMENT *SEG, CELL *buf,int row) */
25 
26 
46 int segment_put_row(const SEGMENT * SEG, const void *buf, int row)
47 {
48  int size;
49  int ncols;
50  int scols;
51  int n, index, col;
52  int result;
53 
54  ncols = SEG->ncols - SEG->spill;
55  scols = SEG->scols;
56  size = scols * SEG->len;
57  /* 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); */
58 
59  for (col = 0; col < ncols; col += scols) {
60  segment_address(SEG, row, col, &n, &index);
61  if (segment_seek(SEG, n, index) < 0) {
62  G_warning
63  ("Failed seek in segment file for index = %d n = %d at col:row %d:%d",
64  index, n, col, row);
65  return -1;
66  }
67 
68  if ((result = write(SEG->fd, buf, size)) != size) {
69  G_warning("segment_put_row write error %s", strerror(errno));
70  /* 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); */
71  return -1;
72  }
73 
74  /* The buf variable is a void pointer and thus points to anything. */
75  /* Therefore, it's size is unknown and thus, it cannot be used for */
76  /* pointer arithmetic (some compilers treat this as an error - SGI */
77  /* MIPSPro compiler for one). Since the read command is reading in */
78  /* "size" bytes, cast the buf variable to char * before incrementing */
79  buf = ((const char *)buf) + size;
80  }
81 
82  if ((size = SEG->spill * SEG->len)) {
83  segment_address(SEG, row, col, &n, &index);
84  if (segment_seek(SEG, n, index) < 0) {
85  G_warning
86  ("Failed seek in segment file for index = %d n = %d at col:row %d:%d",
87  index, n, col, row);
88  return -1;
89  }
90  if (write(SEG->fd, buf, size) != size) {
91  G_warning("segment_put_row final write error: %s",
92  strerror(errno));
93  return -1;
94  }
95  }
96 
97  return 1;
98 }