GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71817
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  (C) 2001-2009 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Original author CERL
12 */
13 
14 #include <stdio.h>
15 #include <grass/rowio.h>
16 
17 static void *my_select(ROWIO *, int);
18 static void pageout(ROWIO *, int);
19 
20 
21 /*!
22  * \brief Read a row
23  *
24  * Rowio_get() returns a buffer which holds the data for row from the
25  * file associated with ROWIO structure <i>R</i>. If the row requested
26  * is not in memory, the getrow() routine specified in
27  * Rowio_setup() is called to read row into memory and a
28  * pointer to the memory buffer containing the row is returned. If the
29  * data currently in the buffer had been changed by Rowio_put(),
30  * the putrow() routine specified in Rowio_setup() is
31  * called first to write the changed row to disk. If row is
32  * already in memory, no disk read is done. The pointer to the data is
33  * simply returned.
34  *
35  * \param R pointer to ROWIO structure
36  * \param row row number
37  *
38  * \return NULL on error
39  * \return pointer to the buffer containing row
40  */
41 void *Rowio_get(ROWIO * R, int row)
42 {
43  int i;
44  int age;
45  int cur;
46 
47  if (row < 0)
48  return NULL;
49 
50  if (row == R->cur)
51  return R->buf;
52 
53  for (i = 0; i < R->nrows; i++)
54  if (row == R->rcb[i].row)
55  return my_select(R, i);
56 
57  age = 0;
58  cur = 0;
59 
60  for (i = 0; i < R->nrows; i++)
61  if (R->rcb[i].row < 0) { /* free slot ! */
62  cur = i;
63  break;
64  }
65  else if (age < R->rcb[i].age) {
66  cur = i;
67  age = R->rcb[i].age;
68  }
69 
70  pageout(R, cur);
71 
72  i = (*R->getrow) (R->fd, R->rcb[cur].buf, R->rcb[cur].row = row, R->len);
73  R->rcb[cur].dirty = 0;
74  if (!i) {
75  R->rcb[cur].row = -1;
76  if (cur == R->cur)
77  R->cur = -1;
78  return NULL;
79  }
80 
81  return my_select(R, cur);
82 }
83 
84 /*!
85  \brief Flush data
86 
87  \param R pointer to ROWIO strcuture
88 */
89 void Rowio_flush(ROWIO * R)
90 {
91  int i;
92 
93  for (i = 0; i < R->nrows; i++)
94  pageout(R, i);
95 }
96 
97 static void pageout(ROWIO * R, int cur)
98 {
99  if (R->rcb[cur].row < 0)
100  return;
101  if (!R->rcb[cur].dirty)
102  return;
103  (*R->putrow) (R->fd, R->rcb[cur].buf, R->rcb[cur].row, R->len);
104  R->rcb[cur].dirty = 0;
105 }
106 
107 static void *my_select(ROWIO * R, int n)
108 {
109  int i;
110 
111  R->rcb[n].age = 0;
112  for (i = 0; i < R->nrows; i++)
113  R->rcb[i].age++;
114  R->cur = R->rcb[n].row;
115  R->buf = R->rcb[n].buf;
116  return R->buf;
117 }
void Rowio_flush(ROWIO *R)
Flush data.
Definition: rowio/get.c:89
void * buf
Definition: rowio.h:16
int cur
Definition: rowio.h:9
#define NULL
Definition: ccmath.h:32
int(* putrow)(int, const void *, int, int)
Definition: rowio.h:12
int len
Definition: rowio.h:8
struct ROWIO::ROWIO_RCB * rcb
void * buf
Definition: rowio.h:10
Definition: rowio.h:4
int(* getrow)(int, void *, int, int)
Definition: rowio.h:11
int fd
Definition: rowio.h:6
void * Rowio_get(ROWIO *R, int row)
Read a row.
Definition: rowio/get.c:41
int nrows
Definition: rowio.h:7