GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 2018
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  int nseg_total;
52 
53  nseg_total = ((nrows + srows - 1) / srows) *
54  ((ncols + scols - 1) / scols);
55 
56  if (nseg >= nseg_total) {
57  G_verbose_message(_("Using memory cache"));
58 
59  SEG->nrows = nrows;
60  SEG->ncols = ncols;
61  SEG->len = len;
62  SEG->nseg = nseg;
63  SEG->cache = G_calloc(sizeof(char) * SEG->nrows * SEG->ncols, SEG->len);
64  SEG->scb = NULL;
65  SEG->open = 1;
66 
67  return 1;
68  }
69 
70  G_verbose_message(_("Using disk cache"));
71 
72  if (!fname) {
73  G_warning(_("Segment file name is NULL"));
74  return -1;
75  }
76  /* file exists? */
77  if (access(fname, F_OK) == 0) {
78  G_warning(_("Segment file exists already"));
79  return -1;
80  }
81 
82  SEG->fname = G_store(fname);
83  SEG->fd = -1;
84 
85  if (-1 == (SEG->fd = creat(SEG->fname, 0666))) {
86  G_warning(_("Unable to create segment file"));
87  return -1;
88  }
89  if (0 > (ret = Segment_format_nofill(SEG->fd, nrows, ncols, srows,
90  scols, len))) {
91  close(SEG->fd);
92  unlink(SEG->fname);
93  if (ret == -1) {
94  G_warning(_("Could not write segment file"));
95  return -2;
96  }
97  else { /* ret = -3 */
98  G_warning(_("Illegal segment configuration parameter(s)"));
99  return ret;
100  }
101  }
102  /* re-open for read and write */
103  close(SEG->fd);
104  SEG->fd = -1;
105  if (-1 == (SEG->fd = open(SEG->fname, 2))) {
106  unlink(SEG->fname);
107  G_warning(_("Unable to re-open segment file"));
108  return -4;
109  }
110  if (0 > (ret = Segment_init(SEG, SEG->fd, nseg))) {
111  close(SEG->fd);
112  unlink(SEG->fname);
113  if (ret == -1) {
114  G_warning(_("Could not read segment file"));
115  return -5;
116  }
117  else {
118  G_warning(_("Out of memory"));
119  return -6;
120  }
121  }
122 
123  return 1;
124 }
int fd
Definition: segment.h:44
int nseg
Definition: segment.h:58
int open
Definition: segment.h:21
int len
Definition: segment.h:24
#define NULL
Definition: ccmath.h:32
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
#define G_calloc(m, n)
Definition: defs/gis.h:113
int Segment_format_nofill(int, off_t, off_t, int, int, int)
Format a segment file.
char * fname
Definition: segment.h:43
off_t ncols
Definition: segment.h:23
off_t nrows
Definition: segment.h:22
int Segment_init(SEGMENT *, int, int)
Initialize segment structure.
Definition: segment/init.c:59
char * cache
Definition: segment.h:62
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
void void G_verbose_message(const char *,...) __attribute__((format(printf
struct SEGMENT::scb * scb