📄 file_sys.c~
字号:
while((*sp != '\\') && (*sp != 0))
{
#ifdef _DEBUG_CHDIR_
_FF_printf(_FF_ChDirValStr, toupper(*sp));
#endif
#if defined(_IAR_EWAVR_)
*qp = toupper(*sp);
qp++; sp++;
#else
*qp++ = toupper(*sp++);
#endif
}
#if defined(_IAR_EWAVR_)
*qp = '\\';
qp++;
*qp = 0;
if((sp[0]==0) || (sp[1]==0))
#else
*qp++ = '\\';
*qp = 0;
if((sp[0]==0) || (sp[1]==0))
#endif
{
break;
}
}
if(*sp) /* if there is more increment */
sp++;
}
return(0);
}
#endif /*defined(_DIRECTORIES_SUPPORTED_)*/
/****************************************************************************
**
** Opens a file for read/write access
**
** Parameters: fname, pointer to the string of the filename/dir path
** fmode, mode to open the file (READ/WRITE/MODIFY)
**
** Returns: FILE pointer location for the open file if successful
** NULL - an error occured
**
****************************************************************************/
#if defined(_CVAVR_) || defined(_ICCAVR_) || defined(_ROWLEY_CWAVR_)
FILE *fopenc(flash int8 *fname, uint8 fmode)
#elif defined(_IAR_EWAVR_)
FILE *fopenc(PGM_P fname, uint8 fmode)
#endif
{
int8 temp_path[_FF_PATH_LENGTH];
_FF_strcpyf(temp_path, fname);
return(fopen(temp_path, fmode));
}
/****************************************************************************
**
** Opens a file for read/write access
**
** Parameters: fname, pointer to the string of the filename/dir path
** fmode, mode to open the file (READ/WRITE/MODIFY)
**
** Returns: FILE pointer location for the open file if successful
** NULL - an error occured
**
****************************************************************************/
FILE *fopen(int8 *fname, uint8 fmode)
{
#ifdef _DIRECTORIES_SUPPORTED_
int8 fpath[14];
uint32 addr_temp;
#endif
FILE *rp;
#ifdef _BIG_ENDIAN_
HiLo32Union temp32;
#endif
#ifdef _READ_ONLY_
if(fmode != READ)
{
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 1);
#endif
return(NULL);
}
#endif
rp = _FF_MALLOC();
if(rp == 0)
{
/* Could not allocate requested memory */
_FF_error = ALLOC_ERR;
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 2);
#endif
return(NULL);
}
#ifdef _DIRECTORIES_SUPPORTED_
addr_temp = _FF_DirAddr; /* save local dir addr */
if(_FF_checkdir(fname, fpath))
{
_FF_DirAddr = addr_temp;
_FF_free(rp);
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 3);
#endif
return(NULL);
}
if(fpath[0] == 0)
{
_FF_DirAddr = addr_temp;
_FF_free(rp);
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 4);
#endif
return(NULL);
}
(rp->file_dep) = scan_directory(fpath, SCAN_FILE_R); /* Find File */
#else
(rp->file_dep) = scan_directory(fname, SCAN_FILE_R); /* Find File */
#endif
if((rp->file_dep) == NULL)
{
/* An Error Occurred, No file found */
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
_FF_free(rp);
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 5);
#endif
return(NULL);
}
#ifndef _READ_ONLY_
if((!(fmode == READ)) && ((rp->file_dep)->attr & ATTR_READ_ONLY))
{
/* if writing to file verify it is not "READ ONLY" */
_FF_error = MODE_ERR;
_FF_free(rp);
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 7);
#endif
return(NULL);
}
#endif
/* Save Starting Cluster */
#ifndef _BIG_ENDIAN_
rp->clus_start = (rp->file_dep)->start_clus_lo;
#else
temp32.uval8.ml = (rp->file_dep)->start_clus_lo.uval8.lo;
temp32.uval8.lo = (rp->file_dep)->start_clus_lo.uval8.hi;
rp->clus_start = temp32.uval16.lo;
#endif
/* Set Current Cluster */
rp->clus_current = rp->clus_start;
/* Set Previous Cluster to 0 (indicating @start) */
rp->clus_prev = 0;
/* Save file length */
#ifndef _BIG_ENDIAN_
rp->length = (rp->file_dep)->file_size;
#else
temp32.uval8.hi = (rp->file_dep)->file_size.uval8.lo;
temp32.uval8.mh = (rp->file_dep)->file_size.uval8.ml;
temp32.uval8.ml = (rp->file_dep)->file_size.uval8.mh;
temp32.uval8.lo = (rp->file_dep)->file_size.uval8.hi;
rp->length = temp32.uval32;
#endif
/* Set Current Position to 0 */
rp->position = 0;
#ifndef _READ_ONLY_
if((rp->length == 0) && (rp->clus_start == 0))
{
/* Check for Blank File */
#ifndef _READ_ONLY_
if(fmode == READ)
#endif
{
/* IF trying to open a blank file to read, ERROR */
_FF_error = MODE_ERR;
_FF_free(rp);
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 11);
#endif
return(NULL);
}
/* Setup blank FILE characteristics */
#ifndef _READ_ONLY_
fmode = WRITE;
#endif
}
if(fmode == WRITE)
{
/* Change file to blank */
if(rp->clus_start)
{
/* Write new directory info so that the file entry is now blank */
#ifndef _BIG_ENDIAN_
(rp->file_dep)->file_size = 0;
(rp->file_dep)->start_clus_lo = 0;
#else
(rp->file_dep)->file_size.uval32 = 0;
(rp->file_dep)->start_clus_lo.uval16 = 0;
#endif
if(_FF_write(_FF_ScanAddr, _FF_Buff))
{
_FF_free(rp);
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 8);
#endif
return(NULL);
}
rp->length = 0; /* Clear length in pointer */
/* Erase the cluster chain of the current file */
if(erase_clus_chain(rp->clus_start))
{
_FF_free(rp);
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 9);
#endif
return(NULL);
}
rp->clus_start = 0; /* Clear starting cluster in pointer */
rp->clus_current = 0;
}
}
else
#endif
{
/* Set and save next cluster # */
rp->clus_next = next_cluster(rp->clus_current, SINGLE);
if(NextClusterValidFlag == 0)
{
/* Not valid cluster */
_FF_free(rp);
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 10);
#endif
return(NULL);
}
}
/* Save the file offset to read entry */
rp->entry_sec_addr = _FF_ScanAddr;
/* Set sector offset to 0 */
rp->sec_offset = 0;
#ifndef _READ_ONLY_
if(fmode == APPEND)
{
rp->EOF_flag = 1;
if(fseek(rp, 0,SEEK_END) == (int16) EOF)
{
_FF_free(rp);
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 12);
#endif
return(NULL);
}
}
else
{
/* Set pointer to the begining of the file */
if(fmode == READ)
#endif
{
#ifndef _NO_FILE_DATA_BUFFER_
if(_FF_read(clust_to_addr(rp->clus_current), rp->buff))
#else
if(_FF_read(clust_to_addr(rp->clus_current), _FF_Buff))
#endif
{
_FF_free(rp);
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
#ifdef _DEBUG_FOPEN_
_FF_printf(_FF_FOpenDebugStr, 13);
#endif
return(NULL);
}
}
#ifndef _NO_FILE_DATA_BUFFER_
rp->pntr = rp->buff;
#else
rp->pntr = _FF_Buff;
#endif
#ifndef _READ_ONLY_
}
#endif
rp->mode = fmode;
_FF_error = NO_ERR;
#ifdef _DIRECTORIES_SUPPORTED_
_FF_DirAddr = addr_temp;
#endif
#ifdef _DEBUG_FCREATE_
_FF_printf("\r\nAddress Entry @ 0x%lX - FOPEN", _FF_ScanAddr << 9);
dump_file_entry_hex(rp->file_dep);
_FF_printf("\r\nfopen success mode - %d:", rp->mode);
_FF_printf("\r\n clus_start - %02X", rp->clus_start);
_FF_printf("\r\n size - %ld", rp->length);
#endif
return(rp);
}
#if !defined(_READ_ONLY_)
/****************************************************************************
**
** Creates and opens a file in WRITE mode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -