first
This commit is contained in:
488
apps/common/file_operate/file_bs_deal.c
Normal file
488
apps/common/file_operate/file_bs_deal.c
Normal file
@ -0,0 +1,488 @@
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* AC51
|
||||
* fs browser select
|
||||
* CODE
|
||||
*
|
||||
* (c) Copyright 2015-2016, ZHUHAI JIELI
|
||||
* All Rights Reserved
|
||||
*
|
||||
* File : *
|
||||
* By : jamin.li
|
||||
* DATE : 2015-10-15 build this file
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
|
||||
#include "file_bs_deal.h"
|
||||
#include "uart.h"
|
||||
#include "dev_manager.h"
|
||||
|
||||
#define FILE_BS_OPT_DBG
|
||||
#ifdef FILE_BS_OPT_DBG
|
||||
|
||||
#define file_bs_puts puts
|
||||
#define file_bs_printf printf
|
||||
|
||||
#else
|
||||
#define file_bs_puts(...)
|
||||
#define file_bs_printf(...)
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 在指定数据中,搜索key(byte)的位置
|
||||
*
|
||||
* Arguments : buf - 指定数据起始地址, buf_len-长度, key-搜索的key,
|
||||
* find_type -搜索方向:0-从前往后搜索,1-从后往前搜索
|
||||
*
|
||||
* Returns : key的位置
|
||||
*
|
||||
* Note:
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
//查找byte的位置
|
||||
static int find_byte_pos(u8 *buf, u16 buf_len, u8 key, u8 find_type)
|
||||
{
|
||||
u8 *data = buf;
|
||||
|
||||
if (0 == find_type) {
|
||||
//从前往后找
|
||||
while (buf_len > 0) {
|
||||
if (*data == key) {
|
||||
return (int)(data - buf);
|
||||
}
|
||||
data++;
|
||||
buf_len--;
|
||||
}
|
||||
} else {
|
||||
//从后往前找
|
||||
data += (buf_len - 1);
|
||||
while (buf_len > 0) {
|
||||
if (*data == key) {
|
||||
return (int)(data - buf);
|
||||
}
|
||||
data--;
|
||||
buf_len--;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 转化路径格式
|
||||
*
|
||||
* Arguments :
|
||||
*
|
||||
* Returns : 0-为成功,其他值 失败
|
||||
*
|
||||
* Note:
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
int file_comm_change_file_path(char *dest, char *src)
|
||||
{
|
||||
u8 start_flag = 0, space_flag, val;
|
||||
|
||||
while (1) {
|
||||
val = *src;
|
||||
if (val == '\0') {
|
||||
*dest = '\0';
|
||||
break;
|
||||
}
|
||||
switch (val) {
|
||||
case '/':
|
||||
if (start_flag != 0) {
|
||||
space_flag = 0;
|
||||
}
|
||||
start_flag++;
|
||||
*dest = val;
|
||||
break;
|
||||
|
||||
case ' ':
|
||||
space_flag = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (space_flag != 0) {
|
||||
*dest = '.';
|
||||
dest++;
|
||||
space_flag = 0;
|
||||
}
|
||||
*dest = val;
|
||||
break;
|
||||
}
|
||||
|
||||
dest++;
|
||||
src++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 修正从fs取等的长名
|
||||
*
|
||||
* Arguments : str,len
|
||||
*
|
||||
* Returns : 字节长度(不包含结束符)
|
||||
*
|
||||
* Note: 过滤0xffff值,自动填入结束符
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
int file_comm_long_name_fix(u8 *str, u16 len)
|
||||
{
|
||||
u8 *src = str;
|
||||
while (len > 1) {
|
||||
if ((*str == 0xff) && (*(str + 1) == 0xff)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ((*str == 0x0) && (*(str + 1) == 0x0)) {
|
||||
break;
|
||||
}
|
||||
|
||||
str += 2;
|
||||
len -= 2;
|
||||
}
|
||||
*str = 0x00;
|
||||
*(str + 1) = 0x00;
|
||||
return (int)(str - src);
|
||||
}
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 转换8+3名字显示
|
||||
*
|
||||
* Arguments : dest,src
|
||||
*
|
||||
* Returns : 转换后名字的长度(不包含结束符)
|
||||
*
|
||||
* Note: 转换后自动填入结束符
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
int file_comm_display_83name(u8 *dest, u8 *src)
|
||||
{
|
||||
u8 offset;
|
||||
|
||||
for (offset = 8; offset > 0; offset--) {
|
||||
if (src[offset - 1] != 0x20) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(dest, src, offset);
|
||||
|
||||
if (src[8] != 0x20) {
|
||||
dest[offset++] = '.';
|
||||
memcpy(dest + offset, src + 8, 3);
|
||||
offset += 3;
|
||||
}
|
||||
|
||||
dest[offset++] = '\0';
|
||||
|
||||
return offset; //name 's len
|
||||
}
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 获取显示的文件夹或文件名字
|
||||
*
|
||||
* Arguments :tpath,disp_file_name,disp_dir_name
|
||||
*
|
||||
* Returns :
|
||||
*
|
||||
* Note: 转换后 lfn_cnt 为0为短名,lfn_cnt不为0 则是长名
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
void file_comm_change_display_name(char *tpath, LONG_FILE_NAME *disp_file_name, LONG_FILE_NAME *disp_dir_name)
|
||||
{
|
||||
u16 len;
|
||||
int pos;
|
||||
|
||||
//取文件名字显示
|
||||
|
||||
if (disp_file_name != NULL) {
|
||||
if (disp_file_name->lfn_cnt != 0) {
|
||||
//long name
|
||||
file_bs_puts("file long name \n");
|
||||
//printf_buf(disp_file_name->lfn, 16);
|
||||
disp_file_name->lfn_cnt = file_comm_long_name_fix((void *)disp_file_name->lfn, disp_file_name->lfn_cnt); //增加结束符
|
||||
} else {
|
||||
//short name
|
||||
file_bs_puts("file short name\n");
|
||||
len = strlen((void *)tpath); //增加结束符
|
||||
pos = find_byte_pos((void *)tpath, len, 0x2f, 1); //
|
||||
if (pos == -1) {
|
||||
strcpy(disp_file_name->lfn, "----");
|
||||
} else {
|
||||
memcpy((void *)&disp_file_name->lfn[32], tpath + pos + 1, 11);
|
||||
file_comm_display_83name((void *)disp_file_name->lfn, (void *)&disp_file_name->lfn[32]);
|
||||
}
|
||||
|
||||
disp_file_name->lfn_cnt = 0;
|
||||
}
|
||||
|
||||
//printf_buf(disp_file_name->lfn, 32);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//取文件夹名字显示
|
||||
if (disp_dir_name != NULL) {
|
||||
if (disp_dir_name->lfn_cnt != 0) {
|
||||
//long name
|
||||
file_bs_puts("folder long name \n");
|
||||
//printf_buf(disp_dir_name->lfn, 16);
|
||||
disp_dir_name->lfn_cnt = file_comm_long_name_fix((void *)&disp_dir_name->lfn, disp_dir_name->lfn_cnt); //增加结束符
|
||||
} else {
|
||||
//short name
|
||||
file_bs_puts("folder short name \n");
|
||||
len = strlen((void *)tpath); //增加结束符
|
||||
pos = find_byte_pos((void *)tpath, len, 0x2f, 1); //
|
||||
|
||||
if (pos != -1) {
|
||||
pos = find_byte_pos((void *)tpath, pos, 0x2f, 1); //
|
||||
}
|
||||
|
||||
if (pos == -1) {
|
||||
strcpy(disp_dir_name->lfn, "ROOT");
|
||||
} else {
|
||||
memcpy(&disp_dir_name->lfn[32], tpath + pos + 1, 11);
|
||||
file_comm_display_83name((void *)disp_dir_name->lfn, (void *)&disp_dir_name->lfn[32]);
|
||||
}
|
||||
disp_dir_name->lfn_cnt = 0;
|
||||
}
|
||||
|
||||
//printf_buf(disp_dir_name->lfn, 32);
|
||||
}
|
||||
}
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 打开文件浏览器
|
||||
*
|
||||
* Arguments : fil_bs
|
||||
*
|
||||
* Returns : dir的总数
|
||||
*
|
||||
* Note:
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
//返回根目录dir总数
|
||||
u32 file_bs_open_handle(FILE_BS_DEAL *fil_bs, u8 *ext_name)
|
||||
{
|
||||
u32 total_dir;
|
||||
|
||||
if (fil_bs == NULL) {
|
||||
puts("*open hdl is null\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
file_bs_puts("open bs hdl\n");
|
||||
file_bs_printf("bs_mapi:0x%x\n", (int)fil_bs);
|
||||
|
||||
if (ext_name == NULL) {
|
||||
ext_name = "MP3";//"WAVWMAMP3FLA";
|
||||
}
|
||||
|
||||
printf("fil_bs->dev = %x\n", fil_bs->dev);
|
||||
printf("%s, %s, %s, %d\n]", dev_manager_get_root_path(fil_bs->dev), dev_manager_get_logo(fil_bs->dev), __FUNCTION__, __LINE__);
|
||||
fset_ext_type(dev_manager_get_root_path(fil_bs->dev), ext_name);
|
||||
printf("%s, %d\n]", __FUNCTION__, __LINE__);
|
||||
|
||||
total_dir = fopen_dir_info(dev_manager_get_root_path(fil_bs->dev), &fil_bs->file, 0);
|
||||
printf("%s, %d\n]", __FUNCTION__, __LINE__);
|
||||
if (fil_bs->file == 0) {
|
||||
file_bs_puts("open bs fail\n");
|
||||
return 0;
|
||||
}
|
||||
file_bs_printf("open bs hdl_out,root_dir = 0x%x\n", total_dir);
|
||||
return total_dir;
|
||||
}
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 关闭文件浏览器
|
||||
*
|
||||
* Arguments : fil_bs
|
||||
*
|
||||
* Returns :
|
||||
*
|
||||
* Note:
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
void file_bs_close_handle(FILE_BS_DEAL *fil_bs)
|
||||
{
|
||||
file_bs_puts("close bs hdl\n");
|
||||
if (fil_bs->file) {
|
||||
fclose(fil_bs->file);
|
||||
fil_bs->file = NULL;
|
||||
}
|
||||
file_bs_puts("close bs hdl_out\n");
|
||||
}
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 进入文件夹目录
|
||||
*
|
||||
* Arguments : fil_bs,dir_info
|
||||
*
|
||||
* Returns : dir的总数
|
||||
*
|
||||
* Note:
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
u32 file_bs_entern_dir(FILE_BS_DEAL *fil_bs, FS_DIR_INFO *dir_info)
|
||||
{
|
||||
u32 total_dir;
|
||||
file_bs_printf("bs_enter dir\n");
|
||||
total_dir = fenter_dir_info(fil_bs->file, dir_info); //使用open获得的file,无需重新申请。
|
||||
file_bs_printf("bs_enter_out,t = 0x%x\n", total_dir);
|
||||
return total_dir;
|
||||
}
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 返回上传目录
|
||||
*
|
||||
* Arguments : fil_bs
|
||||
*
|
||||
* Returns : dir的总数
|
||||
*
|
||||
* Note:
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
u32 file_bs_exit_dir(FILE_BS_DEAL *fil_bs)
|
||||
{
|
||||
u32 total_dir;
|
||||
file_bs_printf("bs_exit dir\n");
|
||||
total_dir = fexit_dir_info(fil_bs->file);//
|
||||
file_bs_printf("bs_exit_out,total = 0x%x\n", total_dir);
|
||||
return total_dir;
|
||||
}
|
||||
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
*
|
||||
* Description: 获取指定位置的dir信息
|
||||
*
|
||||
* Arguments : fil_bs,buf--被填入的buf,start_sn -获取的其实位置(从1开始),get_cnt--获取dir的个数
|
||||
*
|
||||
* Returns : 真正获取到的dir总数
|
||||
*
|
||||
* Note:
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
u32 file_bs_get_dir_info(FILE_BS_DEAL *fil_bs, FS_DIR_INFO *buf, u16 start_sn, u16 get_cnt)
|
||||
{
|
||||
u32 real_dir;
|
||||
/* file_bs_printf("bs_get dir_info\n"); */
|
||||
real_dir = fget_dir_info(fil_bs->file, start_sn, get_cnt, buf);
|
||||
/* file_bs_printf("bs_get_out,t = 0x%x\n", real_dir); */
|
||||
return real_dir;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// test
|
||||
|
||||
#if 0
|
||||
|
||||
#define BS_GET_DIR_NUM 3
|
||||
void file_bs_test(void)
|
||||
{
|
||||
printf("\n\n\n\n %s,%d, ", __func__, __LINE__);
|
||||
FS_DIR_INFO *dir_buf = zalloc(sizeof(FS_DIR_INFO) * BS_GET_DIR_NUM);
|
||||
FILE_BS_DEAL *fil_bs = zalloc(sizeof(FILE_BS_DEAL));
|
||||
if (!fil_bs || !dir_buf) {
|
||||
printf("%s,%d, ", __func__, __LINE__);
|
||||
printf("malloc err");
|
||||
while (1);
|
||||
}
|
||||
fil_bs->dev = storage_dev_last();
|
||||
if (!fil_bs->dev) {
|
||||
printf("%s,%d, ", __func__, __LINE__);
|
||||
printf("have no dev");
|
||||
while (1);
|
||||
}
|
||||
|
||||
/* void *fmnt = mount(fil_bs->dev->dev_name, fil_bs->dev->storage_path, fil_bs->dev->fs_type, 3, NULL); */
|
||||
/* if (!fmnt) { */
|
||||
/* printf("%s,%d, ", __func__, __LINE__); */
|
||||
/* printf("mount err"); */
|
||||
/* while(1); */
|
||||
/* } */
|
||||
|
||||
int i, j, cnt, total;
|
||||
file_bs_open_handle(fil_bs, "MP3");//open完后需要close
|
||||
/* printf("\n root dir num: %d ", total); */
|
||||
/* if (!total) { */
|
||||
/* printf("%s,%d, ", __func__, __LINE__); */
|
||||
/* printf("total dir is zero"); */
|
||||
/* goto __exit; */
|
||||
/* } */
|
||||
|
||||
total = file_bs_entern_dir(fil_bs, dir_buf);
|
||||
/* printf("\n %s,%d, ", __func__, __LINE__); */
|
||||
/* printf("dir num: %d ", total); */
|
||||
if (!total) {
|
||||
printf("%s,%d, ", __func__, __LINE__);
|
||||
printf("total dir is zero");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
u8 flag = 0;
|
||||
for (i = 1; i <= total; i += BS_GET_DIR_NUM) {
|
||||
__again:
|
||||
cnt = file_bs_get_dir_info(fil_bs, dir_buf, i, BS_GET_DIR_NUM);
|
||||
/* printf("\n cnt:%d ", cnt); */
|
||||
for (j = 0; j < cnt; j++) {
|
||||
/* printf("j:%d, dirtype:%d, fntype:%d, sclust:0x%x ", i + j, dir_buf[j].dir_type, dir_buf[j].fn_type, dir_buf[j].sclust); */
|
||||
if (dir_buf[j].dir_type == BS_DIR_TYPE_FORLDER) {
|
||||
if (flag < 2) {
|
||||
flag ++;
|
||||
if (flag == 2) {
|
||||
int sub_i, sub_j, sub_cnt, sub_total;
|
||||
sub_total = file_bs_entern_dir(fil_bs, &dir_buf[j]);
|
||||
/* printf("\n %s,%d, ", __func__, __LINE__); */
|
||||
/* printf("dir num: %d ", sub_total); */
|
||||
if (sub_total) {
|
||||
for (sub_i = 1; sub_i <= sub_total; sub_i += BS_GET_DIR_NUM) {
|
||||
sub_cnt = file_bs_get_dir_info(fil_bs, dir_buf, sub_i, BS_GET_DIR_NUM);
|
||||
/* printf("sub_cnt:%d ", sub_cnt); */
|
||||
for (sub_j = 0; sub_j < sub_cnt; sub_j++) {
|
||||
/* printf("sub_j:%d, dirtype:%d, fntype:%d, sclust:0x%x ", sub_i + sub_j, dir_buf[sub_j].dir_type, dir_buf[sub_j].fn_type, dir_buf[sub_j].sclust); */
|
||||
}
|
||||
}
|
||||
}
|
||||
file_bs_exit_dir(fil_bs);
|
||||
goto __again;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* printf("\n %s,%d, \n ", __func__, __LINE__); */
|
||||
|
||||
file_bs_close_handle(fil_bs);
|
||||
|
||||
/* unmount(fil_bs->dev->storage_path); */
|
||||
|
||||
free(dir_buf);
|
||||
free(fil_bs);
|
||||
}
|
||||
#endif
|
||||
|
||||
47
apps/common/file_operate/file_bs_deal.h
Normal file
47
apps/common/file_operate/file_bs_deal.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
*********************************************************************************************************
|
||||
* AC46
|
||||
* fs browser select
|
||||
* CODE
|
||||
*
|
||||
* (c) Copyright 2015-2016, ZHUHAI JIELI
|
||||
* All Rights Reserved
|
||||
*
|
||||
* File : *
|
||||
* By : jamin.li
|
||||
* DATE : 2015-10-15 build this file
|
||||
*********************************************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef _FILE_BS_DEAL_H_
|
||||
#define _FILE_BS_DEAL_H_
|
||||
|
||||
#include "fs/fs.h"
|
||||
#include "storage_dev/storage_dev.h"
|
||||
|
||||
#define BS_DIR_TYPE_FORLDER 0
|
||||
#define BS_DIR_TYPE_FILE 1
|
||||
|
||||
#define BS_FNAME_TYPE_SHORT 0
|
||||
#define BS_FNAME_TYPE_LONG 1
|
||||
|
||||
typedef struct _FILE_BS_DEAL_ {
|
||||
struct __dev *dev;
|
||||
FILE *file;
|
||||
} FILE_BS_DEAL;
|
||||
|
||||
|
||||
extern int file_comm_change_file_path(char *dest, char *src);
|
||||
extern int file_comm_long_name_fix(u8 *str, u16 len);
|
||||
extern int file_comm_display_83name(u8 *dest, u8 *src);
|
||||
|
||||
extern u32 file_bs_open_handle(FILE_BS_DEAL *fil_bs, u8 *ext_name);
|
||||
extern void file_bs_close_handle(FILE_BS_DEAL *fil_bs);
|
||||
extern u32 file_bs_entern_dir(FILE_BS_DEAL *fil_bs, FS_DIR_INFO *dir_info);
|
||||
extern u32 file_bs_exit_dir(FILE_BS_DEAL *fil_bs);
|
||||
extern u32 file_bs_get_dir_info(FILE_BS_DEAL *fil_bs, FS_DIR_INFO *buf, u16 start_sn, u16 get_cnt);
|
||||
|
||||
extern void file_comm_change_display_name(char *tpath, LONG_FILE_NAME *disp_file_name, LONG_FILE_NAME *disp_dir_name);
|
||||
|
||||
#endif/*_FILE_BS_DEAL_H_*/
|
||||
|
||||
371
apps/common/file_operate/file_manager.c
Normal file
371
apps/common/file_operate/file_manager.c
Normal file
@ -0,0 +1,371 @@
|
||||
#include "file_manager.h"
|
||||
#include "app_config.h"
|
||||
|
||||
#define MODE_FIX 0
|
||||
#if MODE_FIX
|
||||
static int file_manager_mode_deal(struct vfscan *fs, int sel_mode, int *arg)
|
||||
{
|
||||
|
||||
int err;
|
||||
int fnum = 0;
|
||||
int file_start = 1;
|
||||
int file_end = fs->file_number;
|
||||
|
||||
if (file_end == 0) {
|
||||
return -ENOENT;
|
||||
}
|
||||
struct ffolder folder = {0};
|
||||
fget_folder(fs, &folder);
|
||||
/* if ((scan->cycle_mode == FCYCLE_FOLDER) && (scan->ff_api.fileTotalInDir) */
|
||||
/* && ((scan->ff_api.fileTotalOutDir + scan->ff_api.fileTotalInDir) <= scan->ff_api.totalFileNumber) */
|
||||
/* ) { */
|
||||
|
||||
if ((fs->cycle_mode == FCYCLE_FOLDER) && (folder.fileTotal)
|
||||
&& ((folder.fileStart + folder.fileTotal - 1) <= file_end)
|
||||
) {
|
||||
file_start = folder.fileStart;
|
||||
file_end = folder.fileStart + folder.fileTotal - 1;
|
||||
}
|
||||
switch (sel_mode) {
|
||||
case FSEL_LAST_FILE:
|
||||
fnum = fs->file_number;
|
||||
break;
|
||||
case FSEL_FIRST_FILE:
|
||||
fnum = 1;
|
||||
break;
|
||||
case FSEL_AUTO_FILE:
|
||||
/* if (scan->ff_api.fileNumber == 0) { */
|
||||
/* return -EINVAL; */
|
||||
/* } */
|
||||
if (fs->cycle_mode == FCYCLE_ONE) {
|
||||
/* fnum = scan->ff_api.fileNumber; */
|
||||
fnum = fs->file_counter;
|
||||
break;
|
||||
}
|
||||
case FSEL_NEXT_FILE:
|
||||
/* if (scan->ff_api.fileNumber == 0) { */
|
||||
/* return -EINVAL; */
|
||||
/* } */
|
||||
if (fs->cycle_mode == FCYCLE_RANDOM) {
|
||||
fnum = rand32() % (file_end - file_start + 1) + file_start;
|
||||
if (fnum == fs->file_counter) {
|
||||
fnum = fs->file_counter + 1;
|
||||
}
|
||||
} else {
|
||||
fnum = fs->file_counter + 1;
|
||||
}
|
||||
/* if (fnum > scan->last_file_number) { */
|
||||
if (fnum > fs->file_number) {
|
||||
if (fs->cycle_mode == FCYCLE_LIST) {
|
||||
return -ENOENT;
|
||||
} else if (fs->cycle_mode == FCYCLE_FOLDER) {
|
||||
fnum = file_start;
|
||||
} else {
|
||||
fnum = 1;
|
||||
}
|
||||
}
|
||||
if (fnum > file_end) {
|
||||
fnum = file_start;
|
||||
} else if (fnum < file_start) {
|
||||
fnum = file_end;
|
||||
}
|
||||
break;
|
||||
case FSEL_PREV_FILE:
|
||||
/* if (scan->ff_api.fileNumber == 0) { */
|
||||
/* return -EINVAL; */
|
||||
/* } */
|
||||
if (fs->cycle_mode == FCYCLE_RANDOM) {
|
||||
fnum = rand32() % (file_end - file_start + 1) + file_start;
|
||||
if (fnum == fs->file_counter) {
|
||||
fnum = fs->file_counter + 1;
|
||||
}
|
||||
} else {
|
||||
fnum = fs->file_counter - 1;
|
||||
}
|
||||
/* if ((scan->ff_api.fileNumber | BIT(15)) != scan->cur_file_number) { */
|
||||
/* fnum -= scan->last_file_number - scan->ff_api.totalFileNumber; */
|
||||
/* } */
|
||||
/* scan->last_file_number = scan->ff_api.totalFileNumber; */
|
||||
if (fs->cycle_mode == FCYCLE_LIST) {
|
||||
break;
|
||||
}
|
||||
if (fnum > file_end) {
|
||||
fnum = file_start;
|
||||
} else if (fnum < file_start) {
|
||||
fnum = file_end;
|
||||
}
|
||||
break;
|
||||
|
||||
case FSEL_NEXT_FOLDER_FILE:
|
||||
/* fnum = scan->ff_api.fileTotalOutDir + scan->ff_api.fileTotalInDir + 1; */
|
||||
fnum = folder.fileStart + folder.fileTotal;
|
||||
if (fnum > fs->file_number) {
|
||||
fnum = 1;
|
||||
}
|
||||
break;
|
||||
case FSEL_PREV_FOLDER_FILE:
|
||||
/* if ((scan->ff_api.fileTotalOutDir + 1) > 1) { */
|
||||
if (folder.fileStart > 1) {
|
||||
fnum = folder.fileStart - 1;
|
||||
} else {
|
||||
fnum = fs->file_number;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if ((sel_mode != FSEL_NEXT_FOLDER_FILE) && (sel_mode != FSEL_PREV_FOLDER_FILE)) {
|
||||
if (fnum < file_start) {
|
||||
fnum = file_start;
|
||||
} else if (fnum > file_end) {
|
||||
fnum = file_end;
|
||||
}
|
||||
}
|
||||
*arg = fnum;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
FILE *file_manager_select(struct __dev *dev, struct vfscan *fs, int sel_mode, int arg, struct __scan_callback *callback)
|
||||
{
|
||||
FILE *_file = NULL;
|
||||
//clock_add_set(SCAN_DISK_CLK);
|
||||
if (callback && callback->enter) {
|
||||
callback->enter(dev);//扫描前处理, 可以在注册的回调里提高系统时钟等处理
|
||||
}
|
||||
#if MODE_FIX
|
||||
if ((sel_mode != FSEL_BY_SCLUST) && (sel_mode != FSEL_BY_PATH)) {
|
||||
if (file_manager_mode_deal(fs, sel_mode, &arg)) {
|
||||
return NULL;
|
||||
}
|
||||
sel_mode = FSEL_BY_NUMBER;
|
||||
}
|
||||
#endif
|
||||
_file = fselect(fs, sel_mode, arg);
|
||||
//clock_remove_set(SCAN_DISK_CLK);
|
||||
if (callback && callback->exit) {
|
||||
callback->exit(dev);//扫描后处理, 可以在注册的回调里还原到enter前的状态
|
||||
}
|
||||
return _file;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief 文件删除统一处理
|
||||
*
|
||||
* @param path 扫描路径名
|
||||
* @param param 配置参数
|
||||
* @param dir_flag 是否删除文件夹标志
|
||||
* @note 加速处理:(删除文件的时候使用)从前往后依次删除. 文件夹必须从后往前删。
|
||||
*
|
||||
* @return 0成功, 其他失败
|
||||
*/
|
||||
/* ----------------------------------------------------------------------------*/
|
||||
int file_manager_delete_deal(char *path, char *param, u8 dir_flag)
|
||||
{
|
||||
u16 folder_total_file = 0;
|
||||
int d_err = 0;
|
||||
struct vfscan *fsn = NULL;
|
||||
FILE *d_f = NULL;
|
||||
|
||||
fsn = fscan(path, param, 9);
|
||||
if (fsn == NULL) {
|
||||
r_printf(">>>[test]:err!!!!!! fsacn fsn fail\n");
|
||||
return 1;
|
||||
}
|
||||
folder_total_file = fsn->file_number;
|
||||
y_printf(">>>[test]:total = %d\n", folder_total_file);
|
||||
for (int i = folder_total_file; i >= 1; i--) {
|
||||
if (!dir_flag) {
|
||||
d_f = fselect(fsn, FSEL_BY_NUMBER, folder_total_file - i + 1); //加速处理,不用找到最后一个文件。
|
||||
} else {
|
||||
d_f = fselect(fsn, FSEL_BY_NUMBER, i);
|
||||
}
|
||||
if (d_f == NULL) {
|
||||
r_printf(">>>[test]:err!! select file err\n");
|
||||
return 1;
|
||||
}
|
||||
putchar('D');
|
||||
d_err = fdelete(d_f);
|
||||
if (d_err) {
|
||||
r_printf(">>>[test]:err!! delete file err\n");
|
||||
return 1;
|
||||
}
|
||||
d_f = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/** @brief:文件夹删除处理
|
||||
@param:dev_logo :设备logo folder:文件夹路径(短名) folder_len:文件夹路径长度
|
||||
@return:
|
||||
@author:phewlee
|
||||
@note:
|
||||
@date: 2021-05-21,10:16
|
||||
*/
|
||||
/*----------------------------------------------------------------------------*/
|
||||
int file_manager_delete_dir(char *dev_logo, char *folder, u16 folder_len)
|
||||
{
|
||||
int err = 0;
|
||||
struct __dev *dev;
|
||||
char path[128] = {0};
|
||||
|
||||
static const u8 delete_file_param[] = "-t"
|
||||
"ALL"
|
||||
" -sn -r";
|
||||
|
||||
static const u8 delete_folder_param[] = "-t"
|
||||
"ALL"
|
||||
" -sn -d -r";
|
||||
|
||||
dev = dev_manager_find_spec(dev_logo, 0);
|
||||
if (dev == NULL) {
|
||||
r_printf(">>>[test]:errr!!!!!!!!! not find dev\n");
|
||||
return 1;
|
||||
}
|
||||
char *root_path = dev_manager_get_root_path(dev);
|
||||
memcpy(path, root_path, strlen(root_path));
|
||||
memcpy(path + strlen(root_path), folder, folder_len);
|
||||
r_printf(">>>[test]:path = %s\n", path);
|
||||
err = file_manager_delete_deal(path, delete_file_param, 0);
|
||||
if (err) {
|
||||
r_printf(">>>[test]:errr!!!!!!!!! delete file deal fail\n");
|
||||
return 1;
|
||||
}
|
||||
err = file_manager_delete_deal(path, delete_folder_param, 1);
|
||||
if (err) {
|
||||
r_printf(">>>[test]:errr!!!!!!!!! delete folder deal fail\n");
|
||||
return 1;
|
||||
}
|
||||
FILE *folder_f = fopen(path, "r");
|
||||
if (folder_f == NULL) {
|
||||
r_printf(">>>[test]:err open folder\n");
|
||||
return 1;
|
||||
}
|
||||
err = fdelete(folder_f);
|
||||
return err;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/** @brief:文件插入处理
|
||||
@param:d_f: 源文件句柄, i_f:插入文件句柄,fptr:偏移量
|
||||
@return:
|
||||
@author:phewlee
|
||||
@note:
|
||||
@date: 2021-05-25,10:16
|
||||
*/
|
||||
/*----------------------------------------------------------------------------*/
|
||||
int file_manager_insert_file(FILE *d_f, FILE *i_f, u32 fptr)
|
||||
{
|
||||
int err = finsert_file(d_f, i_f, fptr);
|
||||
return err;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/** @brief:文件分割处理
|
||||
@param:f: 源文件句柄, dev_logo:设备logo , file_name : 文件名, name_len: 文件长度, fptr:偏移量, buf:头文件数据, buf_len:头文件数据长度, folder : 需要指定的路径,绝对路径,例如:/RECORD或者/1/2/3/4, 暂时只支持短名文件夹形式, folder 传NULL默认根目录。
|
||||
@return:
|
||||
@author:phewlee
|
||||
@note:
|
||||
@date: 2021-05-25,10:16
|
||||
*/
|
||||
/*----------------------------------------------------------------------------*/
|
||||
int file_manager_division_file(FILE *f, char *dev_logo, char *file_name, u32 name_len, u32 fptr, char *buf, u32 buf_len, char *folder)
|
||||
{
|
||||
struct __dev *dev;
|
||||
char path[128] = {0};
|
||||
char tmp_name[64] = {0};
|
||||
if ((dev_logo == NULL) || (file_name == NULL) || (buf == NULL)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
dev = dev_manager_find_spec(dev_logo, 0);
|
||||
if (dev == NULL) {
|
||||
r_printf(">>>[test]:errr!!!!!!!!! not find dev\n");
|
||||
return -1;
|
||||
}
|
||||
char *root_path = dev_manager_get_root_path(dev);
|
||||
|
||||
/****************分割文件*********************/
|
||||
if (folder) {
|
||||
memcpy(tmp_name, folder, strlen(folder));
|
||||
}
|
||||
strcat(tmp_name, "/");
|
||||
strcat(tmp_name, "jl_test.tmp");
|
||||
if (fdicvision_file(f, tmp_name, fptr)) {
|
||||
r_printf(">>>[test]:file division fail\n");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/****************打开分割出去的文件*********************/
|
||||
memcpy(path, root_path, strlen(root_path) - 1);
|
||||
memcpy(path + strlen(root_path) - 1, tmp_name, strlen(tmp_name));
|
||||
r_printf(">>>[test]:path1 = %s\n", path);
|
||||
FILE *i_f = fopen(path, "r");
|
||||
if (i_f == NULL) {
|
||||
r_printf(">>>[test]:open i_f fail\n");
|
||||
return -1;
|
||||
}
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
/****************创建新文件,写入头数据*********************/
|
||||
memset(path, 0, sizeof(path));
|
||||
memcpy(path, root_path, strlen(root_path));
|
||||
if (folder) {
|
||||
memcpy(path + strlen(path) - 1, folder, strlen(folder));
|
||||
strcat(path, "/");
|
||||
}
|
||||
memcpy(path + strlen(path), file_name, name_len);
|
||||
r_printf(">>>[test]:path1 = %s\n", path);
|
||||
FILE *d_f = fopen(path, "w+");
|
||||
if (d_f == NULL) {
|
||||
r_printf(">>>[test]:open d_f fail\n");
|
||||
fclose(i_f);
|
||||
i_f = NULL;
|
||||
goto __exit;
|
||||
}
|
||||
int wlen = fwrite(d_f, buf, buf_len);
|
||||
if (wlen != buf_len) {
|
||||
r_printf(">>>[test]:err write!!!!!!!!wlen = %d, buf_len = %d\n", wlen, buf_len);
|
||||
fclose(i_f);
|
||||
i_f = NULL;
|
||||
fclose(d_f);
|
||||
d_f = NULL;
|
||||
goto __exit;
|
||||
}
|
||||
fclose(d_f);
|
||||
d_f = NULL;
|
||||
d_f = fopen(path, "r");
|
||||
if (d_f == NULL) {
|
||||
r_printf(">>>[test]:open d_f fail\n");
|
||||
fclose(i_f);
|
||||
i_f = NULL;
|
||||
goto __exit;
|
||||
}
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
int fsize = flen(d_f);
|
||||
int err = finsert_file(d_f, i_f, fsize);
|
||||
fclose(i_f);
|
||||
i_f = NULL;
|
||||
fclose(d_f);
|
||||
d_f = NULL;
|
||||
return err;
|
||||
|
||||
__exit:
|
||||
memset(path, 0, sizeof(path));
|
||||
memcpy(path, root_path, strlen(root_path) - 1);
|
||||
memcpy(path + strlen(root_path) - 1, tmp_name, strlen(tmp_name));
|
||||
r_printf(">>>[test]:error process: path1 = %s\n", path);
|
||||
FILE *f_tmp = fopen(path, "r");
|
||||
if (f_tmp) {
|
||||
fdelete(f_tmp);
|
||||
f_tmp = NULL;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
11
apps/common/file_operate/file_manager.h
Normal file
11
apps/common/file_operate/file_manager.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef __FILE_MANAGER_H__
|
||||
#define __FILE_MANAGER_H__
|
||||
|
||||
#include "system/includes.h"
|
||||
#include "typedef.h"
|
||||
#include "system/fs/fs.h"
|
||||
#include "dev_manager.h"
|
||||
|
||||
FILE *file_manager_select(struct __dev *dev, struct vfscan *fs, int sel_mode, int arg, struct __scan_callback *callback);
|
||||
|
||||
#endif// __FILE_MANAGER_H__
|
||||
Reference in New Issue
Block a user