-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfio.c
252 lines (186 loc) · 5.3 KB
/
bfio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*------------------------------------------------------------------------*/
/* Copyright (c) 2005 - 2010 Armin Biere, Johannes Kepler University. */
/*------------------------------------------------------------------------*/
#include "config.h"
/*------------------------------------------------------------------------*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
/*------------------------------------------------------------------------*/
FILE * fopen64 (const char *, const char *);
/*------------------------------------------------------------------------*/
#include "bfio.h"
#include "bfmem.h"
#include "bfstack.h"
/*------------------------------------------------------------------------*/
#define GUNZIP_FMT "gunzip -c %s"
#define GZIP_FMT "gzip -c > %s"
/*------------------------------------------------------------------------*/
typedef struct InternalFile InternalFile;
/*------------------------------------------------------------------------*/
struct InternalFile
{
char *name;
int pos;
FILE *file;
int zipped;
};
/*------------------------------------------------------------------------*/
DeclarePtrStack (InternalFile);
/*------------------------------------------------------------------------*/
static InternalFilePtrStack files;
/*------------------------------------------------------------------------*/
static int
has_gz_suffix (const char *name)
{
int len;
assert (name);
len = strlen (name);
if (len < 3)
return 0;
if (name[len - 3] != '.')
return 0;
if (name[len - 2] != 'g')
return 0;
if (name[len - 1] != 'z')
return 0;
return 1;
}
/*------------------------------------------------------------------------*/
static InternalFile *
find_file (FILE * file)
{
InternalFile *internal_file;
int i;
for (i = 0; i < count_stack (files); i++)
{
internal_file = files.start[i];
assert (internal_file);
if (internal_file->file == file)
return internal_file;
}
return 0;
}
/*------------------------------------------------------------------------*/
static void
new_file (const char *name, FILE * file)
{
InternalFile *res;
assert (!find_file (file));
BOOLEFORCE_NEW (res);
res->name = booleforce_strdup (name);
res->file = file;
res->zipped = has_gz_suffix (name);
res->pos = count_stack (files);
push_stack (files, res);
assert (files.start[res->pos] == res);
}
/*------------------------------------------------------------------------*/
static void
delete_file (InternalFile * internal_file)
{
InternalFile *last;
assert (internal_file);
assert (files.start[internal_file->pos] == internal_file);
last = pop_stack (files);
if (last != internal_file)
{
assert (0 <= internal_file->pos);
assert (0 < count_stack (files));
files.start[internal_file->pos] = last;
last->pos = internal_file->pos;
}
if (internal_file->zipped)
pclose (internal_file->file);
else
fclose (internal_file->file);
booleforce_delstr (internal_file->name);
BOOLEFORCE_DELETE (internal_file);
if (empty_stack (files))
release_stack (files);
}
/*------------------------------------------------------------------------*/
static FILE *
popen_gzipped_file_for_reading (const char *file_name)
{
size_t bytes;
char *cmd;
FILE *res;
bytes = strlen (GUNZIP_FMT) - 2 + strlen (file_name) + 1;
cmd = booleforce_new (bytes);
sprintf (cmd, GUNZIP_FMT, file_name);
res = popen (cmd, "r");
booleforce_delete (cmd, bytes);
return res;
}
/*------------------------------------------------------------------------*/
FILE *
booleforce_open_file_for_reading (const char *name)
{
FILE *res;
if (has_gz_suffix (name)) {
res = popen_gzipped_file_for_reading (name);
} else {
#ifndef FOPEN64_FOPEN
res = fopen64 (name, "r");
#else
res = fopen(name, "r");
#endif
}
if (res)
new_file (name, res);
return res;
}
/*------------------------------------------------------------------------*/
static FILE *
popen_gzipped_file_for_writing (const char * file_name)
{
size_t bytes;
char *cmd;
FILE *res;
bytes = strlen (GZIP_FMT) - 2 + strlen (file_name) + 1;
cmd = booleforce_new (bytes);
sprintf (cmd, GZIP_FMT, file_name);
res = popen (cmd, "w");
booleforce_delete (cmd, bytes);
return res;
}
/*------------------------------------------------------------------------*/
FILE *
booleforce_open_file_for_writing (const char * name)
{
FILE * res;
if (has_gz_suffix (name))
res = popen_gzipped_file_for_writing (name);
else
res = fopen (name, "w");
if (res)
new_file (name, res);
return res;
}
/*------------------------------------------------------------------------*/
void
booleforce_close_file (FILE * file)
{
InternalFile *internal_file;
assert (file);
internal_file = find_file (file);
assert (internal_file);
delete_file (internal_file);
}
/*------------------------------------------------------------------------*/
int
booleforce_fill_buffer (BooleforceInputBuffer * buffer)
{
size_t bytes;
assert (buffer->next == buffer->end);
if (feof (buffer->file)) /* necessary for <stdin> */
bytes = 0;
else
bytes = fread (buffer->start, 1, BOOLEFORCE_BUFFER_SIZE, buffer->file);
if (!bytes)
return EOF;
buffer->end = buffer->start + bytes;
buffer->next = buffer->start;
return *buffer->next++;
}