Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-core / src / lib / io / gr_file_sink_base.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006,2007 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 3, 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 <gr_file_sink_base.h>
28 #include <cstdio>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdexcept>
33
34 // win32 (mingw/msvc) specific
35 #ifdef HAVE_IO_H
36 #include <io.h>
37 #endif
38 #ifdef O_BINARY
39 #define OUR_O_BINARY O_BINARY
40 #else
41 #define OUR_O_BINARY 0
42 #endif
43
44 // should be handled via configure
45 #ifdef O_LARGEFILE
46 #define OUR_O_LARGEFILE O_LARGEFILE
47 #else
48 #define OUR_O_LARGEFILE 0
49 #endif
50
51 gr_file_sink_base::gr_file_sink_base(const char *filename, bool is_binary)
52   : d_fp(0), d_new_fp(0), d_updated(false), d_is_binary(is_binary)
53 {
54   if (!open(filename))
55     throw std::runtime_error ("can't open file");
56 }
57
58 gr_file_sink_base::~gr_file_sink_base ()
59 {
60   close();
61   if (d_fp){
62     fclose(d_fp);
63     d_fp = 0;
64   }
65 }
66
67 bool
68 gr_file_sink_base::open(const char *filename)
69 {
70   omni_mutex_lock       l(d_mutex);     // hold mutex for duration of this function
71
72   // we use the open system call to get access to the O_LARGEFILE flag.
73   int fd;
74   if ((fd = ::open (filename,
75                     O_WRONLY|O_CREAT|O_TRUNC|OUR_O_LARGEFILE|OUR_O_BINARY,
76                     0664)) < 0){
77     perror (filename);
78     return false;
79   }
80
81   if (d_new_fp){                // if we've already got a new one open, close it
82     fclose(d_new_fp);
83     d_new_fp = 0;
84   }
85
86   if ((d_new_fp = fdopen (fd, d_is_binary ? "wb" : "w")) == NULL){
87     perror (filename);
88     ::close(fd);                // don't leak file descriptor if fdopen fails.
89   }
90
91   d_updated = true;
92   return d_new_fp != 0;
93 }
94
95 void
96 gr_file_sink_base::close()
97 {
98   omni_mutex_lock       l(d_mutex);     // hold mutex for duration of this function
99
100   if (d_new_fp){
101     fclose(d_new_fp);
102     d_new_fp = 0;
103   }
104   d_updated = true;
105 }
106
107 void
108 gr_file_sink_base::do_update()
109 {
110   if (d_updated){
111     omni_mutex_lock     l(d_mutex);     // hold mutex for duration of this block
112     if (d_fp)
113       fclose(d_fp);
114     d_fp = d_new_fp;                    // install new file pointer
115     d_new_fp = 0;
116     d_updated = false;
117   }
118 }