setup.c 1.7 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. {
  38. G_warning ("segment_setup: illegal segment file parameters\n");
  39. return -1;
  40. }
  41. /* This is close to the beginning of the file, so doesn't need to be an off_t */
  42. SEG->offset = (int) lseek (SEG->fd, 0L, SEEK_CUR);
  43. SEG->spr = SEG->ncols / SEG->scols ;
  44. SEG->spill = SEG->ncols % SEG->scols ;
  45. if(SEG->spill)
  46. SEG->spr++ ;
  47. if((SEG->scb =
  48. (struct SEGMENT_SCB *) G_malloc (SEG->nseg * 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. {
  53. if((SEG->scb[i].buf = G_malloc (SEG->size)) == NULL)
  54. return -2;
  55. SEG->scb[i].n = -1; /* mark free */
  56. SEG->scb[i].dirty = 0;
  57. SEG->scb[i].age = 0;
  58. }
  59. SEG->cur = 0;
  60. SEG->open = 1;
  61. return 1;
  62. }