1
/****************************************************************************
5
* COPYRIGHT (C) 2007 Laura Toma
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
*****************************************************************************/
19
#include <sys/types.h>
28
//#include <ami_stream.h>
29
#include <grass/iostream/ami_stream.h>
32
char *ami_str_error[] = {
35
"AMI_ERROR_END_OF_STREAM",
36
"AMI_ERROR_OUT_OF_RANGE",
37
"AMI_ERROR_READ_ONLY",
40
"AMI_ERROR_OBJECT_INITIALIZATION",
41
"AMI_ERROR_PERMISSION_DENIED",
42
"AMI_ERROR_INSUFFICIENT_MAIN_MEMORY",
43
"AMI_ERROR_INSUFFICIENT_AVAILABLE_STREAMS",
44
"AMI_ERROR_ENV_UNDEFINED",
45
"AMI_ERROR_NO_MAIN_MEMORY_OPERATION",
48
/**********************************************************************/
49
/* creates a random file name, opens the file for reading and writing
50
and and returns a file descriptor */
52
ami_single_temp_name(const std::string& base, char* tmp_path) {
58
base_dir = getenv(STREAM_TMPDIR);
60
fprintf(stderr, "ami_stream: %s not set\n", STREAM_TMPDIR);
64
sprintf(tmp_path, "%s/%s_XXXXXX", base_dir, base.c_str());
67
fd = mktemp(tmp_path) ? open(tmp_path, O_CREAT|O_EXCL|O_RDWR, 0600) : -1;
69
fd = mkstemp(tmp_path);
73
cerr << "ami_single_temp_name: ";
75
perror("mktemp failed: ");
77
perror("mkstemp failed: ");
86
/**********************************************************************/
87
/* given fd=fide descriptor, associates with it a stream aopened in
88
access_mode and returns it */
90
open_stream(int fd, AMI_stream_type st) {
96
fp = fdopen(fd, "rb");
98
case AMI_WRITE_STREAM:
99
fp = fdopen(fd, "wb");
101
case AMI_APPEND_WRITE_STREAM:
102
fp = fdopen(fd, "ab");
104
case AMI_APPEND_STREAM:
105
fp = fdopen(fd, "ab+");
107
case AMI_READ_WRITE_STREAM:
108
fp = fdopen(fd, "rb+");
110
//if file does not exist, create it
111
fp = fdopen(fd, "wb+");
124
/**********************************************************************/
125
/* open the file whose name is pathname in access mode */
127
open_stream(char* pathname, AMI_stream_type st) {
133
case AMI_READ_STREAM:
134
fp = fopen(pathname, "rb");
136
case AMI_WRITE_STREAM:
137
fp = fopen(pathname, "wb");
139
case AMI_APPEND_WRITE_STREAM:
140
fp = fopen(pathname, "ab");
142
case AMI_APPEND_STREAM:
143
fp = fopen(pathname, "ab+");
145
if (fseek (fp, 0, SEEK_END) == -1) {
146
perror("AMI_STREAM: fseek failed ");
149
case AMI_READ_WRITE_STREAM:
150
fp = fopen(pathname, "rb+");
152
//if file does not exist, create it
153
fp = fopen(pathname, "wb+");