GRASS GIS 8 Programmer's Manual 8.2.1RC1(2022)-exported
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
46int
47Segment_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}
#define NULL
Definition: ccmath.h:32
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
Definition: gis/error.c:109
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204
int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols, int len)
Format a segment file.
int Segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
Definition: segment/init.c:59
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 * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:87