init.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <grass/gis.h>
  23. #include <grass/segment.h>
  24. static int read_int(int, int *);
  25. /* fd must be open for read and write */
  26. /**
  27. * \fn int segment_init (SEGMENT *SEG, int fd, int nseg)
  28. *
  29. * \brief Initialize segment structure.
  30. *
  31. * Initializes the <b>seg</b> structure. The file on <b>fd</b> is
  32. * a segment file created by <i>segment_format()</i> and must be open
  33. * for reading and writing. The segment file configuration parameters
  34. * <i>nrows, ncols, srows, scols</i>, and <i>len</i>, as written to the
  35. * file by <i>segment_format()</i>, are read from the file and stored in
  36. * the <b>seg</b> structure. <b>nsegs</b> specifies the number of
  37. * segments that will be retained in memory. The minimum value allowed
  38. * is 1.
  39. *
  40. * <b>Note:</b> The size of a segment is <em>scols*srows*len</em> plus a
  41. * few bytes for managing each segment.
  42. *
  43. * \param[in,out] seg segment
  44. * \param[in] fd file descriptor
  45. * \param[in] nsegs number of segments to remain in memory
  46. * \return 1 if successful
  47. * \return -1 if unable to seek or read segment file
  48. * \return -2 if out of memory
  49. */
  50. int segment_init(SEGMENT * SEG, int fd, int nseg)
  51. {
  52. SEG->open = 0;
  53. SEG->fd = fd;
  54. SEG->nseg = nseg;
  55. if (lseek(fd, 0L, SEEK_SET) < 0) {
  56. G_warning("segment_init: %s", strerror(errno));
  57. return -1;
  58. }
  59. /* read the header */
  60. if (!read_int(fd, &SEG->nrows)
  61. || !read_int(fd, &SEG->ncols)
  62. || !read_int(fd, &SEG->srows)
  63. || !read_int(fd, &SEG->scols)
  64. || !read_int(fd, &SEG->len))
  65. return -1;
  66. return segment_setup(SEG);
  67. }
  68. static int read_int(int fd, int *n)
  69. {
  70. int bytes_read;
  71. if ((bytes_read = read(fd, n, sizeof(int))) == -1)
  72. G_warning("read_int: %s", strerror(errno));
  73. bytes_read = (bytes_read == sizeof(int));
  74. return bytes_read;
  75. }