GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
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/gis.h>
20 #include "local_proto.h"
21 
22 
23 /* buf is CELL * WRAT code */
24 /* int Segment_put_row (SEGMENT *SEG, CELL *buf,int row) */
25 
26 
44 int Segment_put_row(const SEGMENT * SEG, const void *buf, off_t row)
45 {
46  int size;
47  off_t ncols;
48  int scols;
49  int n, index;
50  int result;
51  off_t col;
52 
53  ncols = SEG->ncols - SEG->spill;
54  scols = SEG->scols;
55  size = scols * SEG->len;
56  /* 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); */
57 
58  for (col = 0; col < ncols; col += scols) {
59  SEG->address(SEG, row, col, &n, &index);
60  SEG->seek(SEG, n, index);
61 
62  if ((result = write(SEG->fd, buf, size)) != size) {
63  G_warning("Segment_put_row write error %s", strerror(errno));
64  /* 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); */
65  return -1;
66  }
67 
68  /* The buf variable is a void pointer and thus points to anything. */
69  /* Therefore, it's size is unknown and thus, it cannot be used for */
70  /* pointer arithmetic (some compilers treat this as an error - SGI */
71  /* MIPSPro compiler for one). Since the read command is reading in */
72  /* "size" bytes, cast the buf variable to char * before incrementing */
73  buf = ((const char *)buf) + size;
74  }
75 
76  if ((size = SEG->spill * SEG->len)) {
77  SEG->address(SEG, row, col, &n, &index);
78  SEG->seek(SEG, n, index);
79 
80  if (write(SEG->fd, buf, size) != size) {
81  G_warning("Segment_put_row final write error: %s",
82  strerror(errno));
83  return -1;
84  }
85  }
86 
87  return 1;
88 }
int Segment_put_row(const SEGMENT *SEG, const void *buf, off_t row)
Write row to segment file.
Definition: put_row.c:44
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203