📄 zfscfileapi.c
字号:
/*
* File : ZFSCFileApi.c
* Description: This file contains the "C" file API implementation
* Author : Mahadev K C
* Created on : 25-APR-2003
*
* Copyright 2004 ZiLOG Inc. ALL RIGHTS RESERVED.
*
* This file contains unpublished confidential and proprietary information
* of ZiLOG, Inc.
* NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED
* IN ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC.
* This is not a license and no use of any kind of this work is authorized
* in the absence of a written license granted by ZiLOG, Inc. in ZiLOG's
* sole discretion
*/
#ifdef FS_DEBUG
#include <stdio.h>
#endif
#include "zfsdef.h"
#include "cfileapi.h"
#include "zfsapi.h"
#include "zfstruct.h"
#define SIZE_T_DEFINED
#include <string.h>
// Function Name: fopen
// Description: This function will open a file for reading/writing. Returns the handle on success or NULL on failure
FILE *fopen( const INT8 *filename, const INT8 *mode )
{
ZFS_HANDLE_t handle ;
ZFS_STATUS_t status ;
UINT8 new_mode ;
UINT8 new_attr = ZFS_MODE_ASCII ;
UINT len ;
#ifdef FS_DEBUG
printf(" fopen start") ;
#endif
len = strlen( mode ) ;
if( len == 0 || len > 3 )
{
#ifdef FS_DEBUG
printf(" fopen end") ;
#endif
return NULL ;
}
// validate the input values.
if( *mode == 'r' )
{
new_mode = ZFS_READ ;
}
else if( *mode == 'w' )
{
new_mode = ZFS_WRITE ;
}
else if( *mode == 'a' )
{
new_mode = ZFS_APPEND ;
}
else
{
#ifdef FS_DEBUG
printf(" fopen end") ;
#endif
return NULL ;
}
if( len > 1 )
{
mode ++ ;
if( *mode == 'b' )
{
new_attr = ZFS_MODE_BINARY ;
}
else if( *mode == '+' && new_mode == ZFS_READ )
{
new_mode = ZFS_READ_WRITE ;
}
else
{
#ifdef FS_DEBUG
printf(" fopen end") ;
#endif
return NULL ;
}
}
if( len > 2 )
{
mode++;
if( *mode == 'b' && new_mode == ZFS_READ_WRITE )
new_attr = ZFS_MODE_BINARY ;
else
{
#ifdef FS_DEBUG
printf(" fopen end") ;
#endif
return NULL ;
}
}
handle = ZFSOpen( (INT8 *)filename, new_mode, new_attr ) ;
if( new_mode == ZFS_APPEND && handle == NULL )
{
// this implements the functionality of "a" of WINDOWS
//Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn抰 exist.
// check if ZFSOpen returns any error that indicates a file does not exist
// so create one file.
handle = ZFSOpen( (INT8 *)filename, ZFS_WRITE, new_attr ) ;
if( handle )
{
status = ZFSClose( handle ) ;
if( status == ZFSERR_SUCCESS )
{
handle = ZFSOpen( (INT8 *)filename, ZFS_APPEND, new_attr ) ;
}
else
handle = NULL ;
}
}
#ifdef FS_DEBUG
printf(" fopen end") ;
#endif
return handle;
}
// Function Name: fclose
// Description: This function will close an opened file
INT fclose( FILE *stream )
{
#ifdef FS_DEBUG
printf(" fclose start") ;
#endif
if( ZFSClose( stream ) == ZFSERR_SUCCESS )
{
#ifdef FS_DEBUG
printf(" fclose end") ;
#endif
return 0 ;
}
else
{
#ifdef FS_DEBUG
printf(" fclose end") ;
#endif
return -1 ;
}
}
// Function Name: fputc
// Description: This function will write a character to the opened file.
INT fputc( INT c, FILE *stream )
{
UINT8 cdata = ( UINT8 ) c ;
INT32 bytes_written ;
#ifdef FS_DEBUG
printf(" fputc start") ;
#endif
bytes_written = ZFSWrite( stream, &cdata, 1 ) ;
if( bytes_written == 0 )
{
#ifdef FS_DEBUG
printf(" fputc end") ;
#endif
return -1 ;
}
else
{
#ifdef FS_DEBUG
printf(" fputc end") ;
#endif
return c ;
}
}
// Function Name: fputs
// Description: This function will store the given string to the opened file
INT fputs( const INT8 *string, FILE *stream )
{
UINT len = strlen( string ) ;
INT32 bytes_written ;
#ifdef FS_DEBUG
printf(" fputs start") ;
#endif
bytes_written = ZFSWrite( stream, ( UINT8 *) string, len ) ;
#ifdef FS_DEBUG
printf(" fputs end") ;
#endif
if( bytes_written > 0 )
return bytes_written ;
else
return -1 ;
// return (int) bytes_written ;
}
// Function Name: fgetc
// Description: This function will get a character from opened file
INT fgetc( FILE *stream )
{
UINT8 cdata ;
INT32 bytes_read ;
#ifdef FS_DEBUG
printf(" fgetc start") ;
#endif
bytes_read = ZFSRead( stream, &cdata, 1 ) ;
if( bytes_read == ZFSERR_INVALID_OPERATION)
{
#ifdef FS_DEBUG
printf(" fgetc end") ;
#endif
return -1 ;
}
else
{
#ifdef FS_DEBUG
printf(" fgetc end") ;
#endif
return cdata ;
}
}
// Function Name: fgets
// Description: This function will get the string from the opened file
INT8 *fgets( INT8 *string, INT n, FILE *stream )
{
// UINT8 cdata ;
INT32 bytes_read ;
INT8 *ptr = string ;
#ifdef FS_DEBUG
printf(" fgets start") ;
#endif
bytes_read = ZFSRead( stream, ( UINT8*) string, n) ;
if( bytes_read == 0 )
{
#ifdef FS_DEBUG
printf(" fgets end") ;
#endif
return ( INT8 * ) NULL ;
}
else
{
while( n >= 0 )
{
//if( *ptr == '\n' || *ptr == '\r' )
if(*ptr == EOF)
{
*ptr = 0x00 ;
break ;
}
n-- ;
ptr++ ;
}
#ifdef FS_DEBUG
printf(" fgets end") ;
#endif
return string ;
}
}
// Function Name: fread
// Description: This function will get the required number of items from the file
size_t fread( void *buffer, size_t size, size_t count, FILE *stream )
{
INT32 bytes_read ;
size_t new_cnt ;
UINT32 bytes_to_read = size * count ;
#ifdef FS_DEBUG
printf(" fread start") ;
#endif
bytes_read = ZFSRead( stream, ( UINT8 * ) buffer, bytes_to_read ) ;
new_cnt = bytes_read / size ;
#ifdef FS_DEBUG
printf(" fread end") ;
#endif
return new_cnt ;
}
// Function Name: fwrite
// Description: This function will write the required number of items into the file
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream )
{
INT32 bytes_written ;
UINT32 bytes_to_write = size * count ;
size_t new_cnt ;
#ifdef FS_DEBUG
printf(" fwrite start") ;
#endif
bytes_written = ZFSWrite( stream, (UINT8 *)buffer, bytes_to_write ) ;
new_cnt = bytes_written / size ;
#ifdef FS_DEBUG
printf(" fwrite end") ;
#endif
return new_cnt ;
}
// Function Name: fseek
// Description: This function will move the file pointer appropriately
INT fseek( FILE *stream, INT32 offset, INT origin )
{
ZFS_STATUS_t status ;
#ifdef FS_DEBUG
printf(" fseek start") ;
#endif
status = ZFSSeek( stream, offset, origin) ;
#ifdef FS_DEBUG
printf(" fseek end") ;
#endif
return (INT) status ;
}
// Function Name: ftell
// Description: This function will return the file pointer position
INT32 ftell( FILE *stream )
{
#ifdef FS_DEBUG
printf(" ftell start") ;
#endif
// just validate the stream handle
if( ( ( PZFS_OPEN_REC_t ) stream)->status != ZFS_OR_MAGIC_NUM )
{
#ifdef FS_DEBUG
printf(" ftell end") ;
#endif
return -1 ;
}
#ifdef FS_DEBUG
printf(" ftell end") ;
#endif
return ( INT32 ) ( ( PZFS_OPEN_REC_t ) stream)->offset ;
}
// Function Name: feof
// Description: This function will check whether it is end of file or not
INT feof( FILE *stream )
{
#ifdef FS_DEBUG
printf(" feof start") ;
#endif
if( ( ( PZFS_OPEN_REC_t) stream)->offset == ( ( PZFS_OPEN_REC_t) stream)->size )
{
#ifdef FS_DEBUG
printf(" feof end") ;
#endif
return -1 ;
}
else
{
#ifdef FS_DEBUG
printf(" feof end") ;
#endif
return 0 ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -