📄 imf.c
字号:
return (bytes_to_write);
}
/****************************************************************************
*
* FUNCTION
*
* IMF_Delete
*
* DESCRIPTION
*
* This function will delete a file of the file descriptor from file
* buffer.
*
* INPUTS
*
* name Name of the file to delete.
*
* OUTPUTS
*
* Upon successful completion, the function returns NU_SUCCESS,
* otherwise, possible reasons for failure follow:
*
* IMF_NO_FILE The file does not exist.
* IMF_FILE_OPEN The file is open.
*
***************************************************************************/
INT IMF_Delete(CHAR *name)
{
INT i;
INT size;
/* If the name is longer than 12, truncate */
if((size = strlen((CHAR*)name)) > NAME_LENGTH)
size = NAME_LENGTH;
/* Look for the file */
for (i = 0; i < MAX_FILES; i++)
{
if (strncmp(file_desc_ary[i].name, (CHAR*)name, (unsigned int)size) == 0)
break;
}
/* If we did not find the file */
if (i >= MAX_FILES)
{
p_errno = IMF_NO_FILE;
return (-1);
}
/* If the file is open, we cannot delete it */
if (file_desc_ary[i].open)
{
p_errno = IMF_FILE_OPEN;
return (-1);
}
/* Reset the file */
file_desc_ary[i].used = 0;
file_desc_ary[i].open = 0;
file_desc_ary[i].flag = 0;
UTL_Zero(file_desc_ary[i].name, NAME_LENGTH + 1);
return (0);
}
/****************************************************************************
*
* FUNCTION
*
* IMF_Read
*
* DESCRIPTION
*
* This function reads data from a file into the user's buffer.
*
* INPUTS
*
* buffer Pointer to buffer to store data in.
* buffersize Amount of data to read.
* file_desc File descriptor of the file to read the data from.
*
* OUTPUTS
*
* Upon successful completion, the function returns the amount of data
* read from the file; otherwise, it returns 0 and sets p_errno to
* one of the following error codes:
*
* IMF_INVAL_DESC The file descriptor is either larger
* than MAX_FILES or negative.
* IMF_FILE_WRITEONLY The file is write only.
* IMF_INVAL_BUFFER The buffersize is either 0 or the buffer
* is NULL.
* IMF_NO_FILE The file does not exist.
* IMF_FILE_CLOSED The file is closed.
* IMF_END_FILE The file pointer is at the end of the file.
*
***************************************************************************/
INT32 IMF_Read(CHAR *buffer, INT buffersize, INT file_desc)
{
MEM_FILE *fptr;
INT bytes_to_read, init_bytes, bytes_left;
/* Validate the file descriptor */
if((file_desc >= MAX_FILES) || (file_desc < 0))
{
p_errno = IMF_INVAL_DESC;
return (0);
}
/* If the file is write only, return with error */
else if (file_desc_ary[file_desc].flag & IMF_WRONLY)
{
p_errno = IMF_FILE_WRITEONLY;
return (0);
}
/* Verify that the buffersize and buffer ptr are valid */
if((buffersize == 0) || (buffer == NU_NULL))
{
p_errno = IMF_INVAL_BUFFER;
return (0);
}
/* Setup a pointer to a file descriptor */
fptr = &file_desc_ary[file_desc];
/* Make sure that this file exists and that it is currently open */
if(fptr -> used == 0)
{
p_errno = IMF_NO_FILE;
return (0);
}
else if(fptr -> open == 0)
{
p_errno = IMF_FILE_CLOSED;
return (0);
}
/* If there is no data to read return 0 bytes read */
if ((fptr -> length - (UINT32)(fptr -> file_ptr - fptr -> start)) == 0)
{
p_errno = IMF_END_FILE;
return (0);
}
/* Is there enough data in this file to fill the entire buffer. If not
don't read data from beyond the end of the file */
if((fptr -> length - (UINT32)(fptr -> file_ptr - fptr -> start)) < (UINT32)buffersize)
bytes_to_read = (INT)(fptr -> length - (UINT32)(fptr -> file_ptr - fptr -> start));
else
bytes_to_read = buffersize;
/* At this point we know how much data can be read from this file. Check to
see if a wrap is required to read all of the data */
if((fptr -> end - fptr -> file_ptr) >= bytes_to_read)
{
/* Copy the data into the user's buffer */
memcpy(buffer, fptr -> file_ptr, (unsigned int)bytes_to_read);
/* Move the file_ptr forward to fresh data */
fptr -> file_ptr += bytes_to_read;
}
/* Otherwise, a wrap is required to read all data */
else
{
/* Find out how much room is left before the end is reached. */
init_bytes = (INT)(fptr -> end - fptr -> file_ptr);
/* Copy the data into the user's buffer. */
memcpy(buffer, fptr -> file_ptr, (unsigned int)init_bytes);
/* Wrap around to the start of the file. */
fptr -> file_ptr = fptr -> start;
/* Update the buffer ptr. */
buffer += init_bytes;
/* Calculate the amount of data that is left to read. */
bytes_left = bytes_to_read - init_bytes;
/* Copy the rest of the data into the user's buffer. */
memcpy(buffer, fptr -> file_ptr, (unsigned int)bytes_left);
/* Update the file_ptr. */
fptr -> file_ptr += bytes_left;
}
/* Return the number of bytes read from the file. If the read was to the
end of the file but not past it, another read will be necessary before a
user knows the end of the file has been reached, that is the next call to
read will result in 0 being returned.
*/
return (bytes_to_read);
}
/****************************************************************************
*
* FUNCTION
*
* IMF_Seek
*
* DESCRIPTION
*
* This function will move the read and write file pointers offset
* bytes from the origin specified.
*
* INPUTS
*
* file_desc File descriptor of the file to write the data to.
* offset Number of bytes to move the origin to.
* origin Where to begin - PSEEK_SET, PSEEK_CUR, PSEEK_END
*
* OUTPUTS
*
* Upon successful completion, the function returns the new offset.
* Otherwise, the function returns -1 and sets p_errno to one of the
* following:
*
* IMF_FILE_CLOSED The file is closed.
* IMF_INVAL_DESC The file descriptor is invalid.
* IMF_END_FILE The end of the file was reached before the
* pointer could be moved offset bytes from the
* origin.
*
***************************************************************************/
INT32 IMF_Seek(INT file_desc, INT32 offset, INT16 origin)
{
UINT32 new_offset;
/* If the file is not open, return an error */
if(file_desc_ary[file_desc].open == 0)
{
p_errno = IMF_FILE_CLOSED;
return (-1);
}
/* Validate the file descriptor */
if(file_desc >= MAX_FILES)
{
p_errno = IMF_INVAL_DESC;
return (-1);
}
else if (file_desc < 0)
{
p_errno = IMF_INVAL_DESC;
return (-1);
}
/* If the origin is the beginning of the file, set the new_offset */
if (origin == PSEEK_SET)
new_offset = (UINT32) file_desc_ary[file_desc].start + offset;
/* If the origin is at the current position of the file pointer in
* the file, set the new_offset
*/
else if (origin == PSEEK_CUR)
new_offset = (UINT32) file_desc_ary[file_desc].file_ptr + offset;
/* Otherwise, the origin is the end of the file, calculate the new_offset */
else
new_offset = file_desc_ary[file_desc].length + offset;
/* If the new offset is less than or equal to the end of the file, set the
* file pointer to new_offset and return the new_offset
*/
if (new_offset <= (UINT32)file_desc_ary[file_desc].eof_ptr)
{
file_desc_ary[file_desc].file_ptr = (CHAR HUGE*)new_offset;
return(new_offset);
}
/* Otherwise, return an error */
else
{
p_errno = IMF_END_FILE;
return (-1);
}
}
/****************************************************************************
*
* FUNCTION
*
* IMF_Rename
*
* DESCRIPTION
*
* Changes the name in current name to rename path. The file must be
* closed to rename it.
*
* INPUTS
*
* rename_path New name
* curr_name Current name
*
* OUTPUTS
*
* Upon successful completion, the function returns NU_SUCCESS,
* otherwise, the function returns -1 and sets p_errno to one of the
* following:
*
* IMF_INVAL_DESC The file descriptor is invalid.
* IMF_FILE_OPEN The file is open.
*
***************************************************************************/
INT IMF_Rename(CHAR *rename_path, CHAR *curr_name)
{
INT i;
INT size;
/* Set size to length of curr_name or 12 if greater than 11 */
if((size = strlen((CHAR*)curr_name)) > NAME_LENGTH)
size = NAME_LENGTH;
/* Find the file */
for (i = 0; i < MAX_FILES; i++)
{
if (strncmp(file_desc_ary[i].name, (CHAR*)curr_name, (unsigned int)size) == 0)
break;
}
/* If we were not able to find the file, return an error */
if (i >= MAX_FILES)
{
p_errno = IMF_INVAL_DESC;
return (-1);
}
/* If the file is open, return an error */
if (file_desc_ary[i].open)
{
p_errno = IMF_FILE_OPEN;
return (-1);
}
/* Clear out the old name */
UTL_Zero(file_desc_ary[i].name, NAME_LENGTH + 1);
/* Set size to length of rename_path or 12 if greater than 12 */
if((size = strlen((CHAR*)rename_path)) > NAME_LENGTH)
size = NAME_LENGTH;
/* Set the new name and NULL Terminate it */
memmove(file_desc_ary[i].name, rename_path, (unsigned int)size);
file_desc_ary[i].name[size] = NU_NULL;
return (NU_SUCCESS);
}
/****************************************************************************
*
* FUNCTION
*
* IMF_Store_Name
*
* DESCRIPTION
*
* Stores the file name according to the file system's
* implementation standard.
*
* INPUTS
*
* name Name of the file
* r_file Pointer to the file data structure
*
* OUTPUTS
*
* Upon successful completion, the function returns NU_SUCCESS and the
* name is stored as specified in name. Otherwise, the function returns
* -1 and sets p_errno to one of the following:
*
* IMF_FILE_OPEN The file is currently open
* IMF_NO_FILE The file does not exist
*
***************************************************************************/
INT IMF_Store_Name(CHAR *name, MEM_FILE *r_file)
{
INT size;
/* If the file is open, return an error */
if (r_file -> open == 1)
{
p_errno = IMF_FILE_OPEN;
return (-1);
}
if (r_file -> used == 0)
{
p_errno = IMF_NO_FILE;
return (-1);
}
/* Set size to length of curr_name or 12 if greater than 12 */
if((size = strlen(name)) > NAME_LENGTH)
size = NAME_LENGTH;
/* Clear out the old name */
UTL_Zero(r_file->name, NAME_LENGTH + 1);
/* Set the new name and NULL Terminate it */
memmove(r_file -> name, name, (unsigned int)size);
r_file -> name[size] = NU_NULL;
return(NU_SUCCESS);
}
/****************************************************************************
*
* FUNCTION
*
* IMF_Fgets
*
* DESCRIPTION
*
* The functions reads a specific amount of data and returns either
* NULL or the character number of bytes.
*
* INPUTS
*
* line Pointer to the buffer of data to write.
* size Amount of data to write.
* file File descriptor of the file to write the data to.
*
* OUTPUTS
*
* Upon successful completion, the function returns a character.
* Otherwise the function returns 0.
*
****************************************************************************/
CHAR * IMF_Fgets(CHAR *line, INT16 size, INT file)
{
CHAR msg=0;
INT16 count;
CHAR string[512];
count = 0;
msg=0;
/* This function was written to search the file either an end of line
* marker or an EOF marker
*/
while ((!(msg == (CHAR)0x0a)) && (!(msg ==(CHAR)IMF_EOF)) && (count != size))
{
IMF_Read(&msg, 1, file);
string[count] = msg;
count++;
}
/* Send back NULL if end of file marker found. */
if (msg == (CHAR)IMF_EOF)
return(0);
string[count]='\0';
/* Otherwise copy the string into line variable */
strcpy((CHAR *)line,(CHAR *)string);
/* Return a Non-Null character value */
return((CHAR *)1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -