GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
segment/open.c
Go to the documentation of this file.
1 
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <grass/gis.h>
18 #include <grass/glocale.h>
19 #include "local_proto.h"
20 
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.
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
int Segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
Definition: segment/init.c:59
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203