1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef OH_COMMONEVENT_PARAMETERS_PARSE_H
16 #define OH_COMMONEVENT_PARAMETERS_PARSE_H
17
18 #include "oh_commonevent_wrapper.h"
19 #include "securec.h"
20 #include "stdlib.h"
21 #include <string>
22 namespace OHOS {
23 namespace EventFwk {
24
25 const int8_t I32_TYPE = 0;
26 const int8_t DOUBLE_TYPE = 1;
27 const int8_t STR_TYPE = 2;
28 const int8_t BOOL_TYPE = 3;
29 const int8_t I64_TYPE = 4;
30 const int8_t CHAR_TYPE = 5;
31 const int8_t I32_PTR_TYPE = 6;
32 const int8_t I64_PTR_TYPE = 7;
33 const int8_t BOOL_PTR_TYPE = 8;
34 const int8_t DOUBLE_PTR_TYPE = 9;
35
36 char *MallocCString(const std::string &origin);
37 int32_t GetCommonEventData(const CommonEventData &data, CommonEvent_RcvData* cData);
38 void FreeCCommonEventDataCharPtr(CommonEvent_RcvData* cData);
39 void FreeCCommonEventData(CommonEvent_RcvData* cData);
40 bool HasKeyFromParameters(const CArrParameters* parameters, const char* key);
41
42 template <class T>
GetDataFromParams(const CArrParameters * parameters,const char * key,int8_t type,T defaultVal)43 T GetDataFromParams(const CArrParameters* parameters, const char* key, int8_t type, T defaultVal)
44 {
45 if (parameters->head == nullptr) {
46 return defaultVal;
47 }
48 for (int i = 0; i < parameters->size; i++) {
49 CParameters *it = parameters->head + i;
50 if (it->valueType == type && std::strcmp(it->key, key) == 0) {
51 return *(static_cast<T *>(it->value));
52 }
53 }
54 return defaultVal;
55 }
56
57 template <class T>
GetDataArrayFromParams(const CArrParameters * parameters,const char * key,int8_t type,T ** array)58 int32_t GetDataArrayFromParams(const CArrParameters* parameters, const char* key, int8_t type, T** array)
59 {
60 if (parameters->head == nullptr) {
61 return 0;
62 }
63 for (int i = 0; i < parameters->size; i++) {
64 CParameters *it = parameters->head + i;
65 if (it->valueType == type && std::strcmp(it->key, key) == 0) {
66 *array = static_cast<T *>(it->value);
67 return it->size;
68 }
69 }
70 return 0;
71 }
72 } // namespace EventFwk
73 } // namespace OHOS
74 #endif