GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
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
19#include <grass/gis.h>
20#include <grass/glocale.h>
21
22#include "local_proto.h"
23
24/* buf is CELL * WRAT code */
25/* int Segment_put_row (SEGMENT *SEG, CELL *buf,int row) */
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 */
43int Segment_put_row(const SEGMENT *SEG, const void *buf, off_t row)
44{
45 int size;
46 off_t ncols;
47 int scols;
48 int n, index;
49 int result;
50 off_t col;
51
52 if (SEG->cache) {
53 memcpy(SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, buf,
54 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:
63 * %d, SEG->fd: %d\n",ncols,scols,size,col,row, SEG->fd); */
64
65 for (col = 0; col < ncols; col += scols) {
66 SEG->address(SEG, row, col, &n, &index);
67 if (SEG->seek(SEG, n, index) == -1) {
68 int err = errno;
69 G_warning(_("File read/write operation failed: %s (%d)"),
70 strerror(err), err);
71 return -1;
72 }
73
74 if ((result = write(SEG->fd, buf, size)) != size) {
75 G_warning("Segment_put_row write error %s", strerror(errno));
76 /* printf("Segment_put_row result = %d. ncols: %d, scols %d,
77 * size: %d, col %d, row: %d, SEG->fd:
78 * %d\n",result,ncols,scols,size,col,row, SEG->fd); */
79 return -1;
80 }
81
82 /* The buf variable is a void pointer and thus points to anything. */
83 /* Therefore, it's size is unknown and thus, it cannot be used for */
84 /* pointer arithmetic (some compilers treat this as an error - SGI */
85 /* MIPSPro compiler for one). Since the read command is reading in */
86 /* "size" bytes, cast the buf variable to char * before incrementing */
87 buf = ((const char *)buf) + size;
88 }
89
90 if ((size = SEG->spill * SEG->len)) {
91 SEG->address(SEG, row, col, &n, &index);
92 if (SEG->seek(SEG, n, index) == -1) {
93 int err = errno;
94 G_warning(_("File read/write operation failed: %s (%d)"),
95 strerror(err), err);
96 return -1;
97 }
98
99 if (write(SEG->fd, buf, size) != size) {
100 G_warning("Segment_put_row final write error: %s", strerror(errno));
101 return -1;
102 }
103 }
104
105 return 1;
106}
AMI_err seek(off_t offset)
Definition ami_stream.h:445
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition glocale.h:10
int Segment_put_row(const SEGMENT *SEG, const void *buf, off_t row)
Write row to segment file.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
#define write
Definition unistd.h:6