Rev 1 | Rev 41 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1 | Rev 24 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch |
6 | // + Nur für den privaten Gebrauch |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
34 | // + this list of conditions and the following disclaimer. |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
42 | // + clearly linked as origin |
43 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
43 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | 57 | ||
58 | #include "main.h" |
58 | #include "main.h" |
59 | 59 | ||
60 | //________________________________________________________________________________________________________________________________________ |
60 | //________________________________________________________________________________________________________________________________________ |
61 | // Module name: fat16.c |
61 | // Module name: fat16.c |
62 | // Compiler used: avr-gcc 3.4.5 |
62 | // Compiler used: avr-gcc 3.4.5 |
63 | // Last Modifikation: 24.07.2007 |
63 | // Last Modifikation: 21.04.2008 |
64 | // Version: 1.24 |
64 | // Version: 1.28 |
65 | // Authors: Stephan Busker |
65 | // Authors: Stephan Busker |
66 | // Description: Source files for FAT16 implementation with read and write-access using AVR-Mikrocontrollers |
66 | // Description: Source files for FAT16 implementation with read and write-access using AVR-Mikrocontrollers |
67 | // Copyright (C) 2007 Stephan Busker |
67 | // Copyright (C) 2007 Stephan Busker |
68 | //........................................................................................................................................ |
68 | //........................................................................................................................................ |
69 | // Functions: extern u8 InitFat16(void); |
69 | // Functions: extern u8 InitFat16(void); |
70 | // u8 fopen_(u8 *fname,s8 mode, File *file); |
70 | // u8 fopen_(s8 *fname,s8 mode, File *file); |
71 | // void fclose_(File *file); |
71 | // void fclose_(File *file); |
72 | // u32 fread_(void *buffer, u32 size, u32 count, File *file); |
72 | // u32 fread_(void *buffer, u32 size, u32 count, File *file); |
73 | // u32 fwrite_(void *buffer, u32 size, u32 count, File *file); |
73 | // u32 fwrite_(void *buffer, u32 size, u32 count, File *file); |
74 | // s16 fseek_(File *file, s32 offset, s16 origin); |
74 | // s16 fseek_(File *file, s32 offset, s16 origin); |
75 | // s8 fgets8_(File *file); |
75 | // s8 fgets_(s8 *string, s16 count, File *file); |
76 | // u8 fputchar_(File *file,s8 c); |
76 | // u8 fputchar_(File *file,s8 c); |
77 | // u8 fputs_(File *file,s8 *string); |
77 | // u8 fputs_(File *file,s8 *string); |
78 | // s8 * fgets(s8 *, s16, File); |
78 | // s8 * fgets(s8 *, s16, File); |
79 | // u8 fexist_(u8*, File *file); |
79 | // u8 fexist_(u8*); |
- | 80 | // u8 mkdir__(s8 *name); |
|
- | 81 | // u8 chdir__(s8 *name); |
|
- | 82 | // u8 findfirst_(s8 *fname, Find *item, u8 attribute); |
|
- | 83 | // u8 findnext_(Find *item); |
|
- | 84 | // u8 fdelete_(s8 *fname) |
|
- | 85 | // u8 rmdir_(s8 *dname) |
|
- | 86 | // u16 feof_(File); |
|
80 | //........................................................................................................................................ |
87 | //........................................................................................................................................ |
81 | // ext. functions: extern u8 SDC_GetSector (u32,u8 *); |
88 | // ext. functions: extern u8 SDC_GetSector (u32,u8 *); |
82 | // extern u8 SDC_PutSector (u32,u8 *); |
89 | // extern u8 SDC_PutSector (u32,u8 *); |
83 | //........................................................................................................................................ |
90 | //........................................................................................................................................ |
84 | // |
91 | // |
85 | // URL: www.Mikro-Control.de |
92 | // URL: www.Mikro-Control.de |
86 | // mailto: stephan.busker@mikro-control.de |
93 | // mailto: stephan.busker@mikro-control.de |
87 | //________________________________________________________________________________________________________________________________________ |
94 | //________________________________________________________________________________________________________________________________________ |
88 | 95 | ||
89 | 96 | ||
90 | - | ||
91 | 97 | ||
92 | //________________________________________________________________________________________________________________________________________ |
98 | //________________________________________________________________________________________________________________________________________ |
93 | // |
99 | // |
94 | // Global variables needed for read- or write-acces to the FAT16- filesystem. |
100 | // Global variables needed for read- or write-acces to the FAT16- filesystem. |
95 | // |
101 | // |
96 | //________________________________________________________________________________________________________________________________________ |
102 | //________________________________________________________________________________________________________________________________________ |
97 | 103 | ||
98 | u8 SectorsPerCluster = 0; // how many sectors does a cluster contain? |
104 | u8 SectorsPerCluster = 0; // how many sectors does a cluster contain? |
99 | u8 FatCopies = 0; // Numbers of copies of the FAT |
105 | u8 FatCopies = 0; // Numbers of copies of the FAT |
100 | u16 PossibleRootEntries = 0; // Possible number of entries in the root directory. |
106 | u16 PossibleRootEntries = 0; // Possible number of entries in the root directory. |
101 | u16 SectorsPerFat = 0; // how many sectors does a fat16 contain? |
107 | u16 SectorsPerFat = 0; // how many sectors does a fat16 contain? |
102 | u32 ReservedSectors = 0; // Sectors reserved by the filesystem. |
108 | u32 ReservedSectors = 0; // Sectors reserved by the filesystem. |
103 | u32 FirstPartitionSector = 0; // Distance in sectors between the first partition and the master bootrecord. |
109 | u32 FirstPartitionSector = 0; // Distance in sectors between the first partition and the master bootrecord. |
104 | u32 FileAllocationTable = 0; // pointer to the first FAT |
110 | u32 FileAllocationTable = 0; // pointer to the first FAT |
105 | u32 RootDirectory = 0; // Pointer to the rootdirectory of the first partition. |
111 | u32 RootDirectory = 0; // Pointer to the rootdirectory of the first partition. |
106 | u32 FirstDataCluster = 0; // Pointer to the first cluster containing data (cluster0). |
- | |
107 | //u8 FileBuffer[512]; // Buffer for read and write operation from or to the mmc. |
- | |
108 | - | ||
109 | struct DirEntry *DirectoryEntry; // Pointer to an entry of the directory. |
112 | u32 FirstDataCluster = 0; // Pointer to the first cluster containing data (cluster0). |
110 | struct FatEntry *Fat; // Pointer to an entry of the fat (next clusterposition). |
- | |
- | 113 | u32 CWD = 0; // Pointer startcluster to the current working directory |
|
- | 114 | ||
- | 115 | ||
111 | 116 | struct DirEntry *DirectoryEntry; // Pointer to an entry of the directory. |
|
112 | File FilePointer[__MAX_FILES_USED]; // Allocate Memmoryspace for each filepointer used. |
117 | struct FatEntry *Fat; // Pointer to an entry of the fat (next clusterposition). |
113 | 118 | ||
114 | 119 | File FilePointer[__MAX_FILES_USED]; // Allocate Memmoryspace for each filepointer used. |
|
115 | 120 | ||
116 | 121 | ||
117 | //________________________________________________________________________________________________________________________________________ |
122 | //________________________________________________________________________________________________________________________________________ |
118 | // Funtion: InitFat16(void); |
123 | // Funtion: InitFat16(void); |
119 | // |
124 | // |
120 | // Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdirectory |
125 | // Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdirectory |
121 | // and stores the information in global variables. |
126 | // and stores the information in global variables. |
122 | // |
127 | // |
123 | // Returnvalue: The function returns "0" if the filesystem could not be initialized because no partition was found on the disc. |
128 | // Returnvalue: The function returns "0" if the filesystem could not be initialized because no partition was found on the volume. |
124 | //________________________________________________________________________________________________________________________________________ |
129 | //________________________________________________________________________________________________________________________________________ |
125 | 130 | ||
126 | u8 InitFat16(void) |
131 | u8 InitFat16(void) |
127 | { |
132 | { |
128 | u8 retvalue = 0; |
133 | u8 retvalue = 0; |
129 | u8 cnt = 0; |
134 | u8 cnt = 0; |
130 | struct VBR_Entry *VBR; // Pointer to the VolumeBootRecord. |
135 | struct VBR_Entry *VBR; // Pointer to the VolumeBootRecord. |
131 | struct MBR_Entry *MBR; // Pointer to the masterbootrecord. |
136 | struct MBR_Entry *MBR; // Pointer to the masterbootrecord. |
132 | File *file; |
137 | File *file; |
133 | 138 | ||
134 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) |
139 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) |
135 | { |
140 | { |
136 | FilePointer[cnt].state = _UNUSED; // declare the filepointers as unused. |
141 | FilePointer[cnt].state = _UNUSED; // declare the filepointers as unused. |
137 | } |
142 | } |
138 | 143 | ||
139 | file = &FilePointer[0]; |
144 | file = &FilePointer[0]; |
- | 145 | ||
140 | 146 | SerialPutString("\n\rFAT16 init..."); |
|
- | 147 | while((SDC_Init() != SD_SUCCESS) && (cnt++<100)); |
|
141 | while((SDC_Init() != 0) && (cnt++<200)); // initialise the sdcard. |
148 | |
142 | if(cnt <200) //sdcard initialised succesfully |
149 | if(cnt <100) // sdcard initialised succesfully |
143 | { |
150 | { |
144 | SDC_GetSector((u32)MBR_SECTOR,file->buffer); // Read the MasterBootRecord from mmc. |
151 | SDC_GetSector((u32)_MBR_SECTOR,file->buffer); // Read the MasterBootRecord from mmc. |
145 | MBR = (struct MBR_Entry *) file->buffer; |
152 | MBR = (struct MBR_Entry *) file->buffer; |
- | 153 | FirstPartitionSector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
|
- | 154 | if((MBR->PartitionEntry1.Type == _FAT16_32_MB_BIOS_Extension) || |
|
- | 155 | (MBR->PartitionEntry1.Type == _FAT16_ST_32_MB) || |
|
- | 156 | (MBR->PartitionEntry1.Type == _FAT16_LT_32_MB)) |
|
146 | FirstPartitionSector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
157 | { |
147 | SDC_GetSector(FirstPartitionSector,file->buffer); // Read the volume bootrecord from mmc. |
- | |
148 | 158 | SDC_GetSector(FirstPartitionSector,file->buffer); // Read the volume bootrecord from mmc. |
|
149 | 159 | ||
150 | VBR = (struct VBR_Entry *) file->buffer; // Enter the VBR using the structure VBR_Entry. |
160 | VBR = (struct VBR_Entry *) file->buffer; // Enter the VBR using the structure VBR_Entry. |
151 | SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
161 | SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
152 | FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
162 | FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
153 | PossibleRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
163 | PossibleRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
154 | SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
164 | SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
155 | ReservedSectors = VBR->ReservedSectors; // calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. |
165 | ReservedSectors = VBR->ReservedSectors; // calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. |
156 | 166 | ||
157 | FileAllocationTable = (u32)(FirstPartitionSector + (u32)ReservedSectors); // Calculate the position of the FileAllocationTable. |
167 | FileAllocationTable = (u32)(FirstPartitionSector + (u32)ReservedSectors); // Calculate the position of the FileAllocationTable. |
158 | RootDirectory = (u32)((u32)FileAllocationTable + (u32)((u32)SectorsPerFat*(u32)FatCopies)); // Calculate the position of the Rootdirectory. |
168 | RootDirectory = (u32)((u32)FileAllocationTable + (u32)((u32)SectorsPerFat*(u32)FatCopies)); // Calculate the position of the Rootdirectory. |
159 | FirstDataCluster = (u32)((u32)RootDirectory + ((u32)(PossibleRootEntries>>4))); // Calculate the position of the first datacluster. |
- | |
160 | 169 | FirstDataCluster = (u32)((u32)RootDirectory + ((u32)(PossibleRootEntries>>4))); // Calculate the position of the first datacluster. |
|
161 | if((file->buffer[0x36]=='F') &&(file->buffer[0x37]=='A')&&(file->buffer[0x38]=='T')&&(file->buffer[0x39]=='1')&&(file->buffer[0x3A]=='6')) |
- | |
162 | { |
170 | CWD = RootDirectory; // The actual directory is the rootdirectory. |
- | 171 | retvalue = 1; |
|
- | 172 | SerialPutString("\n\rfilesystem ok"); |
|
- | 173 | str_Volume.state = INITIALIZED; |
|
- | 174 | } |
|
- | 175 | else |
|
- | 176 | { |
|
- | 177 | str_Volume.state = NOT_INITIALIZED; |
|
163 | retvalue = 1; |
178 | SerialPutString("\n\rfilesystem nok"); |
164 | } |
179 | } |
165 | } |
180 | } |
166 | return(retvalue); |
181 | return(retvalue); |
167 | } |
182 | } |
168 | 183 | ||
169 | //________________________________________________________________________________________________________________________________________ |
184 | //________________________________________________________________________________________________________________________________________ |
170 | // Funtion: File * fopen_(u8*, u8 *, File *file); |
185 | // Funtion: File * fopen_(u8*, u8 *); |
171 | // |
186 | // |
172 | // Description: This function looks for the specified file in the rootdirectory of the drive. If the file is found the number of the |
187 | // Description: This function looks for the file to be opened in the directory specified by the variable "CWD" of the drive. |
173 | // corrosponding datacluster is returned to main. Only modes 'r' (reading) and 'a' append are implemented yet. |
188 | // If the file was found this function returns a filepoiner to the opened file otherwise NULL// |
174 | // |
189 | // |
175 | // Return: 0 = faild to open speicified file |
190 | // Return: Pointer to the opened file or NULL |
176 | // 1 = file opened |
191 | // |
177 | //________________________________________________________________________________________________________________________________________ |
192 | //________________________________________________________________________________________________________________________________________ |
178 | 193 | ||
179 | File * fopen_(u8 *fname, s8 mode) |
194 | File * fopen_(s8 *fname, s8 mode) |
180 | { |
- | |
181 | u32 cluster_temp = 0; |
- | |
182 | u8 name[11] = " "; |
- | |
183 | u16 temp = 0; |
195 | { |
184 | File *file; |
- | |
185 | - | ||
186 | file=0; |
- | |
187 | for(temp = 0;temp<__MAX_FILES_USED;temp++) |
196 | File *file; |
188 | { |
- | |
189 | if(FilePointer[temp].state == _UNUSED) // free filepointer found? |
- | |
190 | { |
- | |
191 | file = &FilePointer[temp]; |
- | |
192 | FilePointer[temp].state = _USED; // mark as used. |
- | |
193 | break; |
- | |
194 | } |
197 | |
- | 198 | file = ReserveFilePointer(); // reserve a filepointer. |
|
- | 199 | ||
195 | } |
- | |
196 | - | ||
197 | if(file == 0) return(file); // no valid pointer |
- | |
198 | - | ||
199 | file->start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
- | |
200 | file->cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
- | |
201 | file->sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
- | |
202 | file->byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
- | |
203 | file->mode = mode; // mode of fileoperation (read,write) |
- | |
204 | file->filesize = 0; // the size of the opend file in bytes. |
- | |
205 | file->fileposition = 0; // pointer to a character within the file 0 < fileposition < filesize |
- | |
206 | file->sector_in_buffer = 0; // the last sector read, wich is still in the sectorbuffer. |
- | |
207 | file->directory_sector = 0; // the sectorposition where the directoryentry has been made. |
- | |
208 | file->directory_index = 0; // the index to the directoryentry within the specified sector. |
- | |
209 | file->attribute = 0; // the attribute of the file opened. |
- | |
210 | - | ||
211 | if(ScanSubDirectories(&fname[0], file)) // Is the specified filepath available? |
200 | if(file != NULL) // A free filepointer was found. |
212 | { |
201 | { |
- | 202 | file->mode = mode; // mode of fileoperation (read,write) |
|
213 | SeperateFileName(&fname[0], &name[0]); // seperate the filename and attribute from the filepath and bring them in the correct format. |
203 | |
214 | file->attribute = _FILE; |
204 | if(SeekFileInDirectory(fname, file)) // if file was found |
215 | if(SeekDirectoryEntry(&name[0], file)) // if file was found |
205 | { |
216 | { |
206 | SerialPutString("ok"); |
217 | if(mode == 'a') // open existing file for writing (append data at the end of the file) |
207 | if(mode == 'a') // open existing file for writing (append data at the end of the file) |
218 | { |
208 | { |
219 | fseek_(file, 0, SEEK_END); // fseek points to the end of the file |
209 | fseek_(file, 0, SEEK_END); // fseek points to the end of the file |
220 | } |
210 | } |
221 | return(file); |
- | |
222 | } |
211 | } |
223 | else |
212 | else |
224 | { |
213 | { |
225 | if((mode == 'a') || (mode == 'w')) // specified file doesn't exist so create new file for writing data. |
214 | if((mode == 'a') || (mode == 'w')) // specified file doesn't exist so create new file for writing data. |
226 | { |
215 | { |
227 | cluster_temp = (u32)FindNextFreeCluster(file); // the next free cluster on the disk. |
216 | if(CreateFileInDirectory(fname,file)) // Could an entry for the new file in the rootdirectory be created? |
228 | if(cluster_temp) // if a free cluster is available: |
- | |
229 | { |
217 | { |
230 | temp = (u16)cluster_temp; // remember the index of the free datacluster found for the directory entry. |
- | |
231 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
- | |
232 | cluster_temp *= SectorsPerCluster; // Calculate relative sectorindex of first datacluster. |
218 | return(file); |
233 | file->start_cluster = (FirstDataCluster + cluster_temp); // Calculate absolute sectorposition of first datacluster. |
- | |
234 | file->cluster_pointer = file->start_cluster; // start reading the file with the first sector of the first datacluster. |
- | |
235 | if(CreateDirectoryEntry(&name[0],temp,file,_FILE)) // Could an entry for the new file in the rootdirectory be created? |
- | |
236 | { |
219 | } |
237 | return(file); |
220 | else |
238 | } |
221 | { |
- | 222 | FreeFilePointer(file); // free the filepointer. |
|
- | 223 | file = NULL; |
|
239 | } |
224 | } |
240 | } |
225 | } |
- | 226 | else // file with mode 'r' not found |
|
- | 227 | { |
|
- | 228 | FreeFilePointer(file); // free the filepointer. |
|
- | 229 | file = NULL; // file not found |
|
- | 230 | } |
|
241 | } |
231 | } |
242 | } |
232 | } |
243 | return(0); |
233 | return(file); |
244 | } |
234 | } |
- | 235 | ||
- | 236 | ||
245 | 237 | ||
246 | //________________________________________________________________________________________________________________________________________ |
238 | //________________________________________________________________________________________________________________________________________ |
247 | // Funtion: fflush_(File *file); |
239 | // Funtion: fflush_(File *file); |
248 | // |
240 | // |
249 | // Description: This function writes the data already in the buffer but not yet written to the file. |
241 | // Description: This function writes the data already in the buffer but not yet written to the file. |
250 | // |
242 | // |
251 | //________________________________________________________________________________________________________________________________________ |
243 | //________________________________________________________________________________________________________________________________________ |
252 | 244 | ||
253 | s16 fflush_(File *file) |
245 | s16 fflush_(File *file) |
254 | { |
246 | { |
255 | u16 time=0; |
247 | u16 time=0; |
256 | u16 date=0; |
248 | u16 date=0; |
257 | - | ||
258 | #ifdef __USE_TIME_DATE_ATTRIBUTE // has the date and time attribute of the file to be set? |
249 | |
259 | - | ||
260 | time = (((rtctime.hour)<<11) | ((rtctime.minute)<<5) | rtctime.second); |
- | |
261 | date = ((((rtctime.year)-1980) <<9) | ((rtctime.month) <<5) | rtctime.day); |
- | |
262 | - | ||
263 | #endif |
- | |
264 | - | ||
265 | 250 | ||
266 | if(file && file->mode =='a') |
251 | if(file && ((file->mode =='a') || (file->mode =='w'))) |
267 | { |
252 | { |
268 | if(file->byte_index > 0) // has data been added to the file? |
253 | if(file->byte_index > 0) // has data been added to the file? |
269 | { |
254 | { |
270 | SDC_PutSector((u32)(file->cluster_pointer + file->sector_index),file->buffer); // save the data still in the buffer |
255 | SDC_PutSector((u32)(file->cluster_pointer + file->sector_index),file->buffer);// save the data still in the buffer |
271 | } |
256 | } |
272 | SDC_GetSector((u32)file->directory_sector,file->buffer); // read the directoryentry for this file. |
257 | SDC_GetSector((u32)file->directory_sector,file->buffer); // read the directoryentry for this file. |
273 | DirectoryEntry = (struct DirEntry *)file->buffer; |
258 | DirectoryEntry = (struct DirEntry *)file->buffer; |
274 | DirectoryEntry[file->directory_index].size = (u32) file->filesize; |
259 | DirectoryEntry[file->directory_index].size = (u32) file->filesize; |
275 | DirectoryEntry[file->directory_index].time = (u16) time; |
260 | DirectoryEntry[file->directory_index].time = (u16) time; |
276 | DirectoryEntry[file->directory_index].date = (u16) date; |
261 | DirectoryEntry[file->directory_index].date = (u16) date; |
277 | SDC_PutSector((u32)file->directory_sector,file->buffer); |
262 | SDC_PutSector((u32)file->directory_sector,file->buffer); |
278 | } |
263 | } |
279 | return(0); |
264 | return(0); |
280 | } |
265 | } |
281 | 266 | ||
282 | //________________________________________________________________________________________________________________________________________ |
267 | //________________________________________________________________________________________________________________________________________ |
283 | // Funtion: fclose_(File *file); |
268 | // Funtion: fclose_(File *file); |
284 | // |
269 | // |
285 | // Description: This function closes the open file by writing the remaining data from the buffer to the device and entering the filesize |
270 | // Description: This function closes the open file by writing the remaining data from the buffer to the device and entering the filesize |
286 | // in the directory entry. |
271 | // in the directory entry. |
287 | //________________________________________________________________________________________________________________________________________ |
272 | //________________________________________________________________________________________________________________________________________ |
288 | 273 | ||
289 | void fclose_(File *file) |
274 | void fclose_(File *file) |
290 | { |
275 | { |
291 | u8 cnt = 0; |
- | |
292 | - | ||
293 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) |
- | |
294 | { |
- | |
295 | if(&FilePointer[cnt] == file) // filepointer to be freed found? |
- | |
296 | { |
- | |
297 | FilePointer[cnt].state = _UNUSED; |
- | |
298 | } |
- | |
299 | - | ||
300 | fflush_(file); // save buffercontent to the sdcard |
- | |
301 | - | ||
302 | file->start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
- | |
303 | file->cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
- | |
304 | file->sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
- | |
305 | file->byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
276 | |
- | 277 | if(file != NULL) |
|
306 | file->mode = 0; // mode of fileoperation (read,write) |
278 | { |
307 | file->filesize = 0; // the size of the opend file in bytes. |
- | |
308 | file->fileposition = 0; // pointer to a character within the file 0 < fileposition < filesize |
- | |
309 | file->sector_in_buffer = 0; // the last sector read, wich is still in the sectorbuffer. |
- | |
310 | file->directory_sector = 0; // the sectorposition where the directoryentry has been made. |
- | |
311 | file->directory_index = 0; // the index to the directoryentry within the specified sector. |
- | |
312 | file->attribute = 0; // the attribute of the file opened. |
279 | fflush_(file); // save buffered data to the disk |
- | 280 | } |
|
313 | } |
281 | FreeFilePointer(file); // and free the filepointer. |
314 | } |
282 | } |
315 | 283 | ||
316 | //________________________________________________________________________________________________________________________________________ |
284 | //________________________________________________________________________________________________________________________________________ |
317 | // Funtion: s32 fread_(void *buffer, s32 size, s32 count, File *file); |
285 | // Funtion: u32 fread_(void *buffer, s32 size, s32 count, File *file); |
318 | // |
286 | // |
319 | // Description: This function reads count objects of the specified size from the actual position of the file to the specified buffer. |
287 | // Description: This function reads count objects of the specified size from the actual position of the file to the specified buffer. |
320 | // |
288 | // |
321 | // Returnvalue: The function returns the number of objects (not bytes) read from the file. |
289 | // Returnvalue: The function returns the number of objects (not bytes) read from the file. |
322 | // |
290 | // |
323 | //________________________________________________________________________________________________________________________________________ |
291 | //________________________________________________________________________________________________________________________________________ |
324 | 292 | ||
325 | u32 fread_(void *buffer, u32 size, u32 count, File *file) |
293 | u32 fread_(void *buffer, u32 size, u32 count, File *file) |
326 | { |
294 | { |
327 | u32 object_cnt = 0; // count the number of objects read from the file. |
295 | u32 object_cnt = 0; // count the number of objects read from the file. |
328 | u32 object_size = 0; // count the number of bytes read from the actual object. |
296 | u32 object_size = 0; // count the number of bytes read from the actual object. |
329 | u8 *buff_pnt = 0; // a pointer to the actual bufferposition. |
297 | u8 *buff_pnt = 0; // a pointer to the actual bufferposition. |
330 | u8 success = 1; // no error occured during read operation to the file. |
298 | u8 success = 1; // no error occured during read operation to the file. |
331 | 299 | ||
332 | buff_pnt = (u8 *) buffer; // cast the void pointer to an u8 * |
300 | buff_pnt = (u8 *) buffer; // cast the void pointer to an u8 * |
333 | 301 | ||
334 | while((object_cnt < count) && success) |
302 | while((object_cnt < count) && success) |
335 | { |
303 | { |
336 | object_size = size; |
304 | object_size = size; |
337 | while((size > 0) && success) |
305 | while((size > 0) && success) |
338 | { |
306 | { |
339 | *buff_pnt = (u8) fgetchar_(file); // read a byte from the buffer to the opened file. |
307 | *buff_pnt = (u8) fgetchar_(file); // read a byte from the buffer to the opened file. |
340 | buff_pnt++; |
308 | buff_pnt++; |
341 | size--; |
309 | size--; |
342 | } |
310 | } |
343 | if(success) object_cnt++; |
311 | if(success) object_cnt++; |
344 | } |
312 | } |
345 | 313 | ||
346 | return(object_cnt); // return the number of objects succesfully read from the file |
314 | return(object_cnt); // return the number of objects succesfully read from the file |
347 | } |
315 | } |
348 | 316 | ||
349 | //________________________________________________________________________________________________________________________________________ |
317 | //________________________________________________________________________________________________________________________________________ |
350 | // Funtion: s32 fwrite_(void *buffer, s32 size, s32 count, File *file); |
318 | // Funtion: u32 fwrite_(void *buffer, s32 size, s32 count, File *file); |
351 | // |
319 | // |
352 | // Description: This function writes count objects of the specified size from the buffer to the actual positon within the file. |
320 | // Description: This function writes count objects of the specified size from the buffer to the actual positon within the file. |
353 | // |
321 | // |
354 | // Returnvalue: The function returns the number of objects (not bytes) written to the file. |
322 | // Returnvalue: The function returns the number of objects (not bytes) written to the file. |
355 | // |
323 | // |
356 | //________________________________________________________________________________________________________________________________________ |
324 | //________________________________________________________________________________________________________________________________________ |
357 | 325 | ||
358 | u32 fwrite_(void *buffer, u32 size, u32 count, File *file) |
326 | u32 fwrite_(void *buffer, u32 size, u32 count, File *file) |
359 | { |
327 | { |
360 | u32 object_cnt = 0; // count the number of objects written to the file. |
328 | u32 object_cnt = 0; // count the number of objects written to the file. |
361 | u32 object_size = 0; // count the number of bytes written from the actual object. |
329 | u32 object_size = 0; // count the number of bytes written from the actual object. |
362 | u8 *buff_pnt = 0; // a pointer to the actual bufferposition. |
330 | u8 *buff_pnt = 0; // a pointer to the actual bufferposition. |
363 | u8 success = 1; // no error occured during write operation to the file. |
331 | u8 success = 1; // no error occured during write operation to the file. |
364 | 332 | ||
365 | buff_pnt = (u8 *) buffer; // cast the void pointer to an u8 * |
333 | buff_pnt = (u8 *) buffer; // cast the void pointer to an u8 * |
366 | 334 | ||
367 | while((object_cnt < count) && success) |
335 | while((object_cnt < count) && success) |
368 | { |
336 | { |
369 | object_size = size; |
337 | object_size = size; |
370 | while((size > 0) && success) |
338 | while((size > 0) && success) |
371 | { |
339 | { |
372 | success = fputchar_(file, *buff_pnt); // write a byte from the buffer to the opened file. |
340 | success = fputchar_(file, *buff_pnt); // write a byte from the buffer to the opened file. |
373 | buff_pnt++; |
341 | buff_pnt++; |
374 | size--; |
342 | size--; |
375 | } |
343 | } |
376 | if(success) object_cnt++; |
344 | if(success) object_cnt++; |
377 | } |
345 | } |
378 | 346 | ||
379 | return(object_cnt); // return the number of objects succesfully written to the file |
347 | return(object_cnt); // return the number of objects succesfully written to the file |
380 | } // (!!!!! objects and not bytes !!!!) |
348 | } // (!!!!! objects and not bytes !!!!) |
381 | 349 | ||
382 | //________________________________________________________________________________________________________________________________________ |
350 | //________________________________________________________________________________________________________________________________________ |
383 | // Funtion: s16 fseek_(File *, s32, s16) |
351 | // Funtion: s16 fseek_(File *, s32, s16) |
384 | // |
352 | // |
385 | // Description: This function sets the pointer of the stream relative to the position |
353 | // Description: This function sets the pointer of the stream relative to the position |
386 | // specified by origin (SEEK_SET, SEEK_CUR, SEEK_END). |
354 | // specified by origin (SEEK_SET, SEEK_CUR, SEEK_END). |
387 | // |
355 | // |
388 | //________________________________________________________________________________________________________________________________________ |
356 | //________________________________________________________________________________________________________________________________________ |
389 | 357 | ||
390 | s16 fseek_(File *file, s32 offset, s16 origin) |
358 | s16 fseek_(File *file, s32 offset, s16 origin) |
391 | { |
359 | { |
392 | s32 fposition = 0; |
360 | s32 fposition = 0; |
393 | s16 retvalue = 1; |
361 | s16 retvalue = 1; |
394 | u32 temp = 0; |
362 | u32 temp = 0; |
395 | 363 | ||
396 | //...................................................... |
364 | //...................................................... |
397 | if(origin == SEEK_SET) // Fileposition relative to the beginning of the file. |
365 | if(origin == SEEK_SET) // Fileposition relative to the beginning of the file. |
398 | { |
366 | { |
399 | fposition = 0; |
367 | fposition = 0; |
400 | } |
368 | } |
401 | //...................................................... |
369 | //...................................................... |
402 | else if(origin == SEEK_END) // Fileposition relative to the end of the file. |
370 | else if(origin == SEEK_END) // Fileposition relative to the end of the file. |
403 | { |
371 | { |
404 | fposition = (s32) file->filesize; |
372 | fposition = (s32) file->filesize; |
405 | } |
373 | } |
406 | //...................................................... |
374 | //...................................................... |
407 | else if(origin == SEEK_CUR) // Fileposition relative to the current position of the file. |
375 | else if(origin == SEEK_CUR) // Fileposition relative to the current position of the file. |
408 | { |
376 | { |
409 | fposition = file->fileposition; |
377 | fposition = file->fileposition; |
410 | } |
378 | } |
411 | 379 | ||
412 | fposition += offset; |
380 | fposition += offset; |
413 | 381 | ||
414 | if((fposition >= 0) && (fposition <= (s32)file->filesize)) // is the pointer still within the file? |
382 | if((fposition >= 0) && (fposition <= (s32)file->filesize)) // is the pointer still within the file? |
415 | { |
383 | { |
416 | retvalue = 0; |
384 | retvalue = 0; |
417 | file->sector_index = 0; |
385 | file->sector_index = 0; |
418 | file->byte_index = 0; |
386 | file->byte_index = 0; |
419 | file->fileposition = 0; |
387 | file->fileposition = 0; |
420 | file->cluster_pointer = file->start_cluster; |
388 | file->cluster_pointer = file->start_cluster; |
421 | 389 | ||
422 | while(file->fileposition < fposition) |
390 | while(file->fileposition < fposition) |
423 | { |
391 | { |
424 | file->fileposition++; |
392 | file->fileposition++; |
425 | if(file->byte_index < 511) |
393 | if(file->byte_index < 511) |
426 | { |
394 | { |
427 | file->byte_index++; |
395 | file->byte_index++; |
428 | } |
396 | } |
429 | else |
397 | else |
430 | { |
398 | { |
431 | file->byte_index=0; // reading at the beginning of new sector. |
399 | file->byte_index=0; // reading at the beginning of new sector. |
432 | file->sector_index++; // continue reading in next sector |
400 | file->sector_index++; // continue reading in next sector |
433 | if(file->sector_index >= SectorsPerCluster) // When end of cluster is reached, the next datacluster has to be searched in the FAT. |
401 | if(file->sector_index >= SectorsPerCluster) // When end of cluster is reached, the next datacluster has to be searched in the FAT. |
434 | { |
402 | { |
435 | if(file->fileposition < fposition) |
403 | if(file->fileposition < fposition) |
436 | { |
404 | { |
437 | file->sector_index = 0; // start reading new cluster at first sector of the cluster. |
405 | file->sector_index = 0; // start reading new cluster at first sector of the cluster. |
438 | GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster. |
406 | GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster. |
439 | } |
407 | } |
440 | } |
408 | } |
441 | } |
409 | } |
442 | } |
410 | } |
443 | if(file->byte_index) |
411 | if(file->byte_index) |
444 | { |
412 | { |
445 | temp = (u32)((u32)file->cluster_pointer + (u32)file->sector_index); |
413 | temp = (u32)((u32)file->cluster_pointer + (u32)file->sector_index); |
446 | SDC_GetSector((u32)temp,file->buffer); // FileBuffer will be written at once at the end of the cluster and has to be updated. |
414 | SDC_GetSector((u32)temp,file->buffer); // FileBuffer will be written at once at the end of the cluster and has to be updated. |
447 | } |
415 | } |
448 | } |
416 | } |
449 | return(retvalue); |
417 | return(retvalue); |
450 | } |
418 | } |
451 | 419 | ||
452 | //________________________________________________________________________________________________________________________________________ |
420 | //________________________________________________________________________________________________________________________________________ |
453 | // Funtion: fgetchar_(File *file); |
421 | // Funtion: fgetchar_(File *file); |
454 | // |
422 | // |
455 | // Description: This function reads and returns one character from the specified file. Is the end of the actual sector reached the |
423 | // Description: This function reads and returns one character from the specified file. |
456 | // next sector of the cluster is read. Is the last sector of the cluster read the next cluster will be searched in FAT. |
- | |
- | 424 | // |
|
457 | // Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF. |
425 | // Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF. |
458 | //________________________________________________________________________________________________________________________________________ |
426 | //________________________________________________________________________________________________________________________________________ |
459 | 427 | ||
460 | s16 fgetchar_(File *file) |
428 | s16 fgetchar_(File *file) |
461 | { |
429 | { |
462 | s16 c = EOF; |
430 | s16 c = EOF; |
463 | u32 temp1; |
431 | u32 temp1; |
464 | 432 | ||
465 | if(file->filesize > 0) // wen the end of the file is not reached, get the next character. |
433 | if(((file->fileposition)+1) < file->filesize) // wen the end of the file is not reached, get the next character. |
466 | { |
434 | { |
467 | temp1 = (u32)file->cluster_pointer; // calculate the adress of the next character to be read. |
435 | temp1 = (u32)file->cluster_pointer; // calculate the adress of the next character to be read. |
468 | temp1 += (u32)file->sector_index; |
436 | temp1 += (u32)file->sector_index; |
469 | 437 | ||
470 | if(file->sector_in_buffer != temp1) // Has the content of the buffer been modified and has to be updated? |
438 | if(file->sector_in_buffer != temp1) // Has the content of the buffer been modified and has to be updated? |
471 | { |
439 | { |
472 | SDC_GetSector((u32)temp1,file->buffer); // Read the calculated cluster. |
440 | SDC_GetSector((u32)temp1,file->buffer); // Read the calculated cluster. |
473 | file->sector_in_buffer = (u32)temp1; |
441 | file->sector_in_buffer = (u32)temp1; |
474 | } |
442 | } |
475 | c = (s16) file->buffer[file->byte_index]; |
443 | c = (s16) file->buffer[file->byte_index]; |
476 | file->filesize--; // decrement the number of characters available. |
444 | file->fileposition++; |
477 | 445 | ||
478 | if(file->byte_index < 511) // continue reading from this sector until the end of the sector is reached. |
446 | if(file->byte_index < 511) // continue reading from this sector until the end of the sector is reached. |
479 | { |
447 | { |
480 | file->byte_index++; |
448 | file->byte_index++; |
481 | } |
449 | } |
482 | else // has the end of an sector been reached-> |
450 | else // has the end of an sector been reached-> |
483 | { |
451 | { |
484 | file->byte_index=0; // continue reading at the beginning - |
452 | file->byte_index=0; // continue reading at the beginning - |
485 | file->sector_index++; // of new sector. |
453 | file->sector_index++; // of new sector. |
486 | if(file->sector_index >= SectorsPerCluster) // When the end of an cluster is reached, the next datacluster has to be searched in the FAT. |
454 | if(file->sector_index >= SectorsPerCluster) // When the end of an cluster is reached, the next datacluster has to be searched in the FAT. |
487 | { |
455 | { |
488 | file->sector_index = 0; // start reading new cluster at first sector of the cluster. |
456 | file->sector_index = 0; // start reading new cluster at first sector of the cluster. |
489 | GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster. |
457 | GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster. |
490 | } |
458 | } |
491 | } |
459 | } |
492 | } |
460 | } |
493 | return(c); |
461 | return(c); |
494 | } |
462 | } |
495 | 463 | ||
496 | //________________________________________________________________________________________________________________________________________ |
464 | //________________________________________________________________________________________________________________________________________ |
497 | // Funtion: fputchar_(File *file); |
465 | // Funtion: fputchar_(File *file, s8 *); |
498 | // |
466 | // |
499 | // Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries. |
467 | // Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries. |
500 | // |
468 | // |
501 | // Returnvalue: The function returns a value of 0 if the data could not be written. |
469 | // Returnvalue: The function returns a value of 0 if the data could not be written. |
502 | //________________________________________________________________________________________________________________________________________ |
470 | //________________________________________________________________________________________________________________________________________ |
503 | 471 | ||
504 | u8 fputchar_(File *file,s8 c) |
472 | u8 fputchar_(File *file,s8 c) |
505 | { |
473 | { |
506 | u32 ul_temp = 0; |
474 | u32 ul_temp = 0; |
507 | u8 retvalue = 1; |
475 | u8 retvalue = 1; |
508 | 476 | ||
509 | if(file->sector_index >= SectorsPerCluster) // if end of the cluster is reached, find next free cluster |
477 | if(file->sector_index >= SectorsPerCluster) // if end of the cluster is reached, find next free cluster |
510 | { |
478 | { |
511 | file->sector_index = 0; |
479 | file->sector_index = 0; |
512 | if(!AppendCluster(file)) retvalue = 0; // append a new and free cluster at the end of the file. |
480 | if(!AppendCluster(file)) retvalue = 0; // append a new and free cluster at the end of the file. |
513 | } |
481 | } |
514 | 482 | ||
515 | file->buffer[file->byte_index] = c; // write databyte into the buffer. The byte will be written to the device at once |
483 | file->buffer[file->byte_index] = c; // write databyte into the buffer. The byte will be written to the device at once |
516 | if(file->filesize == file->fileposition) file->filesize++; // a character has been written to the file so the size is inkremented but only when the character has been added at the end of the file. |
484 | if(file->filesize == file->fileposition) file->filesize++; // a character has been written to the file so the size is inkremented but only when the character has been added at the end of the file. |
517 | file->fileposition++; // the actual positon within the file. |
485 | file->fileposition++; // the actual positon within the file. |
518 | // if the buffer contains the complete sectordata. |
486 | // if the buffer contains the complete sectordata. |
519 | if(file->byte_index < 511) // if the end of this sector is not reached yet |
487 | if(file->byte_index < 511) // if the end of this sector is not reached yet |
520 | { |
488 | { |
521 | file->byte_index++; // the next byte will be written to the next byteposition in this sector. |
489 | file->byte_index++; // the next byte will be written to the next byteposition in this sector. |
522 | } |
490 | } |
523 | else // otherwise the data in the sectorbuffer will be written to the device and the next sector will be selected. |
491 | else // otherwise the data in the sectorbuffer will be written to the device and the next sector will be selected. |
524 | { |
492 | { |
525 | ul_temp = (u32)file->cluster_pointer; |
493 | ul_temp = (u32)file->cluster_pointer; |
526 | ul_temp += (u32)file->sector_index; |
494 | ul_temp += (u32)file->sector_index; |
527 | 495 | ||
528 | SDC_PutSector((u32)ul_temp,file->buffer); |
496 | SDC_PutSector((u32)ul_temp,file->buffer); |
529 | file->byte_index=0; // and the next byte will be written at the beginning of this new sector. |
497 | file->byte_index=0; // and the next byte will be written at the beginning of this new sector. |
530 | file->sector_index++; |
498 | file->sector_index++; |
531 | if(file->sector_index >= SectorsPerCluster) // if end of the cluster is reached, find next free cluster |
499 | if(file->sector_index >= SectorsPerCluster) // if end of the cluster is reached, find next free cluster |
532 | { |
500 | { |
533 | file->sector_index = 0; |
501 | file->sector_index = 0; |
534 | if(!AppendCluster(file)) retvalue = 0; // append a new and free cluster at the end of the file. |
502 | if(!AppendCluster(file)) retvalue = 0; // append a new and free cluster at the end of the file. |
535 | } |
503 | } |
536 | } |
504 | } |
537 | return(retvalue); |
505 | return(retvalue); |
538 | } |
506 | } |
539 | 507 | ||
540 | //________________________________________________________________________________________________________________________________________ |
508 | //________________________________________________________________________________________________________________________________________ |
541 | // Funtion: fputs_(File *file); |
509 | // Funtion: fputs_(File *file, s8 *string); |
542 | // |
510 | // |
543 | // Description: This function writes a string to the specified file. |
511 | // Description: This function writes a zero terminated string to the specified file. |
544 | // |
512 | // |
545 | //________________________________________________________________________________________________________________________________________ |
513 | //________________________________________________________________________________________________________________________________________ |
546 | 514 | ||
547 | u8 fputs_(File *file,s8 * string) |
515 | u8 fputs_(File *file,s8 * string) |
548 | { |
516 | { |
549 | u8 i=0; |
517 | u8 i=0; |
550 | 518 | ||
551 | while(string[i] != 0) |
519 | while(string[i] != 0) |
552 | { |
520 | { |
553 | fputchar_(file,string[i]); |
521 | fputchar_(file,string[i]); |
554 | i++; |
522 | i++; |
555 | } |
523 | } |
556 | return(0); |
524 | return(0); |
557 | } |
525 | } |
558 | 526 | ||
559 | //________________________________________________________________________________________________________________________________________ |
527 | //________________________________________________________________________________________________________________________________________ |
560 | // Funtion: fgets_(File *file); |
528 | // Funtion: s8 * fgets_(s8 *string, s16 count, File *file) |
561 | // |
529 | // |
562 | // Description: This function reads a string from the file to the specifies string. |
530 | // Description: This function reads a string from the file to the specifies string until EOF or end of line was detected.. |
563 | // |
531 | // |
564 | // Returnvalue: A pointer to the string written from the file. |
532 | // Returnvalue: A pointer to the string or NULL if EOF was read from the file with no other characters before. |
565 | //________________________________________________________________________________________________________________________________________ |
533 | //________________________________________________________________________________________________________________________________________ |
566 | 534 | ||
567 | s8 * fgets_(s8 *string, s16 count, File *file) |
535 | s8 * fgets_(s8 *string, s16 count, File *file) |
568 | { |
536 | { |
569 | s16 buff_pnt = 0; |
537 | s16 buff_pnt = 0; |
570 | s16 buff_tmp = 0; |
538 | s16 buff_tmp = 0; |
571 | u8 state = 0; // Variable used for a statemachine to recognize the end of a line. |
539 | s8 *retvalue = 0; |
- | 540 | ||
- | 541 | retvalue = string; |
|
572 | 542 | ||
573 | while(count > 1) // read the count-1 characters from the file to the string. |
543 | while(count > 1) // read the count-1 characters from the file to the string. |
574 | { |
544 | { |
575 | buff_tmp = fgetchar_(file); // read a character from the opened file. |
545 | buff_tmp = fgetchar_(file); // read a character from the opened file. |
576 | switch(state) |
546 | if(buff_tmp == EOF) // is the end of the file reached, terminate the string with zero. |
577 | { |
- | |
578 | case 0: |
547 | { |
579 | if(buff_tmp == 0x0A)// state++; |
548 | if(buff_pnt != 0) // are there characters within the string? |
580 | { |
549 | { |
581 | count = 1; |
550 | break; |
582 | } |
551 | } |
- | 552 | else |
|
- | 553 | { |
|
583 | state = 0; |
554 | retvalue = NULL; // first character read was EOF -> return(NULL); |
584 | break; |
555 | break; |
585 | 556 | } |
|
586 | case 1: |
557 | } |
587 | if(buff_tmp == 0x0A) |
558 | else if(buff_tmp == 0x0A) // lf detected. |
588 | { |
559 | { |
589 | count = 1; |
- | |
590 | } |
560 | string[buff_pnt] = (s8) buff_tmp; // add lf to the string. |
591 | state = 0; |
561 | buff_pnt++; |
592 | break; |
562 | break; |
593 | } |
- | |
- | 563 | } |
|
- | 564 | else |
|
594 | if(buff_tmp == EOF) {buff_tmp = 0; count = 1;} // is the end of the file reached, terminate the string with zero. |
565 | { |
595 | string[buff_pnt] = (s8) buff_tmp; |
566 | string[buff_pnt] = (s8) buff_tmp; |
596 | count--; |
567 | count--; |
- | 568 | buff_pnt++; |
|
597 | buff_pnt++; |
569 | } |
598 | } |
570 | } |
599 | string[buff_pnt] = 0; |
571 | string[buff_pnt] = 0; // terminate string with zero |
600 | return(string); |
572 | return(string); |
601 | } |
573 | } |
602 | 574 | ||
603 | //________________________________________________________________________________________________________________________________________ |
575 | //________________________________________________________________________________________________________________________________________ |
604 | // Funtion: u8 fexist_(u8*, File *file); |
576 | // Funtion: u8 fexist_(u8*); |
605 | // |
577 | // |
606 | // Description: This function searches the specified file and returns 0 if the file was not found. |
578 | // Description: This function searches the specified file and returns 0 if the file was not found. |
607 | // |
579 | // |
608 | // |
580 | // |
609 | // Return: 0 = file does not exist |
581 | // Return: 0 = file does not exist |
610 | // 1 = file exists |
582 | // 1 = file exists |
611 | //________________________________________________________________________________________________________________________________________ |
583 | //________________________________________________________________________________________________________________________________________ |
612 | 584 | ||
613 | u8 fexist_(u8 *fname) |
585 | u8 fexist_(s8 *fname) |
614 | { |
586 | { |
- | 587 | File *file; |
|
- | 588 | ||
615 | File *file; |
589 | file = fopen_(fname,'r'); // try to open the specified file. |
616 | 590 | ||
617 | if((file = fopen_(fname,'r')) != 0) |
591 | if(file != NULL) |
618 | { |
592 | { |
619 | fclose_(file); |
593 | FreeFilePointer(file); // Free the filepoint // free the filepointer |
620 | return(1); |
594 | return(1); // file was found so return(1); |
621 | } |
595 | } |
622 | else |
596 | else |
623 | { |
597 | { |
624 | return(0); |
598 | return(0); // file not found return(0); |
625 | } |
599 | } |
626 | } |
600 | } |
627 | 601 | ||
628 | //________________________________________________________________________________________________________________________________________ |
602 | //________________________________________________________________________________________________________________________________________ |
629 | // Funtion: GetNextCluster(File *file); |
603 | // Funtion: GetNextCluster(File *file); |
630 | // |
604 | // |
631 | // Description: This function finds the next datacluster of the file specified with File *file. |
605 | // Description: This function finds the next datacluster of the file specified with File *file. |
632 | // |
606 | // |
633 | // Returnvalue: The function returns "0" if the last cluster has already been reached. |
607 | // Returnvalue: The function returns "0" if the last cluster has already been reached. |
634 | //________________________________________________________________________________________________________________________________________ |
608 | //________________________________________________________________________________________________________________________________________ |
635 | 609 | ||
636 | u16 GetNextCluster(File *file) |
610 | u16 GetNextCluster(File *file) |
637 | { |
611 | { |
638 | u32 fat_pointer = 0; |
612 | u32 fat_pointer = 0; |
639 | u32 fat_sector_offset = 0; |
613 | u32 fat_sector_offset = 0; |
640 | u32 ul_tmp = 0; |
614 | u32 ul_tmp = 0; |
641 | u8 retvalue = 0; // no new cluster found yet. |
615 | u8 retvalue = 0; // no new cluster found yet. |
642 | 616 | ||
643 | 617 | ||
644 | if((file->cluster_pointer >= RootDirectory) && (file->cluster_pointer < (RootDirectory + 31))) |
618 | if((file->cluster_pointer >= RootDirectory) && (file->cluster_pointer < (RootDirectory + 31))) |
645 | { // Is the next cluster searched within the rootdirectory and available? |
619 | { // Is the next cluster searched within the rootdirectory and available? |
646 | file->cluster_pointer++; // the rootdirectory is a linear adress space of 32 clusters. |
620 | file->cluster_pointer++; // the rootdirectory is a linear adress space of 32 clusters. |
647 | retvalue = 1; // and the next cluster has been found. |
621 | retvalue = 1; // and the next cluster has been found. |
648 | } |
622 | } |
649 | else if(file->cluster_pointer > (RootDirectory + 31)) // The specified cluster is within the FAT. |
623 | else if(file->cluster_pointer > (RootDirectory + 31)) // The specified cluster is within the FAT. |
650 | { |
624 | { |
651 | fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster within the FAT. |
625 | fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster within the FAT. |
652 | fat_sector_offset /= SectorsPerCluster; // calculate the index of the actual sector within the FAT. |
626 | fat_sector_offset /= SectorsPerCluster; // calculate the index of the actual sector within the FAT. |
653 | fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two. |
627 | fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two. |
654 | fat_pointer = (fat_sector_offset%0x100); // Calculate the sector within the cluster. |
628 | fat_pointer = (fat_sector_offset%0x100); // Calculate the sector within the cluster. |
655 | fat_sector_offset = (fat_sector_offset>>8); // and the position within the sector. |
629 | fat_sector_offset = (fat_sector_offset>>8); // and the position within the sector. |
656 | 630 | ||
657 | SDC_GetSector((u32)(FileAllocationTable + fat_sector_offset),file->buffer); |
631 | SDC_GetSector((u32)(FileAllocationTable + fat_sector_offset),file->buffer); |
658 | file->sector_in_buffer = (FileAllocationTable + fat_sector_offset); // Mark that new sector has been read. |
632 | file->sector_in_buffer = (FileAllocationTable + fat_sector_offset); // Mark that new sector has been read. |
659 | 633 | ||
660 | ul_tmp = (u32)file->buffer[((fat_pointer << 1)+1)]; // Read next sector information from calculated clusterposition. |
634 | ul_tmp = (u32)file->buffer[((fat_pointer << 1)+1)]; // Read next sector information from calculated clusterposition. |
661 | ul_tmp = (ul_tmp << 8); |
635 | ul_tmp = (ul_tmp << 8); |
662 | ul_tmp |= (u32)file->buffer[(fat_pointer << 1)]; |
636 | ul_tmp |= (u32)file->buffer[(fat_pointer << 1)]; |
663 | ul_tmp -=2; // next datacluster is clusterposition in fat - 2. |
637 | ul_tmp -=2; // next datacluster is clusterposition in fat - 2. |
664 | ul_tmp *= SectorsPerCluster; // calculate sectorposition of new cluster |
638 | ul_tmp *= SectorsPerCluster; // calculate sectorposition of new cluster |
665 | ul_tmp += FirstDataCluster; // in relation to first datacluster of the disk. |
639 | ul_tmp += FirstDataCluster; // in relation to first datacluster of the disk. |
666 | 640 | ||
667 | if(ul_tmp < 0xfff7) // has a new cluster been read or was the end of the fat reached? |
641 | if(ul_tmp < 0xfff7) // has a new cluster been read or was the end of the fat reached? |
668 | { |
642 | { |
669 | file->cluster_pointer = (u32) ul_tmp; // continue reading the file at the beginning of new datacluster. |
643 | file->cluster_pointer = (u32) ul_tmp; // continue reading the file at the beginning of new datacluster. |
670 | retvalue = 1; // a new cluster was found. |
644 | retvalue = 1; // a new cluster was found. |
671 | } |
645 | } |
672 | } |
646 | } |
673 | return(retvalue); |
647 | return(retvalue); |
674 | } |
648 | } |
675 | 649 | ||
676 | //________________________________________________________________________________________________________________________________________ |
650 | //________________________________________________________________________________________________________________________________________ |
677 | // Funtion: u16 FindNextFreeCluster(void) |
651 | // Funtion: u16 FindNextFreeCluster(void) |
678 | // |
652 | // |
679 | // Description: This function looks in the FAT to find the next free datacluster |
653 | // Description: This function looks in the FAT to find the next free datacluster |
680 | // |
654 | // |
681 | // Returnvalue: The function returns the adress of the next free cluster found within the fAT. |
655 | // Returnvalue: The function returns the adress of the next free cluster found within the fAT. |
682 | //________________________________________________________________________________________________________________________________________ |
656 | //________________________________________________________________________________________________________________________________________ |
683 | 657 | ||
684 | u16 FindNextFreeCluster(File *file) |
658 | u16 FindNextFreeCluster(File *file) |
685 | { |
659 | { |
686 | u32 fat_pointer = 0; // Pointer to the first sector of the FAT. |
660 | u32 fat_pointer = 0; // Pointer to the first sector of the FAT. |
687 | u32 ul_tmp = 0; // temporary variable used to calculate a sectornumber. |
661 | u32 ul_tmp = 0; // temporary variable used to calculate a sectornumber. |
688 | u16 fat_sector_offset = 0; // index to a sector within the FAT. |
662 | u16 fat_sector_offset = 0; // index to a sector within the FAT. |
689 | u16 fat_entry = 0; // index to an fatentry within the actual sector (256 fatentries are possible within one sector). |
663 | u16 fat_entry = 0; // index to an fatentry within the actual sector (256 fatentries are possible within one sector). |
690 | u16 free_cluster = 0; // a pointer to the first sector of the next free cluster. |
664 | u16 free_cluster = 0; // a pointer to the first sector of the next free cluster. |
691 | 665 | ||
692 | fat_pointer = (u32) FileAllocationTable; // start searching for empty cluster at the beginning of the fat. |
666 | fat_pointer = (u32) FileAllocationTable; // start searching for empty cluster at the beginning of the fat. |
693 | // if the end of the fat is not reached yet and no free cluster has been found |
667 | // if the end of the fat is not reached yet and no free cluster has been found |
694 | while((fat_sector_offset < SectorsPerFat) && (!free_cluster)) |
668 | while((fat_sector_offset < SectorsPerFat) && (!free_cluster)) |
695 | { |
669 | { |
696 | ul_tmp = (u32) ((u32)fat_pointer + (u32)fat_sector_offset); |
670 | ul_tmp = (u32) ((u32)fat_pointer + (u32)fat_sector_offset); |
697 | SDC_GetSector((u32)ul_tmp,file->buffer); // read next sector of FAT. |
671 | SDC_GetSector((u32)ul_tmp,file->buffer); // read next sector of FAT. |
698 | file->sector_in_buffer = ul_tmp; // remember the number of the sector in FileBuffer. |
672 | file->sector_in_buffer = ul_tmp; // remember the number of the sector in FileBuffer. |
699 | Fat = (struct FatEntry *)file->buffer; |
673 | Fat = (struct FatEntry *)file->buffer; |
700 | for(fat_entry=0;fat_entry<256;fat_entry++) // look for an free cluster at all entries in this sector of the fat. |
674 | for(fat_entry=0;fat_entry<256;fat_entry++) // look for an free cluster at all entries in this sector of the fat. |
701 | { |
675 | { |
702 | if(Fat[fat_entry].next_cluster == 0x0000) // empty cluster found!! |
676 | if(Fat[fat_entry].next_cluster == 0x0000) // empty cluster found!! |
703 | { |
677 | { |
704 | Fat[fat_entry].next_cluster = 0xffff; // mark this fat-entry as used and save it to the device. |
678 | Fat[fat_entry].next_cluster = 0xffff; // mark this fat-entry as used and save it to the device. |
705 | SDC_PutSector((u32)file->sector_in_buffer,file->buffer); |
679 | SDC_PutSector((u32)file->sector_in_buffer,file->buffer); |
706 | free_cluster = fat_entry; // the relative position of the free cluster found in this sector of the FAT. |
680 | free_cluster = fat_entry; // the relative position of the free cluster found in this sector of the FAT. |
707 | free_cluster += (fat_sector_offset << 8); // calculate the absolute position of the free cluster in the FAT; |
681 | free_cluster += (fat_sector_offset << 8); // calculate the absolute position of the free cluster in the FAT; |
708 | fat_entry = 256; // terminate the search for a free cluster in this sector. |
682 | fat_entry = 256; // terminate the search for a free cluster in this sector. |
709 | } |
683 | } |
710 | } |
684 | } |
711 | fat_sector_offset++; |
685 | fat_sector_offset++; |
712 | } |
686 | } |
713 | return(free_cluster); |
687 | return(free_cluster); |
714 | } |
688 | } |
715 | 689 | ||
716 | //________________________________________________________________________________________________________________________________________ |
690 | //________________________________________________________________________________________________________________________________________ |
717 | // Funtion: u16 AppendCluster(File *file); |
691 | // Funtion: u16 AppendCluster(File *file); |
718 | // |
692 | // |
719 | // Description: This function finds the next free datacluster on the disk and appends it to the specified file. |
693 | // Description: This function finds the next free datacluster on the disk and appends it to the specified file. |
720 | // |
694 | // |
721 | // Returnvalue: This funktion returns 1 if a cluster was appended to the specified file. |
695 | // Returnvalue: This funktion returns 1 if a cluster was appended to the specified file. |
722 | //________________________________________________________________________________________________________________________________________ |
696 | //________________________________________________________________________________________________________________________________________ |
723 | 697 | ||
724 | u8 AppendCluster(File *file) |
698 | u8 AppendCluster(File *file) |
725 | { |
699 | { |
726 | u16 free_cluster = 0; |
700 | u16 free_cluster = 0; |
727 | u32 fat_pointer = 0; |
701 | u32 fat_pointer = 0; |
728 | u8 retvalue = 0; |
702 | u8 retvalue = 0; |
729 | 703 | ||
730 | free_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
704 | free_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
731 | if(free_cluster) retvalue = 1; // A free cluster was found and can be added to the end of the file. |
705 | if(free_cluster) retvalue = 1; // A free cluster was found and can be added to the end of the file. |
732 | fat_pointer = FileAllocationTable; // Set Pointer to the beginnig of the FAT. |
706 | fat_pointer = FileAllocationTable; // Set Pointer to the beginnig of the FAT. |
733 | fat_pointer += (u32)((u32)GetFatClusterOffset(file) >> 8); // find the sector in the FAT with 256 entries per sector. |
707 | fat_pointer += (u32)((u32)GetFatClusterOffset(file) >> 8); // find the sector in the FAT with 256 entries per sector. |
734 | 708 | ||
735 | SDC_GetSector(fat_pointer,file->buffer); |
709 | SDC_GetSector(fat_pointer,file->buffer); |
736 | Fat = (struct FatEntry *)file->buffer; |
710 | Fat = (struct FatEntry *)file->buffer; |
737 | Fat[GetFatSectorIndex(file)].next_cluster = free_cluster; // append the free cluster to the end of the file in the FAT. |
711 | Fat[GetFatSectorIndex(file)].next_cluster = free_cluster; // append the free cluster to the end of the file in the FAT. |
738 | SDC_PutSector((u32)fat_pointer,file->buffer); // save the modified sector to the FAT. |
712 | SDC_PutSector((u32)fat_pointer,file->buffer); // save the modified sector to the FAT. |
739 | 713 | ||
740 | fat_pointer = (u32)free_cluster; |
714 | fat_pointer = (u32)free_cluster; |
741 | fat_pointer -= 2; |
715 | fat_pointer -= 2; |
742 | fat_pointer *= SectorsPerCluster; |
716 | fat_pointer *= SectorsPerCluster; |
743 | fat_pointer += FirstDataCluster; |
717 | fat_pointer += FirstDataCluster; |
744 | 718 | ||
745 | file->cluster_pointer = fat_pointer; // continue wrtiting to the file in the new and free datacluster. |
719 | file->cluster_pointer = fat_pointer; // continue wrtiting to the file in the new and free datacluster. |
746 | return(retvalue); // return 1 when a new cluster was appended to the file |
720 | return(retvalue); // return 1 when a new cluster was appended to the file |
- | 721 | } |
|
- | 722 | ||
- | 723 | ||
- | 724 | //________________________________________________________________________________________________________________________________________ |
|
- | 725 | // Funtion: void DeleteClusterChain(u16 startcluster); |
|
- | 726 | // |
|
- | 727 | // Description: This function frees all the clusters used for file from the fat. |
|
- | 728 | // |
|
- | 729 | // Returnvalue: none |
|
- | 730 | //________________________________________________________________________________________________________________________________________ |
|
- | 731 | ||
- | 732 | void DeleteClusterChain(u16 startcluster) |
|
- | 733 | { |
|
- | 734 | u16 fat_index = 0; |
|
- | 735 | u16 fat_sector_offset = 0; |
|
- | 736 | u32 sector_in_buffer = 0; |
|
- | 737 | u32 ul_temp = 0; |
|
- | 738 | u8 buffer[512]; |
|
- | 739 | ||
- | 740 | fat_index = (startcluster % 0x100); // Calculate the sector within the cluster. |
|
- | 741 | fat_sector_offset = (startcluster >> 8); // and the position within the sector. |
|
- | 742 | ul_temp = (u32)(FileAllocationTable + fat_sector_offset); |
|
- | 743 | ||
- | 744 | ||
- | 745 | do |
|
- | 746 | { |
|
- | 747 | if(sector_in_buffer != ul_temp) |
|
- | 748 | { |
|
- | 749 | sector_in_buffer = ul_temp; |
|
- | 750 | SDC_GetSector(ul_temp,buffer); |
|
- | 751 | } |
|
- | 752 | Fat = (struct FatEntry *)buffer; |
|
- | 753 | ||
- | 754 | startcluster = Fat[fat_index].next_cluster; |
|
- | 755 | Fat[fat_index].next_cluster = 0x0000; // free the cluster within the fat. |
|
- | 756 | ||
- | 757 | fat_index = (startcluster % 0x100); // Calculate the sector within the cluster. |
|
- | 758 | fat_sector_offset = (startcluster >> 8); // and the position within the sector. |
|
- | 759 | ul_temp = (u32)(FileAllocationTable + fat_sector_offset); |
|
- | 760 | if((sector_in_buffer != ul_temp) || (startcluster == 0xffff)) |
|
- | 761 | { |
|
- | 762 | SDC_PutSector(sector_in_buffer,buffer); |
|
- | 763 | } |
|
- | 764 | } |
|
- | 765 | while(startcluster != 0xffff); // last cluster has been deleted. |
|
- | 766 | } |
|
747 | } |
767 | |
748 | 768 | ||
749 | //________________________________________________________________________________________________________________________________________ |
769 | //________________________________________________________________________________________________________________________________________ |
750 | // Funtion: u16 GetFatClusterIndex(File *file); |
770 | // Funtion: u16 GetFatClusterIndex(File *file); |
751 | // |
771 | // |
752 | // Description: This function returns the clusterindex of the cluster specified by file->cluster_pointer of the specified file. |
772 | // Description: This function returns the clusterindex of the cluster specified by file->cluster_pointer of the specified file. |
753 | // |
773 | // |
754 | //________________________________________________________________________________________________________________________________________ |
774 | //________________________________________________________________________________________________________________________________________ |
755 | 775 | ||
756 | u16 GetFatClusterOffset(File *file) |
776 | u16 GetFatClusterOffset(File *file) |
757 | { |
777 | { |
758 | u32 fat_sector_offset = 0; |
778 | u32 fat_sector_offset = 0; |
759 | 779 | ||
760 | fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster in FAT. |
780 | fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster in FAT. |
761 | fat_sector_offset /= SectorsPerCluster; |
781 | fat_sector_offset /= SectorsPerCluster; |
762 | fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two. |
782 | fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two. |
763 | 783 | ||
764 | return((u16)fat_sector_offset); |
784 | return((u16)fat_sector_offset); |
765 | } |
785 | } |
766 | 786 | ||
767 | //________________________________________________________________________________________________________________________________________ |
787 | //________________________________________________________________________________________________________________________________________ |
768 | // Funtion: u16 GetFatSectorIndex(File *file); |
788 | // Funtion: u16 GetFatSectorIndex(File *file); |
769 | // |
789 | // |
770 | // Description: This function returns the sectorindex of the cluster specified by file->cluster_pointer of the specified file. |
790 | // Description: This function returns the sectorindex of the cluster specified by file->cluster_pointer of the specified file. |
771 | // |
791 | // |
772 | //________________________________________________________________________________________________________________________________________ |
792 | //________________________________________________________________________________________________________________________________________ |
773 | 793 | ||
774 | u16 GetFatSectorIndex(File *file) |
794 | u16 GetFatSectorIndex(File *file) |
775 | { |
795 | { |
776 | u16 fat_pointer = 0; |
796 | u16 fat_pointer = 0; |
777 | 797 | ||
778 | fat_pointer = GetFatClusterOffset(file); |
798 | fat_pointer = GetFatClusterOffset(file); |
779 | fat_pointer = fat_pointer % 0x100; // Calculate the clusterposition in the fat |
799 | fat_pointer = fat_pointer % 0x100; // Calculate the clusterposition in the fat |
780 | 800 | ||
781 | return(fat_pointer); |
801 | return(fat_pointer); |
782 | } |
802 | } |
783 | 803 | ||
784 | //________________________________________________________________________________________________________________________________________ |
804 | //________________________________________________________________________________________________________________________________________ |
785 | // Funtion: u16 CreateDirectoryEntry(u8 *, u16, u8 attrib) |
805 | // Funtion: u16 CreateFileInDirectory(u8 *, File *) |
786 | // |
806 | // |
787 | // Description: This function looks for the next free position in the directory and creates an entry. The type of an directoryentry is |
807 | // Description: This function looks for the next free position in the directory and creates an entry. The type of an directoryentry is |
788 | // specified by the attribute. |
808 | // specified by the attribute. |
789 | // bit0: unused |
809 | // bit0: unused |
790 | // bit1: archive |
810 | // bit1: archive |
791 | // bit2: read_only |
811 | // bit2: read_only |
792 | // bit3: system |
812 | // bit3: system |
793 | // bit4: directory |
813 | // bit4: directory |
794 | // bit5: volume |
814 | // bit5: volume |
795 | //________________________________________________________________________________________________________________________________________ |
815 | //________________________________________________________________________________________________________________________________________ |
796 | 816 | ||
797 | u8 CreateDirectoryEntry(u8 *fname, u16 cluster, File *file,u8 attrib) |
817 | u8 CreateFileInDirectory(s8 *fname, File *file) |
798 | { |
818 | { |
799 | u16 rootentry = 0; // index to an entry in the rootdirectory. |
819 | u16 rootentry = 0; // index to an entry in the rootdirectory. |
800 | u16 cnt_enries_searched = 0; // count the number of rootentries which have been searched already. |
820 | u16 cnt_enries_searched = 0; // count the number of rootentries which have been searched already. |
801 | u8 i = 0; |
821 | u8 i = 0; |
802 | u16 sector_offset = 0; // index to the sector of the Rootentry which is searched momentarily |
822 | u16 sector_offset = 0; // index to the sector of the Rootentry which is searched momentarily |
- | 823 | u8 retvalue = 0; |
|
- | 824 | u32 cluster_temp = 0; |
|
- | 825 | u16 cluster = 0; |
|
- | 826 | s8 name[11] = " "; |
|
- | 827 | ||
- | 828 | SeperateFileName(fname,name); |
|
- | 829 | ||
- | 830 | cluster_temp = (u32)FindNextFreeCluster(file); // the next free cluster on the disk. |
|
- | 831 | ||
- | 832 | if(cluster_temp) // if a free cluster is available: |
|
- | 833 | { |
|
- | 834 | cluster = (u16)cluster_temp; // remember the index of the free datacluster found for the directory entry. |
|
- | 835 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
|
- | 836 | cluster_temp *= SectorsPerCluster; // Calculate relative sectorindex of first datacluster. |
|
- | 837 | file->start_cluster = (FirstDataCluster + cluster_temp); // Calculate absolute sectorposition of first datacluster. |
|
803 | u8 retvalue = 0; |
838 | file->cluster_pointer = file->start_cluster; // start reading the file with the first sector of the first datacluster. |
804 | 839 | ||
805 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
840 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
806 | do |
841 | do |
807 | { // search the next 16 rootentries in this sector of the roordirectory. |
842 | { // search the next 16 rootentries in this sector of the roordirectory. |
808 | rootentry=0; |
843 | rootentry=0; |
809 | SDC_GetSector((u32)(RootDirectory + sector_offset),file->buffer); // Read the Rootdirectory. |
844 | SDC_GetSector((u32)(CWD + sector_offset),file->buffer); // Read the Rootdirectory. |
810 | DirectoryEntry = (struct DirEntry *)file->buffer; |
845 | DirectoryEntry = (struct DirEntry *)file->buffer; |
811 | while((rootentry<16) && (!retvalue)) |
- | |
812 | { |
- | |
813 | if((DirectoryEntry[rootentry].attribute == 0) || (DirectoryEntry[rootentry].attribute == 0xE5)) // empty directory entry found |
846 | while((rootentry<16) && (!retvalue)) |
- | 847 | { |
|
- | 848 | if((DirectoryEntry[rootentry].attribute == 0) || (DirectoryEntry[rootentry].attribute == 0xE5)) // empty directory entry found |
|
814 | { |
849 | { |
815 | for(i=0;i<11;i++) DirectoryEntry[rootentry].name[i] = fname[i]; // Kopie the filename and the file extension to the directoryentry. |
850 | for(i=0;i<11;i++) DirectoryEntry[rootentry].name[i] = name[i];// Kopie the filename and the file extension to the directoryentry. |
816 | DirectoryEntry[rootentry].attribute = attrib; // Set the fileattribute to archive to reserve the directoryentry. |
851 | DirectoryEntry[rootentry].attribute = _FILE; // Set the fileattribute to archive to reserve the directoryentry. |
817 | DirectoryEntry[rootentry].startcluster = cluster; // copy the location of the first datacluster to the directoryentry. |
852 | DirectoryEntry[rootentry].startcluster = cluster; // copy the location of the first datacluster to the directoryentry. |
818 | DirectoryEntry[rootentry].size = 0; // the new createted file has no content yet. |
853 | DirectoryEntry[rootentry].size = 0; // the new createted file has no content yet. |
819 | file->directory_sector = (u32) (RootDirectory + sector_offset); |
854 | file->directory_sector = (u32) (CWD + sector_offset); |
820 | file->directory_index = (u8) rootentry; |
855 | file->directory_index = (u8) rootentry; |
821 | retvalue = 1; |
856 | retvalue = 1; |
822 | SDC_PutSector((u32)(RootDirectory + sector_offset),file->buffer); |
857 | SDC_PutSector((u32)(CWD + sector_offset),file->buffer); |
823 | } |
858 | } |
824 | rootentry++; |
859 | rootentry++; |
825 | cnt_enries_searched++; |
860 | cnt_enries_searched++; |
826 | } |
861 | } |
827 | if(!retvalue) // file not found in this sector so take next sector. |
862 | if(!retvalue) // file not found in this sector so take next sector. |
828 | { |
863 | { |
829 | rootentry = 0; |
864 | rootentry = 0; |
- | 865 | sector_offset++; |
|
830 | sector_offset++; |
866 | } |
831 | } |
- | |
832 | } |
867 | } |
- | 868 | while((cnt_enries_searched< PossibleRootEntries) && (!retvalue)); |
|
833 | while((cnt_enries_searched< PossibleRootEntries) && (!retvalue)); |
869 | |
834 | 870 | } |
|
835 | return(retvalue); // return 1 if file has been created otherwise return 0. |
871 | return(retvalue); // return 1 if file has been created otherwise return 0. |
836 | } |
872 | } |
837 | 873 | ||
838 | //________________________________________________________________________________________________________________________________________ |
874 | //________________________________________________________________________________________________________________________________________ |
839 | // Funtion: u16 SeekDirectoryEntry(u8*, File *, u8) |
875 | // Funtion: u16 SeekFileInDirectory(s8 *fname, File *) |
840 | // |
876 | // |
841 | // Description: this function searches all possible rootentries until the file or directory is found or the end of the rootdirectory is reached |
877 | // Description: this function searches all possible entries withint the actual directory for the specified object. |
842 | // |
878 | // |
843 | // Returnvalue: This function returns 1 if the directoryentry specified was found. |
879 | // Returnvalue: This function returns 1 if the directoryentry specified was found. |
844 | //________________________________________________________________________________________________________________________________________ |
880 | //________________________________________________________________________________________________________________________________________ |
845 | 881 | ||
846 | u8 SeekDirectoryEntry(u8 *fname, File *file) |
882 | u8 SeekFileInDirectory(s8 *fname, File *file) |
847 | { |
883 | { |
848 | u16 rootentry=0; |
884 | u16 rootentry=0; |
849 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
885 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
850 | u8 i=0; |
886 | u8 i=0; |
851 | u8 retvalue = 0; |
887 | u8 retvalue = 0; |
852 | u32 cluster_temp = 0; |
888 | u32 cluster_temp = 0; |
- | 889 | s8 name[11] = " "; |
|
- | 890 | ||
- | 891 | SeperateFileName(fname,name); |
|
- | 892 | ||
853 | u16 cnt=0; |
893 | file->cluster_pointer = CWD; // start looking for the file in the actual directory. |
854 | 894 | ||
855 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
895 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
856 | do |
896 | do |
857 | { // search the next 16 rootentries in this sector of the roordirectory. |
897 | { // search the next 16 rootentries in this sector of the roordirectory. |
858 | rootentry=0; |
898 | rootentry=0; |
859 | SDC_GetSector((u32) file->cluster_pointer,file->buffer); // Read the Rootdirectory. |
899 | SDC_GetSector((u32) file->cluster_pointer,file->buffer); // Read the Rootdirectory. |
860 | DirectoryEntry = (struct DirEntry *)file->buffer; |
900 | DirectoryEntry = (struct DirEntry *)file->buffer; |
861 | 901 | ||
862 | while((!retvalue)&&(rootentry<16)) |
902 | while((!retvalue)&&(rootentry<16)) |
863 | { |
903 | { |
864 | cnt++; |
- | |
865 | - | ||
866 | i=0; |
904 | i=0; |
867 | while((i<10)&&(DirectoryEntry[rootentry].name[i] == fname[i])) |
905 | if(DirectoryEntry[rootentry].name[0] != 0xe5) // ignore deleted items. |
868 | { |
906 | { |
- | 907 | while((i<=10)&&(DirectoryEntry[rootentry].name[i] == name[i])) |
|
- | 908 | { |
|
869 | i++; |
909 | i++; |
- | 910 | } |
|
870 | } |
911 | } |
- | 912 | ||
871 | if((i==10) && (DirectoryEntry[rootentry].attribute == file->attribute)) // entry found!! -> reading startcluster of entry from offset 26. |
913 | if((i==11) && (DirectoryEntry[rootentry].attribute & _FILE)) // entry found!! -> reading startcluster of entry from offset 26. |
872 | { |
914 | { |
873 | cluster_temp = (u32)DirectoryEntry[rootentry].startcluster; |
915 | cluster_temp = (u32)DirectoryEntry[rootentry].startcluster; |
874 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
916 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
875 | cluster_temp *= (u32)SectorsPerCluster; // Calculate positon of first cluster. |
917 | cluster_temp *= (u32)SectorsPerCluster; // Calculate positon of first cluster. |
876 | file->start_cluster = (FirstDataCluster + cluster_temp); |
918 | file->start_cluster = (FirstDataCluster + cluster_temp); |
877 | file->directory_sector = (u32) file->cluster_pointer; |
919 | file->directory_sector = (u32) file->cluster_pointer; |
878 | file->cluster_pointer = file->start_cluster; // start reading the file with the first cluster. |
920 | file->cluster_pointer = file->start_cluster; // start reading the file with the first cluster. |
879 | file->filesize = (u32) DirectoryEntry[rootentry].size; |
921 | file->filesize = (u32) DirectoryEntry[rootentry].size; |
880 | file->directory_index = (u8) rootentry; |
922 | file->directory_index = (u8) rootentry; |
881 | retvalue = 1; |
923 | retvalue = 1; |
882 | } |
924 | } |
883 | rootentry++; |
925 | rootentry++; |
884 | } |
926 | } |
885 | if(!retvalue) // file not found in this sector so take next sector. |
927 | if(!retvalue) // file not found in this sector so take next sector. |
886 | { |
928 | { |
887 | end_of_directory_not_reached = GetNextCluster(file); |
929 | end_of_directory_not_reached = GetNextCluster(file); |
888 | } |
930 | } |
889 | } |
931 | } |
890 | while((end_of_directory_not_reached) && (!retvalue)); |
932 | while((end_of_directory_not_reached) && (!retvalue)); |
891 | return(retvalue); |
933 | return(retvalue); |
892 | } |
934 | } |
- | 935 | ||
- | 936 | ||
893 | 937 | ||
894 | //________________________________________________________________________________________________________________________________________ |
938 | //________________________________________________________________________________________________________________________________________ |
895 | // Funtion: void ScanSubDirectories(u8*, File *file); |
939 | // Funtion: void SeperateFileName(u8*); |
896 | // |
940 | // |
897 | // Description: This function scans the filename for subdirectories and changes the directory until the last directory is reached. |
- | |
898 | // here the specified file is searched. |
941 | // Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST TXT'); |
899 | // |
- | |
900 | // Returnvalue: |
942 | // |
901 | //________________________________________________________________________________________________________________________________________ |
943 | //________________________________________________________________________________________________________________________________________ |
902 | 944 | ||
903 | u8 ScanSubDirectories(u8 *fname, File *file) |
945 | void SeperateFileName(s8 *fname, s8 *name) |
904 | { |
946 | { |
905 | u8 readpointer = 0; // pointer to read a character within the filepath. |
947 | u8 readpointer = 0; |
906 | u8 writepointer = 0; // pointer to write a character into the string dirname. |
- | |
907 | u8 dircnt = 0; // the number of subdirectories found in filepath. |
- | |
908 | u8 dirname[11]; // temporary variable containing the name of a directory within the filepath. |
948 | u8 writepointer = 0; |
- | 949 | u8 attribute = 1; |
|
- | 950 | u8 i = 0; |
|
- | 951 | ||
- | 952 | ||
- | 953 | while((writepointer<=10) && (fname[readpointer]!=0)) // the rootdirectoryentry is 8bytes for filename and 3bytes for fileattribute. |
|
- | 954 | { // the filename in the rootdirectory is in the format "TEST TXT" without the dot. |
|
- | 955 | if(fname[readpointer]=='.') // seperating filename and attribute. |
|
- | 956 | { |
|
- | 957 | if(attribute) // is the filename "." or ".." ? |
|
- | 958 | { |
|
- | 959 | name[writepointer] = fname[readpointer]; |
|
- | 960 | readpointer++; |
|
- | 961 | writepointer++; |
|
- | 962 | } |
|
- | 963 | else |
|
- | 964 | { |
|
- | 965 | if(fname[(readpointer-1)] != '*') |
|
- | 966 | { |
|
- | 967 | for(i=writepointer;i<8;i++) |
|
- | 968 | { |
|
- | 969 | name[i] = ' '; |
|
- | 970 | } |
|
- | 971 | } |
|
- | 972 | readpointer++; |
|
- | 973 | writepointer = 8; |
|
- | 974 | } |
|
- | 975 | } |
|
- | 976 | else if(fname[readpointer] == '*') // wildcard found within the filename + extension. |
|
- | 977 | { |
|
- | 978 | if(writepointer < 8) // in extension. |
|
- | 979 | { |
|
- | 980 | readpointer++; |
|
- | 981 | writepointer = 8; |
|
- | 982 | } |
|
- | 983 | else // in filename. |
|
- | 984 | { |
|
- | 985 | writepointer = 11; // jump to the end of the string to terminate this function. |
|
- | 986 | } |
|
- | 987 | attribute = 0; |
|
- | 988 | } |
|
- | 989 | else |
|
- | 990 | { |
|
- | 991 | if((fname[readpointer]>96) && (fname[readpointer]<123)) |
|
- | 992 | { |
|
- | 993 | name[writepointer]=(fname[readpointer] - 32); // all characters must be upper case. |
|
- | 994 | } |
|
- | 995 | else |
|
- | 996 | { |
|
- | 997 | name[writepointer]=fname[readpointer]; |
|
- | 998 | } |
|
- | 999 | readpointer++; |
|
- | 1000 | writepointer++; |
|
- | 1001 | attribute = 0; |
|
- | 1002 | } |
|
- | 1003 | } |
|
- | 1004 | } |
|
- | 1005 | ||
- | 1006 | //________________________________________________________________________________________________________________________________________ |
|
- | 1007 | // Funtion: File * ReserveFilePointer_(void); |
|
- | 1008 | // |
|
- | 1009 | // Description: This function looks for a free filepointer and reserves it. |
|
- | 1010 | // |
|
- | 1011 | // |
|
- | 1012 | // Return: NULL = faild to reserve a filepointer |
|
- | 1013 | // otherwise filepointer |
|
- | 1014 | //________________________________________________________________________________________________________________________________________ |
|
- | 1015 | ||
- | 1016 | File * ReserveFilePointer(void) |
|
- | 1017 | { |
|
909 | u8 retvalue = 1; // no error opening a subdirectory occured yet. |
1018 | File *file; |
910 | u8 cnt = 0; // maximun number of characters in a path is 256; |
1019 | file = NULL; |
911 | - | ||
912 | 1020 | u8 temp; |
|
913 | while((fname[readpointer]!=0) && (cnt < 255)) // search the string until the end is reached. |
1021 | |
- | 1022 | for(temp = 0;temp<__MAX_FILES_USED;temp++) |
|
- | 1023 | { |
|
- | 1024 | if(FilePointer[temp].state == _UNUSED) // free filepointer found? |
|
- | 1025 | { |
|
- | 1026 | file = &FilePointer[temp]; |
|
- | 1027 | FilePointer[temp].state = _USED; // mark as used. |
|
- | 1028 | FilePointer[temp].mode = 0; // type of access (read/write) not defined yet. |
|
- | 1029 | FilePointer[temp].start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
|
- | 1030 | FilePointer[temp].cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
|
- | 1031 | FilePointer[temp].sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
|
- | 1032 | FilePointer[temp].byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
|
914 | { |
1033 | FilePointer[temp].filesize = 0; // the size of the opend file in bytes. |
- | 1034 | FilePointer[temp].fileposition = 0; // pointer to a character within the file 0 < fileposition < filesize |
|
- | 1035 | FilePointer[temp].sector_in_buffer = 0; // the last sector read, wich is still in the sectorbuffer. |
|
915 | cnt++; |
1036 | FilePointer[temp].directory_sector = 0; // the sectorposition where the directoryentry has been made. |
916 | if((fname[readpointer] == '/') && readpointer) // subdirectory found. ignore first "/" as directory. |
- | |
917 | { |
1037 | FilePointer[temp].directory_index = 0; // the index to the directoryentry within the specified sector. |
- | 1038 | FilePointer[temp].attribute = 0; // the attribute of the file opened. |
|
918 | dircnt++; // count the number of subdirectories found within the filepath. |
1039 | break; |
919 | } |
- | |
920 | readpointer++; |
- | |
921 | } |
- | |
- | 1040 | } |
|
- | 1041 | } |
|
- | 1042 | return(file); |
|
- | 1043 | } |
|
922 | 1044 | ||
- | 1045 | ||
- | 1046 | //________________________________________________________________________________________________________________________________________ |
|
- | 1047 | // Funtion: void FreeFilePointer_(File *); |
|
- | 1048 | // |
|
923 | for(writepointer=0;writepointer<10;writepointer++) dirname[writepointer] = ' '; |
1049 | // Description: This function free's the filepointer by marking it as unused. |
- | 1050 | // |
|
- | 1051 | // |
|
- | 1052 | // Return: none |
|
- | 1053 | // |
|
924 | writepointer = 0; |
1054 | //________________________________________________________________________________________________________________________________________ |
925 | readpointer = 0; // start scanning the string at the beginning. |
1055 | |
926 | 1056 | void FreeFilePointer(File *file) |
|
927 | file->cluster_pointer = RootDirectory; // always start searching in the rootdirectory. |
1057 | { |
- | 1058 | u8 cnt = 0; |
|
- | 1059 | ||
- | 1060 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) // Is the filepointeradress vaild? |
|
- | 1061 | { |
|
- | 1062 | if(&FilePointer[cnt] == file) // filepointer found therefore it must be valid |
|
- | 1063 | { |
|
- | 1064 | FilePointer[cnt].state = _UNUSED; // and can be marked as unused. |
|
- | 1065 | } |
|
- | 1066 | } |
|
- | 1067 | } |
|
- | 1068 | ||
- | 1069 | ||
- | 1070 | //________________________________________________________________________________________________________________________________________ |
|
- | 1071 | // Funtion: void DelteDirectoryEntry(Find *) |
|
- | 1072 | // |
|
- | 1073 | // Description: This function deletes the directoryentry of the specified item. |
|
- | 1074 | // |
|
- | 1075 | // |
|
- | 1076 | // returnvalue: 1 if the directory could be created. |
|
- | 1077 | //________________________________________________________________________________________________________________________________________ |
|
928 | file->attribute = _DIRECTORY; // the attribute of the item to be searched in the specified directory. |
1078 | |
- | 1079 | void DeleteDirectoryEntry(Find *item) |
|
- | 1080 | { |
|
- | 1081 | u8 buffer[512]; |
|
- | 1082 | ||
- | 1083 | ||
- | 1084 | SDC_GetSector((u32) item->cluster_pointer,buffer); // Read the Rootdirectory. |
|
- | 1085 | DirectoryEntry = (struct DirEntry *)buffer; |
|
- | 1086 | ||
- | 1087 | DirectoryEntry[(item->directory_index)-1].attribute = 0; // free the directoryentry. |
|
- | 1088 | DirectoryEntry[(item->directory_index)-1].name[0] = 0xE5; // free the directoryentry. |
|
- | 1089 | SDC_PutSector((u32) item->cluster_pointer,buffer); // Read the Rootdirectory. |
|
- | 1090 | } |
|
- | 1091 | ||
- | 1092 | ||
- | 1093 | ||
- | 1094 | ||
- | 1095 | //________________________________________________________________________________________________________________________________________ |
|
- | 1096 | // Funtion: u8 CreateSubDirectory(u8 *) |
|
- | 1097 | // |
|
- | 1098 | // Description: This function creates an directory within the directory specified by CWD |
|
- | 1099 | // |
|
929 | 1100 | // |
|
- | 1101 | // returnvalue: 1 if the directory could be created. |
|
- | 1102 | //________________________________________________________________________________________________________________________________________ |
|
- | 1103 | ||
- | 1104 | u8 CreateSubDirectory(s8 *fname) |
|
- | 1105 | { |
|
- | 1106 | u16 index = 0; // index to an entry in the rootdirectory. |
|
- | 1107 | u16 cnt_entries_searched = 0; // count the number of rootentries which have been searched already. |
|
- | 1108 | u16 i = 0; |
|
- | 1109 | u16 sector_offset = 0; // index to the sector of the entry which is searched momentarily |
|
- | 1110 | u8 retvalue = 0; |
|
- | 1111 | u32 cluster_temp = 0; |
|
- | 1112 | u16 cluster = 0; |
|
- | 1113 | File file; |
|
- | 1114 | s8 name[11] = {" "}; |
|
- | 1115 | ||
- | 1116 | SeperateFileName(fname,name); |
|
- | 1117 | cluster_temp = (u32)FindNextFreeCluster(&file); // the next free cluster on the disk. |
|
- | 1118 | ||
- | 1119 | if(cluster_temp) // if a free cluster is available: |
|
- | 1120 | { |
|
- | 1121 | cluster = (u16)cluster_temp; // remember the index of the free datacluster found for the directory entry. |
|
- | 1122 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
|
- | 1123 | cluster_temp *= SectorsPerCluster; // Calculate relative sectorindex of first datacluster. |
|
- | 1124 | file.start_cluster = (FirstDataCluster + cluster_temp); // Calculate absolute sectorposition of first datacluster. |
|
- | 1125 | file.cluster_pointer = file.start_cluster; // start reading the file with the first sector of the first datacluster. |
|
- | 1126 | ||
- | 1127 | // -Initialise new cluster to zero-------------------------------------------------------- |
|
- | 1128 | for(i=0;i<512;i++) |
|
- | 1129 | { |
|
- | 1130 | file.buffer[i] = 0; // initialise buffer to zero |
|
- | 1131 | } |
|
- | 1132 | for(sector_offset=0;sector_offset<SectorsPerCluster;sector_offset++) // initialise all sectors of new cluster with buffer. |
|
- | 1133 | { |
|
- | 1134 | SDC_PutSector((u32)(file.start_cluster + sector_offset),file.buffer); // save the initialised sector to the card. |
|
- | 1135 | } |
|
- | 1136 | // -Create directoryentry "." ------------------------------------------------------------- |
|
- | 1137 | DirectoryEntry = (struct DirEntry *)file.buffer; |
|
- | 1138 | DirectoryEntry[0].name[0] = '.'; |
|
- | 1139 | DirectoryEntry[0].attribute = _DIRECTORY; |
|
- | 1140 | DirectoryEntry[0].startcluster = cluster; |
|
930 | while(dircnt && retvalue) // scan all subdirectories found before. |
1141 | // -Create directoryentry "." ------------------------------------------------------------- |
- | 1142 | DirectoryEntry[1].name[0] = '.'; |
|
- | 1143 | DirectoryEntry[1].name[1] = '.'; |
|
- | 1144 | DirectoryEntry[1].attribute = _DIRECTORY; |
|
- | 1145 | if(CWD == RootDirectory) |
|
- | 1146 | { |
|
- | 1147 | DirectoryEntry[1].startcluster = 0; |
|
- | 1148 | } |
|
- | 1149 | else |
|
- | 1150 | { |
|
- | 1151 | cluster_temp = (CWD - FirstDataCluster); |
|
- | 1152 | cluster_temp /= SectorsPerCluster; |
|
- | 1153 | cluster_temp -= 2; |
|
- | 1154 | DirectoryEntry[1].startcluster = (u16) cluster_temp; |
|
- | 1155 | } |
|
- | 1156 | SDC_PutSector((u32) file.start_cluster,file.buffer); // save the initialised sector to the card. |
|
- | 1157 | // -create directoryentry within the cwd -------------------------------------------------- |
|
- | 1158 | sector_offset = 0; |
|
- | 1159 | cnt_entries_searched = 0; |
|
- | 1160 | do |
|
- | 1161 | { // search the next 16 rootentries in this sector of the roordirectory. |
|
- | 1162 | index=0; |
|
- | 1163 | SDC_GetSector((u32)(CWD + sector_offset),file.buffer); // Read the actual directory. |
|
- | 1164 | DirectoryEntry = (struct DirEntry *)file.buffer; |
|
- | 1165 | while((index<16) && (!retvalue)) |
|
- | 1166 | { |
|
- | 1167 | if((DirectoryEntry[index].attribute == 0) || (DirectoryEntry[index].attribute == 0xE5)) // empty directory entry found |
|
- | 1168 | { |
|
- | 1169 | for(i=0;i<11;i++) DirectoryEntry[index].name[i] = name[i]; // Kopie the filename and the file extension to the directoryentry. |
|
- | 1170 | DirectoryEntry[index].attribute = _DIRECTORY; // Set the fileattribute to archive to reserve the directoryentry. |
|
- | 1171 | DirectoryEntry[index].startcluster = cluster; // copy the location of the first datacluster to the directoryentry. |
|
- | 1172 | DirectoryEntry[index].size = 0; // the new createted file has no content yet. |
|
- | 1173 | file.directory_sector = (u32) (CWD + sector_offset); |
|
931 | { |
1174 | file.directory_index = (u8) index; |
932 | if(fname[readpointer] != '/') // is the end of the subdirectory entry not reached yet? |
- | |
- | 1175 | retvalue = 1; |
|
933 | { |
1176 | SDC_PutSector((u32)(CWD + sector_offset),file.buffer); |
- | 1177 | } |
|
934 | if((fname[readpointer]>96) && (fname[readpointer]<123)) |
1178 | index++; |
935 | { // all characters must be upper case. |
1179 | cnt_entries_searched++; |
- | 1180 | } |
|
- | 1181 | if(!retvalue) // file not found in this sector so take next sector. |
|
- | 1182 | { |
|
- | 1183 | index = 0; |
|
- | 1184 | sector_offset++; |
|
- | 1185 | } |
|
- | 1186 | } |
|
- | 1187 | while((cnt_entries_searched< PossibleRootEntries) && (!retvalue)); |
|
- | 1188 | ||
- | 1189 | } |
|
- | 1190 | return(retvalue); // return 1 if file has been created otherwise return 0. |
|
- | 1191 | } |
|
- | 1192 | ||
- | 1193 | ||
- | 1194 | //________________________________________________________________________________________________________________________________________ |
|
- | 1195 | // Funtion: u16 SeekSubDirectory(s8 *fname) |
|
- | 1196 | // |
|
- | 1197 | // Description: looks for the specified directory within the CWD. |
|
- | 1198 | // |
|
- | 1199 | // Returnvalue: If the specified directory was found the startcluster is returned. otherwise 0. |
|
- | 1200 | //________________________________________________________________________________________________________________________________________ |
|
- | 1201 | ||
- | 1202 | u16 SeekSubDirectory(s8 *fname) |
|
- | 1203 | { |
|
- | 1204 | u16 index = 0; |
|
- | 1205 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
|
- | 1206 | u8 i = 0; |
|
- | 1207 | u16 cluster_temp = 0; |
|
- | 1208 | s8 name[11] = " "; |
|
- | 1209 | File file; |
|
- | 1210 | ||
- | 1211 | SeperateFileName(fname,name); |
|
- | 1212 | ||
936 | dirname[writepointer]=(fname[readpointer] - 32); |
1213 | file.cluster_pointer = CWD; // start looking for the file in the actual directory. |
- | 1214 | file.start_cluster = CWD; // start looking for the file in the actual directory. |
|
- | 1215 | ||
- | 1216 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
|
- | 1217 | do |
|
937 | } |
1218 | { // search the next 16 rootentries in this sector of the roordirectory. |
938 | else |
1219 | index=0; |
- | 1220 | SDC_GetSector((u32) file.cluster_pointer,file.buffer); // Read the Rootdirectory. |
|
939 | { |
1221 | DirectoryEntry = (struct DirEntry *)file.buffer; |
- | 1222 | ||
- | 1223 | while((!cluster_temp)&&(index<16)) |
|
940 | dirname[writepointer]=fname[readpointer]; |
1224 | { |
- | 1225 | i=0; |
|
- | 1226 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
|
- | 1227 | { |
|
941 | } |
1228 | while((i<=10)&&(DirectoryEntry[index].name[i] == name[i])) |
942 | writepointer++; |
1229 | { |
943 | } |
1230 | i++; |
944 | else |
1231 | } |
- | 1232 | } |
|
- | 1233 | ||
945 | { |
1234 | if((i==11) && (DirectoryEntry[index].attribute & _DIRECTORY)) // entry found!! -> reading startcluster of entry from offset 26. |
- | 1235 | { |
|
946 | dircnt--; |
1236 | cluster_temp = (u16)DirectoryEntry[index].startcluster; |
947 | if(!(SeekDirectoryEntry(dirname, file))) // was the seperated subdirectory not found? |
1237 | } |
948 | { |
- | |
949 | retvalue = 0; // leave the function with return(0) otherwise continue with the next subdirectory until all directories have been searched. |
1238 | index++; |
- | 1239 | } |
|
- | 1240 | if(!cluster_temp) // file not found in this sector so take next sector. |
|
- | 1241 | { |
|
- | 1242 | end_of_directory_not_reached = GetNextCluster(&file); |
|
- | 1243 | } |
|
- | 1244 | } |
|
- | 1245 | while((end_of_directory_not_reached) && (!cluster_temp)); |
|
- | 1246 | return(cluster_temp); |
|
- | 1247 | } |
|
950 | } |
1248 | |
- | 1249 | ||
- | 1250 | ||
- | 1251 | //________________________________________________________________________________________________________________________________________ |
|
- | 1252 | // Funtion: u8 mkdir_(u8 *) |
|
- | 1253 | // |
|
- | 1254 | // Description: This function checks if the directory to be created already exists. If not the directory will be created. |
|
- | 1255 | // |
|
- | 1256 | // |
|
- | 1257 | // returnvalue: 1 if the directory could be created. |
|
- | 1258 | //________________________________________________________________________________________________________________________________________ |
|
- | 1259 | ||
- | 1260 | u8 mkdir_(s8 *fname) |
|
- | 1261 | { |
|
- | 1262 | u8 retvalue = 0; |
|
- | 1263 | ||
- | 1264 | retvalue = SeekSubDirectory(fname); // check wether the specified directory already exists. |
|
- | 1265 | ||
- | 1266 | if(!retvalue) |
|
- | 1267 | { |
|
- | 1268 | CreateSubDirectory(fname); // if directory doesn't exist, create it. |
|
- | 1269 | retvalue = 1; |
|
951 | for(writepointer=0;writepointer<10;writepointer++) dirname[writepointer] = ' '; |
1270 | } |
952 | writepointer = 0; |
1271 | else |
953 | } |
1272 | { |
954 | readpointer++; |
1273 | retvalue = 0; |
955 | } |
1274 | } |
956 | file->attribute = _FILE; // All subdirectories have been searched. the next thing to find is the specified file. |
1275 | |
957 | return(retvalue); |
1276 | return(retvalue); |
958 | } |
1277 | } |
- | 1278 | ||
959 | 1279 | ||
960 | //________________________________________________________________________________________________________________________________________ |
1280 | //________________________________________________________________________________________________________________________________________ |
961 | // Funtion: void SeperateFileName(u8*); |
1281 | // Funtion: u8 chdir_(u8 *) |
962 | // |
1282 | // |
- | 1283 | // Description: This function changes the CWD to the directory specified. |
|
963 | // Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST TXT'); |
1284 | // |
- | 1285 | // |
|
964 | // |
1286 | // returnvalue: 1 if the directory could be changed. |
965 | //________________________________________________________________________________________________________________________________________ |
1287 | //________________________________________________________________________________________________________________________________________ |
966 | 1288 | ||
967 | void SeperateFileName(u8 *fname, u8 *name) |
1289 | u8 chdir_(s8 *fname) |
968 | { |
1290 | { |
969 | u8 readpointer = 0; |
- | |
970 | u8 writepointer = 0; |
- | |
- | 1291 | u8 retvalue = 0; |
|
- | 1292 | s8 name[11] = {" "}; |
|
- | 1293 | ||
- | 1294 | u32 ultemp = 0; |
|
- | 1295 | ||
- | 1296 | SeperateFileName(fname,name); |
|
- | 1297 | ||
- | 1298 | ultemp = (u32)SeekSubDirectory(name); |
|
971 | u8 dircnt = 0; // the number of subdirectories found in filepath. |
1299 | if(ultemp >= 2) |
- | 1300 | { |
|
- | 1301 | ultemp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
|
- | 1302 | ultemp *= SectorsPerCluster; // Calculate relative sectorindex of first datacluster. |
|
- | 1303 | ultemp += FirstDataCluster; |
|
- | 1304 | CWD = ultemp; |
|
972 | u8 fileindex = 0; // the position in the filenamestring where the last "/" is found. |
1305 | retvalue = 1; |
- | 1306 | } |
|
- | 1307 | else |
|
- | 1308 | { |
|
- | 1309 | CWD = RootDirectory; |
|
- | 1310 | retvalue = 1; |
|
- | 1311 | } |
|
- | 1312 | ||
- | 1313 | return(retvalue); |
|
- | 1314 | } |
|
- | 1315 | ||
- | 1316 | ||
- | 1317 | //________________________________________________________________________________________________________________________________________ |
|
- | 1318 | // Funtion: u8 FindItem(s8 *fname, Find *) |
|
- | 1319 | // |
|
- | 1320 | // Description: finds an item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered. |
|
- | 1321 | // |
|
- | 1322 | // Returnvalue: If an item was found this function returns '1' else '0'. |
|
- | 1323 | //________________________________________________________________________________________________________________________________________ |
|
- | 1324 | ||
- | 1325 | u8 FindItem(Find *item) |
|
- | 1326 | { |
|
- | 1327 | u16 index = 0; |
|
- | 1328 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
|
- | 1329 | u8 i = 0; |
|
- | 1330 | u8 readpointer = 0; |
|
- | 1331 | u8 writepointer = 0; |
|
- | 1332 | u8 retvalue = 0; |
|
- | 1333 | File file; |
|
- | 1334 | ||
- | 1335 | ||
- | 1336 | file.cluster_pointer = item->cluster_pointer; |
|
- | 1337 | file.start_cluster = item->cluster_pointer; |
|
- | 1338 | index = item->directory_index; |
|
- | 1339 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
|
- | 1340 | do |
|
- | 1341 | { // search the next 16 rootentries in this sector of the roordirectory. |
|
- | 1342 | SDC_GetSector((u32) file.cluster_pointer,file.buffer); // Read the Rootdirectory. |
|
- | 1343 | DirectoryEntry = (struct DirEntry *)file.buffer; |
|
- | 1344 | ||
- | 1345 | while((!retvalue)&&(index<16)) |
|
- | 1346 | { |
|
- | 1347 | i=0; |
|
- | 1348 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
|
- | 1349 | { |
|
- | 1350 | while((i<=10)&&((DirectoryEntry[index].name[i] == item->searchstring[i]) || (item->searchstring[i]=='*') || item->searchstring[i]=='?')) |
|
- | 1351 | { |
|
- | 1352 | i++; |
|
- | 1353 | } |
|
- | 1354 | } |
|
- | 1355 | ||
- | 1356 | if((i==11) && (DirectoryEntry[index].attribute & item->attribute)) |
|
- | 1357 | { |
|
- | 1358 | for(readpointer=0;readpointer<=10;readpointer++) |
|
973 | 1359 | { |
|
- | 1360 | if(DirectoryEntry[index].name[readpointer] != ' ') |
|
- | 1361 | { |
|
- | 1362 | item->name[writepointer] = DirectoryEntry[index].name[readpointer]; // copy the name of the item found to the find_structure. |
|
- | 1363 | writepointer++; |
|
- | 1364 | } |
|
- | 1365 | else if((readpointer==7) && (DirectoryEntry[index].attribute == _FILE)) // if the item found is a file |
|
- | 1366 | { |
|
- | 1367 | if(DirectoryEntry[index].name[readpointer] != ' ') |
|
- | 1368 | { |
|
- | 1369 | item->name[writepointer] = DirectoryEntry[index].name[readpointer]; // copy the name of the item found to the find_structure. |
|
- | 1370 | writepointer++; |
|
- | 1371 | } |
|
- | 1372 | item->name[writepointer] = '.'; // then seperate the name and the extension by a '.' at index 8. |
|
- | 1373 | writepointer++; |
|
- | 1374 | } |
|
- | 1375 | } |
|
- | 1376 | item->startcluster = (u16)DirectoryEntry[index].startcluster; |
|
- | 1377 | item->directory_index = ++index; |
|
- | 1378 | item->cluster_pointer = file.cluster_pointer; |
|
- | 1379 | retvalue = 1; |
|
- | 1380 | } |
|
974 | while(fname[readpointer]!=0) // search the string until the end is reached. |
1381 | index++; |
975 | { |
1382 | } |
976 | if((fname[readpointer] == '/') && readpointer) // subdirectory found. ignore first "/" as directory. |
- | |
977 | { |
1383 | if(!retvalue) // file not found in this sector so take next sector. |
978 | dircnt++; // subdirectory entry found. |
1384 | { |
979 | fileindex = (readpointer + 1); // store the index of the last slash found as the beginning of the filename. |
1385 | end_of_directory_not_reached = GetNextCluster(&file); |
- | 1386 | } |
|
- | 1387 | index = 0; |
|
- | 1388 | } |
|
- | 1389 | while((end_of_directory_not_reached) && (!retvalue)); |
|
- | 1390 | ||
- | 1391 | return(retvalue); |
|
- | 1392 | } |
|
- | 1393 | ||
- | 1394 | ||
- | 1395 | //________________________________________________________________________________________________________________________________________ |
|
- | 1396 | // Funtion: u8 findfirst(s8 *fname, Find *) |
|
- | 1397 | // |
|
- | 1398 | // Description: finds the first item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered. |
|
- | 1399 | // |
|
- | 1400 | // Returnvalue: If an item was found this function returns '1' else '0'. |
|
- | 1401 | //________________________________________________________________________________________________________________________________________ |
|
- | 1402 | ||
- | 1403 | u8 findfirst_(s8 *fname, Find *item, u8 attribute) |
|
- | 1404 | { |
|
- | 1405 | u8 retvalue = 0; |
|
- | 1406 | u8 i = 0; |
|
- | 1407 | ||
- | 1408 | for(i=0;i<=11;i++) |
|
- | 1409 | { |
|
- | 1410 | item->searchstring[i] = '*'; // initialise the searchstring with wildcards. |
|
- | 1411 | item->name[i] = 0; |
|
- | 1412 | } |
|
980 | } |
1413 | |
- | 1414 | SeperateFileName(fname,item->searchstring); |
|
- | 1415 | ||
- | 1416 | item->cluster_pointer = CWD; // findfirst_ starts at the beginning of the cwd. |
|
- | 1417 | item->directory_index = 0; |
|
- | 1418 | item->attribute = attribute; |
|
- | 1419 | ||
- | 1420 | retvalue = FindItem(item); |
|
- | 1421 | ||
- | 1422 | return(retvalue); |
|
- | 1423 | } |
|
- | 1424 | ||
- | 1425 | ||
- | 1426 | //________________________________________________________________________________________________________________________________________ |
|
- | 1427 | // Funtion: u8 findnext(Find *) |
|
- | 1428 | // |
|
- | 1429 | // Description: finds the first item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered. |
|
- | 1430 | // |
|
- | 1431 | // Returnvalue: If an item was found this function returns '1' else '0'. |
|
- | 1432 | //________________________________________________________________________________________________________________________________________ |
|
- | 1433 | ||
981 | readpointer++; |
1434 | u8 findnext_(Find *item) |
- | 1435 | { |
|
- | 1436 | u8 retvalue = 0; |
|
- | 1437 | u8 i = 0; |
|
- | 1438 | ||
- | 1439 | for(i=0;i<=11;i++) // copy the name of the searched item to the findstructure. |
|
- | 1440 | { |
|
- | 1441 | item->name[i] = 0; |
|
- | 1442 | } |
|
- | 1443 | ||
- | 1444 | retvalue = FindItem(item); // search the item. |
|
- | 1445 | ||
- | 1446 | return(retvalue); |
|
- | 1447 | } |
|
- | 1448 | ||
- | 1449 | ||
- | 1450 | ||
- | 1451 | //________________________________________________________________________________________________________________________________________ |
|
- | 1452 | // Funtion: u8 fdelete(s8 *fname) |
|
- | 1453 | // |
|
- | 1454 | // Description: Deletes the file specified by fname. |
|
- | 1455 | // |
|
- | 1456 | // Returnvalue: |
|
- | 1457 | //________________________________________________________________________________________________________________________________________ |
|
- | 1458 | ||
- | 1459 | u8 fdelete_(s8 *fname) |
|
- | 1460 | { |
|
- | 1461 | u8 retvalue = 0; |
|
982 | } |
1462 | Find item; |
- | 1463 | ||
983 | readpointer = fileindex; // start seperating the filename from the directory at the stored position. |
1464 | retvalue = findfirst_(fname,&item, _FILE); // look for the item to be deleted. |
984 | dircnt = 0; |
1465 | |
- | 1466 | if(retvalue); // item found? |
|
- | 1467 | { |
|
985 | 1468 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
|
986 | while((writepointer<=10) && (fname[readpointer]!=0)) // the rootdirectoryentry is 8bytes for filename and 3bytes for fileattribute. |
1469 | DeleteDirectoryEntry(&item); // free the directoryentry. |
987 | { // the filename in the rootdirectory is in the format "TEST TXT" without the dot. |
1470 | |
- | 1471 | do |
|
- | 1472 | { |
|
- | 1473 | retvalue = findnext_(&item); |
|
- | 1474 | if(retvalue) |
|
988 | if(fname[readpointer]=='.') // seperating filename and attribute. |
1475 | { |
- | 1476 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
|
- | 1477 | DeleteDirectoryEntry(&item); // free the directoryentry. |
|
- | 1478 | } |
|
- | 1479 | } |
|
- | 1480 | while(retvalue); |
|
- | 1481 | } |
|
- | 1482 | ||
- | 1483 | return(retvalue); |
|
- | 1484 | } |
|
- | 1485 | ||
- | 1486 | ||
- | 1487 | //________________________________________________________________________________________________________________________________________ |
|
- | 1488 | // Funtion: u8 rmdir(s8 *fname) |
|
- | 1489 | // |
|
- | 1490 | // Description: Deletes the directory specified by dname. |
|
- | 1491 | // |
|
- | 1492 | // Returnvalue: |
|
- | 1493 | //________________________________________________________________________________________________________________________________________ |
|
- | 1494 | ||
- | 1495 | u8 rmdir_(s8 *dname) |
|
- | 1496 | { |
|
- | 1497 | u8 retvalue = 0; |
|
- | 1498 | Find item; |
|
- | 1499 | ||
- | 1500 | retvalue = findfirst_(dname,&item, _DIRECTORY); // look for the item to be deleted. |
|
- | 1501 | ||
- | 1502 | if(retvalue); // item found? |
|
989 | { |
1503 | { |
990 | readpointer++; |
1504 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
991 | writepointer=8; |
1505 | DeleteDirectoryEntry(&item); // free the directoryentry. |
- | 1506 | ||
992 | } |
1507 | do |
993 | else |
1508 | { |
- | 1509 | retvalue = findnext_(&item); |
|
994 | { |
1510 | if(retvalue) |
995 | if((fname[readpointer]>96) && (fname[readpointer]<123)) |
- | |
996 | { |
- | |
997 | name[writepointer]=(fname[readpointer] - 32); // all characters must be upper case. |
- | |
998 | } |
- | |
999 | else |
- | |
1000 | { |
- | |
1001 | name[writepointer]=fname[readpointer]; |
1511 | { |
- | 1512 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
|
1002 | } |
1513 | DeleteDirectoryEntry(&item); // free the directoryentry. |
- | 1514 | } |
|
- | 1515 | } |
|
- | 1516 | while(retvalue); |
|
- | 1517 | } |
|
- | 1518 | ||
- | 1519 | return(retvalue); |
|
- | 1520 | } |
|
- | 1521 | ||
- | 1522 | ||
- | 1523 | //________________________________________________________________________________________________________________________________________ |
|
- | 1524 | // Funtion: u16 feof_(File *file) |
|
- | 1525 | // |
|
- | 1526 | // Description: This function checks wether the end of the file has been reached. |
|
- | 1527 | // |
|
- | 1528 | // Returnvalue: 0 if the end of the file was not reached otherwise 1. |
|
- | 1529 | //________________________________________________________________________________________________________________________________________ |
|
- | 1530 | ||
- | 1531 | u16 feof_(File *file) |
|
- | 1532 | { |
|
- | 1533 | if(((file->fileposition)+1) < (file->filesize)) |
|
- | 1534 | { |
|
- | 1535 | return(0); |
|
- | 1536 | } |
|
1003 | readpointer++; |
1537 | else |
1004 | writepointer++; |
1538 | { |
1005 | } |
1539 | return(1); |
1006 | } |
1540 | } |
1007 | } |
1541 | } |
1008 | 1542 |