GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
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 
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 }
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
Definition: symbol/read.c:220
int Segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
Definition: segment/init.c:59
int seg_setup(SEGMENT *SEG)
Internal use only.
Definition: segment/setup.c:36
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203