6ee518a1eb731f48291f0dd6a8bfbf90dc68a817
[platform/framework/web/crosswalk-tizen.git] / tizen / 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[]) : 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]);
48     } else {
49       arguments_.push_back(argv[i]);
50     }
51   }
52
53   // Parse program name and appid from argv_ or arguments_
54   program_ = std::string(argv[0]);
55 }
56
57 CommandLine::~CommandLine() {}
58
59 void CommandLine::AppendOption(const char* value) {
60   std::string option_string(value);
61   std::string option_name;
62   std::string option_value;
63
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);
70   } else {
71     option_name =
72         option_string.substr(strlen(kOptionPrefix), value_separator_pos);
73   }
74
75   options_[option_name] = option_value;
76 }
77
78 bool CommandLine::HasOptionName(const std::string& option_name) {
79   return (options_.find(option_name) != options_.end());
80 }
81
82 std::string CommandLine::GetOptionValue(const std::string& option_name) {
83   if (HasOptionName(option_name)) {
84     return options_[option_name];
85   } else {
86     return std::string();
87   }
88 }
89
90 std::string CommandLine::GetCommandString() {
91   std::string result;
92   result.append(program_);
93   result.append(" ");
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);
100     }
101     result.append(" ");
102   }
103   for (auto& it : arguments_) {
104     result.append(it);
105     result.append(" ");
106   }
107   return result;
108 }
109
110 std::string CommandLine::GetAppIdFromCommandLine(const std::string& program) {
111   if (argc_ > 0) {
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];
117       }
118     } else {
119       return tmp;
120     }
121   }
122   return std::string();
123 }
124
125 // static
126 void CommandLine::Reset() {
127   if (!!current_process_commandline_) {
128     delete current_process_commandline_;
129     current_process_commandline_ = NULL;
130   }
131 }
132
133 // static
134 void CommandLine::Init(int argc, char* argv[]) {
135   if (!current_process_commandline_) {
136     current_process_commandline_ = new CommandLine(argc, argv);
137   }
138 }
139
140 // static
141 CommandLine* CommandLine::ForCurrentProcess() {
142   return current_process_commandline_;
143 }
144
145 }  // namespace common