Tizen 2.1 base
[framework/osp/app-controls.git] / src / media-app-control / ParseUtil.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FBaseSysLog.h>
19 #include "ParseUtil.h"
20
21 using namespace Tizen::Base;
22 using namespace Tizen::Base::Utility;
23
24 result
25 ParseParameter(const Tizen::Base::String& src, const Tizen::Base::String& delimeter, const Tizen::Base::String& key, int &value)
26 {
27         result r = E_SUCCESS;
28         String token;
29
30         StringTokenizer strTok(src, delimeter); 
31         SysTryCatch(NID_APP, strTok.GetTokenCount()>0, r = E_KEY_NOT_FOUND, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] delimeter:%ls", delimeter.GetPointer());
32         
33         while (strTok.HasMoreTokens())
34         {
35                 r = strTok.GetNextToken(token);
36                 SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
37
38                 if ( key == token )
39                 {
40                         String strValue;
41                         r = strTok.GetNextToken(strValue);
42                         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
43
44                         r = Integer::Parse(strValue, value);
45                         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));                     
46                 }
47         }       
48
49 CATCH:
50         return r;
51 }
52
53 result
54 ParseParameter(const Tizen::Base::String& src, const Tizen::Base::String& delimeter, const Tizen::Base::String& key, Tizen::Base::String &value)
55 {
56         result r = E_SUCCESS;
57         String token;
58
59         StringTokenizer strTok(src, delimeter);
60         SysTryCatch(NID_APP, strTok.GetTokenCount()>0, r = E_KEY_NOT_FOUND, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] Propagating.");
61         
62         while (strTok.HasMoreTokens())
63         {
64                 r = strTok.GetNextToken(token);
65                 SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
66
67                 if ( key == token )
68                 {
69                         r = strTok.GetNextToken(value);
70                         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
71                 }
72                 else            // In case of additional collon such as path:http://localhost:8080/xxx
73                 {
74                         r = value.Append(delimeter);
75                         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
76                         r = value.Append(token);
77                         SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
78                 }
79         }       
80
81 CATCH:
82         return r;
83 }
84
85 result
86 SplitParameters(const Tizen::Base::String& src, const Tizen::Base::String& delimeter, Tizen::Base::String& key, Tizen::Base::String &value)
87 {
88         result r = E_SUCCESS;
89         String token;
90         int i = 0;
91
92         StringTokenizer strTok(src, delimeter); 
93         SysTryCatch(NID_APP, strTok.GetTokenCount()>0, r = E_KEY_NOT_FOUND, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] Propagating.");
94
95         while (strTok.HasMoreTokens())
96         {
97                 r = strTok.GetNextToken(token);
98                 SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
99
100                 switch (i)
101                 {
102                         case 0:
103                                 key = token;
104                                 break;
105                         case 1:
106                                 value = token;
107                                 break;
108                         default :
109                                 value.Append(delimeter);
110                                 value.Append(token);
111                                 break;
112                 }
113                 i++;
114         }       
115
116 CATCH:
117         return r;
118 }
119
120 result
121 FindKey(const Tizen::Base::String& src, const Tizen::Base::String& delimeter, Tizen::Base::String& key)
122 {
123         result r = E_SUCCESS;
124         String token;
125
126         StringTokenizer strTok(src, delimeter);
127         SysTryCatch(NID_APP, strTok.GetTokenCount()>0, r = E_KEY_NOT_FOUND, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] Propagating.");
128         
129         while (strTok.HasMoreTokens())
130         {
131                 r = strTok.GetNextToken(token);
132                 SysTryCatch(NID_APP, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
133
134                 key = token;            // Get the key which occurs first.
135                 break;
136         }
137
138 CATCH:
139         return r;
140 }
141
142 result
143 FindKeyUnit(const UnitTransMap* pMap, const Tizen::Base::String& srcKey, Tizen::Base::String& dstKey, UnitType &srcType, UnitType &dstType, Tizen::Base::String &conCatStr, int& conCatIndex )
144 {       
145         result r = E_SUCCESS;
146         int i = 0;      
147
148         SysTryCatch(NID_APP, pMap, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Propagating.");
149
150         while ( pMap[i].srcKey != null && pMap[i].dstKey != null )
151         {
152                 if ( srcKey.Equals(String(pMap[i].srcKey), true))
153                 {
154                         dstKey = String(pMap[i].dstKey);
155                         srcType = pMap[i].srcType;
156                         dstType = pMap[i].dstType;
157
158                         if ( pMap[i].conCatStr )
159                         {
160                                 conCatStr = String(pMap[i].conCatStr);
161                         }
162                         else
163                         {
164                                 conCatStr = String(L"");
165                         }
166
167                         conCatIndex = pMap[i].conCatIndex;
168
169 //                      SysLog(NID_APP, "src:%s, dst:%s, srcType:%d, dstType:%d, conCat:%s, conCatIndex:%d", pMap[i].srcKey,pMap[i].dstKey,pMap[i].srcType,pMap[i].dstType,pMap[i].conCatStr,pMap[i].conCatIndex);
170                 }
171                 i++;
172         }
173         
174 CATCH:
175         return r;
176 }
177
178 result
179 PeekListValue(const Tizen::Base::Collection::IList* pList, int index, const Tizen::Base::String& delimeter, const Tizen::Base::String& key, Tizen::Base::String& value )
180 {
181         result r = E_SUCCESS;
182         String *pNodeData=null;
183
184         SysTryCatch(NID_APP, pList != null, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The obj was not found.\n");
185         SysTryCatch(NID_APP, pList->GetCount() > index , r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Propagating..\n");
186         
187         pNodeData = dynamic_cast<String*>(const_cast<Object*>(pList->GetAt(index)));
188         SysTryCatch(NID_APP, pNodeData != null, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The file was not found.\n");
189         SysTryCatch(NID_APP, pNodeData->GetLength() > 0 , r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The file path is too short.\n");
190
191         r = ParseParameter(*pNodeData, key, delimeter, value);
192
193 CATCH:
194         return r;
195 }
196
197 result
198 FindListKey(const Tizen::Base::Collection::IList* pList, int index, const Tizen::Base::String& delimeter, Tizen::Base::String& key )
199 {
200         result r = E_SUCCESS;
201         String *pNodeData=null;
202
203         SysTryCatch(NID_APP, pList != null, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The obj was not found.\n");
204         SysTryCatch(NID_APP, pList->GetCount() > index , r = E_OUT_OF_RANGE, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] Propagating.list count:%d, index:%d", pList->GetCount(), index);
205         
206         pNodeData = dynamic_cast<String*>(const_cast<Object*>(pList->GetAt(index)));
207         SysTryCatch(NID_APP, pNodeData != null, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The file was not found.\n");
208         SysTryCatch(NID_APP, pNodeData->GetLength() > 0 , r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The file path is too short.\n");
209         
210         r = FindKey(*pNodeData, delimeter, key);
211         
212 CATCH:
213         return r;
214 }
215
216 int
217 CountListKey(const Tizen::Base::Collection::IList* pList, const Tizen::Base::String& delimeter, const Tizen::Base::String& key )
218 {
219         result r = E_SUCCESS;
220         int count = 0;
221         int matchCount = 0;
222         ClearLastResult();
223         SysTryCatch(NID_APP, pList != null, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The obj was not found.\n");
224
225         count = pList->GetCount();
226
227         for (int i = 0; i<count; i++ )
228         {
229                 String value;
230                 r = FindListKey(pList, i, delimeter, value);
231                 if ( r == E_SUCCESS && value == key )
232                 {
233                         matchCount++;
234                 }
235         }
236         
237 CATCH:
238         SetLastResult(r);
239         return matchCount;
240 }
241
242