GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-8828094037
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  * COPYRIGHT: (C) 2000-2009 by the GRASS Development Team
12  *
13  * This program is free software under the GNU General Public
14  * License (>=v2). Read the file COPYING that comes with GRASS
15  * for details.
16  *
17  *****************************************************************************/
18 
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <grass/gis.h>
24 #include "local_proto.h"
25 
26 static int read_int(int, int *);
27 static int read_off_t(int, off_t *);
28 
29 /* fd must be open for read and write */
30 
31 /**
32  * \fn int Segment_init (SEGMENT *SEG, int fd, int nseg)
33  *
34  * \brief Initialize segment structure.
35  *
36  * Initializes the <b>seg</b> structure. The file on <b>fd</b> is
37  * a segment file created by <i>Segment_format()</i> and must be open
38  * for reading and writing. The segment file configuration parameters
39  * <i>nrows, ncols, srows, scols</i>, and <i>len</i>, as written to the
40  * file by <i>Segment_format()</i>, are read from the file and stored in
41  * the <b>seg</b> structure. <b>nsegs</b> specifies the number of
42  * segments that will be retained in memory. The minimum value allowed
43  * is 1.
44  *
45  * <b>Note:</b> The size of a segment is <em>scols*srows*len</em> plus a
46  * few bytes for managing each segment.
47  *
48  * \param[in,out] SEG segment
49  * \param[in] fd file descriptor
50  * \param[in] nseg number of segments to remain in memory
51  * \return 1 if successful
52  * \return -1 if unable to seek or read segment file
53  * \return -2 if out of memory
54  */
55 
56 int Segment_init(SEGMENT *SEG, int fd, int nseg)
57 {
58  SEG->open = 0;
59  SEG->fd = fd;
60  SEG->nseg = nseg;
61 
62  if (lseek(fd, 0L, SEEK_SET) < 0) {
63  int err = errno;
64 
65  G_warning("Segment_init: %s", strerror(err));
66  return -1;
67  }
68 
69  /* read the header */
70  if (!read_off_t(fd, &SEG->nrows) || !read_off_t(fd, &SEG->ncols) ||
71  !read_int(fd, &SEG->srows) || !read_int(fd, &SEG->scols) ||
72  !read_int(fd, &SEG->len))
73  return -1;
74 
75  return seg_setup(SEG);
76 }
77 
78 static int read_int(int fd, int *n)
79 {
80  int bytes_read;
81 
82  if ((bytes_read = read(fd, n, sizeof(int))) == -1)
83  G_warning("read_int: %s", strerror(errno));
84 
85  bytes_read = (bytes_read == sizeof(int));
86 
87  return bytes_read;
88 }
89 
90 static int read_off_t(int fd, off_t *n)
91 {
92  int bytes_read;
93 
94  if ((bytes_read = read(fd, n, sizeof(off_t))) == -1)
95  G_warning("read_off_t: %s", strerror(errno));
96 
97  bytes_read = (bytes_read == sizeof(off_t));
98 
99  return bytes_read;
100 }
void G_warning(const char *,...) __attribute__((format(printf
int Segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
Definition: segment/init.c:56
int seg_setup(SEGMENT *SEG)
Internal use only.
Definition: segment/setup.c:34
int len
Definition: segment.h:23
off_t nrows
Definition: segment.h:21
off_t ncols
Definition: segment.h:22
int nseg
Definition: segment.h:57
int open
Definition: segment.h:20
int fd
Definition: segment.h:43
int srows
Definition: segment.h:24
int scols
Definition: segment.h:25
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
Definition: symbol/read.c:216