2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "common/command_line.h"
21 #include "common/file_utils.h"
27 const char* kOptionPrefix = "--";
28 const char* kOptionValueSeparator = "=";
30 static bool IsValidOptionString(const char* argument) {
31 if (NULL != argument &&
32 strncmp(argument, kOptionPrefix, strlen(kOptionPrefix)) == 0) {
41 CommandLine* CommandLine::current_process_commandline_ = NULL;
43 CommandLine::CommandLine(int argc, char* argv[]) : argc_(argc), argv_(argv) {
44 // Append option or push each arg(not option) into arguments_
45 for (int i = 1; i < argc; ++i) {
46 if (IsValidOptionString(argv[i])) {
47 AppendOption(argv[i]);
49 arguments_.push_back(argv[i]);
53 // Parse program name and appid from argv_ or arguments_
54 program_ = std::string(argv[0]);
57 CommandLine::~CommandLine() {}
59 void CommandLine::AppendOption(const char* value) {
60 std::string option_string(value);
61 std::string option_name;
62 std::string option_value;
64 int value_separator_pos =
65 option_string.find(kOptionValueSeparator, strlen(kOptionPrefix));
66 if (value_separator_pos >= 0) {
67 int substr_len = value_separator_pos - strlen(kOptionPrefix);
68 option_name = option_string.substr(strlen(kOptionPrefix), substr_len);
69 option_value = option_string.substr(value_separator_pos + 1);
72 option_string.substr(strlen(kOptionPrefix), value_separator_pos);
75 options_[option_name] = option_value;
78 bool CommandLine::HasOptionName(const std::string& option_name) {
79 return (options_.find(option_name) != options_.end());
82 std::string CommandLine::GetOptionValue(const std::string& option_name) {
83 if (HasOptionName(option_name)) {
84 return options_[option_name];
90 std::string CommandLine::GetCommandString() {
92 result.append(program_);
94 for (auto& it : options_) {
95 result.append(kOptionPrefix);
96 result.append(it.first);
97 if (!it.second.empty()) {
98 result.append(kOptionValueSeparator);
99 result.append(it.second);
103 for (auto& it : arguments_) {
110 std::string CommandLine::GetAppIdFromCommandLine(const std::string& program) {
112 std::string tmp = utils::BaseName(program_);
113 if (tmp == program) {
114 if (arguments_.size() > 0) {
115 // Suppose that appid is at the first of arguments_
116 return arguments_[0];
122 return std::string();
126 void CommandLine::Reset() {
127 if (!!current_process_commandline_) {
128 delete current_process_commandline_;
129 current_process_commandline_ = NULL;
134 bool CommandLine::Init(int argc, char* argv[]) {
135 if (!current_process_commandline_) {
136 current_process_commandline_ = new CommandLine(argc, argv);
143 CommandLine* CommandLine::ForCurrentProcess() {
144 return current_process_commandline_;
147 } // namespace common