mpp_opt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2020 Rockchip Electronics Co. LTD
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define MODULE_TAG "mpp_opt"
  17. #include "mpp_mem.h"
  18. #include "mpp_log.h"
  19. #include "mpp_trie.h"
  20. #include "mpp_common.h"
  21. #include "mpp_opt.h"
  22. typedef struct MppOptImpl_t {
  23. void *ctx;
  24. MppTrie trie;
  25. RK_S32 node_cnt;
  26. RK_S32 info_cnt;
  27. } MppOptImpl;
  28. MPP_RET mpp_opt_init(MppOpt *opt)
  29. {
  30. MppOptImpl *impl = mpp_calloc(MppOptImpl, 1);
  31. *opt = impl;
  32. return (impl) ? MPP_OK : MPP_NOK;
  33. }
  34. MPP_RET mpp_opt_deinit(MppOpt opt)
  35. {
  36. MppOptImpl *impl = (MppOptImpl *)opt;
  37. if (NULL == impl)
  38. return MPP_NOK;
  39. if (impl->trie) {
  40. mpp_trie_deinit(impl->trie);
  41. impl->trie = NULL;
  42. }
  43. MPP_FREE(impl);
  44. return MPP_OK;
  45. }
  46. MPP_RET mpp_opt_setup(MppOpt opt, void *ctx)
  47. {
  48. MppOptImpl *impl = (MppOptImpl *)opt;
  49. if (NULL == impl)
  50. return MPP_NOK;
  51. mpp_trie_init(&impl->trie, "mpp_opt");
  52. if (impl->trie) {
  53. impl->ctx = ctx;
  54. return MPP_OK;
  55. }
  56. return MPP_NOK;
  57. }
  58. MPP_RET mpp_opt_add(MppOpt opt, MppOptInfo *info)
  59. {
  60. MppOptImpl *impl = (MppOptImpl *)opt;
  61. if (NULL == impl || NULL == impl->trie)
  62. return MPP_NOK;
  63. if (NULL == info)
  64. return mpp_trie_add_info(impl->trie, NULL, NULL, 0);
  65. return mpp_trie_add_info(impl->trie, info->name, info, sizeof(*info));
  66. }
  67. MPP_RET mpp_opt_parse(MppOpt opt, int argc, char **argv)
  68. {
  69. MppOptImpl *impl = (MppOptImpl *)opt;
  70. MPP_RET ret = MPP_NOK;
  71. RK_S32 opt_idx = 0;
  72. if (NULL == impl || NULL == impl->trie || argc < 2 || NULL == argv)
  73. return ret;
  74. ret = MPP_OK;
  75. while (opt_idx <= argc) {
  76. RK_S32 opt_next = opt_idx + 1;
  77. char *opts = argv[opt_idx++];
  78. char *next = (opt_next >= argc) ? NULL : argv[opt_next];
  79. if (NULL == opts)
  80. break;
  81. if (opts[0] == '-' && opts[1] != '\0') {
  82. MppOptInfo *info = NULL;
  83. MppTrieInfo *node = mpp_trie_get_info(impl->trie, opts + 1);
  84. RK_S32 step = 0;
  85. if (NULL == node) {
  86. mpp_err("invalid option %s\n", opts + 1);
  87. continue;
  88. }
  89. info = mpp_trie_info_ctx(node);
  90. if (info->proc)
  91. step = info->proc(impl->ctx, next);
  92. /* option failure or help */
  93. if (step < 0) {
  94. ret = step;
  95. break;
  96. }
  97. opt_idx += step;
  98. }
  99. }
  100. return ret;
  101. }