📄 efs.c
字号:
/*
* Copyright 2006 by Texas Instruments Incorporated.
* All rights reserved. Property of Texas Instruments Incorporated.
* Restricted rights to use, duplicate or disclose this code are
* granted through contract.
*
* @(#) TCP/IP_Network_Developers_Kit 1.91.00.08 08-22-2006 (ndk-a08)
*/
//--------------------------------------------------------------------------
// OS Demonstration Software
//--------------------------------------------------------------------------
// EFS.C
//
// Embedded File System
//
// This file contains a file IO API similar to standard C, which allows
// it to be easily ported to a system which has its own file system.
//
// Author: Michael A. Denio
// Copyright 1999, 2001 by Texas Instruments Inc.
//--------------------------------------------------------------------------
#include <netmain.h>
#include <_oskern.h>
//
// Internal File Header
//
typedef struct _fileheader
{
uint Type;
struct _fileheader *pNext;
char Filename[EFS_FILENAME_MAX];
INT32 Length;
INT32 RefCount;
uint Flags;
#define EFSFLG_DESTROY_PENDING 0x0001
EFSFUN pMemMgrCallback;
UINT32 MemMgrArg;
UINT8 *pData;
} FILEHEADER;
//
// Internal File Pointer
//
typedef struct _fileptr
{
uint Type;
struct _fileheader *pfh;
INT32 Pos;
} FILEPTR;
static FILEHEADER *pRoot = 0;
//--------------------------------------------------------------------
// Local Functions
//--------------------------------------------------------------------
static FILEHEADER *efs_findfile( char *name );
static void efs_internal_remove( FILEHEADER *pfh );
//--------------------------------------------------------------------
// efs_findfile
//
// Finds file by name, and potentially removes file
//
// CALLED IN KERNEL MODE
//--------------------------------------------------------------------
FILEHEADER *efs_findfile( char *name )
{
FILEHEADER *pfh;
// Find
pfh = pRoot;
while( pfh )
{
if( !(pfh->Flags & EFSFLG_DESTROY_PENDING) &&
!stricmp( name, pfh->Filename ) )
break;
pfh = pfh->pNext;
}
return( pfh );
}
//--------------------------------------------------------------------
// efs_internal_remove
//
// Removes file
//
// CALLED IN KERNEL MODE
//--------------------------------------------------------------------
static void efs_internal_remove( FILEHEADER *pfh )
{
FILEHEADER *pfhTmp;
FILEHEADER *pfhPrev;
pfhTmp = pRoot;
pfhPrev = 0;
while( pfhTmp )
{
if( pfhTmp == pfh )
break;
else
{
pfhPrev = pfhTmp;
pfhTmp = pfhTmp->pNext;
}
}
if( !pfhTmp )
return;
// Remove
// pfh is entry to remove
if( !pfhPrev )
pRoot = pfh->pNext;
else
pfhPrev->pNext = pfh->pNext;
// Zap type for debug
pfh->Type = 0;
if( pfh->pMemMgrCallback )
(pfh->pMemMgrCallback)( pfh->MemMgrArg );
mmFree( pfh );
}
//--------------------------------------------------------------------
// efs_createfile
//
// Creates a new file from a given name, length and data pointer
//--------------------------------------------------------------------
void efs_createfile( char *name, INT32 length, UINT8 *pData )
{
efs_createfilecb( name, length, pData, 0, 0 );
}
//--------------------------------------------------------------------
// efs_createfilecb
//
// Creates a new file from a given name, length and data pointer
//--------------------------------------------------------------------
void efs_createfilecb( char *name, INT32 length, UINT8 *pData,
EFSFUN pllDestroyFun, UINT32 MemMgrArg )
{
FILEHEADER *pfh;
// Allocate the file header structure
pfh = mmAlloc( sizeof(FILEHEADER) );
if( !pfh )
return;
pfh->Type = HTYPE_EFSFILEHEADER;
// Record the filename, size, and data pointer
strncpy( pfh->Filename, name, EFS_FILENAME_MAX-1 );
pfh->Filename[EFS_FILENAME_MAX-1] = 0;
pfh->Length = length;
pfh->RefCount = 0;
pfh->pData = pData;
pfh->Flags = 0;
pfh->pMemMgrCallback = pllDestroyFun;
pfh->MemMgrArg = MemMgrArg;
llEnter();
// Put it in our list
pfh->pNext = pRoot;
pRoot = pfh;
llExit();
}
//--------------------------------------------------------------------
// efs_destroyfile
//
// Removes a file from the list and frees header
//--------------------------------------------------------------------
void efs_destroyfile( char *name )
{
FILEHEADER *pfh;
llEnter();
pfh = efs_findfile( name );
if( !pfh )
goto dest_exit;
if( !pfh->RefCount )
efs_internal_remove( pfh );
else
pfh->Flags |= EFSFLG_DESTROY_PENDING;
dest_exit:
llExit();
}
//--------------------------------------------------------------------
// efs_loadfunction
//
// Loads a function into memory by filename and returns a function
// pointer (or NULL on error)
//--------------------------------------------------------------------
EFSFUN efs_loadfunction( char *name )
{
FILEHEADER *pfh;
EFSFUN pFun = 0;
llEnter();
pfh = efs_findfile( name );
if( pfh )
pFun = (EFSFUN)pfh->pData;
llExit();
return( pFun );
}
//--------------------------------------------------------------------
// efs_fopen
//
// Opens a file and returns file pointer (or NULL on error)
//--------------------------------------------------------------------
EFS_FILE *efs_fopen( char *name, char *mode )
{
FILEHEADER *pfh;
FILEPTR *pf;
// Verify the read mode
if( stricmp( mode, "rb" ) )
return(0);
llEnter();
pfh = efs_findfile( name );
if( !pfh )
{
llExit();
return(0);
}
// Allocate the file header structure
pf = mmAlloc( sizeof(FILEPTR) );
if( !pf )
{
llExit();
return(0);
}
pf->Type = HTYPE_EFSFILEPOINTER;
pf->pfh = pfh;
pf->Pos = 0;
pfh->RefCount++;
llExit();
return( pf );
}
//--------------------------------------------------------------------
// efs_fclose
//
// Closes a file
//--------------------------------------------------------------------
int efs_fclose( EFS_FILE *stream )
{
FILEPTR *pf = (FILEPTR *)stream;
FILEHEADER *pfh = pf->pfh;
llEnter();
// Zap type for debug
pf->Type = 0;
mmFree( pf );
pfh->RefCount--;
if( !pfh->RefCount && (pfh->Flags & EFSFLG_DESTROY_PENDING) )
efs_internal_remove( pfh );
llExit();
return(0);
}
//--------------------------------------------------------------------
// efs_feof
//
// Returns non-zero if at end of file
//--------------------------------------------------------------------
int efs_feof( EFS_FILE *stream )
{
FILEPTR *pf = (FILEPTR *)stream;
FILEHEADER *pfh = pf->pfh;
if( pf->Pos >= pfh->Length )
return(1);
return(0);
}
//--------------------------------------------------------------------
// efs_fread
//
// Returns number of objects read (set size to 1 to read bytes)
//--------------------------------------------------------------------
size_t efs_fread( void *ptr, size_t size, size_t nobj, EFS_FILE *stream )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -