init.c 2.9 KB

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