c中文件列表加载和排序

记录项目中用到的一点小东西

结构体定义

#define MAX_NAME_LENTH 64

struct list_head
{
    struct list_head *next;
    struct list_head *prev;
};

struct dir_list_head{
	struct list_head video_dir_head;//video文件链表头
	struct list_head audio_dir_head;//audio文件链表头
	struct list_head image_dir_head;//image文件链表头
	struct list_head face_dir_head;//image文件链表头
};

typedef struct _file_list{
	
	char file_name[MAX_NAME_LENTH];
	struct list_head list_node;
	
}file_list_t,*p_file_list_t;

typedef struct _dir_list{
	
	char dir_name[MAX_NAME_LENTH];
	struct list_head file_list_head;
	struct list_head list_node;
	
}dir_list_t,*p_dir_list_t;

头文件.h

void list_head_init(struct list_head *list);
void list_add_prev(struct list_head *new, struct list_head *pos);
void list_add_next(struct list_head *new, struct list_head *pos);
void list_add_tail(struct list_head *_new, struct list_head *head);
void list_del(struct list_head *entry);

#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

#ifndef container_of
#define container_of(ptr, type, member) ({                      \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

#endif

/**
 * list_entry - get the struct for this entry
 * @ptr:    the &struct list_head pointer.
 * @type:    the type of the struct this is embedded in.
 * @member:    the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:    the &struct list_head to use as a loop cursor.
 * @n:        another &struct list_head to use as temporary storage
 * @head:    the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
        pos = n, n = pos->next)

#endif //_LIST_H_

#define list_for_each_entry(pos, head, member)                \
    for (pos = list_entry((head)->next, typeof(*pos), member);    \
         &pos->member != (head);     \
         pos = list_entry(pos->member.next, typeof(*pos), member))

list.c

#include "list.h"
#include <stdio.h>

static void __list_add(struct list_head *_new, struct list_head *prev, struct list_head *next)
{
    next->prev = _new;
    _new->next = next;
    _new->prev = prev;
    prev->next = _new;
}

static void __list_del(struct list_head *prev, struct list_head *next)
{
    next->prev = prev;
    prev->next = next;
}

void list_head_init(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

/**
 * list_add_tail - insert a new entry before the specified head
 * @_new: new entry to be added
 * @head: list pos to add it before
 */

void list_add_prev(struct list_head *new, struct list_head *pos)
{
    __list_add(new, pos->prev, pos);

}

/**
 * list_add_tail - insert a new entry before the specified head
 * @_new: new entry to be added
 * @head: list pos to add it behind
 */

void list_add_next(struct list_head *new, struct list_head *pos)
{
    __list_add(new, pos, pos->next);

}

/**
 * list_add_tail - insert a new entry before the specified head
 * @_new: new entry to be added
 * @head: list head to add it before
 */
void list_add_tail(struct list_head *_new, struct list_head *head)
{
    __list_add(_new, head->prev, head);
}

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 */
void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = NULL;
    entry->prev = NULL;
}

文件排序

文件夹基于xxxx_20200401_xxx  和 xxxx_20210305

文件排序名字基于xxxxxx_20200401_013013_xxx.xxx和xxxxxx_20200401_013013.xxx(可自行根据需求,更改代码strcmp)

//parma1:字符串
//parma2:需要查找的字符
//parma3:需要查找第几个字符 正数从左到右,负数从右到左
char *SearchStrChr(char *str,char ch,int count)
{
    char *p = str;

    unsigned int num = abs(count);
    if(count < 0){
        int i = 0;
        p = strrchr(p,ch);
        if(!p)
            return NULL;
        while(1){
            i++;
            if(i >= num)
                return p;
            for(;;){
                p--;
                if(p <= str){
                    if(*p != ch){
                        return NULL;
                    }else
                        return p;
                }
                if(*p == ch)
                    break;
            }
        }
    }else if(count > 0){
        int i = 0;
        while(1){
            p = strchr(p,ch);
            if(!p)
                return NULL;
            i++;
            if(i < num)
                 p++;
            else
                return p;
        }
    }else
        return NULL;
    if(*p != ch){
        return NULL;
    }
    return p;
}

