• 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     // Do not allow repeated init
42     HI_ASSERT(!g_mainThrdId);
43 
44     g_mainThrdId = pthread_self();
45     SAMPLE_PRT("mainThrdId=%ld\n", (long)g_mainThrdId);
46 
47     if (cfgFilePath && *cfgFilePath) {
48         g_appCfg = iniparser_load(cfgFilePath);
49         if (!g_appCfg) {
50             SAMPLE_PRT("load '%s' FAIL, all config to default\n", cfgFilePath);
51         }
52     } else {
53         SAMPLE_PRT("no config file provided, all config to default\n");
54         g_appCfg = NULL;
55     }
56     SAMPLE_PRT("conf file init success\n");
57 
58     return 0;
59 }
60 
ConfBaseExt(void)61 void ConfBaseExt(void)
62 {
63     g_mainThrdId = 0;
64     if (g_appCfg) {
65         iniparser_freedict(g_appCfg);
66         g_appCfg = NULL;
67     }
68     SAMPLE_PRT("conf file exit success\n");
69 }
70 
71 /* Get the int type configuration item corresponding to the key */
GetCfgInt(const char * key,int defVal)72 int GetCfgInt(const char* key, int defVal)
73 {
74     HI_ASSERT(key && *key);
75     return g_appCfg ? iniparser_getint(g_appCfg, key, defVal) : defVal;
76 }
77 
78 /* Get the double configuration item corresponding to the key */
GetCfgDouble(const char * key,double defVal)79 double GetCfgDouble(const char* key, double defVal)
80 {
81     HI_ASSERT(key && *key);
82     return g_appCfg ? iniparser_getdouble(g_appCfg, key, defVal) : defVal;
83 }
84 
85 /* Get the string configuration corresponding to the key */
GetCfgStr(const char * key,const char * defVal)86 const char* GetCfgStr(const char* key, const char* defVal)
87 {
88     HI_ASSERT(key && *key);
89     return g_appCfg ? iniparser_getstring(g_appCfg, key, defVal) : defVal;
90 }
91 
92 /* Get the bool type configuration item corresponding to the key */
GetCfgBool(const char * key,bool defVal)93 bool GetCfgBool(const char* key, bool defVal)
94 {
95     static const size_t trueSize = 4;
96     static const size_t falseSize = 5;
97     const char *val;
98 
99     val = GetCfgStr(key, NULL);
100     if (val && *val) {
101         if (val[0] == '1' || val[0] == 'y' || val[0] == 'Y') {
102             return true;
103         } else if (val[0] == '0' || val[0] == 'n' || val[0] == 'N') {
104             return false;
105         } else if (strncmp(val, "true", trueSize) == 0) {
106             return true;
107         } else if (strncmp(val, "false", falseSize) == 0) {
108             return false;
109         } else {
110             return defVal;
111         }
112     } else {
113         return defVal;
114     }
115 }
116 
117 /* Get the int type configuration item corresponding to section+field */
SectGetCfgInt(const char * section,const char * field,int defVal)118 int SectGetCfgInt(const char* section, const char* field, int defVal)
119 {
120     HI_ASSERT(field && *field);
121     char key[CFG_KEY_MAX];
122 
123     if (snprintf_s(key, sizeof(key), sizeof(key) - 1, "%s:%s", (section ? section : ""), field) < 0) {
124         HI_ASSERT(0);
125     }
126     return GetCfgInt(key, defVal);
127 }
128 
129 /* Get the double configuration item corresponding to section+field */
SectGetCfgDouble(const char * section,const char * field,double defVal)130 double SectGetCfgDouble(const char* section, const char* field, double defVal)
131 {
132     HI_ASSERT(field && *field);
133     char key[CFG_KEY_MAX];
134 
135     if (snprintf_s(key, sizeof(key), sizeof(key) - 1, "%s:%s", (section ? section : ""), field) < 0) {
136         HI_ASSERT(0);
137     }
138     return GetCfgDouble(key, defVal);
139 }
140 
141 /* Get the bool type configuration item corresponding to section+field */
SectGetCfgBool(const char * section,const char * field,bool defVal)142 bool SectGetCfgBool(const char* section, const char* field, bool defVal)
143 {
144     HI_ASSERT(field && *field);
145     char key[CFG_KEY_MAX];
146 
147     if (snprintf_s(key, sizeof(key), sizeof(key) - 1, "%s:%s", (section ? section : ""), field) < 0) {
148         HI_ASSERT(0);
149     }
150     return GetCfgBool(key, defVal);
151 }
152 
153 /* Get the string configuration item corresponding to section+field */
SectGetCfgStr(const char * section,const char * field,const char * defVal)154 const char* SectGetCfgStr(const char* section, const char* field, const char* defVal)
155 {
156     HI_ASSERT(field && *field);
157     char key[CFG_KEY_MAX];
158 
159     if (snprintf_s(key, sizeof(key), sizeof(key) - 1, "%s:%s", (section ? section : ""), field) < 0) {
160         HI_ASSERT(0);
161     }
162     return GetCfgStr(key, defVal);
163 }
164 
165 /* strxfrm */
HiStrxfrm(char * s1,char * s2,int n)166 int HiStrxfrm(char *s1, char *s2, int n)
167 {
168     int i;
169 
170     for (i = 0; (i < n - 1) && s2[i]; i++) {
171         s1[i] = s2[i];
172     }
173     s1[i] = 0;
174     return i;
175 }
176 
177 #ifdef __cplusplus
178 #if __cplusplus
179 }
180 #endif
181 #endif /* End of #ifdef __cplusplus */
182