Ejemplo n.º 1
0
void
case_list_clear()
{
    struct list *list = list();
    char *s1 = "s1", *s2 = "s2", *s3 = "s3";
    assert(list_lpush(list, s1) == LIST_OK);
    assert(list_lpush(list, s2) == LIST_OK);
    assert(list_lpush(list, s3) == LIST_OK);
    assert(list_len(list) == 3);
    list_clear(list);
    assert(list_len(list) == 0);
    assert(list->head == NULL);
    assert(list->tail == NULL);
    list_free(list);
}
Ejemplo n.º 2
0
/******************************************************************************
 **函数名称: list_push_desc
 **功    能: 插入降序链表
 **输入参数:
 **     list: 单向链表
 **     data: 需要插入的数据
 **     cmp: 比较函数指针
 **输出参数: NONE
 **返    回: 0:成功 !0:失败
 **实现描述:
 **注意事项:
 **作    者: # Qifeng.zou # 2015-07-01 23:52:35 #
 ******************************************************************************/
int list_push_desc(list_t *list, void *data, cmp_cb_t cmp)
{
    int ret;
    list_node_t *curr, *node, *prev;

    if (NULL == list->head) {
        return list_lpush(list, data);
    }

    /* > 新建结点 */
    node = list->alloc(list->pool, sizeof(list_node_t));
    if (NULL == node) {
        return -1;
    }

    node->data = data;

    prev = NULL;
    curr=list->head;
    for (; NULL!=curr; curr=curr->next) {
        ret = cmp(data, curr->data);
        if ((0 == ret) || (ret > 0)) {
            return list_insert(list, prev, data); /* 插入PREV之后 */
        }
        prev = curr;
    }

    return list_insert(list, prev, data); /* 插入链尾 */
}
Ejemplo n.º 3
0
/******************************************************************************
 **函数名称: list_insert
 **功    能: 在PREV后插入结点
 **输入参数:
 **     list: 单向链表
 **     prev: 前一结点
 **     data: 数据
 **输出参数: NONE
 **返    回: 0:成功 !0:失败
 **实现描述:
 **注意事项:
 **     1. 如果结点prev为NULL, 则将结点node插入链表头
 **     2. 如果结点prev为tail, 则将结点node插入链表尾
 **     3. 如果结点prev不为NULL, 则将结点node插入prev后
 **作    者: # Qifeng.zou # 2014.09.24 #
 ******************************************************************************/
