Audacity 3.2.0
CommandBuilder.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity - A Digital Audio Editor
4 Copyright 1999-2009 Audacity Team
5 File License: wxWidgets
6
7 Dan Horgan
8
9******************************************************************//*******************************************************************/
23
24
25#include "CommandBuilder.h"
26
27#include "CommandDirectory.h"
28#include "Command.h"
29#include "CommandContext.h"
30#include "CommandTargets.h"
31#include "../Shuttle.h"
32
34 AudacityProject &project, const wxString &cmdString)
35 : mValid(false)
36{
37 BuildCommand(project, cmdString);
38}
39
41 const wxString &cmdName, const wxString &params)
42 : mValid(false)
43{
44 BuildCommand(project, cmdName, params);
45}
46
48{
49}
50
52{
53 return mValid;
54}
55
57{
58 wxASSERT(mValid);
59 wxASSERT(mCommand);
60 auto result = mCommand;
61 mCommand.reset();
62 return result;
63}
64
66{
67 if (!mValid && !mError.empty()) {
68 return mError + wxT("\n");
69 }
70 return mResponse->GetResponse() + wxT("\n");
71}
72
73void CommandBuilder::Failure(const wxString &msg)
74{
75 mError = msg;
76 mValid = false;
77}
78
80{
81 mCommand = cmd;
82 mValid = true;
83}
84
86 const wxString &cmdName,
87 const wxString &cmdParamsArg)
88{
89 // Stage 1: create a Command object of the right type
90
91 mResponse = std::make_shared< ResponseTarget >();
92 auto output
93 = std::make_unique<CommandOutputTargets>(std::make_unique<NullProgressTarget>(),
95 mResponse);
96
97#ifdef OLD_BATCH_SYSTEM
99
100 if (factory == NULL)
101 {
102 // Fall back to hoping the Batch Command system can handle it
103#endif
104 OldStyleCommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand"));
105 wxASSERT(type != NULL);
106 mCommand = type->Create(project, nullptr);
107 mCommand->SetParameter(wxT("CommandName"), cmdName);
108 mCommand->SetParameter(wxT("ParamString"), cmdParamsArg);
109 auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output);
110 Success(aCommand);
111 return;
112#ifdef OLD_BATCH_SYSTEM
113 }
114
115 CommandSignature &signature = factory->GetSignature();
116 mCommand = factory->Create(nullptr);
117 //mCommand->SetOutput( std::move(output) );
118 // Stage 2: set the parameters
119
120 ShuttleCli shuttle;
121 shuttle.mParams = cmdParamsArg;
122 shuttle.mbStoreInClient = true;
123
124 ParamValueMap::const_iterator iter;
125 ParamValueMap params = signature.GetDefaults();
126
127 // Iterate through the parameters defined by the command
128 for (iter = params.begin(); iter != params.end(); ++iter)
129 {
130 wxString paramString;
131 // IF there is a match in the args actually used
132 if (shuttle.TransferString(iter->first, paramString, wxT("")))
133 {
134 // Then set that parameter.
135 if (!mCommand->SetParameter(iter->first, paramString))
136 {
137 Failure();
138 return;
139 }
140 }
141 }
142
143 // Check for unrecognised parameters
144
145 wxString cmdParams(cmdParamsArg);
146
147 while (!cmdParams.empty())
148 {
149 cmdParams.Trim(true);
150 cmdParams.Trim(false);
151 int splitAt = cmdParams.Find(wxT('='));
152 if (splitAt < 0 && !cmdParams.empty())
153 {
154 Failure(wxT("Parameter string is missing '='"));
155 return;
156 }
157 wxString paramName = cmdParams.Left(splitAt);
158 if (params.find(paramName) == params.end())
159 {
160 Failure(wxT("Unrecognized parameter: '") + paramName + wxT("'"));
161 return;
162 }
163 // Handling of quoted strings is quite limited.
164 // You start and end with a " or a '.
165 // There is no escaping in the string.
166 cmdParams = cmdParams.Mid(splitAt+1);
167 if( cmdParams.empty() )
168 splitAt =-1;
169 else if( cmdParams[0] == '\"' ){
170 cmdParams = cmdParams.Mid(1);
171 splitAt = cmdParams.Find(wxT('\"'))+1;
172 }
173 else if( cmdParams[0] == '\'' ){
174 cmdParams = cmdParams.Mid(1);
175 splitAt = cmdParams.Find(wxT('\''))+1;
176 }
177 else
178 splitAt = cmdParams.Find(wxT(' '))+1;
179 if (splitAt < 1)
180 {
181 splitAt = cmdParams.length();
182 }
183 cmdParams = cmdParams.Mid(splitAt);
184 }
185 auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output);
186 Success(aCommand);
187#endif
188}
189
191 AudacityProject &project, const wxString &cmdStringArg)
192{
193 wxString cmdString(cmdStringArg);
194
195 // Find the command name terminator... If there is more than one word and
196 // no terminator, the command is badly formed
197 cmdString.Trim(true); cmdString.Trim(false);
198 int splitAt = cmdString.Find(wxT(':'));
199 if (splitAt < 0 && cmdString.Find(wxT(' ')) >= 0) {
200 Failure(wxT("Syntax error!\nCommand is missing ':'"));
201 return;
202 }
203
204 wxString cmdName = cmdString.Left(splitAt);
205 wxString cmdParams = cmdString.Mid(splitAt+1);
206 if( splitAt < 0 )
207 cmdParams = "";
208
209 cmdName.Trim(true);
210 cmdParams.Trim(false);
211
212 BuildCommand(project, cmdName, cmdParams);
213}
wxT("CloseDown"))
Contains declaration of Command base class.
Contains declaration of CommandBuilder class.
Contains declarations for CommandDirectory class.
std::map< wxString, wxVariant > ParamValueMap
Definition: CommandMisc.h:23
EffectDistortion::Params params
Definition: Distortion.cpp:83
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:89
void Failure(const wxString &msg={})
void Success(const OldStyleCommandPointer &cmd)
wxString GetResponse()
void BuildCommand(AudacityProject &project, const wxString &cmdName, const wxString &cmdParams)
ResponseTargetPointer mResponse
OldStyleCommandPointer GetCommand()
CommandBuilder(AudacityProject &project, const wxString &cmdString)
OldStyleCommandPointer mCommand
OldStyleCommandType * LookUp(const wxString &cmdName) const
static CommandDirectory * Get()
Get a pointer to the singleton instance.
Class that maps parameter names to default values and validators.
ParamValueMap GetDefaults() const
OldStyleCommandPointer is a unique_ptr to an OldStyleCommand.
Base class for containing data common to all commands of a given type. Also acts as a factory.
Definition: CommandType.h:45
virtual OldStyleCommandPointer Create(AudacityProject &project, std::unique_ptr< CommandOutputTargets > &&target)=0
Derived from Shuttle, this class exchanges string parameters with a binary representation.
Definition: Shuttle.h:97
wxString mParams
Definition: Shuttle.h:99
virtual bool TransferString(const wxString &Name, wxString &strValue, const wxString &strDefault)
Definition: Shuttle.cpp:219
bool mbStoreInClient
Definition: Shuttle.h:77
static RegisteredToolbarFactory factory