GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 /**************************************************************************** 00003 * 00004 * MODULE: segment 00005 * AUTHOR(S): CERL 00006 * Bernhard Reiter <bernhard intevation.de>, 00007 * Brad Douglas <rez touchofmadness.com>, 00008 * Glynn Clements <glynn gclements.plus.com>, 00009 * Markus Neteler <neteler itc.it> 00010 * PURPOSE: Segment initialization routines 00011 * COPYRIGHT: (C) 2000-2006 by the GRASS Development Team 00012 * 00013 * This program is free software under the GNU General Public 00014 * License (>=v2). Read the file COPYING that comes with GRASS 00015 * for details. 00016 * 00017 *****************************************************************************/ 00018 00019 #include <stdio.h> 00020 #include <unistd.h> 00021 #include <string.h> 00022 #include <errno.h> 00023 #include <grass/segment.h> 00024 00025 00026 static int read_int(int, int *); 00027 00028 /* fd must be open for read and write */ 00029 00030 00056 int segment_init(SEGMENT * SEG, int fd, int nseg) 00057 { 00058 SEG->open = 0; 00059 SEG->fd = fd; 00060 SEG->nseg = nseg; 00061 00062 if (lseek(fd, 0L, SEEK_SET) < 0) { 00063 G_warning("segment_init: %s", strerror(errno)); 00064 return -1; 00065 } 00066 00067 /* read the header */ 00068 if (!read_int(fd, &SEG->nrows) 00069 || !read_int(fd, &SEG->ncols) 00070 || !read_int(fd, &SEG->srows) 00071 || !read_int(fd, &SEG->scols) 00072 || !read_int(fd, &SEG->len)) 00073 return -1; 00074 00075 return segment_setup(SEG); 00076 } 00077 00078 00079 static int read_int(int fd, int *n) 00080 { 00081 int bytes_read; 00082 00083 if ((bytes_read = read(fd, n, sizeof(int))) == -1) 00084 G_warning("read_int: %s", strerror(errno)); 00085 00086 bytes_read = (bytes_read == sizeof(int)); 00087 00088 return bytes_read; 00089 }