open.c 2.6 KB

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