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