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[])
44 : argc_(argc), argv_(argv) {
45 // Append option or push each arg(not option) into arguments_
46 for (int i=1; i < argc; ++i) {
47 if (IsValidOptionString(argv[i])) {
48 AppendOption(argv[i]);
50 arguments_.push_back(argv[i]);
54 // Parse program name and appid from argv_ or arguments_
55 program_ = std::string(argv[0]);
58 CommandLine::~CommandLine() {
61 void CommandLine::AppendOption(const char* value) {
62 std::string option_string(value);
63 std::string option_name;
64 std::string option_value;
66 int value_separator_pos = option_string.find(kOptionValueSeparator,
67 strlen(kOptionPrefix));
68 if (value_separator_pos >= 0) {
69 int substr_len = value_separator_pos - strlen(kOptionPrefix);
70 option_name = option_string.substr(strlen(kOptionPrefix), substr_len);
71 option_value = option_string.substr(value_separator_pos+1);
73 option_name = option_string.substr(strlen(kOptionPrefix),
77 options_[option_name] = option_value;
80 bool CommandLine::HasOptionName(const std::string& option_name) {
81 return (options_.find(option_name) != options_.end());
84 std::string CommandLine::GetOptionValue(const std::string& option_name) {
85 if (HasOptionName(option_name)) {
86 return options_[option_name];
92 std::string CommandLine::GetCommandString() {
94 result.append(program_);
96 for (auto& it : options_) {
97 result.append(kOptionPrefix);
98 result.append(it.first);
99 if (!it.second.empty()) {
100 result.append(kOptionValueSeparator);
101 result.append(it.second);
105 for (auto& it : arguments_) {
112 std::string CommandLine::GetAppIdFromCommandLine(const std::string& program) {
114 std::string tmp = utils::BaseName(program_);
115 if (tmp == program) {
116 if (arguments_.size() > 0) {
117 // Suppose that appid is at the first of arguments_
118 return arguments_[0];
124 return std::string();
128 void CommandLine::Reset() {
129 if (!!current_process_commandline_) {
130 delete current_process_commandline_;
131 current_process_commandline_ = NULL;
136 void CommandLine::Init(int argc, char* argv[]) {
137 if (!current_process_commandline_) {
138 current_process_commandline_ = new CommandLine(argc, argv);
143 CommandLine* CommandLine::ForCurrentProcess() {
144 return current_process_commandline_;
147 } // namespace common