3 * Copyright 2005 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio 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, or (at your option)
12 * GNU Radio 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 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include "time_series.h"
29 #include <sys/types.h>
40 time_series::time_series(size_t itemsize, const std::string filename,
41 unsigned long long starting_offset,
42 long long nsamples_to_process)
43 : d_itemsize(itemsize), d_filename(filename), d_fd(-1),
44 d_start(starting_offset), d_buffer(0)
46 if ((d_fd = open(d_filename.c_str(), O_RDONLY | O_LARGEFILE, 0660)) == -1){
47 perror(d_filename.c_str());
48 throw std::string("open failed: ") + d_filename;
52 if (fstat(d_fd, &statbuf) == -1){
53 perror(d_filename.c_str());
54 throw std::string("fstat failed: ") + d_filename;
56 d_filesize = statbuf.st_size;
57 d_limit = d_filesize / d_itemsize;
59 if (d_start > d_limit){
60 std::string s = std::string("d_start > filesize: ") + d_filename;
62 << " d_start " << d_start
63 << " d_limit " << d_limit << std::endl;
67 if (nsamples_to_process > 0)
68 if ((d_start + nsamples_to_process) < d_limit)
69 d_limit = d_start + nsamples_to_process;
71 d_buffer = mmap(0, d_filesize, PROT_READ, MAP_SHARED, d_fd, 0);
72 if (d_buffer == MAP_FAILED){
74 throw std::string("mmap failed: ") + d_filename;
78 time_series::~time_series()
80 munmap(d_buffer, d_filesize);
85 time_series::seek(unsigned long long pos, unsigned long long blocksize) const
87 if ((d_start + pos + blocksize) >= d_limit)
90 return (const void *)((char *)d_buffer + ((d_start + pos) * d_itemsize));
94 time_series::nsamples_available(unsigned long long pos) const
96 if ((d_start + pos) >= d_limit)
99 return d_limit - (d_start + pos);