open.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * \file lib/segment/open.c
  3. *
  4. * \brief Segment creation routine.
  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 2012
  12. */
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. #include "local_proto.h"
  18. /**
  19. * \brief Initialize segment structure and open segment file.
  20. *
  21. * Initializes the <b>seg</b> structure and prepares a temporary file.
  22. * This fn is a wrapper for Segment_format() and Segment_init()
  23. *
  24. * <b>Note:</b> The file with name fname will be created anew.
  25. *
  26. * \param[in,out] SEG segment
  27. * \param[in] fname file name
  28. * \param[in] nrows number of non-segmented rows
  29. * \param[in] ncols number of non-segmented columns
  30. * \param[in] srows segment rows
  31. * \param[in] scols segment columns
  32. * \param[in] len length of data type
  33. * \param[in] nseg number of segments to remain in memory
  34. * \return 1 if successful
  35. * \return -1 if file name is invalid
  36. * \return -2 if file write error
  37. * \return -3 if illegal parameters are passed
  38. * \return -4 if file could not be re-opened
  39. * \return -5 if prepared file could not be read
  40. * \return -6 if out of memory
  41. */
  42. int
  43. Segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols,
  44. int srows, int scols, int len, int nseg)
  45. {
  46. int ret;
  47. if (!fname) {
  48. G_warning(_("Segment file name is NULL"));
  49. return -1;
  50. }
  51. /* file exists? */
  52. if (access(fname, F_OK) == 0) {
  53. G_warning(_("Segment file exists already"));
  54. return -1;
  55. }
  56. SEG->fname = G_store(fname);
  57. SEG->fd = -1;
  58. if (-1 == (SEG->fd = creat(SEG->fname, 0666))) {
  59. G_warning(_("Unable to create segment file"));
  60. return -1;
  61. }
  62. if (0 > (ret = Segment_format(SEG->fd, nrows, ncols, srows,
  63. scols, len))) {
  64. close(SEG->fd);
  65. unlink(SEG->fname);
  66. if (ret == -1) {
  67. G_warning(_("Could not write segment file"));
  68. return -2;
  69. }
  70. else { /* ret = -3 */
  71. G_warning(_("Illegal segment configuration parameter(s)"));
  72. return ret;
  73. }
  74. }
  75. /* re-open for read and write */
  76. close(SEG->fd);
  77. if (-1 == (SEG->fd = open(SEG->fname, 2))) {
  78. unlink(SEG->fname);
  79. G_warning(_("Unable to re-open segment file"));
  80. return -4;
  81. }
  82. if (0 > (ret = Segment_init(SEG, SEG->fd, nseg))) {
  83. close(SEG->fd);
  84. unlink(SEG->fname);
  85. if (ret == -1) {
  86. G_warning(_("Could not read segment file"));
  87. return -5;
  88. }
  89. else {
  90. G_warning(_("Out of memory"));
  91. return -6;
  92. }
  93. }
  94. return 1;
  95. }