GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
segment/put.c
Go to the documentation of this file.
1 
2 /**
3  * \file lib/segment/put.c
4  *
5  * \brief Segment write 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 <string.h>
16 #include <grass/gis.h>
17 #include "local_proto.h"
18 
19 
20 /*bugfix: buf: char* vs int* -> wrong pointer arithmetics!!!. Pierre de Mouveaux - 09 april 2000 */
21 /* int Segment_put (SEGMENT *SEG,int *buf,int row,int col) */
22 
23 
24 /**
25  * \fn int Segment_put (SEGMENT *SEG, void *buf, int row, int col)
26  *
27  * \brief Write value to segment file.
28  *
29  * Provides random write access to the segmented data. It
30  * copies <i>len</i> bytes of data from <b>buf</b> into the segment
31  * structure <b>seg</b> for the corresponding <b>row</b> and <b>col</b> in
32  * the original data matrix.
33  *
34  * The data is not written to disk immediately. It is stored in a memory segment
35  * until the segment routines decide to page the segment to disk.
36  *
37  * \param[in,out] seg segment
38  * \param[in] buf value to write to segment
39  * \param[in] row
40  * \param[in] col
41  * \return 1 if successful
42  * \return -1 if unable to seek or write segment file
43  */
44 
45 int Segment_put(SEGMENT * SEG, const void *buf, off_t row, off_t col)
46 {
47  int index, n, i;
48 
49  if (SEG->cache) {
50  memcpy(SEG->cache + ((size_t)row * SEG->ncols + col) * SEG->len, buf, SEG->len);
51 
52  return 1;
53  }
54 
55  SEG->address(SEG, row, col, &n, &index);
56  if ((i = seg_pagein(SEG, n)) < 0) {
57  G_warning("segment lib: put: pagein failed");
58  return -1;
59  }
60 
61  SEG->scb[i].dirty = 1;
62 
63  memcpy(&SEG->scb[i].buf[index], buf, SEG->len);
64 
65  return 1;
66 }
int(* address)()
Definition: segment.h:40
int len
Definition: segment.h:24
char * buf
Definition: segment.h:47
off_t ncols
Definition: segment.h:23
char dirty
Definition: segment.h:48
int Segment_put(SEGMENT *SEG, const void *buf, off_t row, off_t col)
Definition: segment/put.c:45
char * cache
Definition: segment.h:62
void G_warning(const char *,...) __attribute__((format(printf
int seg_pagein(SEGMENT *SEG, int n)
Internal use only.
Definition: pagein.c:37
struct SEGMENT::scb * scb