GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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>
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 */
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;
113  fp = fdopen(fd, "ab");
114  break;
115  case AMI_APPEND_STREAM:
116  fp = fdopen(fd, "ab+");
117  break;
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;
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;
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 }
const char * ami_str_error[]
Definition: ami_stream.cpp:52
int ami_single_temp_name(const std::string &base, char *tmp_path)
Definition: ami_stream.cpp:71
FILE * open_stream(int fd, AMI_stream_type st)
Definition: ami_stream.cpp:100
AMI_stream_type
Definition: ami_stream.h:104
@ AMI_APPEND_STREAM
Definition: ami_stream.h:107
@ AMI_WRITE_STREAM
Definition: ami_stream.h:106
@ AMI_READ_STREAM
Definition: ami_stream.h:105
@ AMI_READ_WRITE_STREAM
Definition: ami_stream.h:108
@ AMI_APPEND_WRITE_STREAM
Definition: ami_stream.h:109
#define STREAM_TMPDIR
Definition: ami_stream.h:73
#define NULL
Definition: ccmath.h:32
int G_mkstemp(char *, int, int)
Returns a file descriptor.
Definition: mkstemp.c:128
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
Definition: gis/seek.c:50
#define GPATH_MAX
Definition: gis.h:194
#define assert(condition)
Definition: lz4.c:291
struct state * st
Definition: parser.c:104