GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <assert.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <unistd.h>
45 
46 extern "C" {
47 #include <grass/gis.h>
48 }
49 
50 //#include <ami_stream.h>
52 
53 
54 const char *ami_str_error[] = {
55  "AMI_ERROR_NO_ERROR",
56  "AMI_ERROR_IO_ERROR",
57  "AMI_ERROR_END_OF_STREAM",
58  "AMI_ERROR_OUT_OF_RANGE",
59  "AMI_ERROR_READ_ONLY",
60  "AMI_ERROR_OS_ERROR",
61  "AMI_ERROR_MM_ERROR",
62  "AMI_ERROR_OBJECT_INITIALIZATION",
63  "AMI_ERROR_PERMISSION_DENIED",
64  "AMI_ERROR_INSUFFICIENT_MAIN_MEMORY",
65  "AMI_ERROR_INSUFFICIENT_AVAILABLE_STREAMS",
66  "AMI_ERROR_ENV_UNDEFINED",
67  "AMI_ERROR_NO_MAIN_MEMORY_OPERATION",
68 };
69 
70 /**********************************************************************/
71 /* creates a random file name, opens the file for reading and writing
72  and and returns a file descriptor */
73 int
74 ami_single_temp_name(const std::string& base, char* tmp_path) {
75 
76  char *base_dir;
77  int fd;
78 
79  // get the dir
80  base_dir = getenv(STREAM_TMPDIR);
81  if(!base_dir) {
82  fprintf(stderr, "ami_stream: %s not set\n", STREAM_TMPDIR);
83  assert(base_dir);
84  exit(1);
85  }
86  sprintf(tmp_path, "%s/%s_XXXXXX", base_dir, base.c_str());
87 
88  fd = G_mkstemp(tmp_path, O_RDWR, 0600);
89 
90  if (fd == -1) {
91  cerr << "ami_single_temp_name: ";
92  perror("G_mkstemp() failed: ");
93  assert(0);
94  exit(1);
95  }
96  return fd;
97 }
98 
99 
100 /**********************************************************************/
101 /* given fd=fide descriptor, associates with it a stream aopened in
102  access_mode and returns it */
103 FILE*
105  FILE* fp = NULL;
106 
107  assert(fd > -1);
108  switch (st) {
109  case AMI_READ_STREAM:
110  fp = fdopen(fd, "rb");
111  break;
112  case AMI_WRITE_STREAM:
113  fp = fdopen(fd, "wb");
114  break;
116  fp = fdopen(fd, "ab");
117  break;
118  case AMI_APPEND_STREAM:
119  fp = fdopen(fd, "ab+");
120  break;
121  case AMI_READ_WRITE_STREAM:
122  fp = fdopen(fd, "rb+");
123  if (!fp) {
124  //if file does not exist, create it
125  fp = fdopen(fd, "wb+");
126  }
127  break;
128  }
129  if(!fp) {
130  perror("fdopen");
131  }
132  assert(fp);
133 
134  return fp;
135 }
136 
137 
138 /**********************************************************************/
139 /* open the file whose name is pathname in access mode */
140 FILE*
141 open_stream(char* pathname, AMI_stream_type st) {
142 
143  FILE* fp = NULL;
144  assert(pathname);
145 
146  switch (st) {
147  case AMI_READ_STREAM:
148  fp = fopen(pathname, "rb");
149  break;
150  case AMI_WRITE_STREAM:
151  fp = fopen(pathname, "wb");
152  break;
154  fp = fopen(pathname, "ab");
155  break;
156  case AMI_APPEND_STREAM:
157  fp = fopen(pathname, "ab+");
158  assert(fp);
159  G_fseek (fp, 0, SEEK_END);
160  break;
161  case AMI_READ_WRITE_STREAM:
162  fp = fopen(pathname, "rb+");
163  if (!fp) {
164  //if file does not exist, create it
165  fp = fopen(pathname, "wb+");
166  }
167  break;
168  }
169  if (!fp) {
170  perror(pathname);
171  assert(0);
172  exit(1);
173  }
174  assert(fp);
175  return fp;
176 }
177 
const char * ami_str_error[]
Definition: ami_stream.cpp:54
#define STREAM_TMPDIR
Definition: ami_stream.h:75
#define NULL
Definition: ccmath.h:32
AMI_stream_type
Definition: ami_stream.h:107
struct state * st
Definition: parser.c:104
FILE * open_stream(int fd, AMI_stream_type st)
Definition: ami_stream.cpp:104
#define assert(condition)
Definition: lz4.c:324
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
Definition: gis/seek.c:50
int ami_single_temp_name(const std::string &base, char *tmp_path)
Definition: ami_stream.cpp:74
int G_mkstemp(char *, int, int)
Returns a file descriptor.
Definition: mkstemp.c:127
char * getenv()