• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <limits.h>
22 #include <unistd.h>
23 #include <signal.h>
24 
25 #include "iniparser.h"
26 #include "sample_media_ai.h"
27 
28 #ifdef __cplusplus
29 #if __cplusplus
30 extern "C" {
31 #endif
32 #endif /* End of #ifdef __cplusplus */
33 
34 #define CFG_KEY_MAX         256 // Maximum length of config key buf
35 
36 static dictionary *g_appCfg = NULL; // Global configuration
37 static pthread_t g_mainThrdId = 0; // Main thread ID, the thread that called app_base_init()
38 
ConfBaseInit(const char * cfgFilePath)39 HI_S32 ConfBaseInit(const char* cfgFilePath)
40 {
41     /*
42      * 不允许重复初始化
43      * Do not allow repeated init
44      */
45     HI_ASSERT(!g_mainThrdId);
46 
47     g_mainThrdId = pthread_self();
48     SAMPLE_PRT("mainThrdId=%ld\n", (long)g_mainThrdId);
49 
50     if (cfgFilePath && *cfgFilePath) {
51         g_appCfg = iniparser_load(cfgFilePath);
52         if (!g_appCfg) {
53             SAMPLE_PRT("load '%s' FAIL, all config to default\n", cfgFilePath);
54         }
55     } else {
56         SAMPLE_PRT("no config file provided, all config to default\n");
57         g_appCfg = NULL;
58     }
59     SAMPLE_PRT("conf file init success\n");
60 
61     return 0;
62 }
63 
ConfBaseExt(void)64 void ConfBaseExt(void)
65 {
66     g_mainThrdId = 0;
67     if (g_appCfg) {
68         iniparser_freedict(g_appCfg);
69         g_appCfg = NULL;
70     }
71     SAMPLE_PRT("conf file exit success\n");
72 }
73 
74 /*
75  * 获取key对应的int类型配置项
76  * Get the int type configuration item corresponding to the key
77  */
GetCfgInt(const char * key,int defVal)78 int GetCfgInt(const char* key, int defVal)
79 {
80     HI_ASSERT(key && *key);
81     return g_appCfg ? iniparser_getint(g_appCfg, key, defVal) : defVal;
82 }
83 
84 /*
85  * 获取key对应的double类型配置项
86  * Get the double configuration item corresponding to the key
87  */
GetCfgDouble(const char * key,double defVal)88 double GetCfgDouble(const char* key, double defVal)
89 {
90     HI_ASSERT(key && *key);
91     return g_appCfg ? iniparser_getdouble(g_appCfg, key, defVal) : defVal;
92 }
93 
94 /*
95  * 获取key对应的string类型配置项
96  * Get the string configuration corresponding to the key
97  */
GetCfgStr(const char * key,const char * defVal)98 const char* GetCfgStr(const char* key, const char* defVal)
99 {
100     HI_ASSERT(key && *key);
101     return g_appCfg ? iniparser_getstring(g_appCfg, key, defVal) : defVal;
102 }
103 
104 /*
105  * 获取key对应的bool类型配置项
106  * Get the bool type configuration item corresponding to the key
107  */
GetCfgBool(const char * key,bool defVal)108 bool GetCfgBool(const char* key, bool defVal)
109 {
110     static const size_t trueSize = 4;
111     static const size_t falseSize = 5;
112     const char *val;
113 
114     val = GetCfgStr(key, NULL);
115     if (val && *val) {
116         if (val[0] == '1' || val[0] == 'y' || val[0] == 'Y') {
117             return true;
118         } else if (val[0] == '0' || val[0] == 'n' || val[0] == 'N') {
119             return false;
120         } else if (strncmp(val, "true", trueSize) == 0) {
121             return true;
122         } else if (strncmp(val, "false", falseSize) == 0) {
123             return false;
124         } else {
125             return defVal;
126         }
127     } else {
128         return defVal;
129     }
130 }
131 
132 /*
133  * 获取section+field对应的int类型配置项
134  * Get the int type configuration item corresponding to section+field
135  */
SectGetCfgInt(const char * section,const char * field,int defVal)136 int SectGetCfgInt(const char* section, const char* field, int defVal)
137 {
138     HI_ASSERT(field && *field);
139     char key[CFG_KEY_MAX];
140 
141     if (snprintf_s(key, sizeof(key), sizeof(key) - 1, "%s:%s", (section ? section : ""), field) < 0) {
142         HI_ASSERT(0);
143     }
144     return GetCfgInt(key, defVal);
145 }
146 
147 /*
148  * 获取section+field对应的double类型配置项
149  * Get the double configuration item corresponding to section+field
150  */
SectGetCfgDouble(const char * section,const char * field,double defVal)151 double SectGetCfgDouble(const char* section, const char* field, double defVal)
152 {
153     HI_ASSERT(field && *field);
154     char key[CFG_KEY_MAX];
155 
156     if (snprintf_s(key, sizeof(key), sizeof(key) - 1, "%s:%s", (section ? section : ""), field) < 0) {
157         HI_ASSERT(0);
158     }
159     return GetCfgDouble(key, defVal);
160 }
161 
162 /*
163  * 获取section+field对应的bool类型配置项
164  * Get the bool type configuration item corresponding to section+field
165  */
SectGetCfgBool(const char * section,const char * field,bool defVal)166 bool SectGetCfgBool(const char* section, const char* field, bool defVal)
167 {
168     HI_ASSERT(field && *field);
169     char key[CFG_KEY_MAX];
170 
171     if (snprintf_s(key, sizeof(key), sizeof(key) - 1, "%s:%s", (section ? section : ""), field) < 0) {
172         HI_ASSERT(0);
173     }
174     return GetCfgBool(key, defVal);
175 }
176 
177 /*
178  * 获取section+field对应的string类型配置项
179  * Get the string configuration item corresponding to section+field
180  */
SectGetCfgStr(const char * section,const char * field,const char * defVal)181 const char* SectGetCfgStr(const char* section, const char* field, const char* defVal)
182 {
183     HI_ASSERT(field && *field);
184     char key[CFG_KEY_MAX];
185 
186     if (snprintf_s(key, sizeof(key), sizeof(key) - 1, "%s:%s", (section ? section : ""), field) < 0) {
187         HI_ASSERT(0);
188     }
189     return GetCfgStr(key, defVal);
190 }
191 
192 /*
193  * 字符处理接口
194  * Character processing interface
195  */
HiStrxfrm(char * s1,char * s2,int n)196 int HiStrxfrm(char *s1, char *s2, int n)
197 {
198     int i;
199 
200     for (i = 0; (i < n - 1) && s2[i]; i++) {
201         s1[i] = s2[i];
202     }
203     s1[i] = 0;
204     return i;
205 }
206 
207 #ifdef __cplusplus
208 #if __cplusplus
209 }
210 #endif
211 #endif /* End of #ifdef __cplusplus */
212