📄 fat_file.c
字号:
old_cluster = new_cluster;
if (index == nb_frag)
{ /* end of chain reached */
/* last fragment always contains a single cluster */
chain[nb_frag].number = 0; /* end of chain marker */
fat_last_clust_index = nb_frag;
return KO; /* file too much fragmented */
}
else
{
/* read new entry */
//((Byte *)&new_cluster)[1]= gl_buffer[0+fat_buf_count];
//((Byte *)&new_cluster)[0]= gl_buffer[1+fat_buf_count];
new_cluster = fat_U8_to_U16_LH(gl_buffer[fat_buf_count], gl_buffer[fat_buf_count + 1]);
}
}
fat_last_clust_index = index;
// fat_get_clusters_end(chain,index);
return OK;
}
#endif //COMPILE_FAT_16
//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////
byte fat_get_clusters (fat_st_clust_chain *chain, Byte nb_frag)
{
if(fat_type_id == FAT_IS_16)
{
#if COMPILE_FAT_16 == TRUE
if(fat16_get_clusters (chain, nb_frag) == KO)
return KO;
#endif
}
else if(fat_type_id == FAT_IS_12)
{
#if COMPILE_FAT_12 == TRUE
if(fat12_get_clusters (chain, nb_frag) == KO)
return KO;
#endif
}
else if(fat_type_id == FAT_IS_32)
{
#if COMPILE_FAT_32 == TRUE
if(fat32_get_clusters (chain, nb_frag) == KO)
return KO;
#endif
}
phy_read_close();
/* end of file: last fragment must always contain a single cluster */
if (chain[fat_last_clust_index].number == 1)
{ /* last cluster is the current one */
// fat_last_clust_index = index;
chain[fat_last_clust_index].number = 0; /* end of chain marker */
}
else
{
chain[fat_last_clust_index ].number--;
fat_last_clust_index++; // = index + 1;
chain[fat_last_clust_index].cluster.l = chain[fat_last_clust_index - 1].cluster.l + chain[fat_last_clust_index - 1].number;
chain[fat_last_clust_index].number = 0; /* end of chain marker */
}
return OK;
}
/*F**************************************************************************
* NAME: fat_fopen
*----------------------------------------------------------------------------
* PARAMS:
* mode: READ: open file for read
* WRITE: open file for write
* return:
* - OK: file opened
* - KO: file not opened: - file is empty
* - low level read error
*----------------------------------------------------------------------------
* PURPOSE:
* Open the file in read or write mode
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_fopen (Byte mode)
{
fat_sector_flag = 0;
fat_nb_clust = 0 ;
if (mode == FILE_READ)
{
if (fat_cache.current.size.l == 0)
{
return KO; /* file empty */
}
else
{
fat_fchain_nb_clust = 0; /* start on first contiguous cl */
fat_fclust_byte_count = 0; /* byte 0 of cluster */
fat_fchain_index = 0; /* reset the allocation list variable */
/* get file allocation list */
fat_get_clusters(fclusters, MAX_FILE_FRAGMENT_NUMBER);
/* seek to the beginning of the file */
fat_open_mode = FILE_READ;
}
}
return OK;
}
/*F**************************************************************************
* NAME: fat_fclose
*----------------------------------------------------------------------------
* PURPOSE:
* Close opened file
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_fclose (void)
{
if (fat_open_mode == FILE_READ)
{
phy_read_close(); /* close reading */
}
else
{
#if COMPILE_FAT_WRITE == TRUE
fat_fclust_byte_count=0;
flag_end_disk_file = 0;
fat_open_mode = FILE_READ;
fat_file_size.l = fat_current_file_size;
if(fat_current_file_size == 0)
return ;
else
{
if(fat_file_size.l > 0x6400000)
fat_file_size.l = 0x6400000;
}
fat_update_fats(FALSE,&fclusters); /* Update entry and fat */
fat_create_file_name();
#endif//COMPILE_FAT_WRITE
}
}
/*F**************************************************************************
* NAME: fat_fgetsector
*----------------------------------------------------------------------------
* return:
* page read
*----------------------------------------------------------------------------
* PURPOSE:
* Read one page from file
*----------------------------------------------------------------------------
* NOTE:
* As this function is called very often it must be short and optimized
* in execution time
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_fgetsector (void)
{
Uint32 sectornum;
if((fat_nb_clust % fat_cluster_size) == 0)
{
fat_nb_clust = 0;
/* extract if necessary the next cluster from the allocation list */
if ((fclusters[fat_fchain_index].number == fat_fchain_nb_clust)&&(fclusters[0].number != 0))
{ /* new fragment */
fat_fchain_index++;
fat_fchain_nb_clust = 1;
//sectornum = fat_ptr_data + ((fclusters[fat_fchain_index].cluster.l) * fat_cluster_size);
if(fat_type_id == FAT_IS_32)
{
sectornum = fat_ptr_data + ((fclusters[fat_fchain_index].cluster.l) * fat_cluster_size);
}
else
{
sectornum = fat_ptr_data + ((fclusters[fat_fchain_index].cluster.w[0]) * ((Uint32)fat_cluster_size));
} /////////w[1]///////////////
phy_read_open(sectornum);
}
else
{ /* not new fragment */
if((fat_fchain_index == 0)&&(fat_fchain_nb_clust == 0))
{
//sectornum = fat_ptr_data + (fclusters[fat_fchain_index].cluster.l * fat_cluster_size);
if(fat_type_id == FAT_IS_32)
{
sectornum = fat_ptr_data + ((fclusters[fat_fchain_index].cluster.l) * fat_cluster_size);
}
else
{
sectornum = fat_ptr_data + ((fclusters[fat_fchain_index].cluster.w[0]) * ((Uint32)fat_cluster_size));
} ////////////w[1]////////////////
phy_read_open(sectornum);
}
fat_fchain_nb_clust++; /* one more cluster read */
}
}
phy_read_sector();
fat_nb_clust ++;
fat_fclust_byte_count += fat_sector_size; /* one more byte read */
}
/*F**************************************************************************
* NAME: fat_feof
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Return the file end flag
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_feof (void)
{
if (fat_fchain_index == fat_last_clust_index)
{
if (fat_open_mode == FILE_READ)
return (fat_fclust_byte_count >= (fat_cache.current.size.l ));
else
return flag_end_disk_file;
}
else
{
return FALSE;
}
}
/*F**************************************************************************
* NAME: fat_fseek_abs
*----------------------------------------------------------------------------
* PARAMS:
* offset: absolute seek offset in file
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Move ahead file read pointer of an openned file
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_fseek_abs (Uint32 offset)
{
Uint16 cluster_offset; /* offset size in clusters */
Byte index;
cluster_offset = offset / (((Uint32)fat_sector_size) * fat_cluster_size);
index = 0;
//fat_nb_clust = cluster_offset % fat_cluster_size;
while(cluster_offset > fclusters[index].number)
{
cluster_offset -= fclusters[index].number;
index++;
}
/* init file byte counter */
fat_fclust_byte_count = offset;
/* set the allocation list variable */
fat_fchain_nb_clust = cluster_offset;
fat_fchain_index = index;
fat_nb_clust = (offset/fat_sector_size) % fat_cluster_size ;
if(fat_type_id != FAT_IS_32)
{
fclusters[index].cluster.w[0] = 0; //w[1]////////////////////////////////////BIG LITTLE ENDIAN
}
phy_read_open(((Uint32)(fclusters[index].cluster.l + cluster_offset) * fat_cluster_size)
+ (offset / fat_sector_size) % fat_cluster_size + fat_ptr_data);
}
/*F**************************************************************************
* NAME: fat_goto_next
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* - OK: next file available
* - KO: last file reached
* - KO: low_level memory error
*----------------------------------------------------------------------------
* PURPOSE:
* Fetch the next dir/file info in cache
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_goto_next (void)
{
if(fat_dir_list_index >= (fat_dir_list_last - 1))
return KO;
fat_dir_list_index++;
fat_scan_dir(FALSE);
//fat_save_file_info(SEARCH_FILE);
if (fat_dir_seek(((Uint32)fat_dir_all_count) * DIR_SIZE) == OK)
{
fat_get_dir_entry(&fat_cache.current);// update current file info
return OK;
}
else
{
return KO; // low level error
}
}
/*F**************************************************************************
* NAME: fat_goto_prev
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* - OK: previous file available
* - KO: first file reached
* - KO: low_level memory error
*----------------------------------------------------------------------------
* PURPOSE:
* Fetch the previous directory info in cache
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_goto_prev (void)
{
Byte min;
if ((dir_is_root == FALSE)&&((fat_global_id & FILE_DIR) == FILE_DIR))
min = 2;
else
min = 0;
if(fat_dir_list_index <= min)
return KO;
fat_dir_list_index--;
fat_scan_dir(FALSE);
//fat_save_file_info(SEARCH_FILE);
if (fat_dir_seek(((Uint32)fat_dir_all_count) * DIR_SIZE) == OK)
{ // go to previous file
fat_get_dir_entry(&fat_cache.current);// update current file info
return OK;
}
else
return KO; // low level error
}
/*F**************************************************************************
* NAME: fat_goto_last
*----------------------------------------------------------------------------
* return:
* OK: last file available
* KO: low level error
*----------------------------------------------------------------------------
* PURPOSE:
* Fetch the last directory info in cache
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_goto_last (void)
{
if(fat_dir_list_last == 0)
return KO;
fat_dir_list_index = fat_dir_list_last - 1;
fat_scan_dir(FALSE);
//fat_save_file_info(SEARCH_FILE);
if (fat_dir_seek(((Uint32)fat_dir_all_count) * DIR_SIZE) == OK)
{
fat_get_dir_entry(&fat_cache.current);
return OK;
}
else
return KO; // low level error
}
/*F**************************************************************************
* NAME: fat_goto_first
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* - OK: first file found
* - KO: low level error
*----------------------------------------------------------------------------
* PURPOSE:
* Fetch the first directory info in cache
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte fat_goto_first (void)
{
if((dir_is_root == FALSE)&&((fat_global_id & FILE_DIR) == FILE_DIR))
{ // not root dir
if(fat_dir_list_last <= 2)
return KO;
fat_dir_list_index = 1; // point ".." entry
if (fat_dir_seek(DIR_SIZE) == OK)
{
fat_get_dir_entry(&fat_cache.parent); // update parent dir info
return fat_goto_next(); // update first file info
}
else
return KO; // low level error
}
else
{ // root diretory
if(fat_dir_list_last == 0)
return KO;
fat_dir_list_index = 0; // point first root entry
fat_scan_dir(FALSE);
// fat_save_file_info(SEARCH_FILE);
if (fat_dir_seek(((Uint32)fat_dir_all_count) * DIR_SIZE) == OK)
{
fat_get_dir_entry(&fat_cache.current);// update first file info
return OK;
}
else
{
return KO; // low level error
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -