GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
segment/put_row.c
Go to the documentation of this file.
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 Development Team
10  *
11  * \date 2005-2018
12  */
13 
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <grass/gis.h>
19 #include "local_proto.h"
20 
21 /* buf is CELL * WRAT code */
22 /* int Segment_put_row (SEGMENT *SEG, CELL *buf,int row) */
23 
24 /**
25  * \brief Write row to segment file.
26  *
27  * Transfers non-segmented matrix data, row by row, into a segment
28  * file. <b>seg</b> is the segment structure that was configured from a
29  * call to <i>Segment_init()</i>. <b>buf</b> should contain
30  * <em>ncols*len</em> bytes of data to be transferred to the segment
31  * file. <b>row</b> specifies the row from the data matrix being
32  * transferred.
33  *
34  * \param[in,out] SEG segment
35  * \param[in] buf data to write to segment
36  * \param[in] row
37  * \return 1 if successful
38  * \return -1 if unable to seek or write segment file
39  */
40 int Segment_put_row(const SEGMENT *SEG, const void *buf, off_t row)
41 {
42  int size;
43  off_t ncols;
44  int scols;
45  int n, index;
46  int result;
47  off_t col;
48 
49  if (SEG->cache) {
50  memcpy(SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, buf,
51  SEG->len * SEG->ncols);
52 
53  return 1;
54  }
55 
56  ncols = SEG->ncols - SEG->spill;
57  scols = SEG->scols;
58  size = scols * SEG->len;
59  /* printf("Segment_put_row ncols: %d, scols %d, size: %d, col %d, row:
60  * %d, SEG->fd: %d\n",ncols,scols,size,col,row, SEG->fd); */
61 
62  for (col = 0; col < ncols; col += scols) {
63  SEG->address(SEG, row, col, &n, &index);
64  SEG->seek(SEG, n, index);
65 
66  if ((result = write(SEG->fd, buf, size)) != size) {
67  G_warning("Segment_put_row write error %s", strerror(errno));
68  /* printf("Segment_put_row result = %d. ncols: %d, scols %d,
69  * size: %d, col %d, row: %d, SEG->fd:
70  * %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", strerror(errno));
88  return -1;
89  }
90  }
91 
92  return 1;
93 }
void G_warning(const char *,...) __attribute__((format(printf
int Segment_put_row(const SEGMENT *SEG, const void *buf, off_t row)
Write row to segment file.
int len
Definition: segment.h:23
int spill
Definition: segment.h:29
int(* address)(const struct SEGMENT *, off_t, off_t, int *, int *)
Definition: segment.h:39
off_t ncols
Definition: segment.h:22
char * cache
Definition: segment.h:61
int fd
Definition: segment.h:43
int(* seek)(const struct SEGMENT *S, int, int)
Definition: segment.h:40
int scols
Definition: segment.h:25
#define write
Definition: unistd.h:6