PageRenderTime 34ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ftk_pairs.c

http://ftk.googlecode.com/
C | 167 lines | 114 code | 24 blank | 29 comment | 38 complexity | 7224fd10d13c442aa72657552cbd3c84 MD5 | raw file
  1/*
  2 * File: ftk_pairs.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:  key-value 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-25 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_allocator.h"
 33#include "ftk_log.h"
 34#include "ftk_pairs.h"
 35
 36struct _FtkPairs
 37{
 38	size_t nr;
 39	FtkCompare compare;
 40	FtkKeyValue pairs[ZERO_LEN_ARRAY];
 41};
 42
 43FtkPairs* ftk_pairs_create(size_t nr, FtkCompare compare)
 44{
 45	FtkPairs* thiz = NULL;
 46	return_val_if_fail(nr > 0 && compare != NULL, NULL);
 47
 48	thiz = (FtkPairs*)FTK_ZALLOC(sizeof(FtkPairs) + nr * sizeof(FtkKeyValue));
 49	if(thiz != NULL)
 50	{
 51		thiz->nr = nr;
 52		thiz->compare = compare;
 53	}
 54
 55	return thiz;
 56}
 57
 58Ret ftk_pairs_remove(FtkPairs* thiz, const char* key)
 59{
 60	size_t i = 0;
 61	Ret ret = RET_FAIL;
 62	return_val_if_fail(thiz != NULL && key != NULL, RET_FAIL);
 63
 64	for(i = 0; i < thiz->nr; i++)
 65	{
 66		if(thiz->compare(key, thiz->pairs[i].key) == 0)
 67		{
 68			memset(thiz->pairs+i, 0x00, sizeof(FtkKeyValue));
 69			ret = RET_OK;
 70			break;
 71		}
 72	}
 73
 74	return ret;
 75}
 76
 77Ret ftk_pairs_set(FtkPairs* thiz, const char* key, const char* value)
 78{
 79	size_t i = 0;
 80	Ret ret = RET_FAIL;
 81	return_val_if_fail(thiz != NULL && key != NULL, RET_FAIL);
 82
 83	for(i = 0; i < thiz->nr; i++)
 84	{
 85		if(thiz->compare(key, thiz->pairs[i].key) == 0)
 86		{
 87			ftk_strncpy(thiz->pairs[i].value, value, FTK_VALUE_LEN);
 88			thiz->pairs[i].value[FTK_VALUE_LEN] = '\0';
 89			ret = RET_OK;
 90			break;
 91		}
 92	}
 93
 94	if(ret != RET_OK)
 95	{
 96		ret = ftk_pairs_add(thiz, key, value);
 97	}
 98
 99	return ret;
100}
101
102Ret ftk_pairs_add(FtkPairs* thiz, const char* key, const char* value)
103{
104	size_t i = 0;
105	Ret ret = RET_FAIL;
106	return_val_if_fail(thiz != NULL && key != NULL && value != NULL, RET_FAIL);
107
108	for(i = 0; i < thiz->nr; i++)
109	{
110		if(thiz->pairs[i].key[0] == '\0')
111		{
112			ret = RET_OK;
113			ftk_strncpy(thiz->pairs[i].key, key, FTK_KEY_LEN);
114			ftk_strncpy(thiz->pairs[i].value, value, FTK_VALUE_LEN);
115			thiz->pairs[i].key[FTK_KEY_LEN] = '\0';
116			thiz->pairs[i].value[FTK_VALUE_LEN] = '\0';
117			break;
118		}
119	}
120
121	return ret;
122}
123
124const char*  ftk_pairs_find(FtkPairs* thiz, const char* key)
125{
126	size_t i = 0;
127	const char* value = NULL;
128	return_val_if_fail(thiz != NULL && key != NULL, NULL);
129
130	for(i = 0; i < thiz->nr; i++)
131	{
132		if(thiz->compare(key, thiz->pairs[i].key) == 0)
133		{
134			value = thiz->pairs[i].value;
135			break;
136		}
137	}
138
139	return value;
140}
141
142void ftk_pairs_dump(FtkPairs* thiz)
143{
144	size_t i = 0;
145	if(thiz != NULL)
146	{
147		for(i = 0; i < thiz->nr; i++)
148		{
149			if(thiz->pairs[i].key[0])
150			{
151				ftk_logd("%s=%s\n", thiz->pairs[i].key, thiz->pairs[i].value);
152			}
153		}
154	}
155}
156
157void ftk_pairs_destroy(FtkPairs* thiz)
158{
159	if(thiz != NULL)
160	{
161		FTK_FREE(thiz);
162	}
163
164	return;
165}
166
167