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