int list_insert(list_t *list, list_node_t *prev, void *data)
{
    list_node_t *node;

    /* > 插入链头或链尾 */
    if (NULL == prev) {
        return list_lpush(list, data);
    }
    else if (list->tail == prev) {
        return list_rpush(list, data);
    }

    /* > 新建结点 */
    node = list->alloc(list->pool, sizeof(list_node_t));
    if (NULL == node) {
        return -1;
    }

    node->data = data;

    /* > 插入链表 */
    node->next = prev->next;
    prev->next = node;
    ++list->num;
    return 0;
}
Ejemplo n.º 4
0
void
case_list_lpush()
{
    struct list *list = list();
    char *s1 = "s1", *s2 = "s2", *s3 = "s3";
    assert(list_lpush(list, s1) == LIST_OK);
    assert(list->head->data == s1);
    assert(list->tail->data == s1);
    assert(list_len(list) == 1);
    assert(list_lpush(list, s2) == LIST_OK);
    assert(list->head->data == s2);
    assert(list->tail->data == s1);
    assert(list_len(list) == 2);
    assert(list_lpush(list, s3) == LIST_OK);
    assert(list->head->data == s3);
    assert(list->tail->data == s1);
    assert(list_len(list) == 3);
    list_free(list);
}
Ejemplo n.º 5
0
list_t* parseServerUserGetUser(int sock,void* buf,list_t** outResult){
	uint16_t length = ntohs(*(uint16_t*)buf);
	buf += sizeof(uint16_t);
	char *txt = malloc(length);
	memcpy(txt,buf,length);
	buf += length;	
	cJSON* json = cJSON_Parse(txt);
	if(!json){
		free(txt);
		return NULL;
	}
	list_t* list = *outResult;
	if(list == NULL)
		list = list_new();

	int i = 0;
	for(i=0;i<cJSON_GetArraySize(json);i++){
		user_info_t* uinfo = malloc(sizeof(user_info_t));
		char* str = cJSON_GetObjectItem(json,"username")->valuestring;	
		if(str != NULL){	
			strncpy(uinfo->username,str,strlen(str));	
		}
		str = cJSON_GetObjectItem(json,"userid")->valuestring;	
		if(str != NULL){	
			strncpy(uinfo->userid,str,strlen(str));	
		}
		cJSON* arr = cJSON_GetObjectItem(json,"drivces");	
		uinfo->drivces = list_new();
		uinfo->drivces->free=free;		
		int i=0;
		for(i=0;i<cJSON_GetArraySize(arr);i++){
			str = cJSON_GetArrayItem(arr,i)->valuestring;
			char* drivce = malloc(strlen(str));
			memset(drivce,0,strlen(str));
			list_lpush(uinfo->drivces,list_node_new(drivce));
		}	
		list_lpush(list,list_node_new(uinfo));
	}	
	*outResult = list;
	free(txt);
	cJSON_Delete(json);
	return list;
}
Ejemplo n.º 6
0
void sendMessage(char* clientMessageid,int delytime,
							char* message,
							int messagetype,
							list_t* sendtoList){	
	send_message_t* ms = malloc(sizeof(send_message_t));
	ms->message = malloc(strlen(message));
	strcpy(ms->message,message);
	strcpy(ms->messageid,clientMessageid);
	ms->messagetype = messagetype;
	ms->delytime = delytime;
	ms->sendto = list_new();
	ms->sendto->free = free;
	int i = 0;
	for(i=0;i<sendtoList->len;i++){
		list_node_t* node = list_at(sendtoList,i);
		char* str = malloc(strlen(node->val));
		list_lpush(ms->sendto,list_node_new(str));
	}
	list_lpush(sendlist,list_node_new(ms));
	pthread_cond_signal(&queue_send);
	pthread_mutex_unlock(&send_lock);									
}
Ejemplo n.º 7
0
user_info_t* parseServerUserFindUser(int sock,void* buf,int* outResult){
	uint16_t length = ntohs(*(uint16_t*)buf);
	buf += sizeof(uint16_t);
	char *txt = malloc(length);
	memcpy(txt,buf,length);
	buf += length;	
	cJSON* json = cJSON_Parse(txt);
	if(!json){
		free(txt);
		return NULL;
	}	
	int isSucess = cJSON_GetObjectItem(json,"result")->valueint;			
	user_info_t* info = NULL;
	if(isSucess){	
		info = malloc(sizeof(user_info_t));			
		char* str = cJSON_GetObjectItem(json,"username")->valuestring;
		strncpy(info->username,str,strlen(str));	
		str = cJSON_GetObjectItem(json,"userid")->valuestring;	
		strncpy(info->userid,str,strlen(str));	
		//
		info->drivces = list_new();
		CJSON* arr = cJSON_GetObjectItem(json,"driveces");
		int i = 0;
		for(i=0;i<cJSON_GetArraySize(arr);i++){
			str = cJSON_GetArrayItem(arr,i)->valuestring;
			char* drivceId = malloc(strlen(str)+1);
			list_lpush(info->drivces,list_node_new(drivceId));
		}
		info->drivces->free = free;		
	}
	*outResult = isSucess;	
	/*
	int isSucess = cJSON_GetObjectItem(json,"result")->valueint;
	char* username = NULL;
	char* str = cJSON_GetObjectItem(json,"username")->valuestring;	
	if(str != NULL){
		username = malloc(strlen(str)+1);
		strncpy(username,str,strlen(str));	
	}	
	//free(str);
	//str = cJSON_GetObjectItem(json,"userid")->valuestring;	
		
	//char* userid = malloc(strlen(str)+1);
	*/
	*outResult = isSucess;	
	free(txt);
	cJSON_Delete(json);
	return info;//*outResult;
}
Ejemplo n.º 8
0
static void
test_list_lpush() {
  // Setup
  list_t *list = list_new();
  list_node_t *a = list_node_new("a");
  list_node_t *b = list_node_new("b");
  list_node_t *c = list_node_new("c");

  // c b a 
  list_rpush(list, a);
  list_lpush(list, b);
  list_lpush(list, c);

  // Assertions
  assert(list->head == c);
  assert(list->tail == a);
  assert(list->len == 3);
  assert(a->next == NULL);
  assert(a->prev == b);
  assert(b->next == a);
  assert(b->prev == c);
  assert(c->next == b);
  assert(c->prev == NULL);
}
Ejemplo n.º 9
0
int
json_array_unshift (json_array_t *self, json_value_t *value) {
  list_node_t *node = list_node_new(value);
  list_lpush(self->list, node);
  return (self->length = self->list->len);
}