| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/tools/simple_command_line_parser.h" | 11 #include "webrtc/tools/simple_command_line_parser.h" |
| 12 | 12 |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 #include <stdlib.h> | 14 #include <stdlib.h> |
| 15 | 15 |
| 16 #include <string> | 16 #include <string> |
| 17 | 17 |
| 18 namespace webrtc { | 18 namespace webrtc { |
| 19 namespace test { | 19 namespace test { |
| 20 | 20 |
| 21 using std::string; | |
| 22 | |
| 23 CommandLineParser::CommandLineParser() {} | 21 CommandLineParser::CommandLineParser() {} |
| 24 CommandLineParser::~CommandLineParser() {} | 22 CommandLineParser::~CommandLineParser() {} |
| 25 | 23 |
| 26 void CommandLineParser::Init(int argc, char** argv) { | 24 void CommandLineParser::Init(int argc, char** argv) { |
| 27 args_ = std::vector<std::string> (argv + 1, argv + argc); | 25 args_ = std::vector<std::string> (argv + 1, argv + argc); |
| 28 } | 26 } |
| 29 | 27 |
| 30 bool CommandLineParser::IsStandaloneFlag(std::string flag) { | 28 bool CommandLineParser::IsStandaloneFlag(std::string flag) { |
| 31 return flag.find("=") == string::npos; | 29 return flag.find("=") == std::string::npos; |
| 32 } | 30 } |
| 33 | 31 |
| 34 bool CommandLineParser::IsFlagWellFormed(std::string flag) { | 32 bool CommandLineParser::IsFlagWellFormed(std::string flag) { |
| 35 size_t dash_pos = flag.find("--"); | 33 size_t dash_pos = flag.find("--"); |
| 36 size_t equal_pos = flag.find("="); | 34 size_t equal_pos = flag.find("="); |
| 37 if (dash_pos != 0) { | 35 if (dash_pos != 0) { |
| 38 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str()); | 36 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str()); |
| 39 fprintf(stderr, "Flag doesn't start with --\n"); | 37 fprintf(stderr, "Flag doesn't start with --\n"); |
| 40 return false; | 38 return false; |
| 41 } | 39 } |
| 42 size_t flag_length = flag.length() - 1; | 40 size_t flag_length = flag.length() - 1; |
| 43 | 41 |
| 44 // We use 3 here because we assume that the flags are in the format | 42 // We use 3 here because we assume that the flags are in the format |
| 45 // --flag_name=flag_value, thus -- are at positions 0 and 1 and we should have | 43 // --flag_name=flag_value, thus -- are at positions 0 and 1 and we should have |
| 46 // at least one symbol for the flag name. | 44 // at least one symbol for the flag name. |
| 47 if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) { | 45 if (equal_pos > 0 && (equal_pos < 3 || equal_pos == flag_length)) { |
| 48 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str()); | 46 fprintf(stderr, "Wrong switch format: %s\n", flag.c_str()); |
| 49 fprintf(stderr, "Wrong placement of =\n"); | 47 fprintf(stderr, "Wrong placement of =\n"); |
| 50 return false; | 48 return false; |
| 51 } | 49 } |
| 52 return true; | 50 return true; |
| 53 } | 51 } |
| 54 | 52 |
| 55 std::string CommandLineParser::GetCommandLineFlagName(std::string flag) { | 53 std::string CommandLineParser::GetCommandLineFlagName(std::string flag) { |
| 56 size_t dash_pos = flag.find("--"); | 54 size_t dash_pos = flag.find("--"); |
| 57 size_t equal_pos = flag.find("="); | 55 size_t equal_pos = flag.find("="); |
| 58 if (equal_pos == string::npos) { | 56 if (equal_pos == std::string::npos) { |
| 59 return flag.substr(dash_pos + 2); | 57 return flag.substr(dash_pos + 2); |
| 60 } else { | 58 } else { |
| 61 return flag.substr(dash_pos + 2, equal_pos - 2); | 59 return flag.substr(dash_pos + 2, equal_pos - 2); |
| 62 } | 60 } |
| 63 } | 61 } |
| 64 | 62 |
| 65 std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) { | 63 std::string CommandLineParser::GetCommandLineFlagValue(std::string flag) { |
| 66 size_t equal_pos = flag.find("="); | 64 size_t equal_pos = flag.find("="); |
| 67 if (equal_pos == string::npos) { | 65 if (equal_pos == std::string::npos) { |
| 68 return ""; | 66 return ""; |
| 69 } else { | 67 } else { |
| 70 return flag.substr(equal_pos + 1); | 68 return flag.substr(equal_pos + 1); |
| 71 } | 69 } |
| 72 } | 70 } |
| 73 | 71 |
| 74 void CommandLineParser::PrintEnteredFlags() { | 72 void CommandLineParser::PrintEnteredFlags() { |
| 75 std::map<std::string, std::string>::iterator flag_iter; | 73 std::map<std::string, std::string>::iterator flag_iter; |
| 76 fprintf(stdout, "You have entered:\n"); | 74 fprintf(stdout, "You have entered:\n"); |
| 77 for (flag_iter = flags_.begin(); flag_iter != flags_.end(); ++flag_iter) { | 75 for (flag_iter = flags_.begin(); flag_iter != flags_.end(); ++flag_iter) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 flag_iter = flags_.find(flag_name); | 122 flag_iter = flags_.find(flag_name); |
| 125 // If no such flag. | 123 // If no such flag. |
| 126 if (flag_iter == flags_.end()) { | 124 if (flag_iter == flags_.end()) { |
| 127 return ""; | 125 return ""; |
| 128 } | 126 } |
| 129 return flag_iter->second; | 127 return flag_iter->second; |
| 130 } | 128 } |
| 131 | 129 |
| 132 } // namespace test | 130 } // namespace test |
| 133 } // namespace webrtc | 131 } // namespace webrtc |
| OLD | NEW |