Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SoftSensorPlugin / IndoorTrajectorySensor / src / GeneralData.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "GeneralData.h"
25
26 //#define __INTER_DEBUG__
27 // Hidden Class define.
28 template <typename TYPE>
29 class AllFormat_data : public Virtual_data
30 {
31         typedef Virtual_data base;
32     private :
33         TYPE data;
34
35     public :
36         AllFormat_data(int typenum) : base(typenum)
37         {
38 #ifdef __INTER_DEBUG__
39             printf("[DEBUG] Test_data Constructor().\n");
40 #endif
41         }
42
43         virtual void put_data( void *value)
44         {
45             data = *(TYPE *)value;
46         }
47
48         virtual void *get_data( void )
49         {
50             return (void *)&data;
51         }
52 };
53
54
55
56 /*******************************************
57  * Virtual_data class member function define.
58  */
59 void Virtual_data::s2data( const char *value)
60 {
61     switch (dataType)
62     {
63         case TYPE_CHAR :
64             {
65                 char data = atoi(value);
66                 put_data( (void *)&data );
67             }
68         case TYPE_INT :
69         case TYPE_SHORT :
70             {
71                 int data = atoi(value);
72                 put_data( (void *)&data );
73             }
74             break;
75         case TYPE_FLOAT :
76             {
77                 float data = atof(value);
78                 put_data( (void *)&data );
79             }
80             break;
81         case TYPE_DOUBLE :
82             {
83                 double data = atof(value);
84                 put_data( (void *)&data );
85             }
86             break;
87         case TYPE_NODEFINE :
88             printf("Error: dataType must have defined.\n");
89             break;
90         default :
91             printf("Error: Not yet supported type.\n");
92             break;
93     }
94 }
95
96 Virtual_data::Virtual_data( void )
97 {
98     dataType = TYPE_NODEFINE;
99 }
100
101 Virtual_data::Virtual_data(int type)
102 {
103 #ifdef __INTER_DEBUG__
104     printf("[DEBUG] Virtual_data Constructor().\n");
105 #endif
106     dataType = type;
107 }
108
109 void Virtual_data::put(std::string value)
110 {
111     if (dataType == TYPE_STRING )
112         put_data( (void *)&value );
113     else
114         s2data( value.c_str() );
115 }
116
117 void Virtual_data::put(char *value)
118 {
119     if (dataType == TYPE_STRING )
120     {
121         std::string temp = std::string(value);
122         put_data( (void *)&temp );
123     }
124     else
125         s2data( value );
126 }
127
128 int Virtual_data::get( std::string *value)
129 {
130     if (dataType != TYPE_STRING)
131     {
132         printf("Error : dataType is Not string.\n");
133         return 0;
134     }
135
136     void *data = get_data();
137     *value = *(std::string *)data;
138     return 1;
139 }
140
141 void Virtual_data::put_data( void *value )
142 {
143     printf("Error: virtual function(put_data) called.\n");
144 }
145
146 void *Virtual_data::get_data( void )
147 {
148     printf("Error: virtual function(get) called.\n");
149     return NULL;
150 }
151
152
153
154
155
156
157 /*******************************************
158  * GeneralData class member function define.
159  */
160 void GeneralData::set_dataType(const char *type)
161 {
162     if ( strstr(type, "string") != NULL )
163     {
164         dataType = TYPE_STRING;
165 #ifdef __INTER_DEBUG__
166         printf("DataType is std::string.\n");
167 #endif
168         return ;
169     }
170     if ( strstr(type, "char") != NULL )
171     {
172         dataType = TYPE_CHAR;
173 #ifdef __INTER_DEBUG__
174         printf("DataType is Character.\n");
175 #endif
176         return ;
177     }
178     if ( strstr(type, "int") != NULL )
179     {
180         dataType = TYPE_INT;
181 #ifdef __INTER_DEBUG__
182         printf("DataType is Integer.\n");
183 #endif
184         return ;
185     }
186     if ( strstr(type, "short") != NULL )
187     {
188         dataType = TYPE_SHORT;
189 #ifdef __INTER_DEBUG__
190         printf("DataType is Short.\n");
191 #endif
192         return ;
193     }
194     if ( strstr(type, "float") != NULL )
195     {
196         dataType = TYPE_FLOAT;
197 #ifdef __INTER_DEBUG__
198         printf("DataType is Floating.\n");
199 #endif
200         return ;
201     }
202     if ( strstr(type, "double") != NULL )
203     {
204         dataType = TYPE_DOUBLE;
205 #ifdef __INTER_DEBUG__
206         printf("DataType is Double.\n");
207 #endif
208         return ;
209     }
210
211     dataType = TYPE_NODEFINE;
212 }
213
214 GeneralData::GeneralData( void )
215 {
216 #ifdef __INTER_DEBUG__
217     printf("[DEBUG] Virtual_Test Constructor().\n");
218 #endif
219     flag = false;
220     Name = "";
221     dataType = TYPE_NODEFINE;
222     pValue = 0;
223 }
224
225 GeneralData::GeneralData( std::string name, std::string type )
226 {
227 #ifdef __INTER_DEBUG__
228     printf("[DEBUG] Data_Normal Constructor().\n");
229 #endif
230     flag = false;
231     Name = "";
232     dataType = TYPE_NODEFINE;
233     pValue = 0;
234
235     flag = initial( name, type );
236 }
237
238 GeneralData::GeneralData( std::string name, std::string type, std::string value )
239 {
240 #ifdef __INTER_DEBUG__
241     printf("[DEBUG] Data_Normal Constructor().\n");
242 #endif
243     flag = false;
244     Name = "";
245     dataType = TYPE_NODEFINE;
246     pValue = 0;
247
248     flag = initial( name, type );
249     put(value);
250 }
251
252 bool GeneralData::initial( std::string name, std::string type )
253 {
254     Name = name;
255     set_dataType( type.c_str() );
256
257     switch (dataType)
258     {
259         case TYPE_STRING :
260             pValue = new AllFormat_data<std::string>(dataType);
261             break;
262         case TYPE_CHAR :
263             pValue = new AllFormat_data<char>(dataType);
264             break;
265         case TYPE_INT :
266             pValue = new AllFormat_data<int>(dataType);
267             break;
268         case TYPE_SHORT :
269             pValue = new AllFormat_data<short>(dataType);
270             break;
271         case TYPE_FLOAT :
272             pValue = new AllFormat_data<float>(dataType);
273             break;
274         case TYPE_DOUBLE :
275             pValue = new AllFormat_data<double>(dataType);
276             break;
277         case TYPE_NODEFINE :
278             pValue = 0;
279             printf("Error : set_dataType()function is returned TYPE_NODEFINE.\n");
280             break;
281     }
282
283     return true;
284 }
285
286 bool GeneralData::get_Name( std::string &name )
287 {
288     BOOLINIT_CHECK();
289
290     name = Name;
291     return true;
292 }
293
294 void GeneralData::put( std::string value )
295 {
296     VOIDINIT_CHECK();
297     pValue->put(value);
298 }
299
300 void GeneralData::put( const char *value )
301 {
302     VOIDINIT_CHECK();
303     pValue->put(value);
304 }
305
306 int GeneralData::get_DataType( void )
307 {
308     BOOLINIT_CHECK();
309
310     return dataType;
311 }
312
313 bool GeneralData::get( std::string *data )
314 {
315     BOOLINIT_CHECK();
316
317     if ( pValue->get(data) == NULL )
318     {
319         printf("Error : No data.\n");
320         *data = "";
321         return false;
322     }
323
324     return true;
325 }
326
327
328
329 void Conversion_DataFormat( std::vector < std::map< std::string, std::string > > lVector ,
330                             std::vector< GeneralData > &gVector )
331 {
332 #ifdef __INTERNAL_DEBUG__
333     std::cout << "[DEBUG] ITS::" << __func__ << " is called." << std::endl;
334 #endif
335
336     std::string name;
337     std::string type;
338     std::string value;
339
340     for (unsigned int j = 0; j < lVector.size(); j++)
341     {
342         name = lVector[j]["name"];
343         type = lVector[j]["type"];
344         value = lVector[j]["value"];
345
346         GeneralData pValue(name, type, value);
347         gVector.push_back(pValue);
348     }
349
350     /*************************************************
351      * Debugging print ( GeneralData format confirm. )
352      */
353 #ifdef __INTERNAL_DEBUG__
354     for (unsigned int j = 0; j < gVector.size(); j++)
355     {
356         if ( gVector[j].get_Name(name) == false )
357         {
358             printf("Error : Not initialed.\n");
359             return ;
360         }
361
362         int dataType = gVector[j].get_DataType();
363         switch (dataType)
364         {
365             case TYPE_STRING :
366                 {
367                     std::string data;
368                     if ( gVector[j].get(&data) == false )
369                     {
370                         printf("Error : Not initialed.\n");
371                         return ;
372                     }
373                     printf("name=%s , type=%d, value=%s\n", name.c_str(), dataType, data.c_str() );
374                 }
375                 break;
376             case TYPE_CHAR :
377             case TYPE_SHORT :
378             case TYPE_INT :
379                 {
380                     int data;
381                     if ( gVector[j].get(&data) == false )
382                     {
383                         printf("Error : Not initialed.\n");
384                         return ;
385                     }
386                     printf("name=%s , type=%d, value=%d\n", name.c_str(), dataType, data );
387                 }
388                 break;
389             case TYPE_FLOAT :
390             case TYPE_DOUBLE :
391                 {
392                     float data;
393                     if ( gVector[j].get(&data) == false )
394                     {
395                         printf("Error : Not initialed.\n");
396                         return ;
397                     }
398                     printf("name=%s , type=%d, value=%f\n", name.c_str(), dataType, data );
399                 }
400                 break;
401         }
402     }
403 #endif
404
405     printf("Conversion_DataFormat() is Successful.\n");
406 }