1 /* abGate - LV2 Noise Gate Plugin
2  *
3  * Copyright 2011 Antanas Bružas
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 3 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 /* DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY DIRTY */
21 
22 #include "presets.h"
23 
presets()24 presets::presets() {
25 }
26 
~presets()27 presets::~presets() {
28 }
29 
get_names_xml(string dir)30 vector<string> presets::get_names_xml(string dir) {
31 
32 	ifstream presetsfile(dir.c_str());
33 
34 	if (presetsfile.is_open()) {
35 		while (getline(presetsfile, line)) {
36 			found_preset = line.rfind("\">");
37 			if (found_preset != string::npos) {
38 				names_vector.push_back(line.substr(30, line.length() - 32));
39 			}
40 		}
41 		presetsfile.close();
42 	} else {
43 		cerr << "Unable to open file";
44 	}
45 
46 	return names_vector;
47 }
48 
get_xml(string dir)49 list<preset> presets::get_xml(string dir) {
50 
51 	line_nr = 0;
52 	preset_nr = 0;
53 
54 	ifstream presetsfile(dir.c_str());
55 
56 	if (presetsfile.is_open()) {
57 		while (getline(presetsfile, line)) {
58 			found_preset = line.rfind("\">");
59 			found_param = line.rfind("\" />");
60 
61 			if (found_preset != string::npos) {
62 				line_nr = 0;
63 				preset_name = line.substr(30, line.length() - 32);
64 				preset_nr++;
65 			} else if (found_param != string::npos) {
66 				line_value = line.find("value");
67 				pos = (int) line_value;
68 
69 				float_string = new char[line.substr(pos + 7, line.length() - pos - 11).length() + 1];
70 				strcpy(float_string, line.substr(pos + 7, line.length() - pos - 11).c_str());
71 				sscanf(float_string, "%f", &param_value[line_nr]);
72 
73 				line_nr++;
74 
75 				if (line_nr == 6) {
76 					tmp_preset = new preset();
77 					tmp_preset->construct(preset_name, param_value);
78 					presets_list.push_back(*tmp_preset);
79 				}
80 			}
81 		}
82 		presetsfile.close();
83 	} else {
84 		cerr << "Unable to open file";
85 	}
86 
87 	return presets_list;
88 }
89 
get_one_xml(string name,string dir)90 preset presets::get_one_xml(string name, string dir) {
91 
92 	ifstream presetsfile(dir.c_str());
93 	tmp_preset = new preset();
94 	if (presetsfile.is_open()) {
95 		while (getline(presetsfile, line)) {
96 			found_preset = line.rfind("\"" + name + "\"");
97 			if (found_preset != string::npos) {
98 
99 				for (int i = 0; i < 6; i++) {
100 					getline(presetsfile, line);
101 					line_value = line.find("value");
102 					pos = (int) line_value;
103 
104 					float_string = new char[line.substr(pos + 7, line.length() - pos - 11).length() + 1];
105 					strcpy(float_string, line.substr(pos + 7, line.length() - pos - 11).c_str());
106 					sscanf(float_string, "%f", &param_value[i]);
107 
108 					if (i == 5) {
109 						tmp_preset->construct(name, param_value);
110 						return *tmp_preset;
111 					}
112 				}
113 			}
114 		}
115 		presetsfile.close();
116 
117 	} else {
118 		cerr << "Unable to open file";
119 	}
120 	return *tmp_preset;
121 }
122 
set_xml(preset new_preset,bool clr,string dir)123 bool presets::set_xml(preset new_preset, bool clr, string dir) {
124 
125 	ofstream presetsfile;
126 
127 	if (clr == true) {
128 		presetsfile.open(dir.c_str());
129 	} else {
130 		presetsfile.open(dir.c_str(), ios::app);
131 	}
132 
133 	if (presetsfile.is_open()) {
134 
135 		presetsfile << "<preset plugin=\"abgate\" name=\"" << new_preset.name << "\">\n";
136 		presetsfile << "\t<param name=\"switch\" value=\"" << new_preset.param_value[0] << "\" />\n";
137 		presetsfile << "\t<param name=\"threshold\" value=\"" << new_preset.param_value[1] << "\" />\n";
138 		presetsfile << "\t<param name=\"attack\" value=\"" << new_preset.param_value[2] << "\" />\n";
139 		presetsfile << "\t<param name=\"hold\" value=\"" << new_preset.param_value[3] << "\" />\n";
140 		presetsfile << "\t<param name=\"decay\" value=\"" << new_preset.param_value[4] << "\" />\n";
141 		presetsfile << "\t<param name=\"gaterange\" value=\"" << new_preset.param_value[5] << "\" />\n";
142 		presetsfile << "</preset>\n";
143 
144 		presetsfile.close();
145 
146 	} else {
147 		cerr << "Unable to open file";
148 		return false;
149 	}
150 
151 	return true;
152 }
153 
154 /* ==================== Examples ==================== */
155 
156 /*
157  // isprintina visus names
158 
159  presets *test = new presets();
160  vector<string>names_vector = test->get_names_xml();
161 
162  for (int i = 0; i < names_vector.size(); i++) {
163  cout << names_vector[i] << endl;
164  }
165 
166  */
167 // isprintina visa xml
168 /*
169  presets *test = new presets();
170  list<preset>preset_list = test->get_xml();
171  list<preset>::iterator it;
172 
173  for (it = preset_list.begin(); it != preset_list.end(); it++) {
174  cout << it->name << endl;
175  cout << it->param_value[0] << endl;
176  cout << it->param_value[1] << endl;
177  cout << it->param_value[2] << endl;
178  cout << it->param_value[3] << endl;
179  cout << it->param_value[4] << endl;
180  cout << it->param_value[5] << endl;
181 
182  }
183  */
184 
185 // senas su vektoriais
186 /*presets *test = new presets();
187  vector<preset>preset_vector = test->get_xml();
188  for (int i = 0; i < preset_vector.size(); i++) {
189  cout << preset_vector[i].name << endl;
190  cout << preset_vector[i].param_value[0] << endl;
191  cout << preset_vector[i].param_value[1] << endl;
192  cout << preset_vector[i].param_value[2] << endl;
193  cout << preset_vector[i].param_value[3] << endl;
194  cout << preset_vector[i].param_value[4] << endl;
195  cout << preset_vector[i].param_value[5] << endl;
196  }*/
197 
198 // iterpia nauja preseta, priklausomai nuo true false parametro įterps arba į galą arba viską ištrins ir įterps
199 /*
200  presets *testas = new presets();
201  preset *presetas666 = new preset();
202  float parametrai[6] = { 1, -5, 250, 200, 600, -30 };
203  presetas666->construct("zjbisa", parametrai);
204  testas->set_xml(*presetas666, false);
205  */
206 
207 // išprintina vieną presetą pagal name
208 /*presets *test = new presets();
209  preset presetas1 = test->get_one_xml("zjbis ne");
210 
211  cout << presetas1.name << endl;
212  cout << presetas1.param_value[0] << endl;
213  cout << presetas1.param_value[1] << endl;
214  cout << presetas1.param_value[2] << endl;
215  cout << presetas1.param_value[3] << endl;
216  cout << presetas1.param_value[4] << endl;
217  cout << presetas1.param_value[5] << endl;
218 
219  */
220 
221 /*
222  // paima visa xml, ištrina vieną presetą pagal name ir updeitina visa xml
223 
224  presets *test = new presets();
225  list<preset>preset_list = test->get_xml();
226 
227  list<preset>::iterator it;
228 
229  for (it = preset_list.begin(); it != preset_list.end(); it++) {
230  if (it->name == "Defaulta") { preset_list.erase(it); break; }
231  }
232 
233  // opening and after closing the file to clear the content of it
234  ofstream presetsfile("presets.xml");
235  presetsfile.close();
236 
237  preset *presetas = new preset();
238  for (it = preset_list.begin(); it != preset_list.end(); it++) {
239  float parametrai[6] = { it->param_value[0], it->param_value[1], it->param_value[2], it->param_value[3], it->param_value[4], it->param_value[5] };
240  presetas->construct(it->name, parametrai);
241  test->set_xml(*presetas, false);
242  }
243  */
244 
245