Audacity 3.2.0
EffectAutomationParameters.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 EffectAutomationParameters.h
6 (defining CommandParameters)
7
8 Leland Lucius
9
10 Copyright (c) 2014, Audacity Team
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 3. Neither the name of the copyright holder nor the names of its
25 contributors may be used to endorse or promote products derived from
26 this software without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 POSSIBILITY OF SUCH DAMAGE.
40
41**********************************************************************/
42
43#ifndef __AUDACITY_COMMAND_PARAMETERS_H__
44#define __AUDACITY_COMMAND_PARAMETERS_H__
45
46#include <locale.h>
47
48#include <wx/cmdline.h> // for wxCmdLineParser::ConvertStringToArgs
49#include <wx/fileconf.h> // to inherit
50#include <algorithm>
51
52#include "ComponentInterface.h"
54
66class COMPONENTS_API CommandParameters final : public wxFileConfig
67{
68public:
69 CommandParameters(const wxString & parms = {})
70 : wxFileConfig(wxEmptyString,
71 wxEmptyString,
72 wxEmptyString,
73 wxEmptyString,
74 0)
75 {
76 SetExpandEnvVars(false);
77 SetParameters(parms);
78 }
79
81
82 virtual bool HasGroup(const wxString & strName) const override
83 {
84 return wxFileConfig::HasGroup(NormalizeName(strName));
85 }
86
87 virtual bool HasEntry(const wxString& strName) const override
88 {
89 return wxFileConfig::HasEntry(NormalizeName(strName));
90 }
91
92 virtual bool DoReadString(const wxString & key, wxString *pStr) const override
93 {
94 return wxFileConfig::DoReadString(NormalizeName(key), pStr);
95 }
96
97 virtual bool DoReadLong(const wxString & key, long *pl) const override
98 {
99 return wxFileConfig::DoReadLong(NormalizeName(key), pl);
100 }
101
102 virtual bool DoReadDouble(const wxString & key, double *pd) const override
103 {
104 wxString str;
105 if (Read(key, &str))
106 {
107 struct lconv *info = localeconv();
108 wxString dec =
109 info ? wxString::FromUTF8(info->decimal_point) : wxString(".");
110
111 str.Replace(wxT(","), dec);
112 str.Replace(wxT("."), dec);
113
114 return str.ToDouble(pd);
115 }
116
117 return false;
118 }
119
120 virtual bool DoWriteString(const wxString & key, const wxString & szValue) override
121 {
122 return wxFileConfig::DoWriteString(NormalizeName(key), szValue);
123 }
124
125 virtual bool DoWriteLong(const wxString & key, long lValue) override
126 {
127 return wxFileConfig::DoWriteLong(NormalizeName(key), lValue);
128 }
129
130 virtual bool DoWriteDouble(const wxString & key, double value) override
131 {
132 return DoWriteString(key, wxString::Format(wxT("%.8g"), value));
133 }
134
135 bool ReadFloat(const wxString & key, float *pf) const
136 {
137 double d = *pf;
138 bool success = Read(key, &d);
139 if (success)
140 {
141 *pf = (float) d;
142 }
143 return success;
144 }
145
146 bool ReadFloat(const wxString & key, float *pf, float defVal) const
147 {
148 if (!ReadFloat(key, pf))
149 {
150 *pf = defVal;
151 }
152 return true;
153 }
154
155 bool WriteFloat(const wxString & key, float f)
156 {
157 return Write(key, f);
158 }
159
160 // For reading old config files with enumeration names that have been
161 // changed in later versions. Pair a string with an index into the other
162 // list of non-obsolete names.
163 using ObsoleteMap = std::pair< wxString, size_t >;
164
165 bool ReadEnum(const wxString & key, int *pi,
166 const EnumValueSymbol choices[], size_t nChoices,
167 const ObsoleteMap obsoletes[] = nullptr,
168 size_t nObsoletes = 0) const
169 {
170 wxString s;
171 if (!wxFileConfig::Read(key, &s))
172 {
173 return false;
174 }
175 *pi = std::find( choices, choices + nChoices,
176 EnumValueSymbol{ s, {} } ) - choices;
177 if (*pi == (int)nChoices)
178 *pi = -1;
179 if (*pi < 0 && obsoletes) {
180 auto index = std::find_if(obsoletes, obsoletes + nObsoletes,
181 [&](const ObsoleteMap &entry){
182 return entry.first == s; })
183 - obsoletes;
184 if (index < (int)nObsoletes)
185 *pi = (int)obsoletes[index].second;
186 }
187 return true;
188 }
189
190 bool ReadEnum(const wxString & key, int *pi, int defVal,
191 const EnumValueSymbol choices[], size_t nChoices,
192 const ObsoleteMap obsoletes[] = nullptr,
193 size_t nObsoletes = 0) const
194 {
195 if (!ReadEnum(key, pi, choices, nChoices, obsoletes, nObsoletes))
196 {
197 *pi = defVal;
198 }
199 return true;
200 }
201
202 bool WriteEnum(const wxString & key, int value,
203 const EnumValueSymbol choices[], size_t nChoices)
204 {
205 if (value < 0 || value >= (int)nChoices)
206 {
207 return false;
208 }
209
210 return wxFileConfig::Write(key, choices[value].Internal());
211 }
212
213 bool ReadAndVerify(const wxString & key, float *val, float defVal, float min, float max) const
214 {
215 ReadFloat(key, val, defVal);
216 return (*val >= min && *val <= max);
217 }
218
219 bool ReadAndVerify(const wxString & key, double *val, double defVal, double min, double max) const
220 {
221 Read(key, val, defVal);
222 return (*val >= min && *val <= max);
223 }
224
225 bool ReadAndVerify(const wxString & key, int *val, int defVal, int min, int max) const
226 {
227 Read(key, val, defVal);
228 return (*val >= min && *val <= max);
229 }
230
231 bool ReadAndVerify(const wxString & key, long *val, long defVal, long min, long max) const
232 {
233 Read(key, val, defVal);
234 return (*val >= min && *val <= max);
235 }
236
237 bool ReadAndVerify(const wxString & key, bool *val, bool defVal, bool = false, bool = false) const
238 {
239 Read(key, val, defVal);
240 return true;
241 }
242
243 bool ReadAndVerify(const wxString & key, wxString *val, const wxString & defVal,
244 const wxString& = {}, const wxString& = {} ) const
245 {
246 Read(key, val, defVal);
247 return true;
248 }
249
250 bool ReadAndVerify(const wxString & key, int *val, int defVal,
251 const EnumValueSymbol choices[], size_t nChoices,
252 const ObsoleteMap obsoletes[] = nullptr,
253 size_t nObsoletes = 0) const
254 {
255 ReadEnum(key, val, defVal, choices, nChoices, obsoletes, nObsoletes);
256 return (*val != wxNOT_FOUND);
257 }
258
259 bool GetParameters(wxString & parms)
260 {
261 wxFileConfig::SetPath(wxT("/"));
262
263 wxString str;
264 wxString key;
265
266 long ndx = 0;
267 bool res = wxFileConfig::GetFirstEntry(key, ndx);
268 while (res)
269 {
270 wxString val;
271 if (!wxFileConfig::Read(key, &val))
272 {
273 return false;
274 }
275
276 str += key + wxT("=\"") + Escape(val) + wxT("\" ");
277
278 res = wxFileConfig::GetNextEntry(key, ndx);
279 }
280 str.Trim();
281
282 parms = str;
283
284 return true;
285 }
286
287 bool SetParameters(const wxString & parms)
288 {
289 wxFileConfig::SetPath(wxT("/"));
290
291 auto parsed = wxCmdLineParser::ConvertStringToArgs(parms);
292
293 for (size_t i = 0, cnt = parsed.size(); i < cnt; i++)
294 {
295 wxString key = parsed[i].BeforeFirst(wxT('=')).Trim(false).Trim(true);
296 wxString val = parsed[i].AfterFirst(wxT('=')).Trim(false).Trim(true);
297
298 if (!wxFileConfig::Write(key, Unescape(val)))
299 {
300 return false;
301 }
302 }
303
304 return true;
305 }
306
307 static wxString NormalizeName(const wxString & name)
308 {
309 wxString cleaned = name;
310
311 cleaned.Trim(true).Trim(false);
312 cleaned.Replace(wxT(" "), wxT("_"));
313 cleaned.Replace(wxT("/"), wxT("_"));
314 cleaned.Replace(wxT("\\"), wxT("_"));
315 cleaned.Replace(wxT(":"), wxT("_"));
316 cleaned.Replace(wxT("="), wxT("_"));
317
318 return cleaned;
319 }
320
321 wxString Escape(wxString val)
322 {
323 val.Replace(wxT("\\"), wxT("\\\\"), true);
324 val.Replace(wxT("\""), wxT("\\\""), true);
325 val.Replace(wxT("\n"), wxT("\\n"), true);
326
327 return val;
328 }
329
330 wxString Unescape(wxString val)
331 {
332 val.Replace(wxT("\\n"), wxT("\n"), true);
333 val.Replace(wxT("\\\""), wxT("\""), true);
334 val.Replace(wxT("\\\\"), wxT("\\"), true);
335
336 return val;
337 }
338};
339
340#endif
wxT("CloseDown"))
@ Internal
Indicates internal failure from Audacity.
int min(int a, int b)
#define str(a)
static ProjectFileIORegistry::AttributeWriterEntry entry
static const AudacityProject::AttachedObjects::RegisteredFactory key
wxString name
Definition: TagsEditor.cpp:166
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the SettingsVis...
virtual bool DoReadDouble(const wxString &key, double *pd) const override
bool ReadAndVerify(const wxString &key, float *val, float defVal, float min, float max) const
virtual bool HasEntry(const wxString &strName) const override
virtual bool DoReadLong(const wxString &key, long *pl) const override
virtual bool DoWriteLong(const wxString &key, long lValue) override
CommandParameters(const wxString &parms={})
static wxString NormalizeName(const wxString &name)
bool WriteEnum(const wxString &key, int value, const EnumValueSymbol choices[], size_t nChoices)
virtual bool DoWriteDouble(const wxString &key, double value) override
bool GetParameters(wxString &parms)
bool ReadEnum(const wxString &key, int *pi, const EnumValueSymbol choices[], size_t nChoices, const ObsoleteMap obsoletes[]=nullptr, size_t nObsoletes=0) const
bool ReadEnum(const wxString &key, int *pi, int defVal, const EnumValueSymbol choices[], size_t nChoices, const ObsoleteMap obsoletes[]=nullptr, size_t nObsoletes=0) const
virtual ~CommandParameters()
std::pair< wxString, size_t > ObsoleteMap
bool ReadAndVerify(const wxString &key, wxString *val, const wxString &defVal, const wxString &={}, const wxString &={}) const
bool ReadAndVerify(const wxString &key, int *val, int defVal, const EnumValueSymbol choices[], size_t nChoices, const ObsoleteMap obsoletes[]=nullptr, size_t nObsoletes=0) const
virtual bool DoWriteString(const wxString &key, const wxString &szValue) override
bool WriteFloat(const wxString &key, float f)
bool ReadAndVerify(const wxString &key, double *val, double defVal, double min, double max) const
wxString Escape(wxString val)
wxString Unescape(wxString val)
bool SetParameters(const wxString &parms)
bool ReadAndVerify(const wxString &key, long *val, long defVal, long min, long max) const
virtual bool DoReadString(const wxString &key, wxString *pStr) const override
bool ReadAndVerify(const wxString &key, bool *val, bool defVal, bool=false, bool=false) const
bool ReadFloat(const wxString &key, float *pf, float defVal) const
bool ReadAndVerify(const wxString &key, int *val, int defVal, int min, int max) const
virtual bool HasGroup(const wxString &strName) const override
bool ReadFloat(const wxString &key, float *pf) const
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...