Rev 1 | Rev 41 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1 | Rev 24 | ||
---|---|---|---|
Line 58... | Line 58... | ||
58 | #include "main.h" |
58 | #include "main.h" |
Line 59... | Line 59... | ||
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*); |
|
- | 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) |
|
79 | // u8 fexist_(u8*, File *file); |
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 | //........................................................................................................................................ |
Line 86... | Line 93... | ||
86 | // mailto: stephan.busker@mikro-control.de |
93 | // mailto: stephan.busker@mikro-control.de |
87 | //________________________________________________________________________________________________________________________________________ |
94 | //________________________________________________________________________________________________________________________________________ |
Line 88... | Line -... | ||
88 | - | ||
89 | 95 | ||
90 | 96 | ||
91 | 97 | ||
92 | //________________________________________________________________________________________________________________________________________ |
98 | //________________________________________________________________________________________________________________________________________ |
93 | // |
99 | // |
Line 94... | Line 100... | ||
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. |
- | |
104 | u32 FileAllocationTable = 0; // pointer to the first FAT |
- | |
105 | u32 RootDirectory = 0; // Pointer to the rootdirectory of the first partition. |
- | |
106 | u32 FirstDataCluster = 0; // Pointer to the first cluster containing data (cluster0). |
109 | u32 FirstPartitionSector = 0; // Distance in sectors between the first partition and the master bootrecord. |
Line 107... | Line -... | ||
107 | //u8 FileBuffer[512]; // Buffer for read and write operation from or to the mmc. |
- | |
Line -... | Line 110... | ||
- | 110 | u32 FileAllocationTable = 0; // pointer to the first FAT |
|
- | 111 | u32 RootDirectory = 0; // Pointer to the rootdirectory of the first partition. |
|
Line -... | Line 112... | ||
- | 112 | u32 FirstDataCluster = 0; // Pointer to the first cluster containing data (cluster0). |
|
Line 108... | Line 113... | ||
108 | 113 | u32 CWD = 0; // Pointer startcluster to the current working directory |
|
109 | struct DirEntry *DirectoryEntry; // Pointer to an entry of the directory. |
114 | |
110 | struct FatEntry *Fat; // Pointer to an entry of the fat (next clusterposition). |
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 | ||
Line 116... | Line 121... | ||
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 | // |
Line 123... | Line 128... | ||
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) |
Line 127... | Line 132... | ||
127 | { |
132 | { |
Line -... | Line 133... | ||
- | 133 | u8 retvalue = 0; |
|
128 | u8 retvalue = 0; |
134 | u8 cnt = 0; |
- | 135 | struct VBR_Entry *VBR; // Pointer to the VolumeBootRecord. |
|
129 | u8 cnt = 0; |
136 | struct MBR_Entry *MBR; // Pointer to the masterbootrecord. |
130 | struct VBR_Entry *VBR; // Pointer to the VolumeBootRecord. |
137 | File *file; |
131 | struct MBR_Entry *MBR; // Pointer to the masterbootrecord. |
138 | |
132 | File *file; |
139 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) |
133 | 140 | { |
|
- | 141 | FilePointer[cnt].state = _UNUSED; // declare the filepointers as unused. |
|
- | 142 | } |
|
- | 143 | ||
- | 144 | file = &FilePointer[0]; |
|
134 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) |
145 | |
135 | { |
- | |
136 | FilePointer[cnt].state = _UNUSED; // declare the filepointers as unused. |
146 | SerialPutString("\n\rFAT16 init..."); |
137 | } |
147 | while((SDC_Init() != SD_SUCCESS) && (cnt++<100)); |
138 | 148 | ||
139 | file = &FilePointer[0]; |
149 | if(cnt <100) // sdcard initialised succesfully |
140 | 150 | { |
|
141 | while((SDC_Init() != 0) && (cnt++<200)); // initialise the sdcard. |
151 | SDC_GetSector((u32)_MBR_SECTOR,file->buffer); // Read the MasterBootRecord from mmc. |
142 | if(cnt <200) //sdcard initialised succesfully |
152 | MBR = (struct MBR_Entry *) file->buffer; |
143 | { |
153 | FirstPartitionSector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
144 | SDC_GetSector((u32)MBR_SECTOR,file->buffer); // Read the MasterBootRecord from mmc. |
154 | if((MBR->PartitionEntry1.Type == _FAT16_32_MB_BIOS_Extension) || |
145 | MBR = (struct MBR_Entry *) file->buffer; |
155 | (MBR->PartitionEntry1.Type == _FAT16_ST_32_MB) || |
146 | FirstPartitionSector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
156 | (MBR->PartitionEntry1.Type == _FAT16_LT_32_MB)) |
147 | SDC_GetSector(FirstPartitionSector,file->buffer); // Read the volume bootrecord from mmc. |
- | |
148 | 157 | { |
|
149 | - | ||
150 | VBR = (struct VBR_Entry *) file->buffer; // Enter the VBR using the structure VBR_Entry. |
158 | SDC_GetSector(FirstPartitionSector,file->buffer); // Read the volume bootrecord from mmc. |
- | 159 | ||
- | 160 | VBR = (struct VBR_Entry *) file->buffer; // Enter the VBR using the structure VBR_Entry. |
|
- | 161 | SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
|
- | 162 | FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
|
- | 163 | PossibleRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
|
- | 164 | SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
|
- | 165 | ReservedSectors = VBR->ReservedSectors; // calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. |
|
151 | SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
166 | |
152 | FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
167 | FileAllocationTable = (u32)(FirstPartitionSector + (u32)ReservedSectors); // Calculate the position of the FileAllocationTable. |
153 | PossibleRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
168 | RootDirectory = (u32)((u32)FileAllocationTable + (u32)((u32)SectorsPerFat*(u32)FatCopies)); // Calculate the position of the Rootdirectory. |
154 | SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
169 | FirstDataCluster = (u32)((u32)RootDirectory + ((u32)(PossibleRootEntries>>4))); // Calculate the position of the first datacluster. |
Line 155... | Line 170... | ||
155 | ReservedSectors = VBR->ReservedSectors; // calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. |
170 | CWD = RootDirectory; // The actual directory is the rootdirectory. |
156 | 171 | retvalue = 1; |
|
157 | FileAllocationTable = (u32)(FirstPartitionSector + (u32)ReservedSectors); // Calculate the position of the FileAllocationTable. |
172 | SerialPutString("\n\rfilesystem ok"); |
158 | RootDirectory = (u32)((u32)FileAllocationTable + (u32)((u32)SectorsPerFat*(u32)FatCopies)); // Calculate the position of the Rootdirectory. |
173 | str_Volume.state = INITIALIZED; |
159 | FirstDataCluster = (u32)((u32)RootDirectory + ((u32)(PossibleRootEntries>>4))); // Calculate the position of the first datacluster. |
174 | } |
160 | 175 | else |
|
161 | if((file->buffer[0x36]=='F') &&(file->buffer[0x37]=='A')&&(file->buffer[0x38]=='T')&&(file->buffer[0x39]=='1')&&(file->buffer[0x3A]=='6')) |
176 | { |
162 | { |
177 | str_Volume.state = NOT_INITIALIZED; |
163 | retvalue = 1; |
178 | SerialPutString("\n\rfilesystem nok"); |
Line 164... | Line 179... | ||
164 | } |
179 | } |
165 | } |
180 | } |
166 | return(retvalue); |
- | |
167 | } |
- | |
168 | - | ||
169 | //________________________________________________________________________________________________________________________________________ |
181 | return(retvalue); |
Line 170... | Line -... | ||
170 | // Funtion: File * fopen_(u8*, u8 *, File *file); |
- | |
171 | // |
- | |
172 | // Description: This function looks for the specified file in the rootdirectory of the drive. If the file is found the number of the |
- | |
173 | // corrosponding datacluster is returned to main. Only modes 'r' (reading) and 'a' append are implemented yet. |
182 | } |
174 | // |
- | |
175 | // Return: 0 = faild to open speicified file |
- | |
176 | // 1 = file opened |
- | |
177 | //________________________________________________________________________________________________________________________________________ |
- | |
178 | - | ||
179 | File * fopen_(u8 *fname, s8 mode) |
- | |
Line 180... | Line 183... | ||
180 | { |
183 | |
- | 184 | //________________________________________________________________________________________________________________________________________ |
|
- | 185 | // Funtion: File * fopen_(u8*, u8 *); |
|
Line 181... | Line -... | ||
181 | u32 cluster_temp = 0; |
- | |
182 | u8 name[11] = " "; |
- | |
183 | u16 temp = 0; |
- | |
184 | File *file; |
- | |
185 | - | ||
186 | file=0; |
- | |
187 | for(temp = 0;temp<__MAX_FILES_USED;temp++) |
- | |
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 | } |
- | |
195 | } |
- | |
196 | - | ||
197 | if(file == 0) return(file); // no valid pointer |
186 | // |
198 | 187 | // Description: This function looks for the file to be opened in the directory specified by the variable "CWD" of the drive. |
|
- | 188 | // If the file was found this function returns a filepoiner to the opened file otherwise NULL// |
|
199 | file->start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
189 | // |
200 | file->cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
190 | // Return: Pointer to the opened file or NULL |
201 | file->sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
191 | // |
202 | file->byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
192 | //________________________________________________________________________________________________________________________________________ |
203 | file->mode = mode; // mode of fileoperation (read,write) |
- | |
204 | file->filesize = 0; // the size of the opend file in bytes. |
193 | |
205 | file->fileposition = 0; // pointer to a character within the file 0 < fileposition < filesize |
194 | File * fopen_(s8 *fname, s8 mode) |
206 | file->sector_in_buffer = 0; // the last sector read, wich is still in the sectorbuffer. |
195 | { |
207 | file->directory_sector = 0; // the sectorposition where the directoryentry has been made. |
196 | File *file; |
208 | file->directory_index = 0; // the index to the directoryentry within the specified sector. |
197 | |
209 | file->attribute = 0; // the attribute of the file opened. |
198 | file = ReserveFilePointer(); // reserve a filepointer. |
210 | - | ||
211 | if(ScanSubDirectories(&fname[0], file)) // Is the specified filepath available? |
199 | |
212 | { |
- | |
213 | SeperateFileName(&fname[0], &name[0]); // seperate the filename and attribute from the filepath and bring them in the correct format. |
- | |
214 | file->attribute = _FILE; |
200 | if(file != NULL) // A free filepointer was found. |
215 | if(SeekDirectoryEntry(&name[0], file)) // if file was found |
- | |
216 | { |
- | |
217 | if(mode == 'a') // open existing file for writing (append data at the end of the file) |
- | |
218 | { |
201 | { |
219 | fseek_(file, 0, SEEK_END); // fseek points to the end of the file |
202 | file->mode = mode; // mode of fileoperation (read,write) |
220 | } |
203 | |
- | 204 | if(SeekFileInDirectory(fname, file)) // if file was found |
|
- | 205 | { |
|
221 | return(file); |
206 | SerialPutString("ok"); |
222 | } |
207 | if(mode == 'a') // open existing file for writing (append data at the end of the file) |
- | 208 | { |
|
- | 209 | fseek_(file, 0, SEEK_END); // fseek points to the end of the file |
|
- | 210 | } |
|
- | 211 | } |
|
- | 212 | else |
|
223 | else |
213 | { |
224 | { |
214 | if((mode == 'a') || (mode == 'w')) // specified file doesn't exist so create new file for writing data. |
225 | if((mode == 'a') || (mode == 'w')) // specified file doesn't exist so create new file for writing data. |
215 | { |
226 | { |
216 | if(CreateFileInDirectory(fname,file)) // Could an entry for the new file in the rootdirectory be created? |
Line -... | Line 217... | ||
- | 217 | { |
|
- | 218 | return(file); |
|
227 | cluster_temp = (u32)FindNextFreeCluster(file); // the next free cluster on the disk. |
219 | } |
228 | if(cluster_temp) // if a free cluster is available: |
220 | else |
229 | { |
221 | { |
230 | temp = (u16)cluster_temp; // remember the index of the free datacluster found for the directory entry. |
222 | FreeFilePointer(file); // free the filepointer. |
231 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
223 | file = NULL; |
Line 253... | Line 245... | ||
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; |
Line 257... | Line -... | ||
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; |
Line 286... | Line 271... | ||
286 | // in the directory entry. |
271 | // in the directory entry. |
287 | //________________________________________________________________________________________________________________________________________ |
272 | //________________________________________________________________________________________________________________________________________ |
Line 288... | Line 273... | ||
288 | 273 | ||
289 | void fclose_(File *file) |
274 | void fclose_(File *file) |
290 | { |
- | |
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 | - | ||
Line 300... | Line -... | ||
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). |
275 | { |
- | 276 | ||
305 | file->byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
277 | if(file != NULL) |
306 | file->mode = 0; // mode of fileoperation (read,write) |
- | |
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. |
278 | { |
- | 279 | fflush_(file); // save buffered data to the disk |
|
312 | file->attribute = 0; // the attribute of the file opened. |
280 | } |
Line 313... | Line 281... | ||
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 | // |
Line 321... | Line 289... | ||
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 | { |
Line 327... | Line 295... | ||
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. |
Line 328... | Line 296... | ||
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 | { |
Line 339... | Line 307... | ||
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++; |
Line 341... | Line 309... | ||
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 | ||
Line 349... | Line 317... | ||
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. |
Line 355... | Line 323... | ||
355 | // |
323 | // |
Line 356... | Line 324... | ||
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 | ||
Line 367... | Line 335... | ||
367 | while((object_cnt < count) && success) |
335 | while((object_cnt < count) && success) |
368 | { |
336 | { |
Line 369... | Line 337... | ||
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. |
Line 392... | Line 360... | ||
392 | s32 fposition = 0; |
360 | s32 fposition = 0; |
393 | s16 retvalue = 1; |
361 | s16 retvalue = 1; |
394 | u32 temp = 0; |
362 | u32 temp = 0; |
Line 395... | Line 363... | ||
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; |
Line 410... | Line 378... | ||
410 | } |
378 | } |
Line 411... | Line 379... | ||
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; |
Line 426... | Line 394... | ||
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 | } |
Line 451... | Line 419... | ||
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. |
Line 458... | Line 426... | ||
458 | //________________________________________________________________________________________________________________________________________ |
426 | //________________________________________________________________________________________________________________________________________ |
459 | 427 | ||
460 | s16 fgetchar_(File *file) |
428 | s16 fgetchar_(File *file) |
461 | { |
429 | { |
Line 462... | Line 430... | ||
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. |
Line 466... | Line 434... | ||
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. |
Line 473... | Line 441... | ||
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++; |
Line 492... | Line 460... | ||
492 | } |
460 | } |
493 | return(c); |
461 | return(c); |
494 | } |
462 | } |
Line 495... | Line 463... | ||
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. |
Line 504... | Line 472... | ||
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; |
Line 508... | Line 476... | ||
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. |
Line 513... | Line 481... | ||
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 | { |
Line 525... | Line 493... | ||
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 | } |
Line 536... | Line 504... | ||
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); |
Line 542... | Line 510... | ||
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. |
Line 555... | Line 523... | ||
555 | } |
523 | } |
556 | return(0); |
524 | return(0); |
557 | } |
525 | } |
Line 558... | Line 526... | ||
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. |
Line 565... | Line 533... | ||
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; |
Line -... | Line 538... | ||
- | 538 | s16 buff_tmp = 0; |
|
- | 539 | s8 *retvalue = 0; |
|
570 | s16 buff_tmp = 0; |
540 | |
571 | u8 state = 0; // Variable used for a statemachine to recognize the end of a line. |
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. |
- | |
576 | switch(state) |
545 | buff_tmp = fgetchar_(file); // read a character from the opened file. |
577 | { |
546 | if(buff_tmp == EOF) // is the end of the file reached, terminate the string with zero. |
578 | case 0: |
547 | { |
579 | if(buff_tmp == 0x0A)// state++; |
548 | if(buff_pnt != 0) // are there characters within the string? |
580 | { |
549 | { |
- | 550 | break; |
|
- | 551 | } |
|
581 | count = 1; |
552 | else |
582 | } |
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) |
- | |
588 | { |
558 | else if(buff_tmp == 0x0A) // lf detected. |
589 | count = 1; |
559 | { |
590 | } |
560 | string[buff_pnt] = (s8) buff_tmp; // add lf to the string. |
591 | state = 0; |
- | |
- | 561 | buff_pnt++; |
|
- | 562 | break; |
|
592 | break; |
563 | } |
593 | } |
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 | { |
- | 566 | string[buff_pnt] = (s8) buff_tmp; |
|
595 | string[buff_pnt] = (s8) buff_tmp; |
567 | count--; |
596 | count--; |
568 | buff_pnt++; |
597 | buff_pnt++; |
569 | } |
598 | } |
570 | } |
Line 599... | Line 571... | ||
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 | // |
Line 608... | Line 580... | ||
608 | // |
580 | // |
609 | // Return: 0 = file does not exist |
581 | // Return: 0 = file does not exist |
610 | // 1 = file exists |
582 | // 1 = file exists |
- | 583 | //________________________________________________________________________________________________________________________________________ |
|
- | 584 | ||
Line 611... | Line 585... | ||
611 | //________________________________________________________________________________________________________________________________________ |
585 | u8 fexist_(s8 *fname) |
612 | 586 | { |
|
613 | u8 fexist_(u8 *fname) |
587 | File *file; |
614 | { |
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); |
Line 621... | Line 595... | ||
621 | } |
595 | } |
622 | else |
596 | else |
Line 636... | Line 610... | ||
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. |
Line 642... | Line 616... | ||
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))) |
Line 653... | Line 627... | ||
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. |
Line 656... | Line 630... | ||
656 | 630 | ||
657 | SDC_GetSector((u32)(FileAllocationTable + fat_sector_offset),file->buffer); |
631 | SDC_GetSector((u32)(FileAllocationTable + fat_sector_offset),file->buffer); |
Line 658... | Line 632... | ||
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. |
Line 664... | Line 638... | ||
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 | } |
Line 681... | Line 655... | ||
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 | //________________________________________________________________________________________________________________________________________ |
Line 683... | Line 657... | ||
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). |
Line 690... | Line 664... | ||
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++; |
Line 725... | Line 699... | ||
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; |
Line 729... | Line 703... | ||
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. |
Line 733... | Line 707... | ||
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; |
Line 737... | Line 711... | ||
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; |
Line 741... | Line 715... | ||
741 | fat_pointer -= 2; |
715 | fat_pointer -= 2; |
742 | fat_pointer *= SectorsPerCluster; |
716 | fat_pointer *= SectorsPerCluster; |
743 | fat_pointer += FirstDataCluster; |
717 | fat_pointer += FirstDataCluster; |
Line -... | Line 718... | ||
- | 718 | ||
- | 719 | file->cluster_pointer = fat_pointer; // continue wrtiting to the file in the new and free datacluster. |
|
- | 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 | } |
|
744 | 764 | } |
|
745 | file->cluster_pointer = fat_pointer; // continue wrtiting to the file in the new and free datacluster. |
765 | while(startcluster != 0xffff); // last cluster has been deleted. |
746 | return(retvalue); // return 1 when a new cluster was appended to the file |
766 | } |
747 | } |
767 | |
748 | 768 | ||
Line 755... | Line 775... | ||
755 | 775 | ||
756 | u16 GetFatClusterOffset(File *file) |
776 | u16 GetFatClusterOffset(File *file) |
757 | { |
777 | { |
Line 758... | Line 778... | ||
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. |
Line 761... | Line 781... | ||
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. |
Line 763... | Line 783... | ||
763 | 783 | ||
Line 774... | Line 794... | ||
774 | u16 GetFatSectorIndex(File *file) |
794 | u16 GetFatSectorIndex(File *file) |
775 | { |
795 | { |
776 | u16 fat_pointer = 0; |
796 | u16 fat_pointer = 0; |
Line 777... | Line 797... | ||
777 | 797 | ||
778 | fat_pointer = GetFatClusterOffset(file); |
798 | fat_pointer = GetFatClusterOffset(file); |
Line 779... | Line 799... | ||
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 | ||
Line 781... | Line 801... | ||
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 |
Line 792... | Line 812... | ||
792 | // bit3: system |
812 | // bit3: system |
793 | // bit4: directory |
813 | // bit4: directory |
794 | // bit5: volume |
814 | // bit5: volume |
795 | //________________________________________________________________________________________________________________________________________ |
815 | //________________________________________________________________________________________________________________________________________ |
Line 796... | Line 816... | ||
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 | } |
Line -... | Line 868... | ||
- | 868 | while((cnt_enries_searched< PossibleRootEntries) && (!retvalue)); |
|
833 | while((cnt_enries_searched< PossibleRootEntries) && (!retvalue)); |
869 | |
834 | 870 | } |
|
Line 835... | Line 871... | ||
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. |
Line 842... | Line 878... | ||
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. |
- | 886 | u8 i=0; |
|
- | 887 | u8 retvalue = 0; |
|
- | 888 | u32 cluster_temp = 0; |
|
- | 889 | s8 name[11] = " "; |
|
Line 850... | Line 890... | ||
850 | u8 i=0; |
890 | |
851 | u8 retvalue = 0; |
891 | SeperateFileName(fname,name); |
852 | u32 cluster_temp = 0; |
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. |
Line 856... | Line 896... | ||
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; |
- | |
859 | SDC_GetSector((u32) file->cluster_pointer,file->buffer); // Read the Rootdirectory. |
- | |
860 | DirectoryEntry = (struct DirEntry *)file->buffer; |
898 | rootentry=0; |
861 | 899 | SDC_GetSector((u32) file->cluster_pointer,file->buffer); // Read the Rootdirectory. |
|
862 | while((!retvalue)&&(rootentry<16)) |
900 | DirectoryEntry = (struct DirEntry *)file->buffer; |
- | 901 | ||
- | 902 | while((!retvalue)&&(rootentry<16)) |
|
863 | { |
903 | { |
- | 904 | i=0; |
|
864 | cnt++; |
905 | if(DirectoryEntry[rootentry].name[0] != 0xe5) // ignore deleted items. |
- | 906 | { |
|
865 | 907 | while((i<=10)&&(DirectoryEntry[rootentry].name[i] == name[i])) |
|
866 | i=0; |
908 | { |
867 | while((i<10)&&(DirectoryEntry[rootentry].name[i] == fname[i])) |
909 | i++; |
868 | { |
910 | } |
869 | i++; |
911 | } |
870 | } |
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 | { |
Line -... | Line 929... | ||
- | 929 | end_of_directory_not_reached = GetNextCluster(file); |
|
- | 930 | } |
|
887 | end_of_directory_not_reached = GetNextCluster(file); |
931 | } |
888 | } |
932 | while((end_of_directory_not_reached) && (!retvalue)); |
889 | } |
933 | return(retvalue); |
890 | while((end_of_directory_not_reached) && (!retvalue)); |
934 | } |
891 | return(retvalue); |
- | |
892 | } |
935 | |
893 | - | ||
894 | //________________________________________________________________________________________________________________________________________ |
936 | |
Line 895... | Line 937... | ||
895 | // Funtion: void ScanSubDirectories(u8*, File *file); |
937 | |
896 | // |
938 | //________________________________________________________________________________________________________________________________________ |
897 | // Description: This function scans the filename for subdirectories and changes the directory until the last directory is reached. |
939 | // Funtion: void SeperateFileName(u8*); |
898 | // here the specified file is searched. |
940 | // |
899 | // |
941 | // Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST TXT'); |
900 | // Returnvalue: |
- | |
901 | //________________________________________________________________________________________________________________________________________ |
- | |
902 | 942 | // |
|
Line -... | Line 943... | ||
- | 943 | //________________________________________________________________________________________________________________________________________ |
|
- | 944 | ||
- | 945 | void SeperateFileName(s8 *fname, s8 *name) |
|
- | 946 | { |
|
- | 947 | u8 readpointer = 0; |
|
- | 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 | ||
Line -... | Line 1006... | ||
- | 1006 | //________________________________________________________________________________________________________________________________________ |
|
- | 1007 | // Funtion: File * ReserveFilePointer_(void); |
|
- | 1008 | // |
|
- | 1009 | // Description: This function looks for a free filepointer and reserves it. |
|
- | 1010 | // |
|
- | 1011 | // |
|
903 | u8 ScanSubDirectories(u8 *fname, File *file) |
1012 | // Return: NULL = faild to reserve a filepointer |
904 | { |
1013 | // otherwise filepointer |
905 | u8 readpointer = 0; // pointer to read a character within the filepath. |
- | |
906 | u8 writepointer = 0; // pointer to write a character into the string dirname. |
1014 | //________________________________________________________________________________________________________________________________________ |
907 | u8 dircnt = 0; // the number of subdirectories found in filepath. |
1015 | |
- | 1016 | File * ReserveFilePointer(void) |
|
- | 1017 | { |
|
- | 1018 | File *file; |
|
- | 1019 | file = NULL; |
|
- | 1020 | u8 temp; |
|
- | 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]; |
|
908 | u8 dirname[11]; // temporary variable containing the name of a directory within the filepath. |
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. |
|
909 | u8 retvalue = 1; // no error opening a subdirectory occured yet. |
1030 | FilePointer[temp].cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
910 | u8 cnt = 0; // maximun number of characters in a path is 256; |
- | |
911 | 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). |
|
912 | 1033 | FilePointer[temp].filesize = 0; // the size of the opend file in bytes. |
|
913 | while((fname[readpointer]!=0) && (cnt < 255)) // search the string until the end is reached. |
- | |
914 | { |
- | |
915 | cnt++; |
- | |
Line -... | Line 1034... | ||
- | 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. |
|
- | 1036 | FilePointer[temp].directory_sector = 0; // the sectorposition where the directoryentry has been made. |
|
- | 1037 | FilePointer[temp].directory_index = 0; // the index to the directoryentry within the specified sector. |
|
916 | if((fname[readpointer] == '/') && readpointer) // subdirectory found. ignore first "/" as directory. |
1038 | FilePointer[temp].attribute = 0; // the attribute of the file opened. |
- | 1039 | break; |
|
- | 1040 | } |
|
- | 1041 | } |
|
- | 1042 | return(file); |
|
917 | { |
1043 | } |
- | 1044 | ||
- | 1045 | ||
- | 1046 | //________________________________________________________________________________________________________________________________________ |
|
- | 1047 | // Funtion: void FreeFilePointer_(File *); |
|
Line 918... | Line 1048... | ||
918 | dircnt++; // count the number of subdirectories found within the filepath. |
1048 | // |
919 | } |
1049 | // Description: This function free's the filepointer by marking it as unused. |
920 | readpointer++; |
1050 | // |
921 | } |
1051 | // |
- | 1052 | // Return: none |
|
- | 1053 | // |
|
- | 1054 | //________________________________________________________________________________________________________________________________________ |
|
- | 1055 | ||
- | 1056 | void FreeFilePointer(File *file) |
|
- | 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 *) |
|
922 | 1072 | // |
|
- | 1073 | // Description: This function deletes the directoryentry of the specified item. |
|
- | 1074 | // |
|
- | 1075 | // |
|
- | 1076 | // returnvalue: 1 if the directory could be created. |
|
- | 1077 | //________________________________________________________________________________________________________________________________________ |
|
- | 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 | ||
923 | for(writepointer=0;writepointer<10;writepointer++) dirname[writepointer] = ' '; |
1094 | |
- | 1095 | //________________________________________________________________________________________________________________________________________ |
|
- | 1096 | // Funtion: u8 CreateSubDirectory(u8 *) |
|
- | 1097 | // |
|
- | 1098 | // Description: This function creates an directory within the directory specified by CWD |
|
- | 1099 | // |
|
- | 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. |
|
924 | writepointer = 0; |
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; |
|
- | 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 |
|
925 | readpointer = 0; // start scanning the string at the beginning. |
1168 | { |
926 | - | ||
- | 1169 | for(i=0;i<11;i++) DirectoryEntry[index].name[i] = name[i]; // Kopie the filename and the file extension to the directoryentry. |
|
927 | file->cluster_pointer = RootDirectory; // always start searching in the rootdirectory. |
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. |
|
928 | file->attribute = _DIRECTORY; // the attribute of the item to be searched in the specified directory. |
1172 | DirectoryEntry[index].size = 0; // the new createted file has no content yet. |
929 | 1173 | file.directory_sector = (u32) (CWD + sector_offset); |
|
- | 1174 | file.directory_index = (u8) index; |
|
- | 1175 | retvalue = 1; |
|
- | 1176 | SDC_PutSector((u32)(CWD + sector_offset),file.buffer); |
|
- | 1177 | } |
|
- | 1178 | index++; |
|
- | 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; |
|
930 | while(dircnt && retvalue) // scan all subdirectories found before. |
1207 | u16 cluster_temp = 0; |
- | 1208 | s8 name[11] = " "; |
|
- | 1209 | File file; |
|
- | 1210 | ||
- | 1211 | SeperateFileName(fname,name); |
|
931 | { |
1212 | |
932 | if(fname[readpointer] != '/') // is the end of the subdirectory entry not reached yet? |
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. |
|
933 | { |
1215 | |
- | 1216 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
|
- | 1217 | do |
|
934 | if((fname[readpointer]>96) && (fname[readpointer]<123)) |
1218 | { // search the next 16 rootentries in this sector of the roordirectory. |
- | 1219 | index=0; |
|
- | 1220 | SDC_GetSector((u32) file.cluster_pointer,file.buffer); // Read the Rootdirectory. |
|
- | 1221 | DirectoryEntry = (struct DirEntry *)file.buffer; |
|
935 | { // all characters must be upper case. |
1222 | |
936 | dirname[writepointer]=(fname[readpointer] - 32); |
1223 | while((!cluster_temp)&&(index<16)) |
937 | } |
1224 | { |
938 | else |
1225 | i=0; |
- | 1226 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
|
- | 1227 | { |
|
939 | { |
1228 | while((i<=10)&&(DirectoryEntry[index].name[i] == name[i])) |
- | 1229 | { |
|
940 | dirname[writepointer]=fname[readpointer]; |
1230 | i++; |
941 | } |
1231 | } |
942 | writepointer++; |
- | |
943 | } |
1232 | } |
- | 1233 | ||
- | 1234 | if((i==11) && (DirectoryEntry[index].attribute & _DIRECTORY)) // entry found!! -> reading startcluster of entry from offset 26. |
|
- | 1235 | { |
|
- | 1236 | cluster_temp = (u16)DirectoryEntry[index].startcluster; |
|
- | 1237 | } |
|
- | 1238 | index++; |
|
- | 1239 | } |
|
- | 1240 | if(!cluster_temp) // file not found in this sector so take next sector. |
|
- | 1241 | { |
|
944 | else |
1242 | end_of_directory_not_reached = GetNextCluster(&file); |
- | 1243 | } |
|
- | 1244 | } |
|
- | 1245 | while((end_of_directory_not_reached) && (!cluster_temp)); |
|
- | 1246 | return(cluster_temp); |
|
- | 1247 | } |
|
- | 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 | ||
945 | { |
1264 | retvalue = SeekSubDirectory(fname); // check wether the specified directory already exists. |
946 | dircnt--; |
1265 | |
Line -... | Line 1266... | ||
- | 1266 | if(!retvalue) |
|
947 | if(!(SeekDirectoryEntry(dirname, file))) // was the seperated subdirectory not found? |
1267 | { |
948 | { |
1268 | CreateSubDirectory(fname); // if directory doesn't exist, create it. |
949 | retvalue = 0; // leave the function with return(0) otherwise continue with the next subdirectory until all directories have been searched. |
1269 | retvalue = 1; |
950 | } |
1270 | } |
- | 1271 | else |
|
951 | for(writepointer=0;writepointer<10;writepointer++) dirname[writepointer] = ' '; |
1272 | { |
- | 1273 | retvalue = 0; |
|
952 | writepointer = 0; |
1274 | } |
Line 953... | Line 1275... | ||
953 | } |
1275 | |
954 | readpointer++; |
1276 | return(retvalue); |
955 | } |
1277 | } |
956 | file->attribute = _FILE; // All subdirectories have been searched. the next thing to find is the specified file. |
1278 | |
957 | return(retvalue); |
- | |
958 | } |
- | |
Line -... | Line 1279... | ||
- | 1279 | ||
- | 1280 | //________________________________________________________________________________________________________________________________________ |
|
- | 1281 | // Funtion: u8 chdir_(u8 *) |
|
- | 1282 | // |
|
- | 1283 | // Description: This function changes the CWD to the directory specified. |
|
- | 1284 | // |
|
- | 1285 | // |
|
- | 1286 | // returnvalue: 1 if the directory could be changed. |
|
959 | 1287 | //________________________________________________________________________________________________________________________________________ |
|
- | 1288 | ||
- | 1289 | u8 chdir_(s8 *fname) |
|
- | 1290 | { |
|
- | 1291 | u8 retvalue = 0; |
|
- | 1292 | s8 name[11] = {" "}; |
|
960 | //________________________________________________________________________________________________________________________________________ |
1293 | |
- | 1294 | u32 ultemp = 0; |
|
- | 1295 | ||
- | 1296 | SeperateFileName(fname,name); |
|
- | 1297 | ||
- | 1298 | ultemp = (u32)SeekSubDirectory(name); |
|
- | 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; |
|
- | 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 | { |
|
961 | // Funtion: void SeperateFileName(u8*); |
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++) |
|
- | 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 | { |
|
962 | // |
1369 | item->name[writepointer] = DirectoryEntry[index].name[readpointer]; // copy the name of the item found to the find_structure. |
963 | // Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST TXT'); |
1370 | writepointer++; |
964 | // |
- | |
965 | //________________________________________________________________________________________________________________________________________ |
1371 | } |
966 | 1372 | item->name[writepointer] = '.'; // then seperate the name and the extension by a '.' at index 8. |
|
967 | void SeperateFileName(u8 *fname, u8 *name) |
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 | } |
|
- | 1381 | index++; |
|
- | 1382 | } |
|
- | 1383 | if(!retvalue) // file not found in this sector so take next sector. |
|
- | 1384 | { |
|
- | 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'. |
|
968 | { |
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 | } |
|
- | 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 | ||
969 | u8 readpointer = 0; |
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 | ||
- | 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 | ||
Line 970... | Line 1450... | ||
970 | u8 writepointer = 0; |
1450 | |
- | 1451 | //________________________________________________________________________________________________________________________________________ |
|
971 | u8 dircnt = 0; // the number of subdirectories found in filepath. |
1452 | // Funtion: u8 fdelete(s8 *fname) |
972 | u8 fileindex = 0; // the position in the filenamestring where the last "/" is found. |
1453 | // |
- | 1454 | // Description: Deletes the file specified by fname. |
|
- | 1455 | // |
|
973 | 1456 | // Returnvalue: |
|
974 | while(fname[readpointer]!=0) // search the string until the end is reached. |
1457 | //________________________________________________________________________________________________________________________________________ |
975 | { |
1458 | |
- | 1459 | u8 fdelete_(s8 *fname) |
|
- | 1460 | { |
|
- | 1461 | u8 retvalue = 0; |
|
- | 1462 | Find item; |
|
976 | if((fname[readpointer] == '/') && readpointer) // subdirectory found. ignore first "/" as directory. |
1463 | |
- | 1464 | retvalue = findfirst_(fname,&item, _FILE); // look for the item to be deleted. |
|
- | 1465 | ||
- | 1466 | if(retvalue); // item found? |
|
- | 1467 | { |
|
- | 1468 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
|
- | 1469 | DeleteDirectoryEntry(&item); // free the directoryentry. |
|
- | 1470 | ||
- | 1471 | do |
|
- | 1472 | { |
|
- | 1473 | retvalue = findnext_(&item); |
|
- | 1474 | if(retvalue) |
|
- | 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. |
|
977 | { |
1491 | // |
978 | dircnt++; // subdirectory entry found. |
1492 | // Returnvalue: |
979 | fileindex = (readpointer + 1); // store the index of the last slash found as the beginning of the filename. |
1493 | //________________________________________________________________________________________________________________________________________ |
- | 1494 | ||
980 | } |
1495 | u8 rmdir_(s8 *dname) |
981 | readpointer++; |
1496 | { |
- | 1497 | u8 retvalue = 0; |
|
982 | } |
1498 | Find item; |
983 | readpointer = fileindex; // start seperating the filename from the directory at the stored position. |
- | |
984 | dircnt = 0; |
- | |
985 | - | ||
986 | while((writepointer<=10) && (fname[readpointer]!=0)) // the rootdirectoryentry is 8bytes for filename and 3bytes for fileattribute. |
- | |
987 | { // the filename in the rootdirectory is in the format "TEST TXT" without the dot. |
- | |
988 | if(fname[readpointer]=='.') // seperating filename and attribute. |
- | |
989 | { |
1499 | |
- | 1500 | retvalue = findfirst_(dname,&item, _DIRECTORY); // look for the item to be deleted. |
|
990 | readpointer++; |
1501 | |
- | 1502 | if(retvalue); // item found? |
|
- | 1503 | { |
|
- | 1504 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
|
- | 1505 | DeleteDirectoryEntry(&item); // free the directoryentry. |
|
- | 1506 | ||
- | 1507 | do |
|
- | 1508 | { |
|
- | 1509 | retvalue = findnext_(&item); |
|
- | 1510 | if(retvalue) |
|
- | 1511 | { |
|
- | 1512 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
|
- | 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) |
|
991 | writepointer=8; |
1525 | // |