GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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 int Segment_init(SEGMENT *SEG, int fd, int nseg)
56 {
57  SEG->open = 0;
58  SEG->fd = fd;
59  SEG->nseg = nseg;
60 
61  if (lseek(fd, 0L, SEEK_SET) < 0) {
62  int err = errno;
63 
64  G_warning("Segment_init: %s", strerror(err));
65  return -1;
66  }
67 
68  /* read the header */
69  if (!read_off_t(fd, &SEG->nrows) || !read_off_t(fd, &SEG->ncols) ||
70  !read_int(fd, &SEG->srows) || !read_int(fd, &SEG->scols) ||
71  !read_int(fd, &SEG->len))
72  return -1;
73 
74  return seg_setup(SEG);
75 }
76 
77 static int read_int(int fd, int *n)
78 {
79  int bytes_read;
80 
81  if ((bytes_read = read(fd, n, sizeof(int))) == -1)
82  G_warning("read_int: %s", strerror(errno));
83 
84  bytes_read = (bytes_read == sizeof(int));
85 
86  return bytes_read;
87 }
88 
89 static int read_off_t(int fd, off_t *n)
90 {
91  int bytes_read;
92 
93  if ((bytes_read = read(fd, n, sizeof(off_t))) == -1)
94  G_warning("read_off_t: %s", strerror(errno));
95 
96  bytes_read = (bytes_read == sizeof(off_t));
97 
98  return bytes_read;
99 }
void G_warning(const char *,...) __attribute__((format(printf
int Segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
Definition: segment/init.c:55
int seg_setup(SEGMENT *SEG)
Internal use only.
Definition: segment/setup.c:33
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
#define read
Definition: unistd.h:5