GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
segment/open.c
Go to the documentation of this file.
1
/**
2
* \file lib/segment/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 Development Team
10
*
11
* \date 2018
12
*/
13
14
#include <unistd.h>
15
#include <fcntl.h>
16
#include <grass/gis.h>
17
#include <grass/glocale.h>
18
#include "local_proto.h"
19
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
Segment_open
(SEGMENT *SEG,
char
*fname, off_t nrows, off_t ncols,
int
srows,
45
int
scols,
int
len,
int
nseg)
46
{
47
int
ret;
48
int
nseg_total;
49
50
nseg_total = ((nrows + srows - 1) / srows) * ((ncols + scols - 1) / scols);
51
52
if
(nseg >= nseg_total) {
53
G_verbose_message
(_(
"Using memory cache"
));
54
55
SEG->nrows = nrows;
56
SEG->ncols = ncols;
57
SEG->len = len;
58
SEG->nseg = nseg;
59
SEG->cache = G_calloc(
sizeof
(
char
) * SEG->nrows * SEG->ncols, SEG->len);
60
SEG->scb =
NULL
;
61
SEG->open = 1;
62
63
return
1;
64
}
65
66
G_verbose_message
(_(
"Using disk cache"
));
67
68
if
(!fname) {
69
G_warning
(_(
"Segment file name is NULL"
));
70
return
-1;
71
}
72
/* file exists? */
73
if
(access(fname, F_OK) == 0) {
74
G_warning
(_(
"Segment file exists already"
));
75
return
-1;
76
}
77
78
SEG->fname =
G_store
(fname);
79
SEG->fd = -1;
80
81
if
(-1 == (SEG->fd = creat(SEG->fname, 0666))) {
82
G_warning
(_(
"Unable to create segment file"
));
83
return
-1;
84
}
85
if
(0 > (ret =
Segment_format_nofill
(SEG->fd, nrows, ncols, srows, scols,
86
len))) {
87
close(SEG->fd);
88
unlink(SEG->fname);
89
if
(ret == -1) {
90
G_warning
(_(
"Could not write segment file"
));
91
return
-2;
92
}
93
else
{
/* ret = -3 */
94
G_warning
(_(
"Illegal segment configuration parameter(s)"
));
95
return
ret;
96
}
97
}
98
/* re-open for read and write */
99
close(SEG->fd);
100
SEG->fd = -1;
101
if
(-1 == (SEG->fd = open(SEG->fname, 2))) {
102
unlink(SEG->fname);
103
G_warning
(_(
"Unable to re-open segment file"
));
104
return
-4;
105
}
106
if
(0 > (ret =
Segment_init
(SEG, SEG->fd, nseg))) {
107
close(SEG->fd);
108
unlink(SEG->fname);
109
if
(ret == -1) {
110
G_warning
(_(
"Could not read segment file"
));
111
return
-5;
112
}
113
else
{
114
G_warning
(_(
"Out of memory"
));
115
return
-6;
116
}
117
}
118
119
return
1;
120
}
NULL
#define NULL
Definition
ccmath.h:32
G_verbose_message
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
Definition
gis/error.c:108
G_warning
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition
gis/error.c:203
Segment_format_nofill
int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols, int len)
Format a segment file.
Definition
segment/format.c:97
Segment_init
int Segment_init(SEGMENT *SEG, int fd, int nseg)
Initialize segment structure.
Definition
segment/init.c:55
Segment_open
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:44
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition
strings.c:87
segment
open.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0