GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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-2018
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  if (SEG->cache) {
54  memcpy(SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, buf, SEG->len * SEG->ncols);
55 
56  return 1;
57  }
58 
59  ncols = SEG->ncols - SEG->spill;
60  scols = SEG->scols;
61  size = scols * SEG->len;
62  /* 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); */
63 
64  for (col = 0; col < ncols; col += scols) {
65  SEG->address(SEG, row, col, &n, &index);
66  SEG->seek(SEG, n, index);
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  SEG->address(SEG, row, col, &n, &index);
84  SEG->seek(SEG, n, index);
85 
86  if (write(SEG->fd, buf, size) != size) {
87  G_warning("Segment_put_row final write error: %s",
88  strerror(errno));
89  return -1;
90  }
91  }
92 
93  return 1;
94 }
int fd
Definition: segment.h:44
int(* address)()
Definition: segment.h:40
int len
Definition: segment.h:24
int spill
Definition: segment.h:30
off_t ncols
Definition: segment.h:23
char * cache
Definition: segment.h:62
void G_warning(const char *,...) __attribute__((format(printf
int(* seek)()
Definition: segment.h:41
int scols
Definition: segment.h:26
int Segment_put_row(const SEGMENT *SEG, const void *buf, off_t row)
Write row to segment file.