Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 352 → Rev 353

/trunk/fat16.c
62,7 → 62,6
#include "sdc.h"
#include "uart1.h"
 
void Test(void);
//________________________________________________________________________________________________________________________________________
// Module name: fat16.c
// Compiler used: avr-gcc 3.4.5
323,6 → 322,8
File_t FilePointer[FILE_MAX_OPEN]; // Allocate Memmoryspace for each filepointer used.
 
 
 
 
/****************************************************************************************************************************************/
/* Function: FileDate(DateTime_t *); */
/* */
450,6 → 451,28
// clear dirname with spaces
dirname[11] = 0; // terminate dirname
for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' ';
 
// handle the special dirnames "." and ".." seperately
readpointer = 0;
if(filepath[0] == '/') readpointer++;
// if we are trying to enter directories "." or ".."
if(filepath[readpointer] == '.')
{
// directory '.'
if(filepath[readpointer+1] == 0)
{
dirname[0] = '.';
return((s8*)&filepath[readpointer]);
}
// directory '..'
if((filepath[readpointer+1] == '.') && (filepath[readpointer+2] == 0))
{
dirname[0] = '.';
dirname[1] = '.';
return((s8*)&filepath[readpointer]);
}
}
 
writepointer = 0;
// start seperating the dirname from the filepath.
readpointer = 0;
662,8 → 685,6
Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector;
strcpy(Partition.PathToCwd,"/");
result = 0;
 
// Test();
end:
if(result != 0) Fat16_Deinit();
else UART1_PutString("ok");
756,6 → 777,7
}
 
 
 
/****************************************************************************************************************************************/
/* Function: FindNextFreeCluster(File_t *); */
/* */
2081,9 → 2103,10
 
u8 FindItem(Find_t *findelement)
{
u16 index = 0;
u16 end_of_directory_not_reached = 1; // the directory has been read completely without a result.
u8 i = 0;
u16 index = 0;
u16 max_dir_sector = 0;
u16 end_of_directory_not_reached = 1; // the directory has been read completely without a result.
u8 i = 0;
u8 readpointer = 0;
u8 writepointer = 0;
u8 retvalue = 0;
2094,6 → 2117,16
file.SectorOfCurrCluster = findelement->fp.SectorOfCurrCluster;
index = findelement->fp.DirectoryIndex;
 
// within the root directory area we can read sectors sequentially until the end of this area
if((Partition.FirstRootDirSector <= file.FirstSectorOfCurrCluster) && (file.FirstSectorOfCurrCluster < Partition.FirstDataSector))
{
max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR;
}
// within the data clusters we can read sectors sequentially only within the cluster
else if((Partition.FirstDataSector <= file.FirstSectorOfCurrCluster) && (file.FirstSectorOfCurrCluster <= Partition.LastDataSector))
{
max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster
}
 
do
{ // search the next 16 rootentries in this sector of the roordirectory.
2121,7 → 2154,7
}
else
{
if(DirectoryEntry[index].Attribute == ATTR_ARCHIVE)
if(DirectoryEntry[index].Attribute == ATTR_ARCHIVE)
{
if(readpointer < 8) readpointer=8;
if(DirectoryEntry[index].Name[readpointer] != ' ')
2147,18 → 2180,22
findelement->fp.Attribute = DirectoryEntry[index].Attribute;
retvalue = 1;
}
/* search the next sector */
index++;
}
/* this sector has been searched but we havn't found what we are looking for. Therefore we have to find the next sector */
if(!retvalue) // file not found in this sector so take next sector.
{
file.SectorOfCurrCluster++;
if(file.SectorOfCurrCluster >= Partition.SectorsPerCluster)
{
file.SectorOfCurrCluster=0;
end_of_directory_not_reached = GetNextCluster(&file);
/* in the next sector we start looking for the specified entry beginning at index 0 */
index = 0;
/* there are still sectors to be read within the cluster or within the linear addresspace of the rootdirectory */
if(file.SectorOfCurrCluster < max_dir_sector-1) file.SectorOfCurrCluster++; else end_of_directory_not_reached = 0;
/* if we are looking for an directoryentry outside the rootdirectory and have reached the end of the cluster we have to get the next one */
if(Partition.FirstDataSector <= file.FirstSectorOfCurrCluster)
{
end_of_directory_not_reached = GetNextCluster(&file);
}
}
index = 0;
}
while((end_of_directory_not_reached) && (!retvalue));
 
2166,6 → 2203,8
}
 
 
 
 
/********************************************************************************************************************************************/
/* Function: findnext_(Find_t *); */
/* */
2195,7 → 2234,7
 
 
/********************************************************************************************************************************************/
/* Function: findfirst_(s8* filename, u8 attribfilter, u8 attribmask, Find_t *); */
/* Function: findfirst_(s8* filename, u8 attribmask, Find_t *); */
/* */
/* Description: This function looks for the first item in the specified directory with a matching filename and fileattributes */
/* The filename of the element found is transformed from 8.3 to a string */
2367,11 → 2406,23
/* a pointer to the beginning of the absolute path to the cwd */
s8 * cptr = Partition.PathToCwd;
/* lets find the end of the path to the cwd */
while(*cptr++ != 0);
while(*cptr != 0) cptr++;
/* if the path is terminated with an '/' */
if((*(cptr-1)) == '/') *(cptr-1)=0;
/* now lets find the beginning of the last directorientry */
while(*cptr-- != '/');
/* we delete the direntry by terminating the path with 0 */
*++cptr = 0;
while((*cptr != '/') && cptr > Partition.PathToCwd) cptr--;
/* is there one subdirectory left within the path? */
if(cptr > Partition.PathToCwd)
{
/* we delete the direntry by terminating the path with 0 */
*cptr = 0;
}
/* there is no subdirectory left within the path. Therefore we create the root instead. */
else
{
*cptr = '/';
*(cptr+1) = 0;
}
}
 
