基于glist自定义自己的链表数据结构

项目中经常会需要将service产生的数据结构提供给调用方。

但是数据结构又不想暴露出去,因此要将需要用到的数据结构的处理都封装为API,提供给调用方。

 


=======================================================================
/* Define EXPORT_API */
#ifndef EXPORT_API
#define EXPORT_API __attribute__((visibility("default")))
#endif



EXPORT_API
int test_record_create(test_record_h *record)
{
	IFRET_VM(NULL == record, -1, "record is NULL");

	test_record *data = calloc(1, sizeof(test_record));
	IFRET_VM(NULL == data, -1, "calloc() Fail");
	data->date = NULL;
	data->key = NULL;
	data->value = NULL;
	*record = (test_record_h)data;

	return 0;
}

EXPORT_API
int test_record_destroy(test_record_h record)
{

	test_record *data = (test_record *)record;
	IFRET_VM(NULL == data, -1, "data is NULL");

	g_free(data->key);
	data->key = NULL;
	g_free(data->value);
	data->value = NULL;
	g_free(data->date);
	data->date = NULL;
	g_free(data);

	return 0;
}

EXPORT_API
int test_list_create(test_list_h *record_list)
{
	test_list *list_s = NULL;

	IFRET_VM(NULL == record_list, -1, "record_list is NULL");
	*record_list = NULL;

	list_s = calloc(1, sizeof(test_list));
	IFRET_VM(NULL == list_s, -1, "calloc() Fail");

	*record_list = (test_list_h)list_s;

	return 0;
}

EXPORT_API
int test_list_destroy(test_list_h record_list)
{

	test_list *s_list = NULL;
	GList *cursor = NULL;

	IFRET_VM(NULL == record_list, -1, "record_list is NULL");

	s_list = (test_list*)record_list;

	for (cursor = s_list->records; cursor; cursor = cursor->next)
		test_record_destroy((test_record_h)(cursor->data));

	g_list_free(s_list->records);
	g_free(s_list);

	return 0;
}

EXPORT_API
int test_record_set_key(test_record_h record, const char *key)
{

	test_record *data = (test_record *)record;
	IFRET_VM(NULL == data, -1, "data is NULL");
	IFRET_VM(NULL == key, -1, "key is NULL");

	data->key = g_strdup(key);

	return 0;
}




EXPORT_API
int test_record_get_key(test_record_h record, char **key)
{
	test_error_e 0 = 0;
	test_record *data = (test_record *)record;
	IFRET_VM(NULL == data, -1, "data is NULL");
	IFRET_VM(NULL == key, -1, "key is NULL");

	*key = data->key;

	return 0;
}



EXPORT_API
int test_list_add(test_list_h record_list, test_record_h record)
{
	test_list *s_list = NULL;
	test_record *s_record = NULL;

	IFRET_VM(NULL == record_list || NULL == record, -1, "record_list is NULL");
	s_list = (test_list*)record_list;
	s_record = (test_record*)record;


	s_list->records = g_list_append(s_list->records, s_record);

	if (s_list->count == 0) {
		s_list->cursor = s_list->records;
	}

	s_list->count++;

	return 0;
}

EXPORT_API
int test_list_remove(test_list_h record_list, test_record_h record)
{

	GList *cursor = NULL;
	test_list *s_list = NULL;
	test_record *s_record = NULL;

	IFRET_VM(NULL == record_list || NULL == record, -1, "record_list is NULL");
	s_list = (test_list*)record_list;
	s_record = (test_record*)record;


	for (cursor = s_list->records; cursor; cursor = cursor->next) {
		test_record *data = cursor->data;
		if (data == s_record) {
			s_list->count--;
			if (s_list->cursor == cursor) {
				s_list->cursor = cursor->next;
			}
			s_list->records = g_list_remove(s_list->records, s_record);
			return 0;
		}
	}

	return 0;
}


EXPORT_API
int test_list_get_record_count(test_list_h record_list, int *count)
{
	test_error_e 0 = 0;
	test_list *list_s = NULL;

	IFRET_VM(NULL == count, -1, "count  is NULL");
	*count = 0;

	IFRET_VM(NULL == record_list, -1, "record_list is NULL");
	list_s = (test_list*)record_list;

	*count = list_s->count;

	return 0;
}

EXPORT_API
int test_list_get_current_record(test_list_h record_list, test_record_h *record)
{
	test_error_e 0 = 0;
	test_list *list_s = NULL;

	IFRET_VM(NULL == record, -1, "record is NULL");
	*record = NULL;

	IFRET_VM(NULL == record_list || NULL == record, -1, "record_list is NULL");
	list_s = (test_list*)record_list;

	if (NULL == list_s->cursor) {
		*record = NULL;
		return -2;
	}

	*record = list_s->cursor->data;

	return 0;
}

EXPORT_API
int test_list_move_to_first(test_list_h record_list)
{
	test_error_e 0 = 0;
	test_list *list_s = NULL;

	IFRET_VM(NULL == record_list, -1, "record_list is NULL");
	list_s = (test_list*)record_list;

	list_s->cursor = list_s->records;
	if (NULL == list_s->cursor) {
		return -2;
	}

	return 0;
}

EXPORT_API
int test_list_move_to_last(test_list_h record_list)
{
	test_error_e 0 = 0;
	test_list *list_s;

	IFRET_VM(NULL == record_list, -1, "record_list is NULL");
	list_s = (test_list*)record_list;

	list_s->cursor = g_list_last(list_s->records);
	if (NULL == list_s->cursor) {
		return -2;
	}

	return 0;
}

static int __sc_list_move_cursor(test_list_h list, bool next)
{
	test_list *list_s;

	IFRET_VM(NULL == list, -1, "record_list is NULL");
	list_s = (test_list*)list;

	if (NULL == list_s->cursor)
		return -2;

	list_s->cursor = next ? list_s->cursor->next : list_s->cursor->prev ;

	if (NULL == list_s->cursor)
		return -2;

	return 0;
}

EXPORT_API
int test_list_move_to_next(test_list_h record_list)
{
	return __sc_list_move_cursor(record_list, true);

}

EXPORT_API
int test_list_move_to_prev(test_list_h record_list)
{
	return __sc_list_move_cursor(record_list, false);
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值