www.pudn.com > UDiskFileSystem.rar > MX_FFS_File.cpp


/*************************************************************************/ 
/*                                                                       */ 
/*        Copyright (c) 1993-2001 Macronix, Inc.           				 */ 
/*                                                                    	 */ 
/* PROPRIETARY RIGHTS of Macronix are involved in the      			     */ 
/* subject matter of this material.  All manufacturing, reproduction,    */ 
/* use, and sales rights pertaining to this subject matter are governed  */ 
/* by the license agreement.  The recipient of this software implicitly  */ 
/* accepts the terms of the license.                                     */ 
/*                                                                       */ 
/*************************************************************************/ 
 
/*************************************************************************/ 
/* FILE NAME                                                     	 	 */ 
/*      MX_FFS_File.c        			 						 		 */ 
/* DESCRIPTION                                                           */ 
/*	This file definition only internal used in File System				 */ 
/* AUTHOR                                                                */ 
/*      MXIC ISG , Inc.   System Application Department			 		 */ 
/*                                                                       */ 
/*                                                                       */ 
/* HISTORY                                                               */ 
/*      2006/03/31      Released version 1.0                             */ 
/*************************************************************************/ 
 
#ifndef  MX_FFS_FILE_C 
#define  MX_FFS_FILE_C 
#include "mx_FFS_Layer2.h" 
#include "mx_FFS_fdb.h" 
#ifdef BCB6 
#include "main.h" 
extern FILE *hFile; 
extern void U_Printf(char *fmt,...); 
 
#endif 
 
 
/*File Open Setting															*/ 
/* IN :																		*/ 
/*		Handle  File History        										*/ 
/*		Mode    File Operation Mode, FILE_READONLY/WRITEONLY/RDWR/APPEND  	*/ 
/*OUT:																		*/ 
/*		Handle Record File History 											*/		 
void MX_FileOpenSet(mx_FHANDLE *Handle, u_8 mode)//, mxDRIVE_PROPERTY DiskProperty) 
{ 
	Handle->OpMode = mode; 
	if((Handle->OpMode == FILE_RDONLY) || (Handle->OpMode == FILE_RDWR) || (Handle->OpMode == FILE_APPEND)) // Readonly, RW and Write Append file 
	{ 
		Handle->File_History.CurrentCluster = Handle->LFN.FDB_Entry.start_cluster; 
		Handle->File_History.File1stCluster = Handle->LFN.FDB_Entry.start_cluster; 
	} 
	else// Write File 
	{ 
		Handle->File_History.CurrentCluster = 0; 
		Handle->File_History.File1stCluster = 0; 
	} 
	if(Handle->OpMode == FILE_APPEND) 
	{ 
		u_16 BytesPerSector = Handle->Disk_Property.DiskBoot.BytesPerSector; 
		Handle->File_History.EntryNum= Handle->LFN.FDB_Entry.filesize / BytesPerSector; // not Cluster 
		Handle->Position = Handle->LFN.FDB_Entry.filesize; 
		Handle->Remainder = Handle->LFN.FDB_Entry.filesize % BytesPerSector; 
		SearchFileLinkEntry( Handle,1); 
	} 
	else 
	{ 
		Handle->File_History.EntryNum = 0; 
		Handle->File_History.FATEndFlag = 0; 
		Handle->File_History.FATLinkSize = 0; 
		Handle->Position = 0; 
		Handle->Remainder = 0; 
	} 
} 
 
/*File Seek Setting							*/ 
/* IN :										*/ 
/*		Handle  	File History        				*/ 
/*		pos    	File offset   */ 
/*OUT:											*/ 
/*		Handle Record File History */		 
/*		pCurPos Record the last File offset number */		 
u_8 MX_FileSeekSet(mx_FHANDLE *Handle, u_32 pos, u_32 * pCurPos)// ¦³°ÝåÃD 
{ 
	//u_8 SectorsPerCluster = Handle->Disk_Property.DiskBoot.SectorsPerCluster; 
	u_16 BytesPerSector = Handle->Disk_Property.DiskBoot.BytesPerSector; 
//	u_32 BytesPerCluster = Handle->Disk_Property.DiskBoot.BytesPerCluster; 
	Handle->File_History.EntryNum= pos / BytesPerSector; // not Cluster 
	Handle->Position = pos; 
	Handle->Remainder = pos % BytesPerSector; 
	SearchFileLinkEntry( Handle,1); 
	if(pos > Handle->LFN.FDB_Entry.filesize) 
		*pCurPos = Handle->LFN.FDB_Entry.filesize; 
	else 
		*pCurPos = pos; 
	return 0; 
 
} 
 
 
/*File Read File Data							*/ 
/* IN :										*/ 
/*		Handle  	File Handle         				*/ 
/*		Count    	Read Data Count   */ 
/*OUT:											*/ 
/*		Handle 	Record File History */		 
/*		pBuff 	Return File data buffer to be read */	 
/*		pByteRead   Point to a buffer to stores the return numbers of byte read*/ 
u_8 MX_ReadFileData(mx_FHANDLE *Handle, u_8 *pBuff, u_32 Count, u_32 *pByteRead) 
{ 
	u_8 SectorsPerCluster = Handle->Disk_Property.DiskBoot.SectorsPerCluster; 
	u_16 BytesPerSector = Handle->Disk_Property.DiskBoot.BytesPerSector; 
//	u_32 BytesPerCluster = Handle->Disk_Property.DiskBoot.BytesPerCluster; 
	u_8  TempBuff[0x200]; 
	u_32 ReadCount=0,CurrentLBA; 
	u_8 Error,Index; 
	u_16 i; 
 
	if((Handle->OpMode != FILE_RDONLY) && (Handle->OpMode != FILE_RDWR)) 
	{ 
		return FAT_FILEOPMODE_ERROR; 
	} 
	ReadCount=0; 
	if( Count > (Handle->LFN.FDB_Entry.filesize- Handle->Position)) 
	{ 
		Count = Handle->LFN.FDB_Entry.filesize- Handle->Position; 
	} 
	while(ReadCount != Count)  
	{ 
		if((Handle->File_History.EntryNum % SectorsPerCluster) == 0) 
		{ 
			Error = SearchFileLinkEntry( Handle,0); 
			if(Error == 1) 
			{ 
				break; 
			} 
		} 
		 
		CurrentLBA = Handle->File_History.CurrentLBA; 
		Index = Handle->File_History.EntryNum % SectorsPerCluster; 
		UFI_ReadFile(hFile, CurrentLBA+Index, 1, TempBuff); 
		for( i=Handle->Remainder; i < 512 ; i++) 
		{ 
			pBuff[ReadCount] = TempBuff[i]; 
			ReadCount++; 
			if(ReadCount == Count) 
			{ 
				i++; 
				break; 
			} 
		} 
		Handle->Position += ( i - Handle->Remainder); 
		Handle->Remainder = Handle->Position % BytesPerSector; 
		Handle->File_History.EntryNum = Handle->Position / BytesPerSector; 
	} 
	*pByteRead = ReadCount; 
 
	return FAT_PASS; 
} 
u_8 MX_WriteFileData(mx_FHANDLE *Handle, u_8 *pBuff, u_32 Count, u_32 *pByteWrite) 
{ 
 
	return 0; 
} 
u_8 MX_CloseFile(mx_FHANDLE Handle) 
{ 
 
	return 0; 
} 
 
#endif   /*MX_FFS_FILE_C*/