/********************************************************************************************************************************************/
2382,7 → 2433,7
/* */
/* Returnvalue: */
/********************************************************************************************************************************************/
/*
/*
#define ATTR_NONE 0x00 // normal file
#define ATTR_READONLY 0x01 // file is readonly
#define ATTR_HIDDEN 0x02 // file is hidden
2408,7 → 2459,11
strcpy(tp, Partition.PathToCwd);
cwdt = Partition.CurrentWorkingDirectory;
/* if the path is absolute we begin at the rootdirectory */
strcpy(Partition.PathToCwd, "/");
if(path[0] == '/')
{
strcpy(Partition.PathToCwd, "/");
Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector;
}
/* how many subdirectories are there within the path? */
dircount = GetDirCount(path);
/* now we parse through all the subdirectories within the path */
2447,96 → 2502,103
/* if we could not change to the specified directory we restore the actual path */
if(!retvalue)
{
Partition.CurrentWorkingDirectory = cwdt;
Partition.CurrentWorkingDirectory = cwdt;
strcpy(Partition.PathToCwd, tp);
}
return(retvalue);
}
 
/********************************************************************************************************************************************/
/* Function: void Test(void); */
/* */
/* Description: Diese Funktion demonstriert die Möglichkeiten der Funktionen findfirst_() findnext_() und changedir_() */
/* */
/* */
/* Returnvalue: */
/********************************************************************************************************************************************/
 
 
void Test(void)
{
File_t *fp = NULL;
Find_t fe;
u32 cwd = 0;
Find_t find;
text[64];
 
UART1_PutString("\r\nsearching for elements in CWD:");
UART1_PutString(GetPath());
if(chdir_("log"))
{
UART1_PutString(" done");
UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath());
 
cwd = Partition.CurrentWorkingDirectory;
sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text);
/* Als Erstes suchen wir uns das erste Unterverzeichnis (ATTR_SUBDIRECTORY) auf der Speicherkarte */
if(findfirst_("*.*", ATTR_SUBDIRECTORY, &fe))
{
/* Danach wechseln wir in in dieses Unterverzeichnis auf der speicherkarte */
if(chdir_(fe.name))
if(chdir_("log11"))
{
sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text);
/* Dann finden wir die erste Datei (ATTR_ARCIVE) in diesem Unterverzeichnis */
if(findfirst_("*.*", ATTR_ARCHIVE, &fe))
UART1_PutString(" done");
UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath());
 
if(chdir_("log12"))
{
/* Der Name der gefundenen Datei wird ausgegeben */
UART1_PutString("\r\n");
UART1_PutString(fe.name);
/* nun wir die erste gefundene Datei im Unterverzeichnis lesend geöffnet */
fp = fopen_(fe.name, 'r');
if(fp != NULL){
/* und die erste Zeile aus dem Inhalt ausgegeben */
fgets_(text,60,fp);
UART1_PutString("\r\n");
UART1_PutString(text);
fclose_(fp);
UART1_PutString(" done");
UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath());
 
if(findfirst_("*.*", 0xff, &find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
while(findnext_(&find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
}
}
else {
UART1_PutString("\r\nfile not found");
if(chdir_("."))
{
UART1_PutString(" done");
UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath());
if(findfirst_("*.*", 0xff, &find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
while(findnext_(&find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
}
}
}
 
/* anschließend suchen wir alle weiteren Dateien im aktuellen Unterverzeichnis */
while(findnext_(&fe))
else UART1_PutString(" failed");
if(chdir_(".."))
{
/* und geben ebenfalls deren Namen */
UART1_PutString("\r\n");
UART1_PutString(fe.name);
/* sowie nach dem Öffnen ihren Inhalt aus */
fp = fopen_(fe.name, 'r');
if(fp != NULL)
UART1_PutString(" done");
UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath());
if(findfirst_("*.*", 0xff, &find))
{
fgets_(text,60,fp);
UART1_PutString("\r\n");
UART1_PutString(text);
fclose_(fp);
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
while(findnext_(&find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
}
}
}
}
else UART1_PutString(" failed");
if(chdir_(".."))
{
UART1_PutString(" done");
UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath());
if(findfirst_("*.*", 0xff, &find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
while(findnext_(&find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
}
}
}
else UART1_PutString(" failed");
if(chdir_(".."))
{
UART1_PutString(" done");
UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath());
if(findfirst_("*.*", 0xff, &find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
while(findnext_(&find))
{
sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text);
}
}
}
else UART1_PutString(" failed");
}
}
}
Partition.CurrentWorkingDirectory = cwd;
sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text);
if(chdir_("/ordner1/ordner12/ordner13"))
{
sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text);
fp = fopen_("Datei13.txt", 'r');
if(fp != NULL){
/* und die erste Zeile aus dem Inhalt ausgegeben */
fgets_(text,60,fp);
UART1_PutString("\r\n");
UART1_PutString(text);
fclose_(fp);
}
else {
UART1_PutString("\r\nfile not found");
}
}
else
UART1_PutString("\r\ndirectory not found");
/* jetzt sind wir wieder im Rootverzeichnis */
Partition.CurrentWorkingDirectory = cwd;
sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text);
else UART1_PutString(" failed");
}