sseg_open.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <grass/gis.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <grass/segment.h>
  5. #include "Gwater.h"
  6. int
  7. seg_open(SSEG * sseg, GW_LARGE_INT nrows, GW_LARGE_INT ncols, int row_in_seg, int col_in_seg,
  8. int nsegs_in_memory, int size_struct)
  9. {
  10. char *filename;
  11. int errflag;
  12. int fd;
  13. sseg->filename = NULL;
  14. sseg->fd = -1;
  15. filename = G_tempfile();
  16. if (-1 == (fd = creat(filename, 0666))) {
  17. G_warning("seg_open(): unable to create segment file");
  18. return -2;
  19. }
  20. if (0 > (errflag = segment_format(fd, nrows, ncols,
  21. row_in_seg, col_in_seg, size_struct))) {
  22. close(fd);
  23. unlink(filename);
  24. if (errflag == -1) {
  25. G_warning("seg_open(): could not write segment file");
  26. return -1;
  27. }
  28. else {
  29. G_warning("seg_open(): illegal configuration parameter(s)");
  30. return -3;
  31. }
  32. }
  33. close(fd);
  34. if (-1 == (fd = open(filename, 2))) {
  35. unlink(filename);
  36. G_warning("seg_open(): unable to re-open segment file");
  37. return -4;
  38. }
  39. if (0 > (errflag = segment_init(&(sseg->seg), fd, nsegs_in_memory))) {
  40. close(fd);
  41. unlink(filename);
  42. if (errflag == -1) {
  43. G_warning("seg_open(): could not read segment file");
  44. return -5;
  45. }
  46. else {
  47. G_warning("seg_open(): out of memory");
  48. return -6;
  49. }
  50. }
  51. sseg->filename = filename;
  52. sseg->fd = fd;
  53. return 0;
  54. }