//return dir name
//找出最大的文件夹名字
char* str_sort(void)
{
	int num = 0;
	struct list_head *p_list_node = (struct list_head *)&dir_head_node;
	for(int i = 0;i < (sizeof(dir_head_node)/sizeof(struct list_head));i++){
		if(i)
			p_list_node ++;
		num = i;
		if(p_list_node != p_list_node->prev)
			break;
	}
	if(p_list_node == p_list_node->prev)
		return NULL;
	struct list_head *list_node_min = NULL;
	struct list_head *list_node_max = p_list_node;
	
	for(int i = 1;i < (sizeof(dir_head_node)/sizeof(struct list_head)) - num;i++){
		list_node_min = p_list_node + i;
		if(list_node_min == list_node_min->prev)
			continue;
		dir_list_t *p_min = list_entry(list_node_min->prev,dir_list_t ,list_node);
		dir_list_t *p_max = list_entry(list_node_max->prev,dir_list_t ,list_node);
		if(strcmp(p_min->dir_name,p_max->dir_name) > 0){
			list_node_max = list_node_min;
		}
	}
	return list_entry(list_node_max->prev,dir_list_t ,list_node)->dir_name;
}
//基于内核链表的快速排序
//指针交换
void dir_quick_sort(struct list_head *head_node,struct list_head *end_node) 
{
	//当只有一个文件或者没有文件时
	if (head_node == end_node) {
		return;
	}
	struct list_head *low = NULL,*high = NULL,*base_node = NULL;
	//初始化基准值,和左右max值,tmp_head记录链表头list head
	base_node = head_node,low = head_node,high = end_node;
	dir_list_t *p_cur = NULL,*p_base = NULL;
	
	//记录head和end值
	head_node = head_node->prev;
	end_node = end_node->next;
	
	p_base = list_entry(base_node,dir_list_t,list_node);
	while(low != high){
		//high ->low
		//右到左同base比较,比他小就放到它左边,比它小就原地不动
		while(low != high){
			p_cur = list_entry(high,dir_list_t,list_node);
			if(strcmp(p_cur->dir_name,p_base->dir_name) < 0){
				if(low->next != high){
					low = low->next;
					list_del(base_node);
					list_add_prev(base_node,high);
					list_del(high);
					list_add_prev(high,low);
					high = base_node;
				}else{
					list_del(base_node);
					list_add_next(base_node,high);
					high = base_node;
				}
				break;
			}else{
				high = high->prev;
			}
		}	
		//low ->high
		//左到右同base比较,比他大就放到它右边,比它小就原地不动
		while(low != high){
			p_cur = list_entry(low,dir_list_t,list_node);
			if(strcmp(p_cur->dir_name,p_base->dir_name) > 0){
				if(high->prev != low){
					high = high->prev;
					list_del(base_node);
					list_add_prev(base_node,low);
					list_del(low);
					list_add_next(low,high);
					low = base_node;
				}else{
					list_del(base_node);
					list_add_prev(base_node,low);
					low = base_node;
				}
				break;
			}else{
				low = low->next;
			}
		}
		
	}
	//重置head和end 使其永远指向头和尾
	head_node = head_node->next;
	end_node = end_node->prev;
	
	if(head_node != base_node){
		dir_quick_sort(head_node,base_node->prev);//前半段
	}
	if(base_node != end_node){
		dir_quick_sort(base_node->next,end_node);//后半段
	}
}

