GRASS 8 Programmer's Manual
8.5.0RC1(2026)-3334b87d9c
Loading...
Searching...
No Matches
ami_stream.cpp
Go to the documentation of this file.
1
/****************************************************************************
2
*
3
* MODULE: iostream
4
*
5
6
* COPYRIGHT (C) 2007 Laura Toma
7
*
8
*
9
10
* Iostream is a library that implements streams, external memory
11
* sorting on streams, and an external memory priority queue on
12
* streams. These are the fundamental components used in external
13
* memory algorithms.
14
15
* Credits: The library was developed by Laura Toma. The kernel of
16
* class STREAM is based on the similar class existent in the GPL TPIE
17
* project developed at Duke University. The sorting and priority
18
* queue have been developed by Laura Toma based on communications
19
* with Rajiv Wickremesinghe. The library was developed as part of
20
* porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
21
* Rajiv Wickremesinghe as part of the Terracost project.
22
23
*
24
* This program is free software; you can redistribute it and/or modify
25
* it under the terms of the GNU General Public License as published by
26
* the Free Software Foundation; either version 2 of the License, or
27
* (at your option) any later version.
28
*
29
30
* This program is distributed in the hope that it will be useful,
31
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33
* General Public License for more details. *
34
* **************************************************************************/
35
36
#include <sys/types.h>
37
#include <sys/stat.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <assert.h>
41
#include <fcntl.h>
42
#include <errno.h>
43
#include <unistd.h>
44
45
extern
"C"
{
46
#include <grass/gis.h>
47
}
48
49
// #include <ami_stream.h>
50
#include <grass/iostream/ami_stream.h>
51
52
const
char
*
ami_str_error
[] = {
53
"AMI_ERROR_NO_ERROR"
,
54
"AMI_ERROR_IO_ERROR"
,
55
"AMI_ERROR_END_OF_STREAM"
,
56
"AMI_ERROR_OUT_OF_RANGE"
,
57
"AMI_ERROR_READ_ONLY"
,
58
"AMI_ERROR_OS_ERROR"
,
59
"AMI_ERROR_MM_ERROR"
,
60
"AMI_ERROR_OBJECT_INITIALIZATION"
,
61
"AMI_ERROR_PERMISSION_DENIED"
,
62
"AMI_ERROR_INSUFFICIENT_MAIN_MEMORY"
,
63
"AMI_ERROR_INSUFFICIENT_AVAILABLE_STREAMS"
,
64
"AMI_ERROR_ENV_UNDEFINED"
,
65
"AMI_ERROR_NO_MAIN_MEMORY_OPERATION"
,
66
};
67
68
/**********************************************************************/
69
/* creates a random file name, opens the file for reading and writing
70
and and returns a file descriptor */
71
int
ami_single_temp_name
(
const
std::string &base,
char
*tmp_path)
72
{
73
74
char
*base_dir;
75
int
fd;
76
77
// get the dir
78
base_dir = getenv(STREAM_TMPDIR);
79
if
(!base_dir) {
80
fprintf(stderr,
"ami_stream: %s not set\n"
, STREAM_TMPDIR);
81
assert
(base_dir);
82
exit(1);
83
}
84
snprintf(tmp_path, GPATH_MAX,
"%s/%s_XXXXXX"
, base_dir, base.c_str());
85
86
fd =
G_mkstemp
(tmp_path, O_RDWR, 0600);
87
88
if
(fd == -1) {
89
cerr <<
"ami_single_temp_name: "
;
90
perror(
"G_mkstemp() failed: "
);
91
assert
(0);
92
exit(1);
93
}
94
return
fd;
95
}
96
97
/**********************************************************************/
98
/* given fd=fide descriptor, associates with it a stream aopened in
99
access_mode and returns it */
100
FILE *
open_stream
(
int
fd, AMI_stream_type st)
101
{
102
FILE *fp =
NULL
;
103
104
assert
(fd > -1);
105
switch
(st) {
106
case
AMI_READ_STREAM:
107
fp = fdopen(fd,
"rb"
);
108
break
;
109
case
AMI_WRITE_STREAM:
110
fp = fdopen(fd,
"wb"
);
111
break
;
112
case
AMI_APPEND_WRITE_STREAM:
113
fp = fdopen(fd,
"ab"
);
114
break
;
115
case
AMI_APPEND_STREAM:
116
fp = fdopen(fd,
"ab+"
);
117
break
;
118
case
AMI_READ_WRITE_STREAM:
119
fp = fdopen(fd,
"rb+"
);
120
if
(!fp) {
121
// if file does not exist, create it
122
fp = fdopen(fd,
"wb+"
);
123
}
124
break
;
125
}
126
if
(!fp) {
127
perror(
"fdopen"
);
128
}
129
assert
(fp);
130
131
return
fp;
132
}
133
134
/**********************************************************************/
135
/* open the file whose name is pathname in access mode */
136
FILE *
open_stream
(
char
*pathname, AMI_stream_type st)
137
{
138
139
FILE *fp =
NULL
;
140
assert
(pathname);
141
142
switch
(st) {
143
case
AMI_READ_STREAM:
144
fp = fopen(pathname,
"rb"
);
145
break
;
146
case
AMI_WRITE_STREAM:
147
fp = fopen(pathname,
"wb"
);
148
break
;
149
case
AMI_APPEND_WRITE_STREAM:
150
fp = fopen(pathname,
"ab"
);
151
break
;
152
case
AMI_APPEND_STREAM:
153
fp = fopen(pathname,
"ab+"
);
154
assert
(fp);
155
G_fseek(fp, 0, SEEK_END);
156
break
;
157
case
AMI_READ_WRITE_STREAM:
158
fp = fopen(pathname,
"rb+"
);
159
if
(!fp) {
160
// if file does not exist, create it
161
fp = fopen(pathname,
"wb+"
);
162
}
163
break
;
164
}
165
if
(!fp) {
166
perror(pathname);
167
assert
(0);
168
exit(1);
169
}
170
assert
(fp);
171
return
fp;
172
}
open_stream
FILE * open_stream(int fd, AMI_stream_type st)
Definition
ami_stream.cpp:100
ami_str_error
const char * ami_str_error[]
Definition
ami_stream.cpp:52
ami_single_temp_name
int ami_single_temp_name(const std::string &base, char *tmp_path)
Definition
ami_stream.cpp:71
NULL
#define NULL
Definition
ccmath.h:32
assert
#define assert(condition)
Definition
lz4.c:291
G_mkstemp
int G_mkstemp(char *template, int flags, int mode)
Returns a file descriptor.
Definition
mkstemp.c:127
iostream
ami_stream.cpp
Generated on
for GRASS 8 Programmer's Manual by
1.15.0