1 /* ac_outfile_base.cpp - Declaration of the output file base class, not to be
2  * instantiated, only as a pointer
3  * Copyright (C) 2021 Jeanette C. <jeanette@juliencoder.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 3
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19 
20 #include "ac_outfile_base.hpp"
21 #include <boost/filesystem.hpp>
22 
23 using std::string;
24 using boost::filesystem::path;
25 using boost::filesystem::exists;
26 
27 namespace JBS {
Ac_outfile_base(const string & filename)28 	Ac_outfile_base::Ac_outfile_base(const string& filename):
29 		its_filename(filename), its_error_msg(""), its_good(true), \
30 		its_open(false), its_samplerate(44100), its_item(0), its_size(0), \
31 		its_data(nullptr)
32 	{
33 		its_good = is_valid_path();
34 	}
35 
Ac_outfile_base(const char * filename)36 	Ac_outfile_base::Ac_outfile_base(const char *filename):
37 		Ac_outfile_base(string(filename))
38 	{}
39 
~Ac_outfile_base()40 	Ac_outfile_base::~Ac_outfile_base()
41 	{
42 		if (its_data != nullptr)
43 		{
44 			delete [] its_data;
45 		}
46 	}
47 
48 	// Get the data to be written, only copy input data, if no prior data
49 	// has been copied. Only return true if data was copied.
set_input(double * data,unsigned long int size)50 	bool Ac_outfile_base::set_input(double * data, unsigned long int size)
51 	{
52 		bool action_done = false;
53 		if (its_data == nullptr)
54 		{
55 			its_data = new double[size];
56 			its_size = size;
57 
58 			// Copy data
59 			for (unsigned long int item = 0;item<its_size;item++)
60 			{
61 				its_data[item] = data[item];
62 			}
63 			action_done = true; // data was copied
64 		}
65 		return action_done;
66 	}
67 
68 	// Check if no existing file will be overwritten
is_valid_path()69 	bool Ac_outfile_base::is_valid_path()
70 	{
71 		bool is_valid = false; // state to be returned
72 		path my_path(its_filename); // Create a Boost filesystem path object
73 		if (exists(my_path) == false)
74 		{
75 			is_valid = true;
76 		}
77 		else
78 		{
79 			its_good = false;
80 			its_error_msg = its_filename + string(" already exists and will not be overwritten.");
81 		}
82 
83 		return is_valid;
84 	}
85 } // End of namespace JBS
86