Audacity  3.0.3
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"
53 
54 
66 class CommandParameters final : public wxFileConfig
67 {
68 public:
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  }
83 
84  virtual bool HasGroup(const wxString & strName) const override
85  {
86  return wxFileConfig::HasGroup(NormalizeName(strName));
87  }
88 
89  virtual bool HasEntry(const wxString& strName) const override
90  {
91  return wxFileConfig::HasEntry(NormalizeName(strName));
92  }
93 
94  virtual bool DoReadString(const wxString & key, wxString *pStr) const override
95  {
96  return wxFileConfig::DoReadString(NormalizeName(key), pStr);
97  }
98 
99  virtual bool DoReadLong(const wxString & key, long *pl) const override
100  {
101  return wxFileConfig::DoReadLong(NormalizeName(key), pl);
102  }
103 
104  virtual bool DoReadDouble(const wxString & key, double *pd) const override
105  {
106  wxString str;
107  if (Read(key, &str))
108  {
109  struct lconv *info = localeconv();
110  wxString dec = info ? wxString::FromUTF8(info->decimal_point) : wxT(".");
111 
112  str.Replace(wxT(","), dec);
113  str.Replace(wxT("."), dec);
114 
115  return str.ToDouble(pd);
116  }
117 
118  return false;
119  }
120 
121  virtual bool DoWriteString(const wxString & key, const wxString & szValue) override
122  {
123  return wxFileConfig::DoWriteString(NormalizeName(key), szValue);
124  }
125 
126  virtual bool DoWriteLong(const wxString & key, long lValue) override
127  {
128  return wxFileConfig::DoWriteLong(NormalizeName(key), lValue);
129  }
130 
131  virtual bool DoWriteDouble(const wxString & key, double value) override
132  {
133  return DoWriteString(key, wxString::Format(wxT("%.8g"), value));
134  }
135 
136  bool ReadFloat(const wxString & key, float *pf) const
137  {
138  double d = *pf;
139  bool success = Read(key, &d);
140  if (success)
141  {
142  *pf = (float) d;
143  }
144  return success;
145  }
146 
147  bool ReadFloat(const wxString & key, float *pf, float defVal) const
148  {
149  if (!ReadFloat(key, pf))
150  {
151  *pf = defVal;
152  }
153  return true;
154  }
155 
156  bool WriteFloat(const wxString & key, float f)
157  {
158  return Write(key, f);
159  }
160 
161  // For reading old config files with enumeration names that have been
162  // changed in later versions. Pair a string with an index into the other
163  // list of non-obsolete names.
164  using ObsoleteMap = std::pair< wxString, size_t >;
165 
166  bool ReadEnum(const wxString & key, int *pi,
167  const EnumValueSymbol choices[], size_t nChoices,
168  const ObsoleteMap obsoletes[] = nullptr,
169  size_t nObsoletes = 0) const
170  {
171  wxString s;
172  if (!wxFileConfig::Read(key, &s))
173  {
174  return false;
175  }
176  *pi = std::find( choices, choices + nChoices,
177  EnumValueSymbol{ s, {} } ) - choices;
178  if (*pi == (int)nChoices)
179  *pi = -1;
180  if (*pi < 0 && obsoletes) {
181  auto index = std::find_if(obsoletes, obsoletes + nObsoletes,
182  [&](const ObsoleteMap &entry){
183  return entry.first == s; })
184  - obsoletes;
185  if (index < (int)nObsoletes)
186  *pi = (int)obsoletes[index].second;
187  }
188  return true;
189  }
190 
191  bool ReadEnum(const wxString & key, int *pi, int defVal,
192  const EnumValueSymbol choices[], size_t nChoices,
193  const ObsoleteMap obsoletes[] = nullptr,
194  size_t nObsoletes = 0) const
195  {
196  if (!ReadEnum(key, pi, choices, nChoices, obsoletes, nObsoletes))
197  {
198  *pi = defVal;
199  }
200  return true;
201  }
202 
203  bool WriteEnum(const wxString & key, int value,
204  const EnumValueSymbol choices[], size_t nChoices)
205  {
206  if (value < 0 || value >= (int)nChoices)
207  {
208  return false;
209  }
210 
211  return wxFileConfig::Write(key, choices[value].Internal());
212  }
213 
214  bool ReadAndVerify(const wxString & key, float *val, float defVal, float min, float max) const
215  {
216  ReadFloat(key, val, defVal);
217  return (*val >= min && *val <= max);
218  }
219 
220  bool ReadAndVerify(const wxString & key, double *val, double defVal, double min, double max) const
221  {
222  Read(key, val, defVal);
223  return (*val >= min && *val <= max);
224  }
225 
226  bool ReadAndVerify(const wxString & key, int *val, int defVal, int min, int max) const
227  {
228  Read(key, val, defVal);
229  return (*val >= min && *val <= max);
230  }
231 
232  bool ReadAndVerify(const wxString & key, long *val, long defVal, long min, long max) const
233  {
234  Read(key, val, defVal);
235  return (*val >= min && *val <= max);
236  }
237 
238  bool ReadAndVerify(const wxString & key, bool *val, bool defVal) const
239  {
240  Read(key, val, defVal);
241  return true;
242  }
243 
244  bool ReadAndVerify(const wxString & key, wxString *val, const wxString & defVal) 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  wxString NormalizeName(const wxString & name) const
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
CommandParameters::Escape
wxString Escape(wxString val)
Definition: EffectAutomationParameters.h:321
CommandParameters
CommandParameters, derived from wxFileConfig, is essentially doing the same things as the Shuttle cla...
Definition: EffectAutomationParameters.h:67
CommandParameters::SetParameters
bool SetParameters(const wxString &parms)
Definition: EffectAutomationParameters.h:287
CommandParameters::ReadFloat
bool ReadFloat(const wxString &key, float *pf, float defVal) const
Definition: EffectAutomationParameters.h:147
CommandParameters::ReadEnum
bool ReadEnum(const wxString &key, int *pi, int defVal, const EnumValueSymbol choices[], size_t nChoices, const ObsoleteMap obsoletes[]=nullptr, size_t nObsoletes=0) const
Definition: EffectAutomationParameters.h:191
CommandParameters::WriteEnum
bool WriteEnum(const wxString &key, int value, const EnumValueSymbol choices[], size_t nChoices)
Definition: EffectAutomationParameters.h:203
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, float *val, float defVal, float min, float max) const
Definition: EffectAutomationParameters.h:214
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, long *val, long defVal, long min, long max) const
Definition: EffectAutomationParameters.h:232
CommandParameters::ReadFloat
bool ReadFloat(const wxString &key, float *pf) const
Definition: EffectAutomationParameters.h:136
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, int *val, int defVal, int min, int max) const
Definition: EffectAutomationParameters.h:226
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, bool *val, bool defVal) const
Definition: EffectAutomationParameters.h:238
CommandParameters::GetParameters
bool GetParameters(wxString &parms)
Definition: EffectAutomationParameters.h:259
CommandParameters::HasGroup
virtual bool HasGroup(const wxString &strName) const override
Definition: EffectAutomationParameters.h:84
CommandParameters::WriteFloat
bool WriteFloat(const wxString &key, float f)
Definition: EffectAutomationParameters.h:156
CommandParameters::DoReadString
virtual bool DoReadString(const wxString &key, wxString *pStr) const override
Definition: EffectAutomationParameters.h:94
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterface.h:60
CommandParameters::ReadEnum
bool ReadEnum(const wxString &key, int *pi, const EnumValueSymbol choices[], size_t nChoices, const ObsoleteMap obsoletes[]=nullptr, size_t nObsoletes=0) const
Definition: EffectAutomationParameters.h:166
ComponentInterface.h
CommandParameters::DoWriteLong
virtual bool DoWriteLong(const wxString &key, long lValue) override
Definition: EffectAutomationParameters.h:126
CommandParameters::ObsoleteMap
std::pair< wxString, size_t > ObsoleteMap
Definition: EffectAutomationParameters.h:164
name
const TranslatableString name
Definition: Distortion.cpp:98
CommandParameters::~CommandParameters
virtual ~CommandParameters()
Definition: EffectAutomationParameters.h:80
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, wxString *val, const wxString &defVal) const
Definition: EffectAutomationParameters.h:244
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, int *val, int defVal, const EnumValueSymbol choices[], size_t nChoices, const ObsoleteMap obsoletes[]=nullptr, size_t nObsoletes=0) const
Definition: EffectAutomationParameters.h:250
min
int min(int a, int b)
Definition: CompareAudioCommand.cpp:106
CommandParameters::CommandParameters
CommandParameters(const wxString &parms={})
Definition: EffectAutomationParameters.h:69
Read
gPrefs Read(wxT("/GUI/VerticalZooming"), &bVZoom, false)
key
static const AudacityProject::AttachedObjects::RegisteredFactory key
Definition: CommandManager.cpp:196
CommandParameters::HasEntry
virtual bool HasEntry(const wxString &strName) const override
Definition: EffectAutomationParameters.h:89
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, double *val, double defVal, double min, double max) const
Definition: EffectAutomationParameters.h:220
CommandParameters::DoReadDouble
virtual bool DoReadDouble(const wxString &key, double *pd) const override
Definition: EffectAutomationParameters.h:104
CommandParameters::NormalizeName
wxString NormalizeName(const wxString &name) const
Definition: EffectAutomationParameters.h:307
CommandParameters::Unescape
wxString Unescape(wxString val)
Definition: EffectAutomationParameters.h:330
CommandParameters::DoWriteDouble
virtual bool DoWriteDouble(const wxString &key, double value) override
Definition: EffectAutomationParameters.h:131
CommandParameters::DoWriteString
virtual bool DoWriteString(const wxString &key, const wxString &szValue) override
Definition: EffectAutomationParameters.h:121
CommandParameters::DoReadLong
virtual bool DoReadLong(const wxString &key, long *pl) const override
Definition: EffectAutomationParameters.h:99