/src/ftk_params.c

http://ftk.googlecode.com/ · C · 167 lines · 109 code · 29 blank · 29 comment · 31 complexity · a434554e06450208aae1611677b2b54f MD5 · raw file

  1. /*
  2. * File: ftk_param.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: params manager
  5. *
  6. * Copyright (c) 2009 - 2011 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2011-03-23 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_allocator.h"
  31. #include "ftk_log.h"
  32. #include "ftk_util.h"
  33. #include "ftk_expr.h"
  34. #include "ftk_pairs.h"
  35. #include "ftk_params.h"
  36. struct _FtkParams
  37. {
  38. FtkPairs* vars;
  39. FtkPairs* params;
  40. char eval[FTK_VALUE_LEN << 2];
  41. };
  42. FtkParams* ftk_params_create(int max_params_nr, int max_vars_nr)
  43. {
  44. FtkParams* thiz = (FtkParams*)FTK_ZALLOC(sizeof(FtkParams));
  45. if(thiz != NULL)
  46. {
  47. thiz->vars = ftk_pairs_create(max_vars_nr, (FtkCompare)strcmp);
  48. thiz->params = ftk_pairs_create(max_params_nr, (FtkCompare)strcmp);
  49. }
  50. return thiz;
  51. }
  52. Ret ftk_params_set_param(FtkParams* thiz, const char* name, const char* value)
  53. {
  54. return_val_if_fail(thiz != NULL && name != NULL && value != NULL, RET_FAIL);
  55. return ftk_pairs_set(thiz->params, name, value);
  56. }
  57. Ret ftk_params_set_var(FtkParams* thiz, const char* name, const char* value)
  58. {
  59. return_val_if_fail(thiz != NULL && name != NULL && value != NULL, RET_FAIL);
  60. return ftk_pairs_set(thiz->vars, name, value);
  61. }
  62. int ftk_params_eval_int(FtkParams* thiz, const char* name, int defval)
  63. {
  64. int ret = defval;
  65. const char* value = ftk_params_eval_string(thiz, name);
  66. if(value != NULL)
  67. {
  68. ret = (int)ftk_expr_eval(value);
  69. }
  70. return ret;
  71. }
  72. float ftk_params_eval_float(FtkParams* thiz, const char* name, float defval)
  73. {
  74. float ret = defval;
  75. const char* value = ftk_params_eval_string(thiz, name);
  76. if(value != NULL)
  77. {
  78. ret = (float)ftk_expr_eval(value);
  79. }
  80. return ret;
  81. }
  82. #define FTK_IS_ID_CHAR(c) (isalpha(c) || isdigit(c))
  83. const char* ftk_params_eval_string(FtkParams* thiz, const char* name)
  84. {
  85. const char* value = ftk_pairs_find(thiz->params, name);
  86. if(value != NULL && strstr(value, "$") != NULL)
  87. {
  88. int i = 0;
  89. int dst = 0;
  90. char var[FTK_KEY_LEN+1] = {0};
  91. for(i = 0; value[i]; i++)
  92. {
  93. if(value[i] != '$')
  94. {
  95. thiz->eval[dst++] = value[i];
  96. }
  97. else
  98. {
  99. int j = 0;
  100. const char* var_value = NULL;
  101. for(i = i + 1; FTK_IS_ID_CHAR(value[i]); i++, j++)
  102. {
  103. var[j] = value[i];
  104. }
  105. i--;
  106. var[j] = '\0';
  107. var_value = ftk_pairs_find(thiz->vars, var);
  108. if(var_value != NULL)
  109. {
  110. ftk_strcpy(thiz->eval+dst, var_value);
  111. dst += strlen(var_value);
  112. }
  113. }
  114. }
  115. value = thiz->eval;
  116. }
  117. return value;
  118. }
  119. void ftk_params_dump(FtkParams* thiz)
  120. {
  121. if(thiz != NULL)
  122. {
  123. ftk_logd("-------------param------------------\n");
  124. ftk_pairs_dump(thiz->params);
  125. ftk_logd("-------------var------------------\n");
  126. ftk_pairs_dump(thiz->vars);
  127. }
  128. }
  129. void ftk_params_destroy(FtkParams* thiz)
  130. {
  131. if(thiz != NULL)
  132. {
  133. ftk_pairs_destroy(thiz->params);
  134. ftk_pairs_destroy(thiz->vars);
  135. FTK_FREE(thiz);
  136. }
  137. return;
  138. }