⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filelib.c

📁 嵌入式系统中的完整FAT16和FAT32源码
💻 C
字号:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//					    FAT32 File IO Library for AVR 
//								  V0.1c
// 	  							Rob Riglar
//							Copyright 2003,2004 
//
//   					  Email: rob@robriglar.com
//
//			    Compiled with Imagecraft C Compiler for the AVR series
//-----------------------------------------------------------------------------
//
// This file is part of FAT32 File IO Library.
//
// FAT32 File IO Library is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// FAT32 File IO Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FAT32 File IO Library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------



//-----------------------------------------------------------------------------
// fopenDIR: Cycle through path string to find the start cluster
//			address of the highest subdir.
//-----------------------------------------------------------------------------
UI32 fopenDIR(char *path)
{
int levels;
int sublevel;
char currentfolder[100];
UI32 startcluster;

// Find number of levels
levels = FileString_PathTotalLevels(path);

// Set starting cluster to root cluster
startcluster = 2;

// Start at sublevel 1 , i.e. Bypass the C:\ part
for (sublevel=1;sublevel<levels+1;sublevel++) 
{
	FileString_Path_to_Folder(path, sublevel, currentfolder);

	//Find clusteraddress for folder (currentfolder) 
	startcluster = MatchName(startcluster, currentfolder);
}

return startcluster;
}

//-----------------------------------------------------------------------------
// fopen: Return start cluster to a file start
//-----------------------------------------------------------------------------
UI32 fopen(char *path)
{
	char dirpath[100];
	char filename[50];
	UI32 startcluster;

	// Split full path into filename and directory path
	FileString_SplitPath(path, dirpath, filename);

	// Find last subdirs start cluster
	startcluster = fopenDIR(dirpath);

	// Using dir cluster address search for filename
	startcluster = MatchName(startcluster, filename);

	// Initialise file stream data
	FILE.startcluster = startcluster;
	FILE.bytenum = 0;

	return startcluster;
}

//-----------------------------------------------------------------------------
// fgetc: Get a character in the stream
//-----------------------------------------------------------------------------
byte fgetc(void)
{
	int sector;
	int offset;	
	byte returnchar=0;

	// Calculations for file position
	sector = FILE.bytenum / 512;
	offset = FILE.bytenum - (sector*512);

	if (!FAT32_SectorReader(FILE.startcluster, sector)) 
	{
		returnchar = IDE_SectorByte(offset);
	}

	// Increase next read position
	FILE.bytenum++;

	// Return character read
	return returnchar;
}

//-----------------------------------------------------------------------
// fopen_cluster: Open a file from start cluster
//-----------------------------------------------------------------------
UI32 fopen_cluster(UI32 clusterin)
{
	// Initialise file stream data
	FILE.startcluster = clusterin;
	FILE.bytenum = 0;
	
	return clusterin;
}

// ----------------------------------------------------------------
// fscanf: Read a line from a file
// ----------------------------------------------------------------
void fscanf(char *args, char *string)
{
	byte datalast = 0;
	byte datacurrent= 0;
	int stringpntr=0;

	// -------------------------------------------------
	// Find a string
	// -------------------------------------------------
	if (args=="%s")
	{
		while (datacurrent!=0xFF)
			{
			datacurrent = fgetc();
			if ((datalast==0x0D) && (datacurrent==0x0A)) 
				{
				stringpntr--;
				stringpntr--;
				break;
				}
			string[stringpntr++] = datacurrent;
			datalast = datacurrent;
			}
		string[stringpntr] = '\0';
	}
	// -------------------------------------------------
	// other find types go here
	// -------------------------------------------------
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -