File: setup.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (72 lines) | stat: -rw-r--r-- 2,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*!
   \file rowio/setup.c

   \brief RowIO library - Setup

   (C) 2001-2009 by the GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2).  Read the file COPYING that comes with GRASS for details.

   \author Original author CERL
 */

#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
#include <grass/glocale.h>
#include <grass/rowio.h>

/*!
 * \brief Configure rowio structure
 *
 * Rowio_setup() initializes the ROWIO structure <i>r</i> and
 * allocates the required memory buffers. The file descriptor
 * <i>fd</i> must be open for reading. The number of rows to be held
 * in memory is <i>nrows</i>. The length in bytes of each row is
 * <i>len</i>. The routine which will be called to read data from the
 * file is getrow() and must be provided by the programmer. If the
 * application requires that the rows be written back into the file if
 * changed, the file descriptor <i>fd</i> must be open for write as
 * well, and the programmer must provide a putrow() routine to write
 * the data into the file. If no writing of the file is to occur,
 * specify NULL for putrow().
 *
 * \param R pointer to ROWIO structure
 * \param fd file descriptor
 * \param nrows number of rows
 * \param getrow get row function
 *
 * \return 1 on success
 * \return -1 no memory
 */

int Rowio_setup(ROWIO *R, int fd, int nrows, int len,
                int (*getrow)(int, void *, int, int),
                int (*putrow)(int, const void *, int, int))
{
    int i;

    R->getrow = getrow;
    R->putrow = putrow;
    R->nrows = nrows;
    R->len = len;
    R->cur = -1;
    R->buf = NULL;
    R->fd = fd;

    R->rcb = (struct ROWIO_RCB *)G_malloc(nrows * sizeof(struct ROWIO_RCB));
    if (R->rcb == NULL) {
        G_warning(_("Out of memory"));
        return -1;
    }
    for (i = 0; i < nrows; i++) {
        R->rcb[i].buf = G_malloc(len);
        if (R->rcb[i].buf == NULL) {
            G_warning(_("Out of memory"));
            return -1;
        }
        R->rcb[i].row = -1; /* mark not used */
    }
    return 1;
}