| OLD | NEW |
| (Empty) |
| 1 // Copyright 2005-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 // Parse command-line options | |
| 17 // | |
| 18 // Class CommandParsing supports two kinds of command line options: | |
| 19 // 1) Traditional option, like "-b -v 100" | |
| 20 // 2) Name-value-pairs, like "b=&v=100" | |
| 21 | |
| 22 #ifndef OMAHA_COMMON_COMMANDS_H_ | |
| 23 #define OMAHA_COMMON_COMMANDS_H_ | |
| 24 | |
| 25 #include <atlstr.h> | |
| 26 #include <vector> | |
| 27 #include "base/basictypes.h" | |
| 28 | |
| 29 namespace omaha { | |
| 30 | |
| 31 enum ThreeValue { | |
| 32 VALUE_NOT_SET = 0, | |
| 33 TRUE_VALUE = 1, | |
| 34 FALSE_VALUE = 2 | |
| 35 }; | |
| 36 | |
| 37 enum CommandOptionType { | |
| 38 COMMAND_OPTION_BOOL = 0x1, | |
| 39 COMMAND_OPTION_INT = 0x2, | |
| 40 COMMAND_OPTION_UINT = 0x3, | |
| 41 COMMAND_OPTION_STRING = 0x4, | |
| 42 COMMAND_OPTION_THREE = 0x5, | |
| 43 COMMAND_OPTION_UNESCAPE = 0x1000, | |
| 44 COMMAND_OPTION_MULTIPLE = 0x2000 | |
| 45 }; | |
| 46 | |
| 47 #define COMMAND_OPTION_FLAGS_MASK 0x0FFF | |
| 48 | |
| 49 struct CommandOption { | |
| 50 void Init(const TCHAR* name, CommandOptionType type, | |
| 51 void* value, int max_value_len); | |
| 52 void Copy(const CommandOption& option); | |
| 53 | |
| 54 CString name; | |
| 55 CommandOptionType type; | |
| 56 void* value; | |
| 57 int max_value_len; | |
| 58 }; | |
| 59 | |
| 60 class CommandParsingSimple { | |
| 61 public: | |
| 62 // Static Helper function that splits a command line | |
| 63 // string into executable and any arguments | |
| 64 static HRESULT SplitExeAndArgs(const TCHAR* cmd_line, | |
| 65 CString* exe, | |
| 66 CString* args); | |
| 67 | |
| 68 // Static Helper function that splits a command line | |
| 69 // string into executable and any arguments. Tries to | |
| 70 // guess the positioning of the EXE argument in cases | |
| 71 // where the EXE argument has spaces and is not enclosed | |
| 72 // in quotes. For instance, earlier versions of Google Desktop | |
| 73 // used to have an "Uninstall" string of the form: | |
| 74 // C:\Program Files\Google\Google Toolbar\GoogleToolbarSetup.exe -uninstall | |
| 75 // This function is meant to accomodate such cases. | |
| 76 static HRESULT SplitExeAndArgsGuess(const TCHAR* cmd_line, | |
| 77 CString* exe, | |
| 78 CString* args); | |
| 79 | |
| 80 // Static Helper function that returns the number of arguments | |
| 81 // in the passed in cmd_line | |
| 82 static HRESULT GetNumberOfArgs(const TCHAR* cmd_line, uint32* number_of_args); | |
| 83 | |
| 84 // Converted to a string | |
| 85 HRESULT ToString(CString* cmd_line); | |
| 86 | |
| 87 protected: | |
| 88 // Constructor | |
| 89 CommandParsingSimple(); | |
| 90 | |
| 91 // Constructor | |
| 92 explicit CommandParsingSimple(TCHAR separator); | |
| 93 | |
| 94 // Parse a command line string into args | |
| 95 HRESULT ParseSimple(const TCHAR* cmd_line); | |
| 96 | |
| 97 // Get the arg at specified position from the command line | |
| 98 HRESULT GetAt(uint32 position, CString* arg); | |
| 99 | |
| 100 // Remove the arg at specified position from the command line | |
| 101 HRESULT RemoveAt(uint32 position); | |
| 102 | |
| 103 TCHAR separator_; // Separator | |
| 104 std::vector<CString> args_; // Splitted args | |
| 105 | |
| 106 | |
| 107 private: | |
| 108 DISALLOW_EVIL_CONSTRUCTORS(CommandParsingSimple); | |
| 109 }; | |
| 110 | |
| 111 | |
| 112 class CommandParsing : public CommandParsingSimple { | |
| 113 public: | |
| 114 // Constructor | |
| 115 CommandParsing(CommandOption* options, int options_count); | |
| 116 | |
| 117 CommandParsing(CommandOption* options, int options_count, | |
| 118 TCHAR separator, bool as_name_value_pair); | |
| 119 | |
| 120 // Parse a command line string | |
| 121 HRESULT Parse(const TCHAR* cmd_line, bool ignore_unknown_args); | |
| 122 | |
| 123 // Parse a list of command line arguments | |
| 124 HRESULT ParseArguments(int argc, TCHAR* argv[]); | |
| 125 | |
| 126 // Remove an option from the command line | |
| 127 HRESULT Remove(const TCHAR* option_name); | |
| 128 | |
| 129 private: | |
| 130 // Internal parsing | |
| 131 HRESULT InternalParse(bool ignore_unknown_args); | |
| 132 | |
| 133 // Extract the name | |
| 134 HRESULT ExtractName(CString* name, std::vector<CString>::const_iterator* it); | |
| 135 | |
| 136 // Extract the value | |
| 137 // Also validate the value length if necessary | |
| 138 HRESULT ExtractValue(const CommandOption& option, | |
| 139 CString* value, | |
| 140 std::vector<CString>::const_iterator* it, | |
| 141 const std::vector<CString>::const_iterator& end); | |
| 142 | |
| 143 // Set the parsed value | |
| 144 template<class T> | |
| 145 static void SetParsedValue(const CommandOption& option, const T& value); | |
| 146 | |
| 147 // Helper function to find an option in the CommandOption list | |
| 148 int FindOption(const TCHAR* option_name); | |
| 149 | |
| 150 CommandOption* options_; // Command-line option list | |
| 151 int options_count_; // Count of command-line options | |
| 152 bool as_name_value_pair_; // Parse as name-value-pair | |
| 153 | |
| 154 DISALLOW_EVIL_CONSTRUCTORS(CommandParsing); | |
| 155 }; | |
| 156 | |
| 157 } // namespace omaha | |
| 158 | |
| 159 #endif // OMAHA_COMMON_COMMANDS_H_ | |
| OLD | NEW |