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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* -*- c++ -*- */
/*
* Copyright 2012, 2018 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "file_source_impl.h"
#include <gnuradio/io_signature.h>
#include <gnuradio/thread/thread.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstdio>
#include <stdexcept>
#ifdef _MSC_VER
#define GR_FSEEK _fseeki64
#define GR_FTELL _ftelli64
#define GR_FSTAT _fstat
#define GR_FILENO _fileno
#define GR_STAT _stat
#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
#else
#define GR_FSEEK fseeko
#define GR_FTELL ftello
#define GR_FSTAT fstat
#define GR_FILENO fileno
#define GR_STAT stat
#endif
namespace gr {
namespace blocks {
file_source::sptr file_source::make(size_t itemsize,
const char* filename,
bool repeat,
uint64_t start_offset_items,
uint64_t length_items)
{
return gnuradio::get_initial_sptr(new file_source_impl(
itemsize, filename, repeat, start_offset_items, length_items));
}
file_source_impl::file_source_impl(size_t itemsize,
const char* filename,
bool repeat,
uint64_t start_offset_items,
uint64_t length_items)
: sync_block(
"file_source", io_signature::make(0, 0, 0), io_signature::make(1, 1, itemsize)),
d_itemsize(itemsize),
d_start_offset_items(start_offset_items),
d_length_items(length_items),
d_fp(0),
d_new_fp(0),
d_repeat(repeat),
d_updated(false),
d_file_begin(true),
d_repeat_cnt(0),
d_add_begin_tag(pmt::PMT_NIL)
{
open(filename, repeat, start_offset_items, length_items);
do_update();
std::stringstream str;
str << name() << unique_id();
_id = pmt::string_to_symbol(str.str());
}
file_source_impl::~file_source_impl()
{
if (d_fp)
fclose((FILE*)d_fp);
if (d_new_fp)
fclose((FILE*)d_new_fp);
}
bool file_source_impl::seek(int64_t seek_point, int whence)
{
if (d_seekable) {
seek_point += d_start_offset_items;
switch (whence) {
case SEEK_SET:
break;
case SEEK_CUR:
seek_point += (d_length_items - d_items_remaining);
break;
case SEEK_END:
seek_point = d_length_items - seek_point;
break;
default:
GR_LOG_WARN(d_logger, "bad seek mode");
return 0;
}
if ((seek_point < (int64_t)d_start_offset_items) ||
(seek_point > (int64_t)(d_start_offset_items + d_length_items - 1))) {
GR_LOG_WARN(d_logger, "bad seek point");
return 0;
}
return GR_FSEEK((FILE*)d_fp, seek_point * d_itemsize, SEEK_SET) == 0;
} else {
GR_LOG_WARN(d_logger, "file not seekable");
return 0;
}
}
void file_source_impl::open(const char* filename,
bool repeat,
uint64_t start_offset_items,
uint64_t length_items)
{
// obtain exclusive access for duration of this function
gr::thread::scoped_lock lock(fp_mutex);
if (d_new_fp) {
fclose(d_new_fp);
d_new_fp = 0;
}
if ((d_new_fp = fopen(filename, "rb")) == NULL) {
GR_LOG_ERROR(d_logger, boost::format("%s: %s") % filename % strerror(errno));
throw std::runtime_error("can't open file");
}
struct GR_STAT st;
if (GR_FSTAT(GR_FILENO(d_new_fp), &st)) {
GR_LOG_ERROR(d_logger, boost::format("%s: %s") % filename % strerror(errno));
throw std::runtime_error("can't fstat file");
}
if (S_ISREG(st.st_mode)) {
d_seekable = true;
} else {
d_seekable = false;
}
uint64_t file_size;
if (d_seekable) {
// Check to ensure the file will be consumed according to item size
if (GR_FSEEK(d_new_fp, 0, SEEK_END) == -1) {
throw std::runtime_error("can't fseek()");
}
file_size = GR_FTELL(d_new_fp);
// Make sure there will be at least one item available
if ((file_size / d_itemsize) < (start_offset_items + 1)) {
if (start_offset_items) {
GR_LOG_WARN(d_logger, "file is too small for start offset");
} else {
GR_LOG_WARN(d_logger, "file is too small");
}
fclose(d_new_fp);
throw std::runtime_error("file is too small");
}
} else {
file_size = INT64_MAX;
}
uint64_t items_available = (file_size / d_itemsize - start_offset_items);
// If length is not specified, use the remainder of the file. Check alignment at end.
if (length_items == 0) {
length_items = items_available;
if (file_size % d_itemsize) {
GR_LOG_WARN(d_logger, "file size is not a multiple of item size");
}
}
// Check specified length. Warn and use available items instead of throwing an
// exception.
if (length_items > items_available) {
length_items = items_available;
GR_LOG_WARN(d_logger, "file too short, will read fewer than requested items");
}
// Rewind to start offset
if (d_seekable) {
if (GR_FSEEK(d_new_fp, start_offset_items * d_itemsize, SEEK_SET) == -1) {
throw std::runtime_error("can't fseek()");
}
}
d_updated = true;
d_repeat = repeat;
d_start_offset_items = start_offset_items;
d_length_items = length_items;
d_items_remaining = length_items;
}
void file_source_impl::close()
{
// obtain exclusive access for duration of this function
gr::thread::scoped_lock lock(fp_mutex);
if (d_new_fp != NULL) {
fclose(d_new_fp);
d_new_fp = NULL;
}
d_updated = true;
}
void file_source_impl::do_update()
{
if (d_updated) {
gr::thread::scoped_lock lock(fp_mutex); // hold while in scope
if (d_fp)
fclose(d_fp);
d_fp = d_new_fp; // install new file pointer
d_new_fp = 0;
d_updated = false;
d_file_begin = true;
}
}
void file_source_impl::set_begin_tag(pmt::pmt_t val) { d_add_begin_tag = val; }
int file_source_impl::work(int noutput_items,
gr_vector_const_void_star& input_items,
gr_vector_void_star& output_items)
{
char* o = (char*)output_items[0];
uint64_t size = noutput_items;
do_update(); // update d_fp is reqd
if (d_fp == NULL)
throw std::runtime_error("work with file not open");
gr::thread::scoped_lock lock(fp_mutex); // hold for the rest of this function
// No items remaining - all done
if (d_items_remaining == 0)
return WORK_DONE;
while (size) {
// Add stream tag whenever the file starts again
if (d_file_begin && d_add_begin_tag != pmt::PMT_NIL) {
add_item_tag(0,
nitems_written(0) + noutput_items - size,
d_add_begin_tag,
pmt::from_long(d_repeat_cnt),
_id);
d_file_begin = false;
}
uint64_t nitems_to_read = std::min(size, d_items_remaining);
// Since the bounds of the file are known, unexpected nitems is an error
if (nitems_to_read != fread(o, d_itemsize, nitems_to_read, (FILE*)d_fp))
throw std::runtime_error("fread error");
size -= nitems_to_read;
d_items_remaining -= nitems_to_read;
o += nitems_to_read * d_itemsize;
// Ran out of items ("EOF")
if (d_items_remaining == 0) {
// Repeat: rewind and request tag
if (d_repeat && d_seekable) {
if (GR_FSEEK(d_fp, d_start_offset_items * d_itemsize, SEEK_SET) == -1) {
throw std::runtime_error("can't fseek()");
}
d_items_remaining = d_length_items;
if (d_add_begin_tag != pmt::PMT_NIL) {
d_file_begin = true;
d_repeat_cnt++;
}
}
// No repeat: return
else {
break;
}
}
}
return (noutput_items - size);
}
} /* namespace blocks */
} /* namespace gr */
|