GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-b4e4cb0fe9
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 GIS 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 
41 int Segment_put_row(const SEGMENT *SEG, const void *buf, off_t row)
42 {
43  int size;
44  off_t ncols;
45  int scols;
46  int n, index;
47  int result;
48  off_t col;
49 
50  if (SEG->cache) {
51  memcpy(SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, buf,
52  SEG->len * SEG->ncols);
53 
54  return 1;
55  }
56 
57  ncols = SEG->ncols - SEG->spill;
58  scols = SEG->scols;
59  size = scols * SEG->len;
60  /* printf("Segment_put_row ncols: %d, scols %d, size: %d, col %d, row:
61  * %d, SEG->fd: %d\n",ncols,scols,size,col,row, SEG->fd); */
62 
63  for (col = 0; col < ncols; col += scols) {
64  SEG->address(SEG, row, col, &n, &index);
65  SEG->seek(SEG, n, index);
66 
67  if ((result = write(SEG->fd, buf, size)) != size) {
68  G_warning("Segment_put_row write error %s", strerror(errno));
69  /* printf("Segment_put_row result = %d. ncols: %d, scols %d,
70  * size: %d, col %d, row: %d, SEG->fd:
71  * %d\n",result,ncols,scols,size,col,row, SEG->fd); */
72  return -1;
73  }
74 
75  /* The buf variable is a void pointer and thus points to anything. */
76  /* Therefore, it's size is unknown and thus, it cannot be used for */
77  /* pointer arithmetic (some compilers treat this as an error - SGI */
78  /* MIPSPro compiler for one). Since the read command is reading in */
79  /* "size" bytes, cast the buf variable to char * before incrementing */
80  buf = ((const char *)buf) + size;
81  }
82 
83  if ((size = SEG->spill * SEG->len)) {
84  SEG->address(SEG, row, col, &n, &index);
85  SEG->seek(SEG, n, index);
86 
87  if (write(SEG->fd, buf, size) != size) {
88  G_warning("Segment_put_row final write error: %s", strerror(errno));
89  return -1;
90  }
91  }
92 
93  return 1;
94 }
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