init.c 2.6 KB

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