[Release] wrt_0.8.274
[platform/framework/web/wrt.git] / src / wrt-client / client_command_line_parser.cpp
1 /*
2  * Copyright (c) 2013 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  * @file    client_command_line_parser.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  */
20
21 #include "client_command_line_parser.h"
22
23 #include <cstddef>
24 #include <sstream>
25 #include <string>
26
27 #include <dpl/log/log.h>
28 #include <dpl/optional_typedefs.h>
29 #include <dpl/wrt-dao-ro/common_dao_types.h>
30
31 namespace ClientModule {
32 namespace {
33
34 std::string parseIdField(int argc, char **argv)
35 {
36     if (argv[0] == NULL) {
37         return "";
38     }
39
40     std::string arg = argv[0];
41     if (arg.empty()) {
42         return "";
43     }
44
45     if (arg.find("wrt-client") != std::string::npos) {
46         if (argc <= 1) {
47             return "";
48         }
49
50         arg = argv[1];
51
52         if (arg == "-h" || arg == "--help") {
53             return "";
54         } else if (arg == "-l" ||
55                    arg == "--launch" ||
56                    arg == "-t" ||
57                    arg == "--tizen")
58         {
59             if (argc != 3) {
60                 return "";
61             }
62             return argv[2];
63         } else {
64             return "";
65         }
66     } else {
67         std::size_t pos = arg.find_last_of('/');
68         if (pos != std::string::npos) {
69             arg = arg.erase(0, pos + 1);
70         }
71         return arg;
72     }
73 }
74
75 DPL::OptionalUInt getIndex(const std::string& tizenId)
76 {
77     std::size_t pos =
78         tizenId.find(WrtDB::AppControlPrefix::PROCESS_PREFIX);
79     if (pos != std::string::npos) {
80         std::string index = tizenId.substr(pos);
81         index.erase(strlen(WrtDB::AppControlPrefix::PROCESS_PREFIX));
82         std::stringstream s(index);
83         unsigned int appControlIndex;
84         s >> appControlIndex;
85         return appControlIndex;
86     }
87     return DPL::OptionalUInt::Null;
88 }
89
90 std::string removeIndex(const std::string& tizenId)
91 {
92     std::string id = tizenId;
93     std::size_t pos =
94         id.find(WrtDB::AppControlPrefix::PROCESS_PREFIX);
95     if (pos != std::string::npos) {
96         id.erase(pos);
97     }
98     return id;
99 }
100 }
101
102 std::string CommandLineParser::getTizenId(int argc, char **argv)
103 {
104     std::string id = parseIdField(argc, argv);
105     if (id.empty()) {
106         return "";
107     }
108     return removeIndex(id);
109 }
110
111 DPL::OptionalUInt CommandLineParser::getAppControlIndex(int argc, char **argv)
112 {
113     std::string id = parseIdField(argc, argv);
114     if (id.empty()) {
115         return DPL::OptionalUInt::Null;
116     }
117     return getIndex(id);
118 }
119 } // ClientModule