PageRenderTime 97ms CodeModel.GetById 39ms app.highlight 18ms RepoModel.GetById 22ms app.codeStats 0ms

/services/fconf/fconf.c

http://ftk.googlecode.com/
C | 133 lines | 89 code | 13 blank | 31 comment | 65 complexity | 1aa0e5e6df7c887d69d975a3d0e6a672 MD5 | raw file
  1/*
  2 * File: fconf.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   common used functions for FConf interface.
  5 *
  6 * Copyright (c) 2009 - 2010  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 * 2010-08-01 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31#include "fconf.h"
 32
 33static Ret on_changed(void* ctx, int change_by_self, FConfChangeType type, const char* xpath, const char* value)
 34{
 35	ftk_logd("%s: %d %x %s %s\n", __func__, change_by_self, type, xpath, value);
 36
 37	return RET_OK;
 38}
 39
 40Ret fconf_test(FConf* thiz)
 41{
 42	int i = 0;
 43	int j = 0;
 44	int k = 0;
 45	int n = 0;
 46	char* value = NULL;
 47	char* child = NULL;
 48	char name[32] = {0};
 49
 50	fconf_reg_changed_notify(thiz, on_changed, NULL);
 51	assert(fconf_set(thiz, "/a/b/cc/name1", "1") == RET_OK);
 52	assert(fconf_set(thiz, "/a/b/cc/name2", "2") == RET_OK);
 53	assert(fconf_set(thiz, "/a/b/cc/name3", "2") == RET_OK);
 54	assert(fconf_get(thiz, "/a/b/cc/name3", &value) == RET_OK);
 55	assert(strcmp(value, "2") == 0);
 56	assert(fconf_set(thiz, "/a/b/cc/name3", "3") == RET_OK);
 57	assert(fconf_get(thiz, "/a/b/cc/name1", &value) == RET_OK);
 58	assert(strcmp(value, "1") == 0);
 59	assert(fconf_get(thiz, "/a/b/cc/name2", &value) == RET_OK);
 60	assert(strcmp(value, "2") == 0);
 61	assert(fconf_get(thiz, "/a/b/cc/name3", &value) == RET_OK);
 62	assert(strcmp(value, "3") == 0);
 63	
 64	assert(fconf_set(thiz, "/a/b/dd/name1", "1") == RET_OK);
 65	assert(fconf_set(thiz, "/a/b/dd/name2", "2") == RET_OK);
 66	assert(fconf_set(thiz, "/a/b/dd/name3", "3") == RET_OK);
 67	assert(fconf_get(thiz, "/a/b/dd/name1", &value) == RET_OK);
 68	assert(strcmp(value, "1") == 0);
 69	assert(fconf_get(thiz, "/a/b/dd/name2", &value) == RET_OK);
 70	assert(strcmp(value, "2") == 0);
 71	assert(fconf_get(thiz, "/a/b/dd/name3", &value) == RET_OK);
 72	assert(strcmp(value, "3") == 0);
 73	
 74	assert(fconf_set(thiz, "/b/b/dd/name1", "1") == RET_OK);
 75	assert(fconf_set(thiz, "/b/b/dd/name2", "2") == RET_OK);
 76	assert(fconf_set(thiz, "/b/b/dd/name3", "3") == RET_OK);
 77
 78	assert(fconf_get_child_count(thiz, "/a/", &n) == RET_OK && n == 1);
 79	assert(fconf_get_child_count(thiz, "/a/b", &n) == RET_OK && n == 2);
 80	assert(fconf_get_child_count(thiz, "/a/b/cc", &n) == RET_OK && n == 3);
 81
 82	//fconf_reg_changed_notify(thiz, NULL, NULL);
 83	for(i = 0; i < n; i++)
 84	{
 85		snprintf(name, sizeof(name)-1, "name%d", i+1);
 86		assert(fconf_get_child(thiz, "/a/b/cc", i, &child) == RET_OK);
 87		assert(strcmp(name, child) == 0);
 88	}
 89	assert(fconf_get_child_count(thiz, "/a/b/dd", &n) == RET_OK && n == 3);
 90	for(i = 0; i < n; i++)
 91	{
 92		snprintf(name, sizeof(name)-1, "name%d", i+1);
 93		assert(fconf_get_child(thiz, "/a/b/dd", i, &child) == RET_OK);
 94		assert(strcmp(name, child) == 0);
 95	}
 96	assert(fconf_remove(thiz, "/a/b/dd/name") != RET_OK);
 97	assert(fconf_remove(thiz, "/a/b/dd/name1") == RET_OK);
 98	assert(fconf_get_child_count(thiz, "/a/b/dd", &n) == RET_OK && n == 2);
 99	assert(fconf_remove(thiz, "/a/b/dd/name2") == RET_OK);
100	assert(fconf_get_child_count(thiz, "/a/b/dd", &n) == RET_OK && n == 1);
101	assert(fconf_remove(thiz, "/a/b/dd") == RET_OK);
102	assert(fconf_get_child_count(thiz, "/a/b/dd", &n) != RET_OK);
103	
104	assert(fconf_remove(thiz, "/a/b") == RET_OK);
105	assert(fconf_get_child_count(thiz, "/a/b", &n) != RET_OK);
106
107	assert(fconf_remove(thiz, "/a") == RET_OK);
108	assert(fconf_get_child_count(thiz, "/a", &n) != RET_OK);
109
110	int times = 10;
111	for(i = 0; i < times; i++)
112	{
113		for(j = 0; j < times; j++)
114		{
115			for(k = 0; k < times; k++)
116			{
117				snprintf(name, sizeof(name)-1, "/%d/%04d/%08d", i, j, k);
118				assert(fconf_set(thiz, name, name) == RET_OK);
119			}
120			snprintf(name, sizeof(name)-1, "/%d/%04d", i, j);
121			assert(fconf_get_child_count(thiz, name, &n) == RET_OK);
122			assert(n == times);
123			for(k = 0; k < n; k++)
124			{
125				snprintf(name, sizeof(name)-1, "/%d/%04d", i, j);
126				assert(fconf_get_child(thiz, name, k, &child) == RET_OK);
127			}
128		}
129	}
130	//fconf_reg_changed_notify(thiz, on_changed, NULL);
131
132	return RET_OK;
133}