void file_quick_sort(struct list_head *head_node,struct list_head *end_node) 
{
	//当只有一个文件或者没有文件时
	if (head_node == end_node) {
		return;
	}
	struct list_head *low = NULL,*high = NULL,*base_node = NULL;
	//初始化基准值,和左右max值,tmp_head记录链表头list head
	base_node = head_node,low = head_node,high = end_node;
	file_list_t *p_cur = NULL,*p_base = NULL;
	//记录head和end值
	head_node = head_node->prev;
	end_node = end_node->next;
	
	p_base = list_entry(base_node,file_list_t,list_node);
	while(low != high){
		//high ->low
		//右到左同base比较,比他小就放到它左边,比它小就原地不动
		while(low != high){
			p_cur = list_entry(high,file_list_t,list_node);
			char *p1_prefix = SearchStrChr(p_cur->file_name,'.',-1);
			char *p1 = SearchStrChr(p_cur->file_name,'_',-1);
			if((p1_prefix - p1) != 7){
				p1 = SearchStrChr(p_cur->file_name,'_',-3);
			}else{
				p1 = SearchStrChr(p_cur->file_name,'_',-2);
			}
			char *p2_prefix = SearchStrChr(p_base->file_name,'.',-1);
			char *p2 = SearchStrChr(p_base->file_name,'_',-1);
			if((p2_prefix - p2) != 7){
				p2 = SearchStrChr(p_base->file_name,'_',-3);
			}else{
				p2 = SearchStrChr(p_base->file_name,'_',-2);
			}
			if(strcmp(p1,p2) < 0){
				if(low->next != high){
					low = low->next;
					list_del(base_node);
					list_add_prev(base_node,high);
					list_del(high);
					list_add_prev(high,low);
					high = base_node;
				}else{
					list_del(base_node);
					list_add_next(base_node,high);
					high = base_node;
				}
				break;
			}else{
				high = high->prev;
			}
		}	
		//low ->high
		//左到右同base比较,比他大就放到它右边,比它小就原地不动
		while(low != high){
			p_cur = list_entry(low,file_list_t,list_node);
			char *p1_prefix = SearchStrChr(p_cur->file_name,'.',-1);
			char *p1 = SearchStrChr(p_cur->file_name,'_',-1);
			if((p1_prefix - p1) != 7){
				p1 = SearchStrChr(p_cur->file_name,'_',-3);
			}else{
				p1 = SearchStrChr(p_cur->file_name,'_',-2);
			}
			char *p2_prefix = SearchStrChr(p_base->file_name,'.',-1);
			char *p2 = SearchStrChr(p_base->file_name,'_',-1);
			if((p2_prefix - p2) != 7){
				p2 = SearchStrChr(p_base->file_name,'_',-3);
			}else{
				p2 = SearchStrChr(p_base->file_name,'_',-2);
			}
			if(strcmp(p1,p2) > 0){
				if(high->prev != low){
					high = high->prev;
					list_del(base_node);
					list_add_prev(base_node,low);
					list_del(low);
					list_add_next(low,high);
					low = base_node;
				}else{
					list_del(base_node);
					list_add_prev(base_node,low);
					low = base_node;
				}
				break;
			}else{
				low = low->next;
			}
		}
		
	}
	//重置head和end 使其永远指向头和尾
	head_node = head_node->next;
	end_node = end_node->prev;
	
	if(head_node != base_node){
		file_quick_sort(head_node,base_node->prev);//前半段
	}
	if(base_node != end_node){
		file_quick_sort(base_node->next,end_node);//后半段
	}
}

文件列表加载

