Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gr-radar / src / lib / time_series.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "time_series.h"
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <iostream>
34
35 #ifndef O_LARGEFILE
36 #define O_LARGEFILE 0
37 #endif
38
39
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)
45 {
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;
49   }
50
51   struct stat statbuf;
52   if (fstat(d_fd, &statbuf) == -1){
53     perror(d_filename.c_str());
54     throw std::string("fstat failed: ") + d_filename;
55   }
56   d_filesize = statbuf.st_size;
57   d_limit = d_filesize / d_itemsize;
58
59   if (d_start > d_limit){
60     std::string s = std::string("d_start > filesize: ") + d_filename;
61     std::cerr << s
62               << " d_start " << d_start
63               << " d_limit " << d_limit << std::endl;
64     throw s;
65   }
66
67   if (nsamples_to_process > 0)
68     if ((d_start + nsamples_to_process) < d_limit)
69       d_limit = d_start + nsamples_to_process;
70
71   d_buffer = mmap(0, d_filesize, PROT_READ, MAP_SHARED, d_fd, 0);
72   if (d_buffer == MAP_FAILED){
73     perror("mmap");
74     throw std::string("mmap failed: ") + d_filename;
75   }
76 }
77
78 time_series::~time_series()
79 {
80   munmap(d_buffer, d_filesize);
81   close(d_fd);
82 }
83
84 const void *
85 time_series::seek(unsigned long long pos, unsigned long long blocksize) const
86 {
87   if ((d_start + pos + blocksize) >= d_limit)
88     return 0;
89
90   return (const void *)((char *)d_buffer + ((d_start + pos) * d_itemsize));
91 }
92
93 long long
94 time_series::nsamples_available(unsigned long long pos) const
95 {
96   if ((d_start + pos) >= d_limit)
97     return 0;
98
99   return d_limit - (d_start + pos);
100 }