📄 filelib.c
字号:
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// FAT32 File IO Library Linux Test Version
// V1.0L
// Rob Riglar
// Copyright 2003,2004
//
// Email: admin@robs-projects.com
//
// Compiled and tested on Redhat 'Shrike' with GNU gcc
//-----------------------------------------------------------------------------
//
// 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
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../IDE/IDE_Access.h"
#include "../FAT/FAT32_Access.h"
#include "filelib.h"
#include "../File-Dir-IO/FileString.h"
#include "../File-Dir-IO/DirSearchBrowse.h"
//-----------------------------------------------------------------------------
// 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_TEST(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_TEST.startcluster = startcluster;
FILE_TEST.bytenum = 0;
return startcluster;
}
//-----------------------------------------------------------------------------
// fgetc: Get a character in the stream
//-----------------------------------------------------------------------------
byte fgetc_TEST(void)
{
int sector;
int offset;
byte returnchar=0;
// Calculations for file position
sector = FILE_TEST.bytenum / 512;
offset = FILE_TEST.bytenum - (sector*512);
if (!FAT32_SectorReader(FILE_TEST.startcluster, sector))
{
returnchar = IDE_SectorByte(offset);
}
// Increase next read position
FILE_TEST.bytenum++;
// Return character read
return returnchar;
}
//-----------------------------------------------------------------------
// fopen_cluster: Open a file from start cluster
//-----------------------------------------------------------------------
UI32 fopen_cluster(UI32 clusterin)
{
// Initialise file stream data
FILE_TEST.startcluster = clusterin;
FILE_TEST.bytenum = 0;
return clusterin;
}
// ----------------------------------------------------------------
// fscanf: Read a line from a file
// ----------------------------------------------------------------
void fscanf_TEST(char *args, char *string)
{
byte datalast = 0;
byte datacurrent= 0;
int stringpntr=0;
// -------------------------------------------------
// Find a string
// -------------------------------------------------
if (args=="%s")
{
while (datacurrent!=0xFF)
{
datacurrent = fgetc_TEST();
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 + -