GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
rowio/get.c
Go to the documentation of this file.
1/*!
2 \file rowio/get.c
3
4 \brief RowIO library - Get a row
5
6 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 */
11
12#include <stdio.h>
13#include <grass/rowio.h>
14
15static void *my_select(ROWIO *, int);
16static void pageout(ROWIO *, int);
17
18/*!
19 * \brief Read a row
20 *
21 * Rowio_get() returns a buffer which holds the data for row from the
22 * file associated with ROWIO structure <i>R</i>. If the row requested
23 * is not in memory, the getrow() routine specified in
24 * Rowio_setup() is called to read row into memory and a
25 * pointer to the memory buffer containing the row is returned. If the
26 * data currently in the buffer had been changed by Rowio_put(),
27 * the putrow() routine specified in Rowio_setup() is
28 * called first to write the changed row to disk. If row is
29 * already in memory, no disk read is done. The pointer to the data is
30 * simply returned.
31 *
32 * \param R pointer to ROWIO structure
33 * \param row row number
34 *
35 * \return NULL on error
36 * \return pointer to the buffer containing row
37 */
38void *Rowio_get(ROWIO *R, int row)
39{
40 int i;
41 int age;
42 int cur;
43
44 if (row < 0)
45 return NULL;
46
47 if (row == R->cur)
48 return R->buf;
49
50 for (i = 0; i < R->nrows; i++)
51 if (row == R->rcb[i].row)
52 return my_select(R, i);
53
54 age = 0;
55 cur = 0;
56
57 for (i = 0; i < R->nrows; i++)
58 if (R->rcb[i].row < 0) { /* free slot ! */
59 cur = i;
60 break;
61 }
62 else if (age < R->rcb[i].age) {
63 cur = i;
64 age = R->rcb[i].age;
65 }
66
67 pageout(R, cur);
68
69 i = (*R->getrow)(R->fd, R->rcb[cur].buf, R->rcb[cur].row = row, R->len);
70 R->rcb[cur].dirty = 0;
71 if (!i) {
72 R->rcb[cur].row = -1;
73 if (cur == R->cur)
74 R->cur = -1;
75 return NULL;
76 }
77
78 return my_select(R, cur);
79}
80
81/*!
82 \brief Flush data
83
84 \param R pointer to ROWIO structure
85 */
87{
88 int i;
89
90 for (i = 0; i < R->nrows; i++)
91 pageout(R, i);
92}
93
94static void pageout(ROWIO *R, int cur)
95{
96 if (R->rcb[cur].row < 0)
97 return;
98 if (!R->rcb[cur].dirty)
99 return;
100 (*R->putrow)(R->fd, R->rcb[cur].buf, R->rcb[cur].row, R->len);
101 R->rcb[cur].dirty = 0;
102}
103
104static void *my_select(ROWIO *R, int n)
105{
106 int i;
107
108 R->rcb[n].age = 0;
109 for (i = 0; i < R->nrows; i++)
110 R->rcb[i].age++;
111 R->cur = R->rcb[n].row;
112 R->buf = R->rcb[n].buf;
113 return R->buf;
114}
#define NULL
Definition ccmath.h:32
void Rowio_flush(ROWIO *R)
Flush data.
Definition rowio/get.c:86
void * Rowio_get(ROWIO *R, int row)
Read a row.
Definition rowio/get.c:38
Definition rowio.h:4
void * buf
Definition rowio.h:9
int len
Definition rowio.h:7
int(* getrow)(int, void *, int, int)
Definition rowio.h:10
struct ROWIO::ROWIO_RCB * rcb
int fd
Definition rowio.h:5
int(* putrow)(int, const void *, int, int)
Definition rowio.h:11
int nrows
Definition rowio.h:6
int cur
Definition rowio.h:8