int load_file_list(const char *dirname,struct list_head *head_node,enum CODEC_MEDIA_TYPE media_type)
{
	/* 打开要进行匹配的文件目录 */
	struct dirent *ent = NULL;
	char folderName[260] = {0};
	char fileName[520] = {0};
	char *old_file = NULL,*new_file = NULL;//区别id不同产生错误判断,如果没有id,可以取'.'用mp4后缀判断
	char *pType = NULL,*p = NULL;//判断是否为mp4文件
	//打开ADUIO IMAGE VIDEO 目录
	DIR *firstDir = opendir(dirname);
	if (firstDir == NULL){
	    ERR_PRINT("opendir %s fail\n",dirname);
	    return -1;
	}
	list_head_init(head_node);
	//***********************dir*********************
	while(1){
		//read ADUIO IMAGE VIDEO 目录
		u8 file_exsit = 0;
		ent = readdir (firstDir);
        if (ent == NULL){
			break;
        }
        if ((strcmp(".", ent->d_name)==0) || (strcmp("..", ent->d_name)==0)||(ent->d_type != DT_DIR)){
            continue;
        }
		if(VIDEO == media_type){
			if(strlen(ent->d_name) != 13&&strlen(ent->d_name) != 17)
				continue;
		}else{
			if(strlen(ent->d_name) != 13)
				continue;
		}
		//将文件夹名字填入链表中存储
		p_dir_list_t dir_node = (p_dir_list_t)malloc(sizeof(dir_list_t));
		if(dir_node == NULL){
			DEBUG_PRINT("dir_node malloc fail");
			return -1;
		}
		sprintf(dir_node->dir_name,"%s",ent->d_name);
		list_add_tail(&dir_node->list_node,head_node);
		memset(folderName,0,sizeof(folderName));
		sprintf(folderName,"%s/%s",dirname,ent->d_name);
		//打开日期文件夹
		DIR *secDir = opendir(folderName);
		if (secDir == NULL){
		    ERR_PRINT("opendir %s fail\n",folderName);
		    return -1;
		}
		//*********************file***********************
		//file tmp node
		p_file_list_t file_tmp_node = NULL;
		list_head_init(&dir_node->file_list_head);
		while(1){
			//read 日期文件夹
			ent = readdir (secDir);
	        if (ent == NULL){
				break;
	        }
	        if ((strcmp(".", ent->d_name)==0) || (strcmp("..", ent->d_name)==0)||ent->d_type != DT_REG){
	            continue;
	        }
			file_exsit = 1;
			char *p = strrchr(ent->d_name,'_');
			char *p1 = strrchr(ent->d_name,'.');
			if(NULL == p || p1 == NULL)
				continue;
			if(VIDEO == media_type){
				if((p1-p) != 7&&(p1-p) != 4)
					continue;
			}else if(AUDIO == media_type){
				if((p1-p) != 7)
					continue;
			}else if(IMAGE == media_type){
				if((p1-p) != 7
&&(p1-p) != 3)
					continue;
			}
			p_file_list_t file_node = (p_file_list_t)malloc(sizeof(file_list_t));
			if(file_node == NULL){
				DEBUG_PRINT("file_node malloc fail");
				return -1;
			}
			sprintf(file_node->file_name,"%s",ent->d_name);
			if(NULL == file_tmp_node){
				list_add_tail(&file_node->list_node,&dir_node->file_list_head);
				file_tmp_node = file_node;
			}else{
				list_add_next(&file_node->list_node,&file_tmp_node->list_node);
				file_tmp_node = file_node;
			}
		}
		closedir(secDir);
		//*******************************************
		if(!file_exsit){
			list_del(&dir_node->list_node);
			free(dir_node);
			dir_node = NULL;
			char cmd_buf[128] = {0};
			sprintf(cmd_buf,"rm -rf %s",folderName);
			if(exeCmd(cmd_buf) < 0){
				DEBUG_PRINT("remove dir fail");
				return -1;
			}
		}else{
			file_quick_sort(dir_node->file_list_head.next,dir_node->file_list_head.prev);
		}
	}
	//******************************************************
	closedir(firstDir);
	return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[1.7.38] 修复了所有示例。兼容1.7之后的版本。 优化了 Memcached支持。将 libmemcached 的内存分配器改成了易语言统一申请内存的方式。 修复了URI解析过程的一个逻辑陷阱。兼容文等情况的URI二次解析可能出现的问题。 修复了XML解析等情况下得到的文本内容超出预计范围的问题。 修复了 模板编辑器 的若干问题。 1. 标签[正常输出]为空的情况 2. 包含文件列表为空的情况 修复了 通过后缀名获取文件列表的一些场景下,后缀名包含“.”无法得到文件列表的情况。 [1.7.37] 服务器响应对象加入了标准反馈信息的支持(API)。如果您希望把您的视图做为远程调用函数,强烈建议使用这组方法来写出反馈结果。未来E2EE也会提供更加完善的远程调用机制,也会将此反馈模型做为标准协议提供更多功能的支持。 响应.写出结果() 响应.写出成功() 响应.写出失败() [网站客户端] 修复了设置HTTP请求头后再次执行请求奔溃的问题。 修复了针对JSON和JS的自动编码转换无效的问题(神6、物联科技)。 HTTP返回的协议头里的Content-Type,网站客户端只转换了 txt/ 开头的MIME类型,JSON和JS的MIME是以 application 开头的,特地加入了针对这种情况的支持。 [存取键值表] 优化了 合并到() 方法。结果将以 合并到 左边的键值表为主,强制覆盖 目标 键值表所有主键名称相同的主键和添加不存在的主键内容。 加入了 置为空值() 方法。可设置主键对应的内容为 空 值。 [存取列表] 加入了 置为空值() 方法。可设置指定位置的内容为 空 值。 [1.7.36] 修复了设置Session的ID某些场景下无效的问题(用户首次系统自动产生SessionID时无法再次手动设置等情况)。允许同一次请求执行多次Session的ID设置,只使用最后一次的设置。 [数据库分页] 修复了查询到分页实际的分页尺寸永远为10条的问题。为啥没有人告诉我这个问题!!!(感谢 左边的石头) [通用缓存对象] 在取键值表和列表的数据之前,加入了对象清空操作。避免取到脏数据。 [WebSocket服务器] 加入了 取客户地址 方法。可以获取客户的IP地址。 [1.7.35] [网页内容解析器] 加入了 取下级文本/取下级内部文本 方法,可对选择结果进行二次筛选。 对应的示例[717]做了更新。 [网站客户端] 修复了 某些特殊环境下GET不受控制得不到HTTP响应头的内容(不知道是不是盗版引起)。 渲染() 方法加入了列表的立即值输出。 例如: 列表名为“名称组”,值为 ["a", "b", "c", 1, 2, 3] 则页面上可以使用 {{#名称组}} {{名称组}}  <= 直接使用列表的变量名即可直接输出变量的值 {{/名称组}} [存取键值表] 加入了 子列表添加() 命令。可以对 键值表 列表值,进行 添加值 的操作。 [1.7.34] 修复了URL处理路径包含文和特殊符号等可能无法正确定位文件的问题。 存取列表 加入了 排序() 方法。具体查看[009 基础对象测试]下的“列表排序”示例。可通过列表内容进行多种形式的排序。 存取键值表 加入了 子列表排序() 方法。作用等同于 列表.排序()。 WebSocket客户端 加入了 置请求头() 方法。可以设置连接到服务器之后提交给服务器的HTTP头内容。 升级到最新版的易语言支持库开发SDK。 [1.7.33] 修复了 URL 解码时遇到的长度极值问题。之前的算法超过127位长度无法获取参数数据。 [1.7.32] 修复了URLEncoded类型的表单提交时解码的问题。 优化了内部流程URL解码部分的逻辑。 [1.7.31] [ADODB数据库连接池] 加入了 测试连接 方法。可以使用此方法测试指定数据库是否能够连接成功。 [存取键值表/存取列表] JSON处理 修复了某些特殊文本(全角文本或字符)加载失败的问题。 [请求对象] 修复了 取参数()/取所有参数() 包含 http:// 等关键字的参数解析失败的问题。 重构了URI解析算法。兼容性更好,效率更高。(实测平均每秒能够处理2000万次请求路径) [数据库] 屏蔽了ADO查询到分页数据为空时,输出的错误信息内容。 [1.7.29] 改进了 静态编译 机制。 * 无论是静态编译EXE还是DLL,都能正确释放和调用原始库(因为是伪静态,机制等同于独立编译),不需要手动拷贝e2ee.fne到运行目录。 * 优化了原始库释放和调用流程,只有在临时目录无法使用且不存在原始库的情况下,才会释放原始库到运行目录。 修复了 文件列表 功能可能引起的奔溃问题(第三只眼)。 * 网站打包加入后,通用获

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值