setup.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * \file setup.c
  3. *
  4. * \brief Segment setup routines.
  5. *
  6. * This program is free software under the GNU General Public License
  7. * (>=v2). Read the file COPYING that comes with GRASS for details.
  8. *
  9. * \author GRASS GIS Development Team
  10. *
  11. * \date 2005-2006
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <grass/gis.h>
  16. #include <grass/segment.h>
  17. /**
  18. * \fn int segment_setup (SEGMENT *SEG)
  19. *
  20. * \brief Setup segment.
  21. *
  22. * <b>SEG</b> must have the following parms set:
  23. * fd (open for read and write), nrows, ncols, srows, scols, len, nseg
  24. *
  25. * \param[in,out] SEG segment
  26. * \return 1 if successful
  27. * \return -1 if illegal parameters are passed in <b>SEG</b>
  28. * \return -2 if unable to allocate memory
  29. */
  30. int segment_setup(SEGMENT * SEG)
  31. {
  32. int i;
  33. SEG->open = 0;
  34. if (SEG->nrows <= 0 || SEG->ncols <= 0
  35. || SEG->srows <= 0 || SEG->scols <= 0
  36. || SEG->len <= 0 || SEG->nseg <= 0) {
  37. G_warning("segment_setup: illegal segment file parameters\n");
  38. return -1;
  39. }
  40. /* This is close to the beginning of the file, so doesn't need to be an off_t */
  41. SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
  42. SEG->spr = SEG->ncols / SEG->scols;
  43. SEG->spill = SEG->ncols % SEG->scols;
  44. if (SEG->spill)
  45. SEG->spr++;
  46. if ((SEG->scb =
  47. (struct SEGMENT_SCB *)G_malloc(SEG->nseg *
  48. sizeof(struct SEGMENT_SCB))) == NULL)
  49. return -2;
  50. SEG->size = SEG->srows * SEG->scols * SEG->len;
  51. for (i = 0; i < SEG->nseg; i++) {
  52. if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
  53. return -2;
  54. SEG->scb[i].n = -1; /* mark free */
  55. SEG->scb[i].dirty = 0;
  56. SEG->scb[i].age = 0;
  57. }
  58. SEG->cur = 0;
  59. SEG->open = 1;
  60. return 1;
  61. }