OLD | NEW |
| (Empty) |
1 // Copyright 2008-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 | |
17 #ifndef OMAHA_BASE_COMMAND_LINE_VALIDATOR_H__ | |
18 #define OMAHA_BASE_COMMAND_LINE_VALIDATOR_H__ | |
19 | |
20 #include <windows.h> | |
21 #include <atlstr.h> | |
22 #include <map> | |
23 #include <vector> | |
24 #include "base/basictypes.h" | |
25 | |
26 namespace omaha { | |
27 | |
28 class CommandLineParser; | |
29 | |
30 // This class allows creation of scenarios for command line combinations and | |
31 // then provides a mechanism to validate a command line against those scenarios | |
32 // to determine if there's a match. | |
33 class CommandLineValidator { | |
34 public: | |
35 class ScenarioParameter { | |
36 public: | |
37 ScenarioParameter(const TCHAR* switch_name, int num_required_parameters) | |
38 : switch_name_(switch_name), | |
39 num_required_parameters_(num_required_parameters) { | |
40 } | |
41 ~ScenarioParameter() {} | |
42 | |
43 CString switch_name_; | |
44 int num_required_parameters_; | |
45 | |
46 private: | |
47 DISALLOW_EVIL_CONSTRUCTORS(ScenarioParameter); | |
48 }; | |
49 | |
50 typedef std::vector<ScenarioParameter*> ScenarioParameterVector; | |
51 typedef ScenarioParameterVector::iterator ScenarioParameterVectorIter; | |
52 typedef ScenarioParameterVector::const_iterator | |
53 ScenarioParameterVectorConstIter; | |
54 | |
55 struct ScenarioParameters { | |
56 public: | |
57 ScenarioParameterVector required; | |
58 ScenarioParameterVector optional; | |
59 }; | |
60 | |
61 typedef std::map<CString, ScenarioParameters> MapScenarios; | |
62 typedef MapScenarios::iterator MapScenariosIter; | |
63 typedef MapScenarios::const_iterator MapScenariosConstIter; | |
64 | |
65 CommandLineValidator(); | |
66 ~CommandLineValidator(); | |
67 | |
68 void Clear(); | |
69 | |
70 // Parses a command line rule and builds a scenario from it. Returns a | |
71 // generated scenario name. | |
72 // Rules have required and optional parameters. An example of a rule is: | |
73 // "gu.exe /install <extraargs> [/oem [/appargs <appargs> [/silent" | |
74 HRESULT CreateScenarioFromCmdLine(const CString& command_line, | |
75 CString* scenario_name); | |
76 | |
77 // Validates a CommandLineParser against all scenarios. If a match, returns | |
78 // S_OK and the scenario_name. Fails if not a match. | |
79 // command_line_parser must already be compiled before calling. | |
80 HRESULT Validate(const CommandLineParser& command_line_parser, | |
81 CString* scenario_name) const; | |
82 | |
83 // Creates a scenario by name. | |
84 HRESULT CreateScenario(const CString& scenario_name); | |
85 | |
86 // Adds a switch and its parameter count to an existing scenario. | |
87 HRESULT AddScenarioParameter(const CString& scenario_name, | |
88 const CString& switch_name, | |
89 int num_required_parameters); | |
90 HRESULT AddOptionalScenarioParameter(const CString& scenario_name, | |
91 const CString& switch_name, | |
92 int num_required_parameters); | |
93 | |
94 private: | |
95 bool DoesScenarioMatch(const CommandLineParser& command_line_parser, | |
96 const ScenarioParameters& scenario_parameters) const; | |
97 | |
98 int scenario_sequence_number_; | |
99 MapScenarios scenarios_; | |
100 DISALLOW_EVIL_CONSTRUCTORS(CommandLineValidator); | |
101 }; | |
102 | |
103 } // namespace omaha | |
104 | |
105 #endif // OMAHA_BASE_COMMAND_LINE_VALIDATOR_H__ | |
106 | |
OLD | NEW |