GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71924
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
segment/put_row.c
Go to the documentation of this file.
1 
2 /**
3  * \file lib/segment/put_row.c
4  *
5  * \brief Write segment row routines.
6  *
7  * This program is free software under the GNU General Public License
8  * (>=v2). Read the file COPYING that comes with GRASS for details.
9  *
10  * \author GRASS GIS Development Team
11  *
12  * \date 2005-2009
13  */
14 
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 
27 /**
28  * \brief Write row to segment file.
29  *
30  * Transfers non-segmented matrix data, row by row, into a segment
31  * file. <b>seg</b> is the segment structure that was configured from a
32  * call to <i>Segment_init()</i>. <b>buf</b> should contain
33  * <em>ncols*len</em> bytes of data to be transferred to the segment
34  * file. <b>row</b> specifies the row from the data matrix being
35  * transferred.
36  *
37  * \param[in,out] SEG segment
38  * \param[in] buf data to write to segment
39  * \param[in] row
40  * \return 1 if successful
41  * \return -1 if unable to seek or write segment file
42  */
43 
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 fd
Definition: segment.h:44
int len
Definition: segment.h:24
int spill
Definition: segment.h:30
int(* address)()
Definition: segment.h:40
off_t ncols
Definition: segment.h:23
int scols
Definition: segment.h:26
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
int Segment_put_row(const SEGMENT *SEG, const void *buf, off_t row)
Write row to segment file.
int(* seek)()
Definition: segment.h:41