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"
54 
66 class COMPONENTS_API 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 
80  virtual ~CommandParameters();
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) const
238  {
239  Read(key, val, defVal);
240  return true;
241  }
242 
243  bool ReadAndVerify(const wxString & key, wxString *val, const wxString & defVal) const
244  {
245  Read(key, val, defVal);
246  return true;
247  }
248 
249  bool ReadAndVerify(const wxString & key, int *val, int defVal,
250  const EnumValueSymbol choices[], size_t nChoices,
251  const ObsoleteMap obsoletes[] = nullptr,
252  size_t nObsoletes = 0) const
253  {
254  ReadEnum(key, val, defVal, choices, nChoices, obsoletes, nObsoletes);
255  return (*val != wxNOT_FOUND);
256  }
257 
258  bool GetParameters(wxString & parms)
259  {
260  wxFileConfig::SetPath(wxT("/"));
261 
262  wxString str;
263  wxString key;
264 
265  long ndx = 0;
266  bool res = wxFileConfig::GetFirstEntry(key, ndx);
267  while (res)
268  {
269  wxString val;
270  if (!wxFileConfig::Read(key, &val))
271  {
272  return false;
273  }
274 
275  str += key + wxT("=\"") + Escape(val) + wxT("\" ");
276 
277  res = wxFileConfig::GetNextEntry(key, ndx);
278  }
279  str.Trim();
280 
281  parms = str;
282 
283  return true;
284  }
285 
286  bool SetParameters(const wxString & parms)
287  {
288  wxFileConfig::SetPath(wxT("/"));
289 
290  auto parsed = wxCmdLineParser::ConvertStringToArgs(parms);
291 
292  for (size_t i = 0, cnt = parsed.size(); i < cnt; i++)
293  {
294  wxString key = parsed[i].BeforeFirst(wxT('=')).Trim(false).Trim(true);
295  wxString val = parsed[i].AfterFirst(wxT('=')).Trim(false).Trim(true);
296 
297  if (!wxFileConfig::Write(key, Unescape(val)))
298  {
299  return false;
300  }
301  }
302 
303  return true;
304  }
305 
306  wxString NormalizeName(const wxString & name) const
307  {
308  wxString cleaned = name;
309 
310  cleaned.Trim(true).Trim(false);
311  cleaned.Replace(wxT(" "), wxT("_"));
312  cleaned.Replace(wxT("/"), wxT("_"));
313  cleaned.Replace(wxT("\\"), wxT("_"));
314  cleaned.Replace(wxT(":"), wxT("_"));
315  cleaned.Replace(wxT("="), wxT("_"));
316 
317  return cleaned;
318  }
319 
320  wxString Escape(wxString val)
321  {
322  val.Replace(wxT("\\"), wxT("\\\\"), true);
323  val.Replace(wxT("\""), wxT("\\\""), true);
324  val.Replace(wxT("\n"), wxT("\\n"), true);
325 
326  return val;
327  }
328 
329  wxString Unescape(wxString val)
330  {
331  val.Replace(wxT("\\n"), wxT("\n"), true);
332  val.Replace(wxT("\\\""), wxT("\""), true);
333  val.Replace(wxT("\\\\"), wxT("\\"), true);
334 
335  return val;
336  }
337 };
338 
339 #endif
CommandParameters::Escape
wxString Escape(wxString val)
Definition: EffectAutomationParameters.h:320
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:286
CommandParameters::ReadFloat
bool ReadFloat(const wxString &key, float *pf, float defVal) const
Definition: EffectAutomationParameters.h:146
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:190
CommandParameters::WriteEnum
bool WriteEnum(const wxString &key, int value, const EnumValueSymbol choices[], size_t nChoices)
Definition: EffectAutomationParameters.h:202
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, float *val, float defVal, float min, float max) const
Definition: EffectAutomationParameters.h:213
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, long *val, long defVal, long min, long max) const
Definition: EffectAutomationParameters.h:231
CommandParameters::ReadFloat
bool ReadFloat(const wxString &key, float *pf) const
Definition: EffectAutomationParameters.h:135
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, int *val, int defVal, int min, int max) const
Definition: EffectAutomationParameters.h:225
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, bool *val, bool defVal) const
Definition: EffectAutomationParameters.h:237
CommandParameters::GetParameters
bool GetParameters(wxString &parms)
Definition: EffectAutomationParameters.h:258
ComponentInterfaceSymbol.h
CommandParameters::HasGroup
virtual bool HasGroup(const wxString &strName) const override
Definition: EffectAutomationParameters.h:82
CommandParameters::WriteFloat
bool WriteFloat(const wxString &key, float f)
Definition: EffectAutomationParameters.h:155
CommandParameters::DoReadString
virtual bool DoReadString(const wxString &key, wxString *pStr) const override
Definition: EffectAutomationParameters.h:92
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
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:165
ComponentInterface.h
CommandParameters::DoWriteLong
virtual bool DoWriteLong(const wxString &key, long lValue) override
Definition: EffectAutomationParameters.h:125
CommandParameters::ObsoleteMap
std::pair< wxString, size_t > ObsoleteMap
Definition: EffectAutomationParameters.h:163
name
const TranslatableString name
Definition: Distortion.cpp:98
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, wxString *val, const wxString &defVal) const
Definition: EffectAutomationParameters.h:243
CommandParameters::~CommandParameters
virtual ~CommandParameters()
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:249
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:197
CommandParameters::HasEntry
virtual bool HasEntry(const wxString &strName) const override
Definition: EffectAutomationParameters.h:87
CommandParameters::ReadAndVerify
bool ReadAndVerify(const wxString &key, double *val, double defVal, double min, double max) const
Definition: EffectAutomationParameters.h:219
CommandParameters::DoReadDouble
virtual bool DoReadDouble(const wxString &key, double *pd) const override
Definition: EffectAutomationParameters.h:102
CommandParameters::NormalizeName
wxString NormalizeName(const wxString &name) const
Definition: EffectAutomationParameters.h:306
ExceptionType::Internal
@ Internal
Indicates internal failure from Audacity.
CommandParameters::Unescape
wxString Unescape(wxString val)
Definition: EffectAutomationParameters.h:329
CommandParameters::DoWriteDouble
virtual bool DoWriteDouble(const wxString &key, double value) override
Definition: EffectAutomationParameters.h:130
CommandParameters::DoWriteString
virtual bool DoWriteString(const wxString &key, const wxString &szValue) override
Definition: EffectAutomationParameters.h:120
CommandParameters::DoReadLong
virtual bool DoReadLong(const wxString &key, long *pl) const override
Definition: EffectAutomationParameters.h:97