[utils]: Add new option module using trie

Change-Id: I9c4836d35366ff8ab2f16ac9c474b65471862bd2
Signed-off-by: Herman Chen <herman.chen@rock-chips.com>
This commit is contained in:
Herman Chen
2021-12-16 10:47:46 +08:00
parent fdd12d9fb6
commit 94ab4c1e5f
3 changed files with 200 additions and 0 deletions

View File

@@ -2,10 +2,13 @@
# ----------------------------------------------------------------------------
# add libvpu implement
# ----------------------------------------------------------------------------
include_directories(${PROJECT_SOURCE_DIR}/mpp/base/inc)
add_library(utils STATIC
mpp_enc_roi_utils.c
mpi_enc_utils.c
mpi_dec_utils.c
mpp_opt.c
utils.c
iniparser.c
dictionary.c

143
utils/mpp_opt.c Normal file
View File

@@ -0,0 +1,143 @@
/*
* Copyright 2020 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define MODULE_TAG "mpp_opt"
#include "mpp_mem.h"
#include "mpp_log.h"
#include "mpp_trie.h"
#include "mpp_common.h"
#include "mpp_opt.h"
typedef struct MppOptImpl_t {
void *ctx;
MppTrie trie;
RK_S32 node_cnt;
RK_S32 info_cnt;
} MppOptImpl;
MPP_RET mpp_opt_init(MppOpt *opt)
{
MppOptImpl *impl = mpp_calloc(MppOptImpl, 1);
*opt = impl;
return (impl) ? MPP_OK : MPP_NOK;
}
MPP_RET mpp_opt_deinit(MppOpt opt)
{
MppOptImpl *impl = (MppOptImpl *)opt;
if (NULL == impl)
return MPP_NOK;
if (impl->trie) {
mpp_trie_deinit(impl->trie);
impl->trie = NULL;
}
MPP_FREE(impl);
return MPP_OK;
}
MPP_RET mpp_opt_setup(MppOpt opt, void *ctx, RK_S32 node_cnt, RK_S32 opt_cnt)
{
MppOptImpl *impl = (MppOptImpl *)opt;
if (NULL == impl)
return MPP_NOK;
mpp_trie_init(&impl->trie, node_cnt, opt_cnt);
if (impl->trie) {
impl->ctx = ctx;
impl->node_cnt = node_cnt;
impl->info_cnt = opt_cnt;
return MPP_OK;
}
mpp_err_f("failed to setup node %d opt %d\n", node_cnt, opt_cnt);
return MPP_NOK;
}
MPP_RET mpp_opt_add(MppOpt opt, MppOptInfo *info)
{
MppOptImpl *impl = (MppOptImpl *)opt;
if (NULL == impl || NULL == impl->trie)
return MPP_NOK;
if (NULL == info) {
RK_S32 node_cnt = mpp_trie_get_node_count(impl->trie);
RK_S32 info_cnt = mpp_trie_get_info_count(impl->trie);
if (impl->node_cnt != node_cnt || impl->info_cnt != info_cnt)
mpp_log("setup:real node %d:%d info %d:%d\n",
impl->node_cnt, node_cnt, impl->info_cnt, info_cnt);
return MPP_OK;
}
return mpp_trie_add_info(impl->trie, &info->name);
}
MPP_RET mpp_opt_parse(MppOpt opt, int argc, char **argv)
{
MppOptImpl *impl = (MppOptImpl *)opt;
MPP_RET ret = MPP_NOK;
RK_S32 opt_idx = 0;
if (NULL == impl || NULL == impl->trie || argc < 2 || NULL == argv)
return ret;
ret = MPP_OK;
while (opt_idx <= argc) {
RK_S32 opt_next = opt_idx + 1;
char *opts = argv[opt_idx++];
char *next = (opt_next >= argc) ? NULL : argv[opt_next];
if (NULL == opts)
break;
if (opts[0] == '-' && opts[1] != '\0') {
MppOptInfo *info = NULL;
const char **name = mpp_trie_get_info(impl->trie, opts + 1);
RK_S32 step = 0;
if (NULL == name) {
mpp_err("invalid option %s\n", opts + 1);
continue;
}
info = container_of(name, MppOptInfo, name);
if (info->proc)
step = info->proc(impl->ctx, next);
/* option failure or help */
if (step < 0) {
ret = step;
break;
}
opt_idx += step;
}
}
return ret;
}

54
utils/mpp_opt.h Normal file
View File

@@ -0,0 +1,54 @@
/*
* Copyright 2021 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_OPT_H__
#define __MPP_OPT_H__
#include "rk_type.h"
/*
* return zero or positive for option parser aproach step
* return negative for failure or option help
*/
typedef RK_S32 (*OptParser)(void *ctx, const char *next);
typedef struct MppOptInfo_t {
const char* name;
const char* full_name;
const char* help;
OptParser proc;
} MppOptInfo;
typedef void* MppOpt;
#ifdef __cplusplus
extern "C" {
#endif
MPP_RET mpp_opt_init(MppOpt *opt);
MPP_RET mpp_opt_deinit(MppOpt opt);
MPP_RET mpp_opt_setup(MppOpt opt, void *ctx, RK_S32 node_cnt, RK_S32 opt_cnt);
/* Add NULL info to mark end of options */
MPP_RET mpp_opt_add(MppOpt opt, MppOptInfo *info);
MPP_RET mpp_opt_parse(MppOpt opt, int argc, char **argv);
#ifdef __cplusplus
}
#endif
#endif /*__MPP_OPT_H__*/