GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72729
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
segment/open.c
Go to the documentation of this file.
1 
2 /**
3  * \file lib/segment/open.c
4  *
5  * \brief Segment creation routine.
6  *
7  * This program is free software under the GNU General Public License
8  * (>=v2). Read the file COPYING that comes with GRASS for details.
9  *
10  * \author GRASS GIS Development Team
11  *
12  * \date 2012
13  */
14 
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <grass/gis.h>
18 #include <grass/glocale.h>
19 #include "local_proto.h"
20 
21 /**
22  * \brief Initialize segment structure and open segment file.
23  *
24  * Initializes the <b>seg</b> structure and prepares a temporary file.
25  * This fn is a wrapper for Segment_format() and Segment_init()
26  *
27  * <b>Note:</b> The file with name fname will be created anew.
28  *
29  * \param[in,out] SEG segment
30  * \param[in] fname file name
31  * \param[in] nrows number of non-segmented rows
32  * \param[in] ncols number of non-segmented columns
33  * \param[in] srows segment rows
34  * \param[in] scols segment columns
35  * \param[in] len length of data type
36  * \param[in] nseg number of segments to remain in memory
37  * \return 1 if successful
38  * \return -1 if file name is invalid
39  * \return -2 if file write error
40  * \return -3 if illegal parameters are passed
41  * \return -4 if file could not be re-opened
42  * \return -5 if prepared file could not be read
43  * \return -6 if out of memory
44  */
45 
46 int
47 Segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols,
48  int srows, int scols, int len, int nseg)
49 {
50  int ret;
51 
52  if (!fname) {
53  G_warning(_("Segment file name is NULL"));
54  return -1;
55  }
56  /* file exists? */
57  if (access(fname, F_OK) == 0) {
58  G_warning(_("Segment file exists already"));
59  return -1;
60  }
61 
62  SEG->fname = G_store(fname);
63  SEG->fd = -1;
64 
65  if (-1 == (SEG->fd = creat(SEG->fname, 0666))) {
66  G_warning(_("Unable to create segment file"));
67  return -1;
68  }
69  if (0 > (ret = Segment_format(SEG->fd, nrows, ncols, srows,
70  scols, len))) {
71  close(SEG->fd);
72  unlink(SEG->fname);
73  if (ret == -1) {
74  G_warning(_("Could not write segment file"));
75  return -2;
76  }
77  else { /* ret = -3 */
78  G_warning(_("Illegal segment configuration parameter(s)"));
79  return ret;
80  }
81  }
82  /* re-open for read and write */
83  close(SEG->fd);
84  SEG->fd = -1;
85  if (-1 == (SEG->fd = open(SEG->fname, 2))) {
86  unlink(SEG->fname);
87  G_warning(_("Unable to re-open segment file"));
88  return -4;
89  }
90  if (0 > (ret = Segment_init(SEG, SEG->fd, nseg))) {
91  close(SEG->fd);
92  unlink(SEG->fname);
93  if (ret == -1) {
94  G_warning(_("Could not read segment file"));
95  return -5;
96  }
97  else {
98  G_warning(_("Out of memory"));
99  return -6;
100  }
101  }
102 
103  return 1;
104 }
int Segment_format(int fd, off_t nrows, off_t ncols, int srows, int scols, int len)
Format a segment file.
int fd
Definition: segment.h:44
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int Segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols, int srows, int scols, int len, int nseg)
Initialize segment structure and open segment file.
Definition: segment/open.c:47
char * fname
Definition: segment.h:43
int Segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
Definition: segment/init.c:59
#define _(str)
Definition: glocale.h:13
close(fd)
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204