GRASS 8 Programmer's Manual 8.6.0dev(2026)-8843f13794
Loading...
Searching...
No Matches
segment/init.c
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * MODULE: segment
4 * AUTHOR(S): CERL
5 * Bernhard Reiter <bernhard intevation.de>,
6 * Brad Douglas <rez touchofmadness.com>,
7 * Glynn Clements <glynn gclements.plus.com>,
8 * Markus Neteler <neteler itc.it>,
9 * Markus Metz <markus.metz.giswork googlemail.com>
10 * PURPOSE: Segment initialization routines
11 * SPDX-FileCopyrightText: 2000-2009 GRASS Development Team
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 *****************************************************************************/
15
16#include <stdio.h>
17#include <unistd.h>
18#include <string.h>
19#include <errno.h>
20#include <grass/gis.h>
21#include "local_proto.h"
22
23static int read_int(int, int *);
24static int read_off_t(int, off_t *);
25
26/* fd must be open for read and write */
27
28/**
29 * \fn int Segment_init (SEGMENT *SEG, int fd, int nseg)
30 *
31 * \brief Initialize segment structure.
32 *
33 * Initializes the <b>seg</b> structure. The file on <b>fd</b> is
34 * a segment file created by <i>Segment_format()</i> and must be open
35 * for reading and writing. The segment file configuration parameters
36 * <i>nrows, ncols, srows, scols</i>, and <i>len</i>, as written to the
37 * file by <i>Segment_format()</i>, are read from the file and stored in
38 * the <b>seg</b> structure. <b>nsegs</b> specifies the number of
39 * segments that will be retained in memory. The minimum value allowed
40 * is 1.
41 *
42 * <b>Note:</b> The size of a segment is <em>scols*srows*len</em> plus a
43 * few bytes for managing each segment.
44 *
45 * \param[in,out] SEG segment
46 * \param[in] fd file descriptor
47 * \param[in] nseg number of segments to remain in memory
48 * \return 1 if successful
49 * \return -1 if unable to seek or read segment file
50 * \return -2 if out of memory
51 */
52int Segment_init(SEGMENT *SEG, int fd, int nseg)
53{
54 SEG->open = 0;
55 SEG->fd = fd;
56 SEG->nseg = nseg;
57
58 if (lseek(fd, 0L, SEEK_SET) == -1) {
59 int err = errno;
60
61 G_warning("Segment_init: %s", strerror(err));
62 return -1;
63 }
64
65 /* read the header */
66 if (!read_off_t(fd, &SEG->nrows) || !read_off_t(fd, &SEG->ncols) ||
67 !read_int(fd, &SEG->srows) || !read_int(fd, &SEG->scols) ||
68 !read_int(fd, &SEG->len))
69 return -1;
70
71 return seg_setup(SEG);
72}
73
74static int read_int(int fd, int *n)
75{
76 int bytes_read;
77
78 if ((bytes_read = read(fd, n, sizeof(int))) == -1)
79 G_warning("read_int: %s", strerror(errno));
80
81 bytes_read = (bytes_read == sizeof(int));
82
83 return bytes_read;
84}
85
86static int read_off_t(int fd, off_t *n)
87{
88 int bytes_read;
89
90 if ((bytes_read = read(fd, n, sizeof(off_t))) == -1)
91 G_warning("read_off_t: %s", strerror(errno));
92
93 bytes_read = (bytes_read == sizeof(off_t));
94
95 return bytes_read;
96}
void G_warning(const char *,...) __attribute__((format(printf
int Segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
int seg_setup(SEGMENT *SEG)
Internal use only.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
#define read
Definition unistd.h:5