2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
17 #include "MsgPluginConfig.h"
18 #include "MsgException.h"
21 /*==================================================================================================
22 IMPLEMENTATION OF MsgPlgToken - Member Functions
23 ==================================================================================================*/
24 int MsgPlgToken::tokenize(char *pStr)
29 THROW(MsgException::INVALID_PARAM, "pstr is NULL");
31 char *pFirst = index(pStr, '[');
33 if (pFirst) // title is enclosed by [ ]
35 pFirst++; // excluding '['
37 char *pEnd = index(pStr, ']');
43 tokenType = TOK_PLG_TITLE;
44 int len = pEnd-pFirst+1;
46 memcpy(tokenVal, pFirst, len);
49 MSG_DEBUG("%s", tokenVal);
52 THROW(MsgException::INVALID_RESULT, "']' not found");
54 else // (attribute, value) pair
56 char tokStr[strlen(pStr)+1];
57 strncpy(tokStr, pStr, strlen(pStr));
59 char *attr = strtok(tokStr, "=");
60 char *val = strtok(NULL, "=");
64 tokenType = TOK_UNDEFINED;
69 char *tmp = index(val,'\n');
74 MSG_DEBUG("attr \"%s\", val \"%s\" ", attr, val);
76 // classifying the attribute
77 if (strcmp(attr, "type") == 0)
79 tokenType = TOK_PLG_TYPE;
80 strncpy(tokenVal, val, 255);
82 else if (strcmp(attr, "path") == 0)
84 tokenType = TOK_PLG_PATH;
85 strncpy(tokenVal, val, 255);
89 tokenType = TOK_UNDEFINED;
100 /*==================================================================================================
101 IMPLEMENTATION OF MsgPlgConfig - Member Functions
102 ==================================================================================================*/
103 MsgPlgConfig::MsgPlgConfig(FILE* fp)
108 THROW(MsgException::INVALID_PARAM, "fp is NULL");
110 MsgPlgToken tokTitle, tokMsgType, tokLibPath;
113 memset(str, 0x00, sizeof(str));
115 MsgPlgToken tok = MsgPlgToken();
117 while (fgets(str, 255, fp))
119 tok.tokenize(str); // parsing the line into tokens
121 switch ( tok.getType() )
123 case MsgPlgToken::TOK_PLG_TITLE:
124 // processing previous items
125 if( tokTitle && tokMsgType && tokLibPath )
127 insert(tokTitle, tokMsgType, tokLibPath);
136 case MsgPlgToken::TOK_PLG_TYPE:
140 case MsgPlgToken::TOK_PLG_PATH:
145 MSG_DEBUG("the line \"%s\" is not accecpted", str);
150 if (tokTitle && tokMsgType && tokLibPath)
152 insert(tokTitle, tokMsgType, tokLibPath);
159 void MsgPlgConfig::insert(const MsgPlgToken& tokTitle, const MsgPlgToken& tokMsgType, const MsgPlgToken& tokLibPath)
163 MsgPlgTokenVec item2add;
164 item2add.push_back(tokMsgType);
165 item2add.push_back(tokLibPath);
168 tokTitle.getVal(titleVec);
169 MsgConfigMap::iterator it=configMap.find(titleVec);
171 if (it == configMap.end())
172 configMap.insert(std::make_pair(titleVec, item2add));
174 THROW(MsgException::PLUGIN_ERROR, "duplicated plugin title");
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());
185 const CharVector& MsgPlgConfig::title(unsigned int pos)
187 if (pos >= configMap.size())
188 THROW(MsgException::OUT_OF_RANGE, "Input Parameter is not valid [%d]", pos);
190 MsgConfigMap::iterator it = configMap.begin();
194 while (it != configMap.end())
196 if (i++ == pos) break;
200 if (it == configMap.end())
201 THROW(MsgException::INVALID_RESULT, "no title");
203 MSG_DEBUG("searched title:%s", &(it->first)[0]);
209 void MsgPlgConfig::token(const CharVector& key, unsigned int pos, MsgPlgToken& retTok)
211 MsgConfigMap::iterator it = configMap.find(key);
213 if (it != configMap.end()) //found
215 MSG_DEBUG("searched title:%s", &(it->first)[0]);
217 MsgPlgTokenVec tokVec = it->second;
218 retTok = tokVec[pos];
220 MSG_DEBUG("searched token:%d,value:%s", retTok.getType(), retTok.getVal());
224 THROW(MsgException::INVALID_RESULT, "no title");
229 void MsgPlgConfig::token(int i, unsigned int pos, MsgPlgToken& retTok)
231 const CharVector& key = title(i);
233 token(key, pos, retTok);
235 MSG_DEBUG("returned token:%d,value:%s", retTok.getType(), retTok.getVal());