📄 fdi_file.c
字号:
*### stream file stream identifier
*###
*### OUTPUTS:
*###
*### RETURNS:
*### Returns EOF if an error is detected; otherwise, it returns zero.
*###*/
int
FDI_fclose(FILE_ID stream)
{
COMMAND_CONTROL cmd_cntrl; /* command control structure for APIS */
ERR_CODE status = ERR_NONE; /* define ERROR_CODE variable */
STREAM_INFO *stream_info_ptr; /* pointer to table entry */
/* make sure file init function has been called */
if (FileInitComplete == FALSE)
{
FDI_errno = ERR_INIT;
return EOF;
}
/* make sure stream is valid */
if ((stream == 0) || (stream > NUM_OPEN_FILES))
{
FDI_errno = ERR_PARAM;
return EOF;
}
/* lock the File Manager API semaphore */
SEM_MTX_WAIT(FileAPIMutexSemaphore);
/* make sure stream is open */
if (StreamInfoTable[stream].mode_flag == MODE_INVALID)
{
FDI_errno = ERR_NOTOPEN;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
/* initialize STREAM_INFO pointer to reduce array math */
stream_info_ptr = &(StreamInfoTable[stream]);
cmd_cntrl.type = FILE_SUPPORT_DATA_TYPE;
cmd_cntrl.priority = FDI_MIN_PRIORITY;
/* check if file data stream is valid */
if (stream_info_ptr->open_file_info.data_id != FILE_INVALID)
{
/* prepare to call FDI_Close for the open file data stream */
cmd_cntrl.identifier = stream_info_ptr->open_file_info.data_id;
/* call FDI_Close to close the actual file data stream */
status = FDI_Close(&cmd_cntrl);
/* ignore return value incase file was never opened */
}
/* if the file size is zero, remove the file, no action was taken */
if (stream_info_ptr->open_file_info.size == 0)
{
status = FileDelete(stream_info_ptr->file_info_id,
stream_info_ptr->open_file_info.data_id, FALSE);
}
/* clear STREAM_INFO of all the file's information */
INIT_STREAM_INFO(stream_info_ptr);
if (status != ERR_NONE)
{
FDI_errno = status; /* log error for use by ferror */
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
else
{
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return FILE_NOERR;
}
} /* ENDOF FDI_fclose */
/*############################################################################
*### FDI_fseek
*###
*### DESCRIPTION:
*### The function FDI_fseek allows random access within a file. The first
*### argument must be an open stream. The second two arguments specify a
*### file position. The stream is positioned as requested and fseek
*### returns zero if successful.
*###
*### USAGE:
*### status = FDI_fseek(stream, offset, SEEK_SET);
*###
*### PARAMETERS:
*###
*### INPUTS:
*### stream file stream identifier
*### offset a signed integer specifying a number of characters
*### wherefrom a "seek code" indicating from what point in the file
*### offset should be measured; modes supported:
*### SEEK_SET = set file offset to offset
*### SEEK_CUR = set file offset to current plus offset
*### SEEK_END = set file offset to end plus offset (offset
*### must be <= 0 since seeking beyond current
*### end of file is not supported and returns
*### error)
*### OUTPUTS:
*###
*### RETURNS:
*### Returns zero if successful or EOF if an error occurs.
*###*/
int
FDI_fseek(FILE_ID stream, long offset, int wherefrom)
{
COMMAND_CONTROL cmd_cntrl; /* command control structure for APIS */
ERR_CODE status; /* define ERROR_CODE variable */
BYTE data_byte; /* used by FDI_Read call */
STREAM_INFO *stream_info_ptr; /* pointer to table entry */
/* make sure file init function has been called */
if (FileInitComplete == FALSE)
{
FDI_errno = ERR_INIT;
return EOF;
}
/* make sure stream is valid */
if ((stream == 0) || (stream > NUM_OPEN_FILES))
{
FDI_errno = ERR_PARAM;
return EOF;
}
/* lock the File Manager API semaphore */
SEM_MTX_WAIT(FileAPIMutexSemaphore);
/* make sure stream is open */
if (StreamInfoTable[stream].mode_flag == MODE_INVALID)
{
FDI_errno = ERR_NOTOPEN;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
/* initialize STREAM_INFO pointer to reduce array math */
stream_info_ptr = &(StreamInfoTable[stream]);
/* test if wherefrom is SEEK_SET */
if (wherefrom == SEEK_SET)
{
/*
* test parameter offset is less than file stream field file info
* field size, and greater than -1.
*/
if ((offset <= (long)stream_info_ptr->open_file_info.size) &&
(offset >= 0))
{
/* assign command control field offset to parameter offset */
cmd_cntrl.offset = (DWORD)offset;
}
/* input parameter error */
else
{
FDI_errno = ERR_PARAM;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
}
else if (wherefrom == SEEK_CUR) /* else if wherefrom is SEEK_CUR */
{
/*
* test parameter offset plus file stream field file_pointer is less
* than file stream field file info field size and greater than -1.
*/
if ((((long)stream_info_ptr->file_pointer + offset) <=
(long)stream_info_ptr->open_file_info.size) &&
(((long)stream_info_ptr->file_pointer + offset) >= 0))
{
/*
* assign command control field offset to parameter offset plus file
* stream field file_pointer
*/
cmd_cntrl.offset = (DWORD)((long)stream_info_ptr->file_pointer +
offset);
}
else /* input parameter error */
{
FDI_errno = ERR_PARAM;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
}
else if (wherefrom == SEEK_END) /* else if wherefrom is SEEK_END */
{
/*
* test parameter offset plus file stream field file info field
* size is less than file stream field file info field size and
* greater than -1.
*/
if ((((long)stream_info_ptr->open_file_info.size + offset) <=
(long)stream_info_ptr->open_file_info.size) &&
(((long)stream_info_ptr->open_file_info.size + offset) >= 0))
{
/*
* assign command control field offset to parameter offset plus file
* stream field file info field size
*/
cmd_cntrl.offset = (DWORD)((long)stream_info_ptr->open_file_info.size +
offset);
}
else /* input parameter error */
{
FDI_errno = ERR_PARAM;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
}
else /* wherefrom is not SEEK_SET, SEEK_CUR or SEEK_END return an error */
{
FDI_errno = ERR_PARAM;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
/* prepare to use read to setup the internal FDI_OpenStream structure */
cmd_cntrl.buffer_ptr = &data_byte;
cmd_cntrl.count = 0;
cmd_cntrl.identifier = stream_info_ptr->open_file_info.data_id;
cmd_cntrl.type = FILE_SUPPORT_DATA_TYPE;
cmd_cntrl.priority = FDI_MIN_PRIORITY;
/* call FDI_Read to setup the current_file_offset in FDI_OpenStream */
status = FDI_Read(&cmd_cntrl);
if (status != ERR_NONE)
{
FDI_errno = status;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
/* set the new location of the file pointer */
stream_info_ptr->file_pointer = cmd_cntrl.offset;
/* clear end-of-file indicator */
stream_info_ptr->read_eof = FILE_NOEOF;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return FILE_NOERR;
} /* ENDOF FDI_seek */
/*############################################################################
*### FDI_findfirst
*###
*### DESCRIPTION:
*### The function FDI_findfirst begins a search for files specified by
*### wildcards. The parameter filename_ptr is a string specifying the
*### file name. Wildcard match characters (* and ?) are supported. The
*### parameter fileinfo_ptr is a pointer to the type FILE_INFO which is
*### filled with the file information.
*###
*### USAGE:
*### status = FDI_findfirst("*.ext", &the_file_info);
*###
*### PARAMETERS:
*###
*### INPUTS:
*### filename_ptr const character string for file name specifier
*###
*### OUTPUTS:
*### fileinfo_ptr pointer to type FILE_INFO structure filled with
*### located file information
*###
*### RETURNS:
*### Returns zero if successful in finding a file matching the search of
*### filename_ptr, otherwise it returns EOF.
*###*/
int
FDI_findfirst(const FDI_TCHAR *filename_ptr, FILE_INFO *fileinfo_ptr)
{
ERR_CODE status; /* define ERROR_CODE variable */
/* make sure file init function has been called */
if (FileInitComplete == FALSE)
{
FDI_errno = ERR_INIT;
return EOF;
}
/* make sure filename_ptr is valid length
* can't call FileNameValid since filename_ptr can contain wild cards
*/
if ((FM_GetStringLength(filename_ptr) == 0) ||
(FM_GetStringLength(filename_ptr) > FILE_NAME_SIZE))
{
FDI_errno = ERR_PARAM;
return EOF;
}
/* lock the File Manager API semaphore */
SEM_MTX_WAIT(FileAPIMutexSemaphore);
/* initialize or clear global STREAM_INFO structure search info */
INIT_STREAM_INFO(SearchInfoPtr);
/* save the file name to the global search name */
FM_CopyString(FLTSearchName, filename_ptr, (FILE_NAME_SIZE + 1));
/* look for an existing file matching file_name from wildcard search */
status = FileNameSearch(FLTSearchName,
&(SearchInfoPtr->file_info_id),
&(SearchInfoPtr->open_file_info));
if (status == ERR_NONE)
{
/* copy file info structure to parameter fileinfo_ptr */
MemoryMove((BYTE_PTR)fileinfo_ptr,
(BYTE_PTR)&(SearchInfoPtr->open_file_info),
sizeof(FILE_INFO));
/* save the info structure's identifier in data_id field of FILE_INFO */
fileinfo_ptr->data_id = SearchInfoPtr->file_info_id;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return FILE_NOERR;
} /* ENDIF FileNameSearch successful */
else
{
INIT_STREAM_INFO(SearchInfoPtr);
MemorySet((BYTE_PTR)FLTSearchName, INIT_BYTE, sizeof(FLTSearchName));
FDI_errno = status;
/* unlock the File Manager API semaphore */
SEM_MTX_POST(FileAPIMutexSemaphore);
return EOF;
}
} /* ENDOF FDI_findfirst */
/*############################################################################
*### FDI_findnext
*###
*### DESCRIPTION:
*### The function FDI_findnext fetches su
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -