PageRenderTime 422ms CodeModel.GetById 120ms app.highlight 11ms RepoModel.GetById 100ms app.codeStats 0ms

/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/*
 26 * History:
 27 * ================================================================
 28 * 2011-03-23 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_allocator.h"
 33#include "ftk_log.h"
 34#include "ftk_util.h"
 35#include "ftk_expr.h"
 36#include "ftk_pairs.h"
 37#include "ftk_params.h"
 38
 39struct _FtkParams
 40{
 41	FtkPairs* vars;
 42	FtkPairs* params;
 43	char eval[FTK_VALUE_LEN << 2];
 44};
 45
 46FtkParams* ftk_params_create(int max_params_nr, int max_vars_nr)
 47{
 48	FtkParams* thiz = (FtkParams*)FTK_ZALLOC(sizeof(FtkParams));
 49
 50	if(thiz != NULL)
 51	{
 52		thiz->vars = ftk_pairs_create(max_vars_nr, (FtkCompare)strcmp);
 53		thiz->params = ftk_pairs_create(max_params_nr, (FtkCompare)strcmp);
 54	}
 55
 56	return thiz; 
 57}
 58
 59Ret ftk_params_set_param(FtkParams* thiz, const char* name, const char* value)
 60{
 61	return_val_if_fail(thiz != NULL && name != NULL && value != NULL, RET_FAIL);
 62
 63	return ftk_pairs_set(thiz->params, name, value);
 64}
 65
 66Ret ftk_params_set_var(FtkParams* thiz, const char* name, const char* value)
 67{
 68	return_val_if_fail(thiz != NULL && name != NULL && value != NULL, RET_FAIL);
 69
 70	return ftk_pairs_set(thiz->vars, name, value);
 71}
 72
 73int ftk_params_eval_int(FtkParams* thiz, const char* name, int defval)
 74{
 75	int ret = defval;
 76	const char* value = ftk_params_eval_string(thiz, name);
 77
 78	if(value != NULL)
 79	{
 80		ret = (int)ftk_expr_eval(value);
 81	}
 82
 83	return ret;
 84}
 85
 86float ftk_params_eval_float(FtkParams* thiz, const char* name, float defval)
 87{
 88	float ret = defval;
 89	const char* value = ftk_params_eval_string(thiz, name);
 90
 91	if(value != NULL)
 92	{
 93		ret = (float)ftk_expr_eval(value);
 94	}
 95
 96	return ret;
 97}
 98
 99#define FTK_IS_ID_CHAR(c) (isalpha(c) || isdigit(c))
100const char* ftk_params_eval_string(FtkParams* thiz, const char* name)
101{
102	const char* value = ftk_pairs_find(thiz->params, name);
103
104	if(value != NULL && strstr(value, "$") != NULL)
105	{
106		int i = 0;
107		int dst = 0;
108		char var[FTK_KEY_LEN+1] = {0};
109
110		for(i = 0; value[i]; i++)
111		{
112			if(value[i] != '$')
113			{
114				thiz->eval[dst++] = value[i];
115			}
116			else
117			{
118				int j = 0;
119				const char* var_value = NULL;
120				for(i = i + 1; FTK_IS_ID_CHAR(value[i]); i++, j++)
121				{
122					var[j] = value[i];
123				}
124
125				i--;
126				var[j] = '\0';
127
128				var_value = ftk_pairs_find(thiz->vars, var);
129				if(var_value != NULL)
130				{
131					ftk_strcpy(thiz->eval+dst, var_value);
132					dst += strlen(var_value);
133				}
134			}
135		}
136
137		value = thiz->eval;
138	}
139
140	return value;
141}
142
143void ftk_params_dump(FtkParams* thiz)
144{
145	if(thiz != NULL)
146	{
147		ftk_logd("-------------param------------------\n");
148		ftk_pairs_dump(thiz->params);
149		ftk_logd("-------------var------------------\n");
150		ftk_pairs_dump(thiz->vars);
151	}
152}
153
154void ftk_params_destroy(FtkParams* thiz)
155{
156	if(thiz != NULL)
157	{
158		ftk_pairs_destroy(thiz->params);
159		ftk_pairs_destroy(thiz->vars);
160
161		FTK_FREE(thiz);
162	}
163
164	return;
165}
166
167