summaryrefslogtreecommitdiff
path: root/libs/audiographer/audiographer/sndfile/tmp_file.h
blob: d19241a21e8565e55847c4cac6371e9f697756e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef AUDIOGRAPHER_TMP_FILE_H
#define AUDIOGRAPHER_TMP_FILE_H

#include <cstdio>
#include <string>

#include <glib.h>
#include <pbd/gstdio_compat.h>

#include "sndfile_writer.h"
#include "sndfile_reader.h"

namespace AudioGrapher
{

/// A temporary file deleted after this class is destructed
template<typename T = DefaultSampleType>
class TmpFile : public SndfileWriter<T>, public SndfileReader<T>
{
  public:

	/// \a filename_template must match the requirements for mkstemp, i.e. end in "XXXXXX"
	TmpFile (char * filename_template, int format, ChannelCount channels, framecnt_t samplerate)
		: SndfileHandle (g_mkstemp(filename_template), true, SndfileBase::ReadWrite, format, channels, samplerate)
		, filename (filename_template)
	{}

	TmpFile (int format, ChannelCount channels, framecnt_t samplerate)
	  : SndfileHandle (fileno (tmpfile()), true, SndfileBase::ReadWrite, format, channels, samplerate)
	{}

	TmpFile (TmpFile const & other) : SndfileHandle (other) {}
	using SndfileHandle::operator=;

	~TmpFile()
	{
		/* explicitly close first, some OS (yes I'm looking at you windows)
		 * cannot delet files that are still open
		 */
		if (!filename.empty()) {
			SndfileBase::close();
			std::remove(filename.c_str());
		}
	}

  private:
	std::string filename;
};

} // namespace

#endif // AUDIOGRAPHER_TMP_FILE_H