Rev 1 | Rev 41 | Go to most recent revision | Show entire file | Regard 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 102... | Line 108... | ||
102 | u32 ReservedSectors = 0; // Sectors reserved by the filesystem. |
108 | u32 ReservedSectors = 0; // Sectors reserved by the filesystem. |
103 | u32 FirstPartitionSector = 0; // Distance in sectors between the first partition and the master bootrecord. |
109 | u32 FirstPartitionSector = 0; // Distance in sectors between the first partition and the master bootrecord. |
104 | u32 FileAllocationTable = 0; // pointer to the first FAT |
110 | u32 FileAllocationTable = 0; // pointer to the first FAT |
105 | u32 RootDirectory = 0; // Pointer to the rootdirectory of the first partition. |
111 | u32 RootDirectory = 0; // Pointer to the rootdirectory of the first partition. |
106 | u32 FirstDataCluster = 0; // Pointer to the first cluster containing data (cluster0). |
112 | u32 FirstDataCluster = 0; // Pointer to the first cluster containing data (cluster0). |
107 | //u8 FileBuffer[512]; // Buffer for read and write operation from or to the mmc. |
113 | u32 CWD = 0; // Pointer startcluster to the current working directory |
- | 114 | ||
Line 108... | Line 115... | ||
108 | 115 | ||
109 | struct DirEntry *DirectoryEntry; // Pointer to an entry of the directory. |
116 | struct DirEntry *DirectoryEntry; // Pointer to an entry of the directory. |
Line 110... | Line 117... | ||
110 | struct FatEntry *Fat; // Pointer to an entry of the fat (next clusterposition). |
117 | struct FatEntry *Fat; // Pointer to an entry of the fat (next clusterposition). |
Line 111... | Line -... | ||
111 | - | ||
112 | File FilePointer[__MAX_FILES_USED]; // Allocate Memmoryspace for each filepointer used. |
- | |
113 | 118 | ||
114 | 119 | File FilePointer[__MAX_FILES_USED]; // Allocate Memmoryspace for each filepointer used. |
|
115 | 120 | ||
116 | 121 | ||
117 | //________________________________________________________________________________________________________________________________________ |
122 | //________________________________________________________________________________________________________________________________________ |
118 | // Funtion: InitFat16(void); |
123 | // Funtion: InitFat16(void); |
119 | // |
124 | // |
120 | // Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdirectory |
125 | // Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdirectory |
Line 121... | Line 126... | ||
121 | // and stores the information in global variables. |
126 | // and stores the information in global variables. |
122 | // |
127 | // |
123 | // Returnvalue: The function returns "0" if the filesystem could not be initialized because no partition was found on the disc. |
128 | // Returnvalue: The function returns "0" if the filesystem could not be initialized because no partition was found on the volume. |
Line 136... | Line 141... | ||
136 | FilePointer[cnt].state = _UNUSED; // declare the filepointers as unused. |
141 | FilePointer[cnt].state = _UNUSED; // declare the filepointers as unused. |
137 | } |
142 | } |
Line 138... | Line 143... | ||
138 | 143 | ||
Line -... | Line 144... | ||
- | 144 | file = &FilePointer[0]; |
|
139 | file = &FilePointer[0]; |
145 | |
- | 146 | SerialPutString("\n\rFAT16 init..."); |
|
140 | 147 | while((SDC_Init() != SD_SUCCESS) && (cnt++<100)); |
|
141 | while((SDC_Init() != 0) && (cnt++<200)); // initialise the sdcard. |
148 | |
142 | if(cnt <200) //sdcard initialised succesfully |
149 | if(cnt <100) // sdcard initialised succesfully |
143 | { |
150 | { |
144 | SDC_GetSector((u32)MBR_SECTOR,file->buffer); // Read the MasterBootRecord from mmc. |
151 | SDC_GetSector((u32)_MBR_SECTOR,file->buffer); // Read the MasterBootRecord from mmc. |
- | 152 | MBR = (struct MBR_Entry *) file->buffer; |
|
- | 153 | FirstPartitionSector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
|
- | 154 | if((MBR->PartitionEntry1.Type == _FAT16_32_MB_BIOS_Extension) || |
|
- | 155 | (MBR->PartitionEntry1.Type == _FAT16_ST_32_MB) || |
|
145 | MBR = (struct MBR_Entry *) file->buffer; |
156 | (MBR->PartitionEntry1.Type == _FAT16_LT_32_MB)) |
Line 146... | Line -... | ||
146 | FirstPartitionSector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
- | |
147 | SDC_GetSector(FirstPartitionSector,file->buffer); // Read the volume bootrecord from mmc. |
157 | { |
148 | 158 | SDC_GetSector(FirstPartitionSector,file->buffer); // Read the volume bootrecord from mmc. |
|
149 | 159 | ||
150 | VBR = (struct VBR_Entry *) file->buffer; // Enter the VBR using the structure VBR_Entry. |
160 | VBR = (struct VBR_Entry *) file->buffer; // Enter the VBR using the structure VBR_Entry. |
151 | SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
161 | SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
152 | FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
162 | FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
Line 153... | Line 163... | ||
153 | PossibleRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
163 | PossibleRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
154 | SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
164 | SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
155 | ReservedSectors = VBR->ReservedSectors; // calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. |
165 | ReservedSectors = VBR->ReservedSectors; // calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. |
156 | - | ||
157 | FileAllocationTable = (u32)(FirstPartitionSector + (u32)ReservedSectors); // Calculate the position of the FileAllocationTable. |
166 | |
158 | RootDirectory = (u32)((u32)FileAllocationTable + (u32)((u32)SectorsPerFat*(u32)FatCopies)); // Calculate the position of the Rootdirectory. |
- | |
159 | FirstDataCluster = (u32)((u32)RootDirectory + ((u32)(PossibleRootEntries>>4))); // Calculate the position of the first datacluster. |
167 | FileAllocationTable = (u32)(FirstPartitionSector + (u32)ReservedSectors); // Calculate the position of the FileAllocationTable. |
- | 168 | RootDirectory = (u32)((u32)FileAllocationTable + (u32)((u32)SectorsPerFat*(u32)FatCopies)); // Calculate the position of the Rootdirectory. |
|
- | 169 | FirstDataCluster = (u32)((u32)RootDirectory + ((u32)(PossibleRootEntries>>4))); // Calculate the position of the first datacluster. |
|
- | 170 | CWD = RootDirectory; // The actual directory is the rootdirectory. |
|
- | 171 | retvalue = 1; |
|
- | 172 | SerialPutString("\n\rfilesystem ok"); |
|
- | 173 | str_Volume.state = INITIALIZED; |
|
- | 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); |
181 | return(retvalue); |
167 | } |
182 | } |
168 | 183 | ||
- | 184 | //________________________________________________________________________________________________________________________________________ |
|
- | 185 | // Funtion: File * fopen_(u8*, u8 *); |
|
169 | //________________________________________________________________________________________________________________________________________ |
186 | // |
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 |
187 | // Description: This function looks for the file to be opened in the directory specified by the variable "CWD" of the drive. |
Line 173... | Line 188... | ||
173 | // corrosponding datacluster is returned to main. Only modes 'r' (reading) and 'a' append are implemented yet. |
188 | // If the file was found this function returns a filepoiner to the opened file otherwise NULL// |
174 | // |
189 | // |
175 | // Return: 0 = faild to open speicified file |
- | |
176 | // 1 = file opened |
- | |
177 | //________________________________________________________________________________________________________________________________________ |
- | |
178 | 190 | // Return: Pointer to the opened file or NULL |
|
Line 179... | Line -... | ||
179 | File * fopen_(u8 *fname, s8 mode) |
- | |
180 | { |
- | |
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 | // |
Line 191... | Line -... | ||
191 | file = &FilePointer[temp]; |
- | |
192 | FilePointer[temp].state = _USED; // mark as used. |
192 | //________________________________________________________________________________________________________________________________________ |
193 | break; |
- | |
194 | } |
- | |
- | 193 | ||
195 | } |
194 | File * fopen_(s8 *fname, s8 mode) |
196 | - | ||
197 | if(file == 0) return(file); // no valid pointer |
- | |
198 | - | ||
199 | file->start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
- | |
200 | file->cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
- | |
201 | file->sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
- | |
202 | file->byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
195 | { |
203 | file->mode = mode; // mode of fileoperation (read,write) |
- | |
204 | file->filesize = 0; // the size of the opend file in bytes. |
- | |
205 | file->fileposition = 0; // pointer to a character within the file 0 < fileposition < filesize |
- | |
206 | file->sector_in_buffer = 0; // the last sector read, wich is still in the sectorbuffer. |
- | |
207 | file->directory_sector = 0; // the sectorposition where the directoryentry has been made. |
196 | File *file; |
208 | file->directory_index = 0; // the index to the directoryentry within the specified sector. |
197 | |
- | 198 | file = ReserveFilePointer(); // reserve a filepointer. |
|
209 | file->attribute = 0; // the attribute of the file opened. |
199 | |
210 | 200 | if(file != NULL) // A free filepointer was found. |
|
211 | if(ScanSubDirectories(&fname[0], file)) // Is the specified filepath available? |
201 | { |
212 | { |
202 | file->mode = mode; // mode of fileoperation (read,write) |
213 | SeperateFileName(&fname[0], &name[0]); // seperate the filename and attribute from the filepath and bring them in the correct format. |
- | |
214 | file->attribute = _FILE; |
203 | |
215 | if(SeekDirectoryEntry(&name[0], file)) // if file was found |
204 | if(SeekFileInDirectory(fname, file)) // if file was found |
216 | { |
205 | { |
217 | if(mode == 'a') // open existing file for writing (append data at the end of the file) |
206 | SerialPutString("ok"); |
218 | { |
207 | if(mode == 'a') // open existing file for writing (append data at the end of the file) |
219 | fseek_(file, 0, SEEK_END); // fseek points to the end of the file |
- | |
220 | } |
- | |
221 | return(file); |
- | |
222 | } |
- | |
223 | else |
- | |
224 | { |
- | |
225 | if((mode == 'a') || (mode == 'w')) // specified file doesn't exist so create new file for writing data. |
- | |
226 | { |
- | |
227 | cluster_temp = (u32)FindNextFreeCluster(file); // the next free cluster on the disk. |
208 | { |
228 | if(cluster_temp) // if a free cluster is available: |
209 | fseek_(file, 0, SEEK_END); // fseek points to the end of the file |
229 | { |
210 | } |
230 | temp = (u16)cluster_temp; // remember the index of the free datacluster found for the directory entry. |
211 | } |
- | 212 | else |
|
- | 213 | { |
|
- | 214 | if((mode == 'a') || (mode == 'w')) // specified file doesn't exist so create new file for writing data. |
|
- | 215 | { |
|
- | 216 | if(CreateFileInDirectory(fname,file)) // Could an entry for the new file in the rootdirectory be created? |
|
231 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
217 | { |
- | 218 | return(file); |
|
- | 219 | } |
|
- | 220 | else |
|
- | 221 | { |
|
232 | cluster_temp *= SectorsPerCluster; // Calculate relative sectorindex of first datacluster. |
222 | FreeFilePointer(file); // free the filepointer. |
233 | file->start_cluster = (FirstDataCluster + cluster_temp); // Calculate absolute sectorposition of first datacluster. |
223 | file = NULL; |
234 | file->cluster_pointer = file->start_cluster; // start reading the file with the first sector of the first datacluster. |
224 | } |
235 | if(CreateDirectoryEntry(&name[0],temp,file,_FILE)) // Could an entry for the new file in the rootdirectory be created? |
225 | } |
236 | { |
226 | else // file with mode 'r' not found |
Line -... | Line 227... | ||
- | 227 | { |
|
- | 228 | FreeFilePointer(file); // free the filepointer. |
|
237 | return(file); |
229 | file = NULL; // file not found |
238 | } |
230 | } |
239 | } |
231 | } |
240 | } |
232 | } |
241 | } |
233 | return(file); |
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? |
- | |
259 | - | ||
260 | time = (((rtctime.hour)<<11) | ((rtctime.minute)<<5) | rtctime.second); |
- | |
Line 261... | Line -... | ||
261 | date = ((((rtctime.year)-1980) <<9) | ((rtctime.month) <<5) | rtctime.day); |
- | |
262 | - | ||
263 | #endif |
- | |
264 | 249 | ||
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 | { |
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 | { |
- | |
Line 291... | Line 275... | ||
291 | u8 cnt = 0; |
275 | { |
292 | 276 | ||
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 | } |
277 | if(file != NULL) |
299 | - | ||
300 | fflush_(file); // save buffercontent to the sdcard |
- | |
301 | - | ||
302 | file->start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
- | |
303 | file->cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
- | |
304 | file->sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
- | |
305 | file->byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
- | |
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. |
Line 345... | Line 313... | ||
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 |
Line 347... | Line 315... | ||
347 | } |
315 | } |
348 | 316 | ||
349 | //________________________________________________________________________________________________________________________________________ |
317 | //________________________________________________________________________________________________________________________________________ |
350 | // Funtion: s32 fwrite_(void *buffer, s32 size, s32 count, File *file); |
318 | // Funtion: u32 fwrite_(void *buffer, s32 size, s32 count, File *file); |
351 | // |
319 | // |
352 | // Description: This function writes count objects of the specified size from the buffer to the actual positon within the file. |
320 | // Description: This function writes count objects of the specified size from the buffer to the actual positon within the file. |
353 | // |
321 | // |
Line 450... | Line 418... | ||
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 536... | Line 504... | ||
536 | } |
504 | } |
537 | return(retvalue); |
505 | return(retvalue); |
538 | } |
506 | } |
Line 539... | Line 507... | ||
539 | 507 | ||
540 | //________________________________________________________________________________________________________________________________________ |
508 | //________________________________________________________________________________________________________________________________________ |
541 | // Funtion: fputs_(File *file); |
509 | // Funtion: fputs_(File *file, s8 *string); |
542 | // |
510 | // |
543 | // Description: This function writes a string to the specified file. |
511 | // Description: This function writes a zero terminated string to the specified file. |
544 | // |
512 | // |
Line 545... | Line 513... | ||
545 | //________________________________________________________________________________________________________________________________________ |
513 | //________________________________________________________________________________________________________________________________________ |
546 | 514 | ||
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 | { |
- | 537 | s16 buff_pnt = 0; |
|
- | 538 | s16 buff_tmp = 0; |
|
569 | s16 buff_pnt = 0; |
539 | s8 *retvalue = 0; |
Line 570... | Line 540... | ||
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: |
- | |
579 | if(buff_tmp == 0x0A)// state++; |
- | |
580 | { |
- | |
581 | count = 1; |
547 | { |
582 | } |
548 | if(buff_pnt != 0) // are there characters within the string? |
583 | state = 0; |
549 | { |
584 | break; |
- | |
585 | 550 | break; |
|
- | 551 | } |
|
586 | case 1: |
552 | else |
587 | if(buff_tmp == 0x0A) |
553 | { |
- | 554 | retvalue = NULL; // first character read was EOF -> return(NULL); |
|
- | 555 | break; |
|
- | 556 | } |
|
- | 557 | } |
|
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 | { |
595 | string[buff_pnt] = (s8) buff_tmp; |
566 | 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 |
Line 611... | Line 583... | ||
611 | //________________________________________________________________________________________________________________________________________ |
583 | //________________________________________________________________________________________________________________________________________ |
- | 584 | ||
- | 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 744... | Line 718... | ||
744 | 718 | ||
745 | file->cluster_pointer = fat_pointer; // continue wrtiting to the file in the new and free datacluster. |
719 | file->cluster_pointer = fat_pointer; // continue wrtiting to the file in the new and free datacluster. |
746 | return(retvalue); // return 1 when a new cluster was appended to the file |
720 | return(retvalue); // return 1 when a new cluster was appended to the file |
Line -... | Line 721... | ||
- | 721 | } |
|
- | 722 | ||
- | 723 | ||
- | 724 | //________________________________________________________________________________________________________________________________________ |
|
- | 725 | // Funtion: void DeleteClusterChain(u16 startcluster); |
|
- | 726 | // |
|
- | 727 | // Description: This function frees all the clusters used for file from the fat. |
|
- | 728 | // |
|
- | 729 | // Returnvalue: none |
|
- | 730 | //________________________________________________________________________________________________________________________________________ |
|
- | 731 | ||
- | 732 | void DeleteClusterChain(u16 startcluster) |
|
- | 733 | { |
|
- | 734 | u16 fat_index = 0; |
|
- | 735 | u16 fat_sector_offset = 0; |
|
- | 736 | u32 sector_in_buffer = 0; |
|
- | 737 | u32 ul_temp = 0; |
|
- | 738 | u8 buffer[512]; |
|
- | 739 | ||
- | 740 | fat_index = (startcluster % 0x100); // Calculate the sector within the cluster. |
|
- | 741 | fat_sector_offset = (startcluster >> 8); // and the position within the sector. |
|
- | 742 | ul_temp = (u32)(FileAllocationTable + fat_sector_offset); |
|
- | 743 | ||
- | 744 | ||
- | 745 | do |
|
- | 746 | { |
|
- | 747 | if(sector_in_buffer != ul_temp) |
|
- | 748 | { |
|
- | 749 | sector_in_buffer = ul_temp; |
|
- | 750 | SDC_GetSector(ul_temp,buffer); |
|
- | 751 | } |
|
- | 752 | Fat = (struct FatEntry *)buffer; |
|
- | 753 | ||
- | 754 | startcluster = Fat[fat_index].next_cluster; |
|
- | 755 | Fat[fat_index].next_cluster = 0x0000; // free the cluster within the fat. |
|
- | 756 | ||
- | 757 | fat_index = (startcluster % 0x100); // Calculate the sector within the cluster. |
|
- | 758 | fat_sector_offset = (startcluster >> 8); // and the position within the sector. |
|
- | 759 | ul_temp = (u32)(FileAllocationTable + fat_sector_offset); |
|
- | 760 | if((sector_in_buffer != ul_temp) || (startcluster == 0xffff)) |
|
- | 761 | { |
|
- | 762 | SDC_PutSector(sector_in_buffer,buffer); |
|
- | 763 | } |
|
- | 764 | } |
|
- | 765 | while(startcluster != 0xffff); // last cluster has been deleted. |
|
- | 766 | } |
|
747 | } |
767 | |
748 | 768 | ||
749 | //________________________________________________________________________________________________________________________________________ |
769 | //________________________________________________________________________________________________________________________________________ |
750 | // Funtion: u16 GetFatClusterIndex(File *file); |
770 | // Funtion: u16 GetFatClusterIndex(File *file); |
751 | // |
771 | // |
Line 780... | Line 800... | ||
780 | 800 | ||
781 | return(fat_pointer); |
801 | return(fat_pointer); |
Line 782... | Line 802... | ||
782 | } |
802 | } |
783 | 803 | ||
784 | //________________________________________________________________________________________________________________________________________ |
804 | //________________________________________________________________________________________________________________________________________ |
785 | // Funtion: u16 CreateDirectoryEntry(u8 *, u16, u8 attrib) |
805 | // Funtion: u16 CreateFileInDirectory(u8 *, File *) |
786 | // |
806 | // |
787 | // Description: This function looks for the next free position in the directory and creates an entry. The type of an directoryentry is |
807 | // Description: This function looks for the next free position in the directory and creates an entry. The type of an directoryentry is |
788 | // specified by the attribute. |
808 | // specified by the attribute. |
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. |
|
Line 803... | Line 838... | ||
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)) |
846 | while((rootentry<16) && (!retvalue)) |
812 | { |
847 | { |
813 | if((DirectoryEntry[rootentry].attribute == 0) || (DirectoryEntry[rootentry].attribute == 0xE5)) // empty directory entry found |
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++; |
Line 830... | Line 865... | ||
830 | sector_offset++; |
865 | sector_offset++; |
831 | } |
866 | } |
832 | } |
867 | } |
833 | while((cnt_enries_searched< PossibleRootEntries) && (!retvalue)); |
868 | while((cnt_enries_searched< PossibleRootEntries) && (!retvalue)); |
Line -... | Line 869... | ||
- | 869 | ||
834 | 870 | } |
|
835 | return(retvalue); // return 1 if file has been created otherwise return 0. |
871 | return(retvalue); // return 1 if file has been created otherwise return 0. |
Line 836... | Line 872... | ||
836 | } |
872 | } |
837 | 873 | ||
838 | //________________________________________________________________________________________________________________________________________ |
874 | //________________________________________________________________________________________________________________________________________ |
839 | // Funtion: u16 SeekDirectoryEntry(u8*, File *, u8) |
875 | // Funtion: u16 SeekFileInDirectory(s8 *fname, File *) |
840 | // |
876 | // |
841 | // Description: this function searches all possible rootentries until the file or directory is found or the end of the rootdirectory is reached |
877 | // Description: this function searches all possible entries withint the actual directory for the specified object. |
842 | // |
878 | // |
Line 843... | Line 879... | ||
843 | // Returnvalue: This function returns 1 if the directoryentry specified was found. |
879 | // Returnvalue: This function returns 1 if the directoryentry specified was found. |
844 | //________________________________________________________________________________________________________________________________________ |
880 | //________________________________________________________________________________________________________________________________________ |
845 | 881 | ||
846 | u8 SeekDirectoryEntry(u8 *fname, File *file) |
882 | u8 SeekFileInDirectory(s8 *fname, File *file) |
847 | { |
883 | { |
848 | u16 rootentry=0; |
884 | u16 rootentry=0; |
849 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
885 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
850 | u8 i=0; |
886 | u8 i=0; |
- | 887 | u8 retvalue = 0; |
|
- | 888 | u32 cluster_temp = 0; |
|
- | 889 | s8 name[11] = " "; |
|
- | 890 | ||
Line 851... | Line 891... | ||
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. |
856 | do |
896 | do |
Line 857... | Line 897... | ||
857 | { // search the next 16 rootentries in this sector of the roordirectory. |
897 | { // search the next 16 rootentries in this sector of the roordirectory. |
858 | rootentry=0; |
898 | rootentry=0; |
859 | SDC_GetSector((u32) file->cluster_pointer,file->buffer); // Read the Rootdirectory. |
- | |
860 | DirectoryEntry = (struct DirEntry *)file->buffer; |
- | |
861 | 899 | SDC_GetSector((u32) file->cluster_pointer,file->buffer); // Read the Rootdirectory. |
|
- | 900 | DirectoryEntry = (struct DirEntry *)file->buffer; |
|
- | 901 | ||
862 | while((!retvalue)&&(rootentry<16)) |
902 | while((!retvalue)&&(rootentry<16)) |
863 | { |
903 | { |
864 | cnt++; |
904 | i=0; |
865 | 905 | if(DirectoryEntry[rootentry].name[0] != 0xe5) // ignore deleted items. |
|
- | 906 | { |
|
- | 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. |
Line 889... | Line 931... | ||
889 | } |
931 | } |
890 | while((end_of_directory_not_reached) && (!retvalue)); |
932 | while((end_of_directory_not_reached) && (!retvalue)); |
891 | return(retvalue); |
933 | return(retvalue); |
892 | } |
934 | } |
Line -... | Line 935... | ||
- | 935 | ||
- | 936 | ||
893 | 937 | ||
894 | //________________________________________________________________________________________________________________________________________ |
938 | //________________________________________________________________________________________________________________________________________ |
895 | // Funtion: void ScanSubDirectories(u8*, File *file); |
939 | // Funtion: void SeperateFileName(u8*); |
896 | // |
940 | // |
897 | // Description: This function scans the filename for subdirectories and changes the directory until the last directory is reached. |
- | |
898 | // here the specified file is searched. |
941 | // Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST TXT'); |
899 | // |
- | |
900 | // Returnvalue: |
942 | // |
Line 901... | Line 943... | ||
901 | //________________________________________________________________________________________________________________________________________ |
943 | //________________________________________________________________________________________________________________________________________ |
902 | 944 | ||
903 | u8 ScanSubDirectories(u8 *fname, File *file) |
945 | void SeperateFileName(s8 *fname, s8 *name) |
904 | { |
946 | { |
905 | u8 readpointer = 0; // pointer to read a character within the filepath. |
947 | u8 readpointer = 0; |
906 | u8 writepointer = 0; // pointer to write a character into the string dirname. |
- | |
907 | u8 dircnt = 0; // the number of subdirectories found in filepath. |
- | |
908 | u8 dirname[11]; // temporary variable containing the name of a directory within the filepath. |
948 | u8 writepointer = 0; |
Line -... | Line 949... | ||
- | 949 | u8 attribute = 1; |
|
- | 950 | u8 i = 0; |
|
909 | u8 retvalue = 1; // no error opening a subdirectory occured yet. |
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]; |
|
910 | u8 cnt = 0; // maximun number of characters in a path is 256; |
960 | readpointer++; |
911 | - | ||
912 | 961 | writepointer++; |
|
913 | while((fname[readpointer]!=0) && (cnt < 255)) // search the string until the end is reached. |
962 | } |
914 | { |
963 | else |
- | 964 | { |
|
- | 965 | if(fname[(readpointer-1)] != '*') |
|
- | 966 | { |
|
915 | cnt++; |
967 | for(i=writepointer;i<8;i++) |
916 | if((fname[readpointer] == '/') && readpointer) // subdirectory found. ignore first "/" as directory. |
968 | { |
- | 969 | name[i] = ' '; |
|
917 | { |
970 | } |
918 | dircnt++; // count the number of subdirectories found within the filepath. |
971 | } |
919 | } |
- | |
920 | readpointer++; |
- | |
921 | } |
972 | readpointer++; |
922 | - | ||
923 | for(writepointer=0;writepointer<10;writepointer++) dirname[writepointer] = ' '; |
- | |
924 | writepointer = 0; |
- | |
925 | readpointer = 0; // start scanning the string at the beginning. |
- | |
926 | - | ||
927 | file->cluster_pointer = RootDirectory; // always start searching in the rootdirectory. |
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++; |
|
928 | file->attribute = _DIRECTORY; // the attribute of the item to be searched in the specified directory. |
981 | writepointer = 8; |
- | 982 | } |
|
- | 983 | else // in filename. |
|
- | 984 | { |
|
- | 985 | writepointer = 11; // jump to the end of the string to terminate this function. |
|
929 | 986 | } |
|
930 | while(dircnt && retvalue) // scan all subdirectories found before. |
987 | attribute = 0; |
- | 988 | } |
|
931 | { |
989 | else |
932 | if(fname[readpointer] != '/') // is the end of the subdirectory entry not reached yet? |
- | |
933 | { |
990 | { |
934 | if((fname[readpointer]>96) && (fname[readpointer]<123)) |
991 | if((fname[readpointer]>96) && (fname[readpointer]<123)) |
935 | { // all characters must be upper case. |
992 | { |
936 | dirname[writepointer]=(fname[readpointer] - 32); |
993 | name[writepointer]=(fname[readpointer] - 32); // all characters must be upper case. |
937 | } |
994 | } |
- | 995 | else |
|
938 | else |
996 | { |
- | 997 | name[writepointer]=fname[readpointer]; |
|
- | 998 | } |
|
- | 999 | readpointer++; |
|
- | 1000 | writepointer++; |
|
- | 1001 | attribute = 0; |
|
- | 1002 | } |
|
- | 1003 | } |
|
- | 1004 | } |
|
- | 1005 | ||
- | 1006 | //________________________________________________________________________________________________________________________________________ |
|
- | 1007 | // Funtion: File * ReserveFilePointer_(void); |
|
- | 1008 | // |
|
- | 1009 | // Description: This function looks for a free filepointer and reserves it. |
|
- | 1010 | // |
|
- | 1011 | // |
|
- | 1012 | // Return: NULL = faild to reserve a filepointer |
|
- | 1013 | // otherwise filepointer |
|
- | 1014 | //________________________________________________________________________________________________________________________________________ |
|
- | 1015 | ||
- | 1016 | File * ReserveFilePointer(void) |
|
- | 1017 | { |
|
- | 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]; |
|
- | 1027 | FilePointer[temp].state = _USED; // mark as used. |
|
- | 1028 | FilePointer[temp].mode = 0; // type of access (read/write) not defined yet. |
|
- | 1029 | FilePointer[temp].start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
|
- | 1030 | FilePointer[temp].cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
|
- | 1031 | FilePointer[temp].sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
|
- | 1032 | FilePointer[temp].byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
|
- | 1033 | FilePointer[temp].filesize = 0; // the size of the opend file in bytes. |
|
- | 1034 | FilePointer[temp].fileposition = 0; // pointer to a character within the file 0 < fileposition < filesize |
|
- | 1035 | FilePointer[temp].sector_in_buffer = 0; // the last sector read, wich is still in the sectorbuffer. |
|
- | 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. |
|
- | 1038 | FilePointer[temp].attribute = 0; // the attribute of the file opened. |
|
- | 1039 | break; |
|
- | 1040 | } |
|
- | 1041 | } |
|
- | 1042 | return(file); |
|
- | 1043 | } |
|
- | 1044 | ||
- | 1045 | ||
- | 1046 | //________________________________________________________________________________________________________________________________________ |
|
- | 1047 | // Funtion: void FreeFilePointer_(File *); |
|
- | 1048 | // |
|
- | 1049 | // Description: This function free's the filepointer by marking it as unused. |
|
- | 1050 | // |
|
- | 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 *) |
|
- | 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 | ||
- | 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. |
|
- | 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] = '.'; |
|
939 | { |
1144 | DirectoryEntry[1].attribute = _DIRECTORY; |
940 | dirname[writepointer]=fname[readpointer]; |
1145 | if(CWD == RootDirectory) |
941 | } |
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 -------------------------------------------------- |
|
942 | writepointer++; |
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 |
|
943 | } |
1168 | { |
- | 1169 | for(i=0;i<11;i++) DirectoryEntry[index].name[i] = name[i]; // Kopie the filename and the file extension to the directoryentry. |
|
- | 1170 | DirectoryEntry[index].attribute = _DIRECTORY; // Set the fileattribute to archive to reserve the directoryentry. |
|
- | 1171 | DirectoryEntry[index].startcluster = cluster; // copy the location of the first datacluster to the directoryentry. |
|
- | 1172 | DirectoryEntry[index].size = 0; // the new createted file has no content yet. |
|
- | 1173 | file.directory_sector = (u32) (CWD + sector_offset); |
|
- | 1174 | file.directory_index = (u8) index; |
|
- | 1175 | retvalue = 1; |
|
- | 1176 | SDC_PutSector((u32)(CWD + sector_offset),file.buffer); |
|
- | 1177 | } |
|
944 | else |
1178 | index++; |
- | 1179 | cnt_entries_searched++; |
|
945 | { |
1180 | } |
946 | dircnt--; |
1181 | if(!retvalue) // file not found in this sector so take next sector. |
947 | if(!(SeekDirectoryEntry(dirname, file))) // was the seperated subdirectory not found? |
- | |
948 | { |
- | |
949 | retvalue = 0; // leave the function with return(0) otherwise continue with the next subdirectory until all directories have been searched. |
1182 | { |
950 | } |
1183 | index = 0; |
- | 1184 | sector_offset++; |
|
951 | for(writepointer=0;writepointer<10;writepointer++) dirname[writepointer] = ' '; |
1185 | } |
952 | writepointer = 0; |
1186 | } |
953 | } |
- | |
954 | readpointer++; |
1187 | while((cnt_entries_searched< PossibleRootEntries) && (!retvalue)); |
Line -... | Line 1188... | ||
- | 1188 | ||
955 | } |
1189 | } |
956 | file->attribute = _FILE; // All subdirectories have been searched. the next thing to find is the specified file. |
1190 | return(retvalue); // return 1 if file has been created otherwise return 0. |
957 | return(retvalue); |
1191 | } |
958 | } |
1192 | |
959 | 1193 | ||
- | 1194 | //________________________________________________________________________________________________________________________________________ |
|
960 | //________________________________________________________________________________________________________________________________________ |
1195 | // Funtion: u16 SeekSubDirectory(s8 *fname) |
Line 961... | Line 1196... | ||
961 | // Funtion: void SeperateFileName(u8*); |
1196 | // |
962 | // |
1197 | // Description: looks for the specified directory within the CWD. |
963 | // Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST TXT'); |
1198 | // |
- | 1199 | // Returnvalue: If the specified directory was found the startcluster is returned. otherwise 0. |
|
- | 1200 | //________________________________________________________________________________________________________________________________________ |
|
964 | // |
1201 | |
- | 1202 | u16 SeekSubDirectory(s8 *fname) |
|
- | 1203 | { |
|
- | 1204 | u16 index = 0; |
|
- | 1205 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
|
- | 1206 | u8 i = 0; |
|
- | 1207 | u16 cluster_temp = 0; |
|
965 | //________________________________________________________________________________________________________________________________________ |
1208 | s8 name[11] = " "; |
- | 1209 | File file; |
|
- | 1210 | ||
- | 1211 | SeperateFileName(fname,name); |
|
966 | 1212 | ||
- | 1213 | file.cluster_pointer = CWD; // start looking for the file in the actual directory. |
|
- | 1214 | file.start_cluster = CWD; // start looking for the file in the actual directory. |
|
- | 1215 | ||
Line -... | Line 1216... | ||
- | 1216 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
|
- | 1217 | do |
|
- | 1218 | { // search the next 16 rootentries in this sector of the roordirectory. |
|
967 | void SeperateFileName(u8 *fname, u8 *name) |
1219 | index=0; |
968 | { |
1220 | SDC_GetSector((u32) file.cluster_pointer,file.buffer); // Read the Rootdirectory. |
969 | u8 readpointer = 0; |
1221 | DirectoryEntry = (struct DirEntry *)file.buffer; |
970 | u8 writepointer = 0; |
1222 | |
971 | u8 dircnt = 0; // the number of subdirectories found in filepath. |
1223 | while((!cluster_temp)&&(index<16)) |
972 | u8 fileindex = 0; // the position in the filenamestring where the last "/" is found. |
- | |
973 | 1224 | { |
|
974 | while(fname[readpointer]!=0) // search the string until the end is reached. |
- | |
975 | { |
1225 | i=0; |
976 | if((fname[readpointer] == '/') && readpointer) // subdirectory found. ignore first "/" as directory. |
- | |
977 | { |
- | |
Line 978... | Line 1226... | ||
978 | dircnt++; // subdirectory entry found. |
1226 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
979 | fileindex = (readpointer + 1); // store the index of the last slash found as the beginning of the filename. |
- | |
980 | } |
- | |
981 | readpointer++; |
1227 | { |
- | 1228 | while((i<=10)&&(DirectoryEntry[index].name[i] == name[i])) |
|
- | 1229 | { |
|
982 | } |
1230 | i++; |
- | 1231 | } |
|
- | 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 | { |
|
- | 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) |
|
983 | readpointer = fileindex; // start seperating the filename from the directory at the stored position. |
1261 | { |
984 | dircnt = 0; |
1262 | u8 retvalue = 0; |
985 | 1263 | ||
986 | while((writepointer<=10) && (fname[readpointer]!=0)) // the rootdirectoryentry is 8bytes for filename and 3bytes for fileattribute. |
1264 | retvalue = SeekSubDirectory(fname); // check wether the specified directory already exists. |
- | 1265 | ||
- | 1266 | if(!retvalue) |
|
- | 1267 | { |
|
- | 1268 | CreateSubDirectory(fname); // if directory doesn't exist, create it. |
|
- | 1269 | retvalue = 1; |
|
- | 1270 | } |
|
- | 1271 | else |
|
- | 1272 | { |
|
- | 1273 | retvalue = 0; |
|
- | 1274 | } |
|
- | 1275 | ||
- | 1276 | return(retvalue); |
|
- | 1277 | } |
|
987 | { // the filename in the rootdirectory is in the format "TEST TXT" without the dot. |
1278 | |
- | 1279 | ||
- | 1280 | //________________________________________________________________________________________________________________________________________ |
|
- | 1281 | // Funtion: u8 chdir_(u8 *) |
|
988 | if(fname[readpointer]=='.') // seperating filename and attribute. |
1282 | // |
- | 1283 | // Description: This function changes the CWD to the directory specified. |
|
- | 1284 | // |
|
- | 1285 | // |
|
- | 1286 | // returnvalue: 1 if the directory could be changed. |
|
- | 1287 | //________________________________________________________________________________________________________________________________________ |
|
- | 1288 | ||
- | 1289 | u8 chdir_(s8 *fname) |
|
- | 1290 | { |
|
- | 1291 | u8 retvalue = 0; |
|
- | 1292 | s8 name[11] = {" "}; |
|
- | 1293 | ||
989 | { |
1294 | u32 ultemp = 0; |
- | 1295 | ||
- | 1296 | SeperateFileName(fname,name); |
|
- | 1297 | ||
990 | readpointer++; |
1298 | ultemp = (u32)SeekSubDirectory(name); |
991 | writepointer=8; |
1299 | if(ultemp >= 2) |
992 | } |
1300 | { |
993 | else |
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. |
|
994 | { |
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 | // |
|
995 | if((fname[readpointer]>96) && (fname[readpointer]<123)) |
1322 | // Returnvalue: If an item was found this function returns '1' else '0'. |
- | 1323 | //________________________________________________________________________________________________________________________________________ |
|
- | 1324 | ||
- | 1325 | u8 FindItem(Find *item) |
|
- | 1326 | { |
|
- | 1327 | u16 index = 0; |
|
- | 1328 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
|
- | 1329 | u8 i = 0; |
|
- | 1330 | u8 readpointer = 0; |
|
- | 1331 | u8 writepointer = 0; |
|
- | 1332 | u8 retvalue = 0; |
|
- | 1333 | File file; |
|
- | 1334 | ||
- | 1335 | ||
- | 1336 | file.cluster_pointer = item->cluster_pointer; |
|
- | 1337 | file.start_cluster = item->cluster_pointer; |
|
- | 1338 | index = item->directory_index; |
|
- | 1339 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
|
- | 1340 | do |
|
- | 1341 | { // search the next 16 rootentries in this sector of the roordirectory. |
|
- | 1342 | SDC_GetSector((u32) file.cluster_pointer,file.buffer); // Read the Rootdirectory. |
|
- | 1343 | DirectoryEntry = (struct DirEntry *)file.buffer; |
|
- | 1344 | ||
- | 1345 | while((!retvalue)&&(index<16)) |
|
- | 1346 | { |
|
- | 1347 | i=0; |
|
- | 1348 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
|
- | 1349 | { |
|
- | 1350 | while((i<=10)&&((DirectoryEntry[index].name[i] == item->searchstring[i]) || (item->searchstring[i]=='*') || item->searchstring[i]=='?')) |
|
- | 1351 | { |
|
- | 1352 | i++; |
|
- | 1353 | } |
|
- | 1354 | } |
|
- | 1355 | ||
- | 1356 | if((i==11) && (DirectoryEntry[index].attribute & item->attribute)) |
|
- | 1357 | { |
|
- | 1358 | for(readpointer=0;readpointer<=10;readpointer++) |
|
- | 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 | } |
|
996 | { |
1365 | else if((readpointer==7) && (DirectoryEntry[index].attribute == _FILE)) // if the item found is a file |
997 | name[writepointer]=(fname[readpointer] - 32); // all characters must be upper case. |
1366 | { |
998 | } |
1367 | if(DirectoryEntry[index].name[readpointer] != ' ') |
- | 1368 | { |
|
- | 1369 | item->name[writepointer] = DirectoryEntry[index].name[readpointer]; // copy the name of the item found to the find_structure. |
|
- | 1370 | writepointer++; |
|
- | 1371 | } |
|
- | 1372 | item->name[writepointer] = '.'; // then seperate the name and the extension by a '.' at index 8. |
|
- | 1373 | writepointer++; |
|
- | 1374 | } |
|
- | 1375 | } |
|
- | 1376 | item->startcluster = (u16)DirectoryEntry[index].startcluster; |
|
- | 1377 | item->directory_index = ++index; |
|
- | 1378 | item->cluster_pointer = file.cluster_pointer; |
|
- | 1379 | retvalue = 1; |
|
- | 1380 | } |
|
- | 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'. |
|
- | 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 | ||
- | 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 | ||
- | 1450 | ||
- | 1451 | //________________________________________________________________________________________________________________________________________ |
|
- | 1452 | // Funtion: u8 fdelete(s8 *fname) |
|
- | 1453 | // |
|
- | 1454 | // Description: Deletes the file specified by fname. |
|
- | 1455 | // |
|
- | 1456 | // Returnvalue: |
|
- | 1457 | //________________________________________________________________________________________________________________________________________ |
|
- | 1458 | ||
- | 1459 | u8 fdelete_(s8 *fname) |
|
- | 1460 | { |
|
- | 1461 | u8 retvalue = 0; |
|
- | 1462 | Find item; |
|
- | 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. |
|
- | 1491 | // |
|
- | 1492 | // Returnvalue: |
|
- | 1493 | //________________________________________________________________________________________________________________________________________ |
|
- | 1494 | ||
- | 1495 | u8 rmdir_(s8 *dname) |
|
- | 1496 | { |
|
- | 1497 | u8 retvalue = 0; |
|
- | 1498 | Find item; |
|
- | 1499 | ||
- | 1500 | retvalue = findfirst_(dname,&item, _DIRECTORY); // look for the item to be deleted. |
|
- | 1501 | ||
- | 1502 | if(retvalue); // item found? |
|
- | 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) |
|
- | 1525 | // |
|
- | 1526 | // Description: This function checks wether the end of the file has been reached. |
|
- | 1527 | // |
|
- | 1528 | // Returnvalue: 0 if the end of the file was not reached otherwise 1. |
|
- | 1529 | //________________________________________________________________________________________________________________________________________ |
|
- | 1530 | ||
- | 1531 | u16 feof_(File *file) |
|
- | 1532 | { |
|
999 | else |
1533 | if(((file->fileposition)+1) < (file->filesize)) |