File: init.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 (100 lines) | stat: -rw-r--r-- 2,994 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/****************************************************************************
 *
 * MODULE:       segment
 * AUTHOR(S):    CERL
 *               Bernhard Reiter <bernhard intevation.de>,
 *               Brad Douglas <rez touchofmadness.com>,
 *               Glynn Clements <glynn gclements.plus.com>,
 *               Markus Neteler <neteler itc.it>,
 *               Markus Metz <markus.metz.giswork googlemail.com>
 * PURPOSE:      Segment initialization routines
 * COPYRIGHT:    (C) 2000-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.
 *
 *****************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <grass/gis.h>
#include "local_proto.h"

static int read_int(int, int *);
static int read_off_t(int, off_t *);

/* fd must be open for read and write */

/**
 * \fn int Segment_init (SEGMENT *SEG, int fd, int nseg)
 *
 * \brief Initialize segment structure.
 *
 * Initializes the <b>seg</b> structure. The file on <b>fd</b> is
 * a segment file created by <i>Segment_format()</i> and must be open
 * for reading and writing. The segment file configuration parameters
 * <i>nrows, ncols, srows, scols</i>, and <i>len</i>, as written to the
 * file by <i>Segment_format()</i>, are read from the file and stored in
 * the <b>seg</b> structure. <b>nsegs</b> specifies the number of
 * segments that will be retained in memory. The minimum value allowed
 * is 1.
 *
 * <b>Note:</b> The size of a segment is <em>scols*srows*len</em> plus a
 * few bytes for managing each segment.
 *
 * \param[in,out] SEG segment
 * \param[in] fd file descriptor
 * \param[in] nseg number of segments to remain in memory
 * \return 1 if successful
 * \return -1 if unable to seek or read segment file
 * \return -2 if out of memory
 */

int Segment_init(SEGMENT *SEG, int fd, int nseg)
{
    SEG->open = 0;
    SEG->fd = fd;
    SEG->nseg = nseg;

    if (lseek(fd, 0L, SEEK_SET) < 0) {
        int err = errno;

        G_warning("Segment_init: %s", strerror(err));
        return -1;
    }

    /* read the header */
    if (!read_off_t(fd, &SEG->nrows) || !read_off_t(fd, &SEG->ncols) ||
        !read_int(fd, &SEG->srows) || !read_int(fd, &SEG->scols) ||
        !read_int(fd, &SEG->len))
        return -1;

    return seg_setup(SEG);
}

static int read_int(int fd, int *n)
{
    int bytes_read;

    if ((bytes_read = read(fd, n, sizeof(int))) == -1)
        G_warning("read_int: %s", strerror(errno));

    bytes_read = (bytes_read == sizeof(int));

    return bytes_read;
}

static int read_off_t(int fd, off_t *n)
{
    int bytes_read;

    if ((bytes_read = read(fd, n, sizeof(off_t))) == -1)
        G_warning("read_off_t: %s", strerror(errno));

    bytes_read = (bytes_read == sizeof(off_t));

    return bytes_read;
}