Specifiy that this supports M47
[platform/framework/web/crosswalk-tizen.git] / common / command_line.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "common/command_line.h"
18
19 #include <cstring>
20
21 #include "common/file_utils.h"
22
23 namespace common {
24
25 namespace {
26
27 const char* kOptionPrefix = "--";
28 const char* kOptionValueSeparator = "=";
29
30 static bool IsValidOptionString(const char* argument) {
31   if (NULL != argument &&
32       strncmp(argument, kOptionPrefix, strlen(kOptionPrefix)) == 0) {
33     return true;
34   } else {
35     return false;
36   }
37 }
38
39 }  // namespace
40
41 CommandLine* CommandLine::current_process_commandline_ = NULL;
42
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]);
49     } else {
50       arguments_.push_back(argv[i]);
51     }
52   }
53
54   // Parse program name and appid from argv_ or arguments_
55   program_ = std::string(argv[0]);
56 }
57
58 CommandLine::~CommandLine() {
59 }
60
61 void CommandLine::AppendOption(const char* value) {
62   std::string option_string(value);
63   std::string option_name;
64   std::string option_value;
65
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);
72   } else {
73     option_name = option_string.substr(strlen(kOptionPrefix),
74                                        value_separator_pos);
75   }
76
77   options_[option_name] = option_value;
78 }
79
80 bool CommandLine::HasOptionName(const std::string& option_name) {
81   return (options_.find(option_name) != options_.end());
82 }
83
84 std::string CommandLine::GetOptionValue(const std::string& option_name) {
85   if (HasOptionName(option_name)) {
86     return options_[option_name];
87   } else {
88     return std::string();
89   }
90 }
91
92 std::string CommandLine::GetCommandString() {
93   std::string result;
94   result.append(program_);
95   result.append(" ");
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);
102     }
103     result.append(" ");
104   }
105   for (auto& it : arguments_) {
106     result.append(it);
107     result.append(" ");
108   }
109   return result;
110 }
111
112 std::string CommandLine::GetAppIdFromCommandLine(const std::string& program) {
113   if (argc_ > 0) {
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];
119       }
120     } else {
121       return tmp;
122     }
123   }
124   return std::string();
125 }
126
127 // static
128 void CommandLine::Reset() {
129   if (!!current_process_commandline_) {
130     delete current_process_commandline_;
131     current_process_commandline_ = NULL;
132   }
133 }
134
135 // static
136 void CommandLine::Init(int argc, char* argv[]) {
137   if (!current_process_commandline_) {
138     current_process_commandline_ = new CommandLine(argc, argv);
139   }
140 }
141
142 // static
143 CommandLine* CommandLine::ForCurrentProcess() {
144   return current_process_commandline_;
145 }
146
147 }  // namespace common