Modify flora license version.
[platform/core/messaging/msg-service.git] / framework / plugin-manager / MsgPluginConfig.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 "MsgPluginConfig.h"
18 #include "MsgException.h"
19
20
21 /*==================================================================================================
22                                      IMPLEMENTATION OF MsgPlgToken - Member Functions
23 ==================================================================================================*/
24 int MsgPlgToken::tokenize(char *pStr)
25 {
26         MSG_BEGIN();
27
28         if (pStr == NULL)
29                 THROW(MsgException::INVALID_PARAM, "pstr is NULL");
30
31         char *pFirst = index(pStr, '[');
32
33         if (pFirst) // title is enclosed by [ ]
34         {
35                 pFirst++; // excluding '['
36
37                 char *pEnd = index(pStr, ']');
38
39                 if (pEnd)
40                 {
41                         pEnd--;
42
43                         tokenType = TOK_PLG_TITLE;
44                         int len = pEnd-pFirst+1;
45
46                         memcpy(tokenVal, pFirst, len);
47                         tokenVal[len] = '\0';
48
49                         MSG_DEBUG("%s", tokenVal);
50                 }
51                 else
52                         THROW(MsgException::INVALID_RESULT, "']' not found");
53         }
54         else // (attribute, value) pair
55         {
56                 char tokStr[strlen(pStr)+1];
57                 strncpy(tokStr, pStr, strlen(pStr));
58
59                 char *attr = strtok(tokStr, "=");
60                 char *val = strtok(NULL, "=");
61
62                 if (!attr || !val)
63                 {
64                         tokenType = TOK_UNDEFINED;
65                 }
66                 else
67                 {
68                         // trimming enter key
69                         char *tmp = index(val,'\n');
70
71                         if (tmp)
72                                 *tmp = '\0';
73
74                         MSG_DEBUG("attr \"%s\", val \"%s\" ", attr, val);
75
76                         // classifying the attribute
77                         if (strcmp(attr, "type") == 0)
78                         {
79                                 tokenType = TOK_PLG_TYPE;
80                                 strncpy(tokenVal, val, 255);
81                         }
82                         else if (strcmp(attr, "path") == 0)
83                         {
84                                 tokenType = TOK_PLG_PATH;
85                                 strncpy(tokenVal, val, 255);
86                         }
87                         else
88                         {
89                                 tokenType = TOK_UNDEFINED;
90                         }
91                 }
92         }
93
94         MSG_END();
95
96         return tokenType;
97 }
98
99
100 /*==================================================================================================
101                                      IMPLEMENTATION OF MsgPlgConfig - Member Functions
102 ==================================================================================================*/
103 MsgPlgConfig::MsgPlgConfig(FILE* fp)
104 {
105         MSG_BEGIN();
106
107         if (fp == NULL)
108                 THROW(MsgException::INVALID_PARAM, "fp is NULL");
109
110         MsgPlgToken tokTitle, tokMsgType, tokLibPath;
111
112         char str[256];
113         memset(str, 0x00, sizeof(str));
114
115         MsgPlgToken tok = MsgPlgToken();
116
117         while (fgets(str, 255, fp))
118         {
119                 tok.tokenize(str); // parsing the line into tokens
120
121                 switch ( tok.getType() )
122                 {
123                         case MsgPlgToken::TOK_PLG_TITLE:
124                                 // processing previous items
125                                 if( tokTitle && tokMsgType && tokLibPath )
126                                 {
127                                         insert(tokTitle, tokMsgType, tokLibPath);
128                                 }
129
130                                 tokTitle = tok;
131                                 tokMsgType.reset();
132                                 tokLibPath.reset();
133
134                                 break;
135
136                         case MsgPlgToken::TOK_PLG_TYPE:
137                                 tokMsgType = tok;
138                                 break;
139
140                         case MsgPlgToken::TOK_PLG_PATH:
141                                 tokLibPath = tok;
142                                 break;
143
144                         default:
145                                 MSG_DEBUG("the line \"%s\" is not accecpted", str);
146                                 break;
147                 }
148         }
149
150         if (tokTitle && tokMsgType && tokLibPath)
151         {
152                 insert(tokTitle, tokMsgType, tokLibPath);
153         }
154
155         MSG_END();
156 }
157
158
159 void MsgPlgConfig::insert(const MsgPlgToken& tokTitle, const MsgPlgToken& tokMsgType, const MsgPlgToken& tokLibPath)
160 {
161         MSG_BEGIN();
162
163         MsgPlgTokenVec item2add;
164         item2add.push_back(tokMsgType);
165         item2add.push_back(tokLibPath);
166
167         CharVector titleVec;
168         tokTitle.getVal(titleVec);
169         MsgConfigMap::iterator it=configMap.find(titleVec);
170
171         if (it == configMap.end())
172                 configMap.insert(std::make_pair(titleVec, item2add));
173         else
174                 THROW(MsgException::PLUGIN_ERROR, "duplicated plugin title");
175
176         MSG_DEBUG("item inserted");
177         MSG_DEBUG("token:%d,value:%s", tokTitle.getType(), tokTitle.getVal());
178         MSG_DEBUG("token:%d,value:%s", tokMsgType.getType(), tokMsgType.getVal());
179         MSG_DEBUG("token:%d,value:%s", tokLibPath.getType(), tokLibPath.getVal());
180
181         MSG_END();
182 }
183
184
185 const CharVector& MsgPlgConfig::title(unsigned int pos)
186 {
187         if (pos >= configMap.size())
188                 THROW(MsgException::OUT_OF_RANGE, "Input Parameter is not valid [%d]", pos);
189
190         MsgConfigMap::iterator it = configMap.begin();
191
192         unsigned int i=0;
193
194         while (it != configMap.end())
195         {
196                 if (i++ == pos) break;
197                 it++;
198         }
199
200         if (it == configMap.end())
201                 THROW(MsgException::INVALID_RESULT, "no title");
202
203         MSG_DEBUG("searched title:%s", &(it->first)[0]);
204
205         return it->first;
206 }
207
208
209 void MsgPlgConfig::token(const CharVector& key, unsigned int pos, MsgPlgToken& retTok)
210 {
211         MsgConfigMap::iterator it = configMap.find(key);
212
213         if (it != configMap.end()) //found
214         {
215                 MSG_DEBUG("searched title:%s", &(it->first)[0]);
216
217                 MsgPlgTokenVec tokVec = it->second;
218                 retTok = tokVec[pos];
219
220                 MSG_DEBUG("searched token:%d,value:%s", retTok.getType(), retTok.getVal());
221         }
222         else
223         {
224                 THROW(MsgException::INVALID_RESULT, "no title");
225         }
226 }
227
228
229 void MsgPlgConfig::token(int i, unsigned int pos, MsgPlgToken& retTok)
230 {
231         const CharVector& key = title(i);
232
233         token(key, pos, retTok);
234
235         MSG_DEBUG("returned token:%d,value:%s", retTok.getType(), retTok.getVal());
236 }
237