Initialize Tizen 2.3
[framework/web/wrt-installer.git] / src_wearable / wrt-installer / command_parser.cpp
1 /*
2  * Copyright (c) 2011 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    command_parser.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @brief   Implementation file for OptionParser.
20  */
21
22 #include <dpl/string.h>
23 #include <dpl/errno_string.h>
24 #include <installer_log.h>
25 #include "command_parser.h"
26
27 namespace {
28 typedef std::pair<std::string, std::string> DataPair;
29
30 const char* const KEY_OP = "op";
31 const char* const KEY_PATH = "path";
32 const char* const KEY_REMOVABLE = "removable";
33 }
34
35 bool CommandParser::CscCommandParser(const std::string& arg, CscOption &option)
36 {
37     using namespace Command;
38     // path=/opt/system/csc/Ozq2iEG15R-2.0.0-arm.wgt:op=install:removable=true
39     // parsing CSC configuration string
40     _D("CscCommandParser");
41     if (arg.empty()) {
42         return false;
43     }
44     DataMap cmdMap = ArgumentsParser(arg);
45
46     DataMap::iterator it;
47     it = cmdMap.find(KEY_OP);
48     if (it == cmdMap.end()) {
49         return false;
50     }
51
52     if (it->second == VALUE_INSTALL) {
53         _D("operation = %s", it->second.c_str());
54         option.operation = VALUE_INSTALL;
55         it = cmdMap.find(KEY_PATH);
56         if (it == cmdMap.end()) {
57             return false;
58         }
59         option.path = it->second;
60         _D("path      = %s", option.path.c_str());
61
62         it = cmdMap.find(KEY_REMOVABLE);
63         if (it == cmdMap.end()) {
64             return false;
65         }
66
67         option.removable = true;
68         if (0 == it->second.compare(VALUE_FALSE)) {
69             option.removable = false;
70         }
71     } else if (it->second == VALUE_UNINSTALL) {
72         _D("operation = %s", it->second.c_str());
73         // uninstall command isn't confirmed yet
74         it = cmdMap.find(KEY_PATH);
75         if (it == cmdMap.end()) {
76             return false;
77         }
78         option.path = it->second;
79         _D("operation = uninstall");
80         _D("path      = %s", option.path.c_str());
81     } else {
82         _E("Unknown operation : %s", it->second.c_str());
83         _D("operation = %s", it->second.c_str());
84         return false;
85     }
86
87     return true;
88 }
89
90 bool CommandParser::FotaCommandParser(const std::string& arg, FotaOption
91         &option)
92 {
93     using namespace Command;
94     // path=pkgid:op=install
95     _D("FotaCommandParser");
96     DataMap cmdMap = ArgumentsParser(arg);
97
98     DataMap::iterator it;
99     it = cmdMap.find(KEY_OP);
100     if (it == cmdMap.end()) {
101         return false;
102     }
103     option.operation = it->second;
104
105     it = cmdMap.find(KEY_PATH);
106     if (it == cmdMap.end()) {
107         return false;
108     }
109
110     option.pkgId = it->second;
111     _D("Fota : package_id [%s], operaion [%s]", option.pkgId.c_str(),
112             option.operation.c_str());
113
114     return true;
115 }
116
117 CommandParser::DataMap CommandParser::ArgumentsParser(const std::string& arg)
118 {
119     DataMap result;
120
121     if (arg.empty()) {
122         _D("Input argument is empty");
123         return result;
124     }
125
126     const char* ptr = strtok(const_cast<char*>(arg.c_str()),":");
127     while (ptr != NULL) {
128         std::string string = ptr;
129         ptr = strtok (NULL, ":");
130         size_t pos = string.find('=');
131         if (pos == std::string::npos) {
132             continue;
133         }
134         result.insert(
135                 DataPair(string.substr(0, pos),
136                     string.substr(pos+1)));
137     }
138
139     return result;
140 }