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