Rev 24 | Rev 72 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 24 | Rev 41 | ||
---|---|---|---|
Line 52... | Line 52... | ||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | 57 | #include <stdio.h> |
|
- | 58 | #include "91x_lib.h" |
|
- | 59 | #include "timer.h" |
|
- | 60 | #include "fat16.h" |
|
- | 61 | #include "sdc.h" |
|
- | 62 | #include "uart1.h" |
|
Line 57... | Line -... | ||
57 | - | ||
Line 58... | Line 63... | ||
58 | #include "main.h" |
63 | |
59 | 64 | ||
60 | //________________________________________________________________________________________________________________________________________ |
65 | //________________________________________________________________________________________________________________________________________ |
61 | // Module name: fat16.c |
66 | // Module name: fat16.c |
62 | // Compiler used: avr-gcc 3.4.5 |
67 | // Compiler used: avr-gcc 3.4.5 |
63 | // Last Modifikation: 21.04.2008 |
68 | // Last Modifikation: 16.06.2008 |
64 | // Version: 1.28 |
69 | // Version: 2.00 |
65 | // Authors: Stephan Busker |
70 | // Authors: Stephan Busker & Gregor Stobrawa |
66 | // Description: Source files for FAT16 implementation with read and write-access using AVR-Mikrocontrollers |
71 | // Description: Source files for FAT16 implementation with read and write-access |
67 | // Copyright (C) 2007 Stephan Busker |
72 | // Copyright (C) 2008 Stephan Busker & Gregor Stobrawa |
68 | //........................................................................................................................................ |
73 | //........................................................................................................................................ |
69 | // Functions: extern u8 InitFat16(void); |
74 | // Functions: extern s16 Fat16_Init(void); |
70 | // u8 fopen_(s8 *fname,s8 mode, File *file); |
75 | // extern s16 Fat16_Deinit(void); |
71 | // void fclose_(File *file); |
76 | // extern File_t * fopen_(const u8 *filename, const s8 mode); |
72 | // u32 fread_(void *buffer, u32 size, u32 count, File *file); |
77 | // extern s16 fclose_(File_t *File); |
73 | // u32 fwrite_(void *buffer, u32 size, u32 count, File *file); |
- | |
74 | // s16 fseek_(File *file, s32 offset, s16 origin); |
78 | // extern s16 fflush_(File_t *File); |
75 | // s8 fgets_(s8 *string, s16 count, File *file); |
79 | // extern s16 fseek_(File_t *File, s32 offset, s16 origin); |
76 | // u8 fputchar_(File *file,s8 c); |
80 | // extern s16 fgetc_(File_t *File); |
77 | // u8 fputs_(File *file,s8 *string); |
- | |
78 | // s8 * fgets(s8 *, s16, File); |
- | |
79 | // u8 fexist_(u8*); |
- | |
80 | // u8 mkdir__(s8 *name); |
81 | // extern s16 fputc_(u8 c, File_t *File); |
81 | // u8 chdir__(s8 *name); |
82 | // extern u32 fread_(void *buffer, u32 size, u32 count, File_t *File); |
82 | // u8 findfirst_(s8 *fname, Find *item, u8 attribute); |
83 | // extern u32 fwrite_(void *buffer, u32 size, u32 count, File_t *File); |
83 | // u8 findnext_(Find *item); |
- | |
84 | // u8 fdelete_(s8 *fname) |
- | |
85 | // u8 rmdir_(s8 *dname) |
84 | // extern s16 fputs_(const u8 *string, File_t *File); |
86 | // u16 feof_(File); |
85 | // extern u8 * fgets_(u8 *string, s16 length, File_t *File); |
- | 86 | //........................................................................................................................................ |
|
- | 87 | // ext. functions: extern SD_Result_t SDC_Init(void;) |
|
87 | //........................................................................................................................................ |
88 | // extern SD_Result_t SDC_Deinit(void); |
88 | // ext. functions: extern u8 SDC_GetSector (u32,u8 *); |
89 | // extern SD_Result_t SDC_GetSector (u32,u8 *); |
89 | // extern u8 SDC_PutSector (u32,u8 *); |
90 | // extern SD_Result_t SDC_PutSector (u32,u8 *); |
90 | //........................................................................................................................................ |
91 | //........................................................................................................................................ |
91 | // |
92 | // |
92 | // URL: www.Mikro-Control.de |
93 | // URL: www.Mikro-Control.de |
Line 137... | Line 340... | ||
137 | File *file; |
340 | u8 i; |
138 | 341 | File_t * File = 0; |
|
139 | for(cnt=0;cnt<__MAX_FILES_USED;cnt++) |
- | |
140 | { |
- | |
141 | FilePointer[cnt].state = _UNUSED; // declare the filepointers as unused. |
- | |
142 | } |
- | |
143 | - | ||
144 | file = &FilePointer[0]; |
- | |
145 | - | ||
146 | SerialPutString("\n\rFAT16 init..."); |
- | |
147 | while((SDC_Init() != SD_SUCCESS) && (cnt++<100)); |
- | |
148 | - | ||
149 | if(cnt <100) // sdcard initialised succesfully |
- | |
150 | { |
- | |
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) || |
342 | for(i = 0; i < FILE_MAX_OPEN; i++) |
155 | (MBR->PartitionEntry1.Type == _FAT16_ST_32_MB) || |
- | |
156 | (MBR->PartitionEntry1.Type == _FAT16_LT_32_MB)) |
- | |
157 | { |
- | |
158 | SDC_GetSector(FirstPartitionSector,file->buffer); // Read the volume bootrecord from mmc. |
- | |
159 | 343 | { |
|
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. |
344 | if(FilePointer[i].State == FSTATE_UNUSED) // found an unused one |
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. |
- | |
166 | - | ||
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"); |
345 | { |
173 | str_Volume.state = INITIALIZED; |
- | |
174 | } |
- | |
175 | else |
- | |
176 | { |
346 | File = &FilePointer[i]; // set pointer to that entry |
177 | str_Volume.state = NOT_INITIALIZED; |
347 | FilePointer[i].State = FSTATE_USED; // mark it as used |
178 | SerialPutString("\n\rfilesystem nok"); |
348 | break; |
179 | } |
349 | } |
Line 180... | Line 350... | ||
180 | } |
350 | } |
181 | return(retvalue); |
351 | return(File); |
182 | } |
- | |
- | 352 | } |
|
183 | 353 | ||
184 | //________________________________________________________________________________________________________________________________________ |
354 | /****************************************************************************************************************************************/ |
185 | // Funtion: File * fopen_(u8*, u8 *); |
- | |
186 | // |
355 | /* Function: UnlockFilePointer(file_t *); */ |
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// |
356 | /* */ |
189 | // |
- | |
190 | // Return: Pointer to the opened file or NULL |
357 | /* Description: This function trys to unlock a file pointer. */ |
191 | // |
358 | /* */ |
192 | //________________________________________________________________________________________________________________________________________ |
359 | /* Returnvalue: Returns 1 if file pointer was freed else 0. */ |
193 | - | ||
194 | File * fopen_(s8 *fname, s8 mode) |
360 | /****************************************************************************************************************************************/ |
195 | { |
- | |
196 | File *file; |
361 | u8 UnlockFilePointer(File_t * file) |
197 | 362 | { |
|
198 | file = ReserveFilePointer(); // reserve a filepointer. |
- | |
199 | - | ||
200 | if(file != NULL) // A free filepointer was found. |
363 | u8 cnt; |
201 | { |
364 | if(file == NULL) return(0); |
202 | file->mode = mode; // mode of fileoperation (read,write) |
365 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
203 | 366 | { |
|
204 | if(SeekFileInDirectory(fname, file)) // if file was found |
367 | if(&FilePointer[cnt] == file) // filepointer to be freed found? |
205 | { |
368 | { |
206 | SerialPutString("ok"); |
- | |
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 | } |
369 | file->State = FSTATE_UNUSED; |
211 | } |
- | |
212 | else |
370 | file->FirstSectorOfFirstCluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
213 | { |
- | |
214 | if((mode == 'a') || (mode == 'w')) // specified file doesn't exist so create new file for writing data. |
371 | file->FirstSectorOfCurrCluster = 0; |
215 | { |
- | |
216 | if(CreateFileInDirectory(fname,file)) // Could an entry for the new file in the rootdirectory be created? |
- | |
217 | { |
- | |
218 | return(file); |
372 | file->SectorOfCurrCluster = 0; // Pointer to the cluster which is edited at the moment. |
219 | } |
- | |
220 | else |
- | |
221 | { |
- | |
222 | FreeFilePointer(file); // free the filepointer. |
373 | file->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
223 | file = NULL; |
374 | file->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
224 | } |
375 | file->Mode = 0; // mode of fileoperation (read,write) |
225 | } |
376 | file->Size = 0; // the size of the opend file in bytes. |
- | 377 | file->Position = 0; // pointer to a character within the file 0 < fileposition < filesize |
|
226 | else // file with mode 'r' not found |
378 | file->SectorInCache = 0; // the last sector read, wich is still in the sectorbuffer. |
- | 379 | file->DirectorySector = 0; // the sectorposition where the directoryentry has been made. |
|
227 | { |
380 | file->DirectoryIndex = 0; // the index to the directoryentry within the specified sector. |
228 | FreeFilePointer(file); // free the filepointer. |
381 | file->Attribute = 0; // the attribute of the file opened. |
229 | file = NULL; // file not found |
- | |
230 | } |
- | |
231 | } |
- | |
232 | } |
- | |
233 | return(file); |
- | |
234 | } |
- | |
235 | - | ||
236 | - | ||
237 | - | ||
238 | //________________________________________________________________________________________________________________________________________ |
- | |
239 | // Funtion: fflush_(File *file); |
- | |
240 | // |
- | |
241 | // Description: This function writes the data already in the buffer but not yet written to the file. |
- | |
242 | // |
- | |
243 | //________________________________________________________________________________________________________________________________________ |
- | |
244 | - | ||
245 | s16 fflush_(File *file) |
- | |
246 | { |
- | |
247 | u16 time=0; |
- | |
248 | u16 date=0; |
- | |
249 | - | ||
250 | - | ||
251 | if(file && ((file->mode =='a') || (file->mode =='w'))) |
- | |
252 | { |
- | |
253 | if(file->byte_index > 0) // has data been added to the file? |
- | |
254 | { |
- | |
255 | SDC_PutSector((u32)(file->cluster_pointer + file->sector_index),file->buffer);// save the data still in the buffer |
- | |
256 | } |
- | |
257 | SDC_GetSector((u32)file->directory_sector,file->buffer); // read the directoryentry for this file. |
- | |
258 | DirectoryEntry = (struct DirEntry *)file->buffer; |
- | |
259 | DirectoryEntry[file->directory_index].size = (u32) file->filesize; |
- | |
260 | DirectoryEntry[file->directory_index].time = (u16) time; |
382 | file = NULL; |
261 | DirectoryEntry[file->directory_index].date = (u16) date; |
383 | return(1); |
Line 262... | Line 384... | ||
262 | SDC_PutSector((u32)file->directory_sector,file->buffer); |
384 | } |
263 | } |
385 | } |
264 | return(0); |
- | |
- | 386 | return(0); |
|
265 | } |
387 | } |
- | 388 | ||
- | 389 | /****************************************************************************************************************************************/ |
|
266 | 390 | /* Function: SeperateDirName(s8*, s8*); */ |
|
- | 391 | /* */ |
|
267 | //________________________________________________________________________________________________________________________________________ |
392 | /* Description: This function seperates the first dirname from filepath and brings them */ |
268 | // Funtion: fclose_(File *file); |
- | |
269 | // |
393 | /* into the needed format ('test.txt' -> 'TEST TXT') */ |
270 | // Description: This function closes the open file by writing the remaining data from the buffer to the device and entering the filesize |
394 | /* The subpath is the pointer to the remaining substring if the filepath */ |
- | 395 | /* */ |
|
- | 396 | /* Returnvalue: Return NULL on error or pointer to subpath */ |
|
- | 397 | /****************************************************************************************************************************************/ |
|
Line -... | Line 398... | ||
- | 398 | s8* SeperateDirName(const s8 *filepath, s8 *dirname) |
|
271 | // in the directory entry. |
399 | { |
272 | //________________________________________________________________________________________________________________________________________ |
- | |
273 | - | ||
274 | void fclose_(File *file) |
- | |
275 | { |
- | |
276 | - | ||
277 | if(file != NULL) |
- | |
278 | { |
- | |
279 | fflush_(file); // save buffered data to the disk |
- | |
280 | } |
- | |
281 | FreeFilePointer(file); // and free the filepointer. |
- | |
282 | } |
- | |
283 | - | ||
284 | //________________________________________________________________________________________________________________________________________ |
- | |
285 | // Funtion: u32 fread_(void *buffer, s32 size, s32 count, File *file); |
- | |
286 | // |
- | |
287 | // Description: This function reads count objects of the specified size from the actual position of the file to the specified buffer. |
400 | s8* subpath = NULL; |
288 | // |
- | |
289 | // Returnvalue: The function returns the number of objects (not bytes) read from the file. |
- | |
290 | // |
- | |
291 | //________________________________________________________________________________________________________________________________________ |
401 | u8 readpointer = 0; |
292 | - | ||
293 | u32 fread_(void *buffer, u32 size, u32 count, File *file) |
- | |
294 | { |
402 | u8 writepointer = 0; |
295 | u32 object_cnt = 0; // count the number of objects read from the file. |
- | |
296 | u32 object_size = 0; // count the number of bytes read from the actual object. |
- | |
297 | u8 *buff_pnt = 0; // a pointer to the actual bufferposition. |
403 | |
298 | u8 success = 1; // no error occured during read operation to the file. |
- | |
299 | 404 | // search subpath from beginning of filepath |
|
300 | buff_pnt = (u8 *) buffer; // cast the void pointer to an u8 * |
405 | subpath = NULL; |
301 | 406 | readpointer = 0; |
|
302 | while((object_cnt < count) && success) |
- | |
303 | { |
- | |
304 | object_size = size; |
407 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
305 | while((size > 0) && success) |
408 | while(subpath == NULL) // search the filepath until a subpath was found. |
306 | { |
409 | { |
307 | *buff_pnt = (u8) fgetchar_(file); // read a byte from the buffer to the opened file. |
- | |
308 | buff_pnt++; |
- | |
309 | size--; |
- | |
310 | } |
- | |
311 | if(success) object_cnt++; |
- | |
312 | } |
- | |
313 | - | ||
314 | return(object_cnt); // return the number of objects succesfully read from the file |
- | |
315 | } |
- | |
316 | - | ||
317 | //________________________________________________________________________________________________________________________________________ |
- | |
318 | // Funtion: u32 fwrite_(void *buffer, s32 size, s32 count, File *file); |
- | |
Line 319... | Line 410... | ||
319 | // |
410 | if(((filepath[readpointer] == 0) || (filepath[readpointer] == '/'))) // if '/' found or end of filepath reached |
320 | // Description: This function writes count objects of the specified size from the buffer to the actual positon within the file. |
- | |
321 | // |
411 | { |
322 | // Returnvalue: The function returns the number of objects (not bytes) written to the file. |
412 | subpath = (s8*)&filepath[readpointer]; // store the position of the first "/" found after the beginning of the filenpath |
323 | // |
413 | } |
324 | //________________________________________________________________________________________________________________________________________ |
414 | readpointer++; |
325 | - | ||
- | 415 | } |
|
326 | u32 fwrite_(void *buffer, u32 size, u32 count, File *file) |
416 | |
327 | { |
- | |
328 | u32 object_cnt = 0; // count the number of objects written to the file. |
417 | // clear dirname with spaces |
329 | u32 object_size = 0; // count the number of bytes written from the actual object. |
418 | dirname[11] = 0; // terminate dirname |
330 | u8 *buff_pnt = 0; // a pointer to the actual bufferposition. |
419 | for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' '; |
331 | u8 success = 1; // no error occured during write operation to the file. |
420 | writepointer = 0; |
332 | 421 | // start seperating the dirname from the filepath. |
|
- | 422 | readpointer = 0; |
|
- | 423 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
|
333 | buff_pnt = (u8 *) buffer; // cast the void pointer to an u8 * |
424 | while( &filepath[readpointer] < subpath) |
334 | 425 | { |
|
335 | while((object_cnt < count) && success) |
426 | if(writepointer >= 11) return(NULL); // dirname to long |
- | 427 | if(filepath[readpointer] == '.') // seperating dirname and extension. |
|
336 | { |
428 | { |
337 | object_size = size; |
- | |
338 | while((size > 0) && success) |
429 | if(writepointer <= 8) |
339 | { |
- | |
340 | success = fputchar_(file, *buff_pnt); // write a byte from the buffer to the opened file. |
- | |
341 | buff_pnt++; |
- | |
342 | size--; |
- | |
343 | } |
- | |
344 | if(success) object_cnt++; |
- | |
345 | } |
- | |
346 | - | ||
347 | return(object_cnt); // return the number of objects succesfully written to the file |
- | |
348 | } // (!!!!! objects and not bytes !!!!) |
- | |
349 | - | ||
350 | //________________________________________________________________________________________________________________________________________ |
- | |
351 | // Funtion: s16 fseek_(File *, s32, s16) |
- | |
352 | // |
- | |
353 | // Description: This function sets the pointer of the stream relative to the position |
- | |
354 | // specified by origin (SEEK_SET, SEEK_CUR, SEEK_END). |
- | |
355 | // |
- | |
356 | //________________________________________________________________________________________________________________________________________ |
- | |
357 | - | ||
358 | s16 fseek_(File *file, s32 offset, s16 origin) |
- | |
359 | { |
- | |
360 | s32 fposition = 0; |
- | |
361 | s16 retvalue = 1; |
- | |
362 | u32 temp = 0; |
- | |
363 | - | ||
364 | //...................................................... |
- | |
365 | if(origin == SEEK_SET) // Fileposition relative to the beginning of the file. |
- | |
366 | { |
- | |
367 | fposition = 0; |
- | |
368 | } |
- | |
369 | //...................................................... |
- | |
370 | else if(origin == SEEK_END) // Fileposition relative to the end of the file. |
- | |
371 | { |
- | |
372 | fposition = (s32) file->filesize; |
- | |
373 | } |
- | |
374 | //...................................................... |
- | |
375 | else if(origin == SEEK_CUR) // Fileposition relative to the current position of the file. |
- | |
376 | { |
- | |
377 | fposition = file->fileposition; |
- | |
378 | } |
- | |
379 | - | ||
380 | fposition += offset; |
- | |
381 | - | ||
382 | if((fposition >= 0) && (fposition <= (s32)file->filesize)) // is the pointer still within the file? |
- | |
383 | { |
- | |
384 | retvalue = 0; |
430 | { |
385 | file->sector_index = 0; |
- | |
386 | file->byte_index = 0; |
431 | readpointer++; // next character in filename |
387 | file->fileposition = 0; |
432 | writepointer = 8; // jump to start of extension |
388 | file->cluster_pointer = file->start_cluster; |
433 | } |
389 | 434 | else return(NULL); // dirbasename to long |
|
390 | while(file->fileposition < fposition) |
435 | } |
391 | { |
436 | else |
392 | file->fileposition++; |
- | |
393 | if(file->byte_index < 511) |
- | |
394 | { |
- | |
395 | file->byte_index++; |
- | |
396 | } |
437 | { |
397 | else |
- | |
398 | { |
- | |
399 | file->byte_index=0; // reading at the beginning of new sector. |
- | |
400 | file->sector_index++; // continue reading in next sector |
- | |
401 | if(file->sector_index >= SectorsPerCluster) // When end of cluster is reached, the next datacluster has to be searched in the FAT. |
- | |
402 | { |
438 | if((0x60 < filepath[readpointer]) && (filepath[readpointer] < 0x7B)) |
403 | if(file->fileposition < fposition) |
- | |
404 | { |
- | |
405 | file->sector_index = 0; // start reading new cluster at first sector of the cluster. |
- | |
406 | GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster. |
- | |
407 | } |
- | |
408 | } |
- | |
409 | } |
- | |
410 | } |
- | |
411 | if(file->byte_index) |
- | |
412 | { |
- | |
413 | temp = (u32)((u32)file->cluster_pointer + (u32)file->sector_index); |
- | |
414 | SDC_GetSector((u32)temp,file->buffer); // FileBuffer will be written at once at the end of the cluster and has to be updated. |
- | |
415 | } |
- | |
416 | } |
- | |
417 | return(retvalue); |
- | |
418 | } |
- | |
419 | - | ||
420 | //________________________________________________________________________________________________________________________________________ |
- | |
421 | // Funtion: fgetchar_(File *file); |
- | |
422 | // |
- | |
423 | // Description: This function reads and returns one character from the specified file. |
- | |
424 | // |
- | |
425 | // Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF. |
- | |
426 | //________________________________________________________________________________________________________________________________________ |
- | |
427 | - | ||
428 | s16 fgetchar_(File *file) |
- | |
429 | { |
- | |
430 | s16 c = EOF; |
- | |
431 | u32 temp1; |
- | |
432 | - | ||
433 | if(((file->fileposition)+1) < file->filesize) // wen the end of the file is not reached, get the next character. |
- | |
434 | { |
- | |
435 | temp1 = (u32)file->cluster_pointer; // calculate the adress of the next character to be read. |
- | |
436 | temp1 += (u32)file->sector_index; |
- | |
437 | 439 | { |
|
438 | if(file->sector_in_buffer != temp1) // Has the content of the buffer been modified and has to be updated? |
- | |
439 | { |
- | |
440 | SDC_GetSector((u32)temp1,file->buffer); // Read the calculated cluster. |
- | |
441 | file->sector_in_buffer = (u32)temp1; |
440 | dirname[writepointer] = (filepath[readpointer] - 0x20); // all characters must be upper case. |
442 | } |
- | |
443 | c = (s16) file->buffer[file->byte_index]; |
- | |
444 | file->fileposition++; |
- | |
445 | - | ||
446 | if(file->byte_index < 511) // continue reading from this sector until the end of the sector is reached. |
- | |
447 | { |
- | |
448 | file->byte_index++; |
- | |
449 | } |
- | |
450 | else // has the end of an sector been reached-> |
- | |
451 | { |
- | |
452 | file->byte_index=0; // continue reading at the beginning - |
441 | } |
453 | file->sector_index++; // of new sector. |
442 | else |
454 | if(file->sector_index >= SectorsPerCluster) // When the end of an cluster is reached, the next datacluster has to be searched in the FAT. |
443 | { |
455 | { |
444 | dirname[writepointer] = filepath[readpointer]; |
Line 456... | Line -... | ||
456 | file->sector_index = 0; // start reading new cluster at first sector of the cluster. |
- | |
457 | GetNextCluster(file); // Sets the clusterpointer of the file to the next datacluster. |
- | |
458 | } |
- | |
459 | } |
- | |
460 | } |
- | |
461 | return(c); |
- | |
462 | } |
- | |
Line -... | Line 445... | ||
- | 445 | } |
|
- | 446 | readpointer++; |
|
- | 447 | writepointer++; |
|
- | 448 | } |
|
- | 449 | } |
|
- | 450 | return(subpath); |
|
- | 451 | } |
|
- | 452 | ||
- | 453 | ||
- | 454 | /**************************************************************************************************************************************+*/ |
|
- | 455 | /* Function: Fat16ClusterToSector( u16 cluster); */ |
|
- | 456 | /* */ |
|
- | 457 | /* Description: This function converts a cluster number given by the fat to the corresponding */ |
|
- | 458 | /* sector that points to the start of the data area that is represented by the cluster number. */ |
|
- | 459 | /* */ |
|
- | 460 | /* Returnvalue: The sector number with the data area of the given cluster */ |
|
- | 461 | /****************************************************************************************************************************************/ |
|
- | 462 | u32 Fat16ClusterToSector(u16 cluster) |
|
- | 463 | { |
|
- | 464 | if(!Partition.IsValid) return 0; |
|
- | 465 | if (cluster < 2) cluster = 2; // the 0. and 1. cluster in the fat are used for the media descriptor |
|
- | 466 | return ( (cluster - 2) * Partition.SectorsPerCluster) + Partition.FirstDataSector; // the first data sector is represented by the 2nd cluster |
|
- | 467 | } |
|
- | 468 | ||
- | 469 | /****************************************************************************************************************************************/ |
|
- | 470 | /* Function: SectorToFat16Cluster( u32 sector); */ |
|
- | 471 | /* */ |
|
- | 472 | /* Description: This function converts a given sector number given to the corresponding */ |
|
- | 473 | /* cluster number in the fat that represents this data area. */ |
|
- | 474 | /* */ |
|
- | 475 | /* Returnvalue: The cluster number representing the data area of the sector. */ |
|
- | 476 | /****************************************************************************************************************************************/ |
|
- | 477 | u16 SectorToFat16Cluster(u32 sector) |
|
- | 478 | { |
|
- | 479 | if(!Partition.IsValid) return 0; |
|
- | 480 | return ((u16)((sector - Partition.FirstDataSector) / Partition.SectorsPerCluster) + 2); |
|
- | 481 | } |
|
463 | 482 | ||
- | 483 | ||
- | 484 | /****************************************************************************************************************************************/ |
|
- | 485 | /* Function: Fat16_Deinit(void); */ |
|
- | 486 | /* */ |
|
- | 487 | /* Description: This function uninitializes the fat 16 api */ |
|
- | 488 | /* */ |
|
- | 489 | /* Returnvalue: The function returns "0" on success */ |
|
- | 490 | /****************************************************************************************************************************************/ |
|
- | 491 | u8 Fat16_Deinit(void) |
|
- | 492 | { |
|
- | 493 | s16 returnvalue = 0; |
|
- | 494 | u8 cnt; |
|
- | 495 | // declare the filepointers as unused. |
|
- | 496 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
|
- | 497 | { |
|
- | 498 | if(FilePointer[cnt].State == FSTATE_USED) |
|
- | 499 | { |
|
- | 500 | returnvalue += fclose_(&FilePointer[cnt]); // try to close open file pointers |
|
- | 501 | } |
|
- | 502 | ||
- | 503 | } |
|
- | 504 | SDC_Deinit(); // uninitialize interface to sd-card |
|
- | 505 | Partition.IsValid = 0; // mark data in partition structure as invalid |
|
- | 506 | return(returnvalue); |
|
- | 507 | } |
|
- | 508 | ||
464 | //________________________________________________________________________________________________________________________________________ |
509 | /****************************************************************************************************************************************/ |
- | 510 | /* Function: Fat16_Init(void); */ |
|
- | 511 | /* */ |
|
- | 512 | /* Description: This function reads the Masterbootrecord and finds the position of the Volumebootrecord, the FAT and the Rootdir */ |
|
- | 513 | /* and stores the information in global variables. */ |
|
- | 514 | /* */ |
|
- | 515 | /* Returnvalue: The function returns "0" if the filesystem is initialized. */ |
|
- | 516 | /****************************************************************************************************************************************/ |
|
- | 517 | u8 Fat16_Init(void) |
|
- | 518 | { |
|
- | 519 | u8 cnt = 0; |
|
- | 520 | u32 partitionfirstsector; |
|
- | 521 | VBR_Entry_t *VBR; |
|
- | 522 | MBR_Entry_t *MBR; |
|
- | 523 | File_t *file; |
|
- | 524 | u8 result = 0; |
|
- | 525 | ||
- | 526 | SerialPutString("\r\n FAT16 init..."); |
|
- | 527 | Partition.IsValid = 0; |
|
- | 528 | ||
- | 529 | // declare the filepointers as unused. |
|
- | 530 | for(cnt = 0; cnt < FILE_MAX_OPEN; cnt++) |
|
- | 531 | { |
|
- | 532 | FilePointer[cnt].State = FSTATE_UNUSED; |
|
- | 533 | } |
|
- | 534 | // set current file pinter to first position in list |
|
- | 535 | file = &FilePointer[0]; |
|
- | 536 | ||
- | 537 | // try to initialise the sd-card. |
|
- | 538 | if(SD_SUCCESS != SDC_Init()) |
|
- | 539 | { |
|
- | 540 | SerialPutString("SD-Card could not be initialized."); |
|
- | 541 | result = 1; |
|
- | 542 | goto end; |
|
- | 543 | } |
|
- | 544 | ||
- | 545 | // SD-Card is initialized successfully |
|
- | 546 | if(SD_SUCCESS != SDC_GetSector((u32)MBR_SECTOR,file->Cache)) // Read the MasterBootRecord |
|
- | 547 | { |
|
- | 548 | SerialPutString("Error reading the MBR."); |
|
- | 549 | result = 2; |
|
- | 550 | goto end; |
|
- | 551 | } |
|
- | 552 | MBR = (MBR_Entry_t *)file->Cache; // Enter the MBR using the structure MBR_Entry_t. |
|
- | 553 | if((MBR->PartitionEntry1.Type == PART_TYPE_FAT16_ST_32_MB) || |
|
- | 554 | (MBR->PartitionEntry1.Type == PART_TYPE_FAT16_LT_32_MB) || |
|
- | 555 | (MBR->PartitionEntry1.Type == PART_TYPE_FAT16LBA)) |
|
- | 556 | { |
|
- | 557 | // get sector offset 1st partition |
|
- | 558 | partitionfirstsector = MBR->PartitionEntry1.NoSectorsBeforePartition; |
|
- | 559 | // Start of Partition is the Volume Boot Sector |
|
- | 560 | if(SD_SUCCESS != SDC_GetSector(partitionfirstsector,file->Cache)) // Read the volume boot record |
|
- | 561 | { |
|
- | 562 | SerialPutString("Error reading the VBR."); |
|
- | 563 | result = 3; |
|
- | 564 | goto end; |
|
- | 565 | } |
|
- | 566 | } |
|
- | 567 | else // maybe the medium has no partition assuming sector 0 is the vbr |
|
- | 568 | { |
|
- | 569 | partitionfirstsector = 0; |
|
- | 570 | } |
|
- | 571 | ||
- | 572 | VBR = (VBR_Entry_t *) file->Cache; // Enter the VBR using the structure VBR_Entry_t. |
|
- | 573 | if(VBR->BytesPerSector != BYTES_PER_SECTOR) |
|
- | 574 | { |
|
- | 575 | SerialPutString("VBR: Sector size not supported."); |
|
- | 576 | result = 4; |
|
- | 577 | goto end; |
|
- | 578 | } |
|
- | 579 | Partition.SectorsPerCluster = VBR->SectorsPerCluster; // Number of sectors per cluster. Depends on the memorysize of the sd-card. |
|
- | 580 | Partition.FatCopies = VBR->NoFATCopies; // Number of fatcopies. |
|
- | 581 | Partition.MaxRootEntries = VBR->MaxRootEntries; // How many Entries are possible in the rootdirectory (FAT16 allows max. 512 entries). |
|
- | 582 | Partition.SectorsPerFat = VBR->SectorsPerFAT; // The number of sectors per FAT. |
|
- | 583 | ||
- | 584 | /* Calculate the sectorpositon of the FAT, the Rootdirectory and the first Datacluster. */ |
|
- | 585 | // Calculate the position of the FileAllocationTable: |
|
- | 586 | // Start + # of Reserved Sectors |
|
- | 587 | Partition.FirstFatSector = (u32)(partitionfirstsector + (u32)(VBR->ReservedSectors)); |
|
- | 588 | // Calculate the position of the Rootdirectory: |
|
- | 589 | // Start + # of Reserved Sectors + (# of Sectors Per FAT * # of FAT Copies) |
|
- | 590 | Partition.FirstRootDirSector = Partition.FirstFatSector + (u32)((u32)Partition.SectorsPerFat*(u32)Partition.FatCopies); |
|
- | 591 | // Calculate the position of the first datacluster: |
|
- | 592 | // Start + # of Reserved + (# of Sectors Per FAT * # of FAT Copies) + ((Maximum Root Directory Entries * 32) / Bytes per Sector) |
|
- | 593 | Partition.FirstDataSector = Partition.FirstRootDirSector + (u32)(Partition.MaxRootEntries>>4); // assuming 512 Byte Per Sector |
|
- | 594 | // Calculate the last data sector |
|
- | 595 | if(VBR->NoSectors == 0) |
|
- | 596 | { |
|
- | 597 | SerialPutString("VBR: Bad number of sectors."); |
|
- | 598 | result = 5; |
|
- | 599 | goto end; |
|
- | 600 | } |
|
465 | // Funtion: fputchar_(File *file, s8 *); |
601 | Partition.LastDataSector = Partition.FirstDataSector + VBR->NoSectors - 1; |
- | 602 | // check for FAT16 in VBR of first partition |
|
- | 603 | if(!((VBR->FATName[0]=='F') && (VBR->FATName[1]=='A') && (VBR->FATName[2]=='T') && (VBR->FATName[3]=='1')&&(VBR->FATName[4]=='6'))) |
|
- | 604 | { |
|
- | 605 | SerialPutString("VBR: Partition ist not FAT16 type."); |
|
- | 606 | result = 6; |
|
- | 607 | goto end; |
|
- | 608 | } |
|
- | 609 | Partition.IsValid = 1; // mark data in partition structure as valid |
|
- | 610 | result = 0; |
|
- | 611 | end: |
|
- | 612 | if(result != 0) Fat16_Deinit(); |
|
- | 613 | else SerialPutString("ok"); |
|
- | 614 | return(result); |
|
- | 615 | } |
|
- | 616 | ||
- | 617 | ||
- | 618 | ||
466 | // |
619 | /****************************************************************************************************************************************/ |
- | 620 | /* Function: ClearCurrCluster(File_t*); */ |
|
Line 467... | Line -... | ||
467 | // Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries. |
- | |
468 | // |
- | |
469 | // Returnvalue: The function returns a value of 0 if the data could not be written. |
621 | /* */ |
470 | //________________________________________________________________________________________________________________________________________ |
- | |
471 | - | ||
Line 472... | Line -... | ||
472 | u8 fputchar_(File *file,s8 c) |
- | |
473 | { |
- | |
474 | u32 ul_temp = 0; |
622 | /* Description: This function fills the current cluster with 0. */ |
475 | u8 retvalue = 1; |
623 | /* */ |
476 | - | ||
477 | if(file->sector_index >= SectorsPerCluster) // if end of the cluster is reached, find next free cluster |
- | |
478 | { |
- | |
479 | file->sector_index = 0; |
- | |
480 | if(!AppendCluster(file)) retvalue = 0; // append a new and free cluster at the end of the file. |
- | |
481 | } |
624 | /* Returnvalue: The function returns 1 on success else 0. */ |
482 | 625 | /****************************************************************************************************************************************/ |
|
483 | file->buffer[file->byte_index] = c; // write databyte into the buffer. The byte will be written to the device at once |
- | |
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. |
- | |
485 | file->fileposition++; // the actual positon within the file. |
626 | u8 ClearCurrCluster(File_t * file) |
486 | // if the buffer contains the complete sectordata. |
- | |
487 | if(file->byte_index < 511) // if the end of this sector is not reached yet |
- | |
488 | { |
- | |
489 | file->byte_index++; // the next byte will be written to the next byteposition in this sector. |
627 | { |
490 | } |
628 | u8 retvalue = 1; |
491 | else // otherwise the data in the sectorbuffer will be written to the device and the next sector will be selected. |
629 | u32 i; |
492 | { |
630 | |
493 | ul_temp = (u32)file->cluster_pointer; |
631 | if((!Partition.IsValid) || (file == NULL)) return(0); |
494 | ul_temp += (u32)file->sector_index; |
632 | |
495 | - | ||
496 | SDC_PutSector((u32)ul_temp,file->buffer); |
- | |
497 | file->byte_index=0; // and the next byte will be written at the beginning of this new sector. |
- | |
498 | file->sector_index++; |
- | |
499 | if(file->sector_index >= SectorsPerCluster) // if end of the cluster is reached, find next free cluster |
- | |
500 | { |
- | |
501 | file->sector_index = 0; |
- | |
502 | if(!AppendCluster(file)) retvalue = 0; // append a new and free cluster at the end of the file. |
- | |
503 | } |
- | |
504 | } |
- | |
505 | return(retvalue); |
- | |
506 | } |
- | |
507 | - | ||
508 | //________________________________________________________________________________________________________________________________________ |
- | |
509 | // Funtion: fputs_(File *file, s8 *string); |
- | |
510 | // |
- | |
511 | // Description: This function writes a zero terminated string to the specified file. |
- | |
512 | // |
- | |
513 | //________________________________________________________________________________________________________________________________________ |
- | |
514 | 633 | for(i = 0; i < BYTES_PER_SECTOR; i++) file->Cache[i] = 0; // clear file cache |
|
Line 515... | Line 634... | ||
515 | u8 fputs_(File *file,s8 * string) |
634 | for(i = 0; i < Partition.SectorsPerCluster; i++) |
516 | { |
635 | { |
517 | u8 i=0; |
- | |
- | 636 | file->SectorInCache = file->FirstSectorOfCurrCluster + i; |
|
518 | 637 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
|
519 | while(string[i] != 0) |
- | |
- | 638 | { |
|
520 | { |
639 | Fat16_Deinit(); |
521 | fputchar_(file,string[i]); |
640 | retvalue = 0; |
522 | i++; |
- | |
523 | } |
641 | } |
524 | return(0); |
642 | } |
525 | } |
643 | return(retvalue); |
526 | 644 | } |
|
527 | //________________________________________________________________________________________________________________________________________ |
645 | |
528 | // Funtion: s8 * fgets_(s8 *string, s16 count, File *file) |
646 | /*****************************************************************************************************************************************/ |
- | 647 | /* Function: GetNextCluster(File_t* ); */ |
|
529 | // |
648 | /* */ |
- | 649 | /* Description: This function finds the next datacluster of the file specified with File *File. */ |
|
530 | // Description: This function reads a string from the file to the specifies string until EOF or end of line was detected.. |
650 | /* */ |
- | 651 | /* Returnvalue: The function returns the next cluster or 0 if the last cluster has already reached. */ |
|
- | 652 | /*****************************************************************************************************************************************/ |
|
- | 653 | u16 GetNextCluster(File_t * file) |
|
- | 654 | { |
|
- | 655 | u16 cluster = 0; |
|
- | 656 | u32 fat_byte_offset, sector, byte; |
|
531 | // |
657 | Fat16Entry_t * fat; |
- | 658 | ||
- | 659 | if((!Partition.IsValid) || (file == NULL)) return(cluster); |
|
- | 660 | // if sector is within the data area |
|
532 | // Returnvalue: A pointer to the string or NULL if EOF was read from the file with no other characters before. |
661 | if((Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)&& (file->FirstSectorOfCurrCluster <= Partition.LastDataSector)) |
533 | //________________________________________________________________________________________________________________________________________ |
662 | { |
534 | 663 | // determine current file cluster |
|
- | 664 | cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); |
|
- | 665 | // calculate byte offset in the fat for corresponding entry |
|
- | 666 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
|
- | 667 | // calculate the sector that contains the current cluster within the fat |
|
- | 668 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
|
- | 669 | // calculate byte offset of the current cluster within that fat sector |
|
- | 670 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
|
- | 671 | // read this sector to the file cache |
|
- | 672 | if(file->SectorInCache != sector) |
|
- | 673 | { |
|
535 | s8 * fgets_(s8 *string, s16 count, File *file) |
674 | file->SectorInCache = sector; // update sector stored in buffer |
536 | { |
- | |
537 | s16 buff_pnt = 0; |
- | |
538 | s16 buff_tmp = 0; |
- | |
539 | s8 *retvalue = 0; |
- | |
540 | - | ||
541 | retvalue = string; |
- | |
542 | - | ||
543 | while(count > 1) // read the count-1 characters from the file to the string. |
- | |
544 | { |
- | |
545 | buff_tmp = fgetchar_(file); // read a character from the opened file. |
- | |
546 | if(buff_tmp == EOF) // is the end of the file reached, terminate the string with zero. |
- | |
547 | { |
- | |
548 | if(buff_pnt != 0) // are there characters within the string? |
- | |
549 | { |
675 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
550 | break; |
- | |
551 | } |
676 | { |
552 | else |
677 | Fat16_Deinit(); |
553 | { |
678 | return (cluster); |
554 | retvalue = NULL; // first character read was EOF -> return(NULL); |
679 | } |
555 | break; |
680 | } |
556 | } |
681 | // read the next cluster from cache |
557 | } |
682 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
558 | else if(buff_tmp == 0x0A) // lf detected. |
683 | cluster = fat->NextCluster; |
559 | { |
- | |
560 | string[buff_pnt] = (s8) buff_tmp; // add lf to the string. |
684 | // if last cluster fat entry |
561 | buff_pnt++; |
- | |
562 | break; |
- | |
563 | } |
- | |
564 | else |
- | |
565 | { |
- | |
566 | string[buff_pnt] = (s8) buff_tmp; |
- | |
567 | count--; |
- | |
568 | buff_pnt++; |
- | |
569 | } |
- | |
570 | } |
- | |
571 | string[buff_pnt] = 0; // terminate string with zero |
- | |
572 | return(string); |
- | |
573 | } |
- | |
574 | - | ||
575 | //________________________________________________________________________________________________________________________________________ |
- | |
576 | // Funtion: u8 fexist_(u8*); |
- | |
577 | // |
- | |
578 | // Description: This function searches the specified file and returns 0 if the file was not found. |
- | |
579 | // |
- | |
580 | // |
- | |
581 | // Return: 0 = file does not exist |
- | |
582 | // 1 = file exists |
- | |
583 | //________________________________________________________________________________________________________________________________________ |
- | |
584 | - | ||
585 | u8 fexist_(s8 *fname) |
- | |
586 | { |
- | |
587 | File *file; |
- | |
588 | 685 | if(FAT16_CLUSTER_LAST_MIN <= cluster) |
|
Line 589... | Line -... | ||
589 | file = fopen_(fname,'r'); // try to open the specified file. |
- | |
590 | - | ||
591 | if(file != NULL) |
- | |
592 | { |
- | |
593 | FreeFilePointer(file); // Free the filepoint // free the filepointer |
- | |
594 | return(1); // file was found so return(1); |
- | |
595 | } |
- | |
Line -... | Line 686... | ||
- | 686 | { |
|
- | 687 | cluster = 0; |
|
- | 688 | } |
|
- | 689 | else |
|
- | 690 | { |
|
- | 691 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(cluster); |
|
- | 692 | file->SectorOfCurrCluster = 0; |
|
596 | else |
693 | file->ByteOfCurrSector = 0; |
597 | { |
694 | } |
598 | return(0); // file not found return(0); |
695 | } |
599 | } |
696 | return(cluster); |
600 | } |
697 | } |
601 | 698 | ||
- | 699 | ||
Line 602... | Line -... | ||
602 | //________________________________________________________________________________________________________________________________________ |
- | |
603 | // Funtion: GetNextCluster(File *file); |
700 | /****************************************************************************************************************************************/ |
604 | // |
- | |
605 | // Description: This function finds the next datacluster of the file specified with File *file. |
- | |
606 | // |
- | |
607 | // Returnvalue: The function returns "0" if the last cluster has already been reached. |
- | |
608 | //________________________________________________________________________________________________________________________________________ |
- | |
609 | - | ||
610 | u16 GetNextCluster(File *file) |
- | |
611 | { |
- | |
612 | u32 fat_pointer = 0; |
- | |
613 | u32 fat_sector_offset = 0; |
- | |
614 | u32 ul_tmp = 0; |
- | |
Line 615... | Line 701... | ||
615 | u8 retvalue = 0; // no new cluster found yet. |
701 | /* Function: FindNextFreeCluster(File_t *); */ |
616 | 702 | /* */ |
|
- | 703 | /* Description: This function looks in the fat to find the next free cluster */ |
|
617 | 704 | /* */ |
|
618 | if((file->cluster_pointer >= RootDirectory) && (file->cluster_pointer < (RootDirectory + 31))) |
- | |
619 | { // Is the next cluster searched within the rootdirectory and available? |
- | |
620 | file->cluster_pointer++; // the rootdirectory is a linear adress space of 32 clusters. |
- | |
621 | retvalue = 1; // and the next cluster has been found. |
- | |
622 | } |
705 | /* Returnvalue: The function returns the cluster number of the next free cluster found within the fat. */ |
623 | else if(file->cluster_pointer > (RootDirectory + 31)) // The specified cluster is within the FAT. |
706 | /****************************************************************************************************************************************/ |
624 | { |
- | |
625 | fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster within the FAT. |
707 | u16 FindNextFreeCluster(File_t *file) |
626 | fat_sector_offset /= SectorsPerCluster; // calculate the index of the actual sector within the FAT. |
708 | { |
627 | fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two. |
- | |
628 | fat_pointer = (fat_sector_offset%0x100); // Calculate the sector within the cluster. |
709 | u32 fat_sector; // current sector within the fat relative to the first sector of the fat. |
629 | fat_sector_offset = (fat_sector_offset>>8); // and the position within the sector. |
- | |
630 | - | ||
631 | SDC_GetSector((u32)(FileAllocationTable + fat_sector_offset),file->buffer); |
710 | u32 curr_sector; // current sector |
632 | file->sector_in_buffer = (FileAllocationTable + fat_sector_offset); // Mark that new sector has been read. |
711 | u16 fat_entry; // index to an fatentry within the actual sector (256 fatentries are possible within one sector). |
Line 633... | Line -... | ||
633 | - | ||
634 | ul_tmp = (u32)file->buffer[((fat_pointer << 1)+1)]; // Read next sector information from calculated clusterposition. |
- | |
635 | ul_tmp = (ul_tmp << 8); |
- | |
636 | ul_tmp |= (u32)file->buffer[(fat_pointer << 1)]; |
- | |
637 | ul_tmp -=2; // next datacluster is clusterposition in fat - 2. |
- | |
638 | ul_tmp *= SectorsPerCluster; // calculate sectorposition of new cluster |
712 | u16 free_cluster = 0; // next free cluster number. |
639 | ul_tmp += FirstDataCluster; // in relation to first datacluster of the disk. |
- | |
Line 640... | Line -... | ||
640 | - | ||
641 | if(ul_tmp < 0xfff7) // has a new cluster been read or was the end of the fat reached? |
- | |
642 | { |
- | |
643 | file->cluster_pointer = (u32) ul_tmp; // continue reading the file at the beginning of new datacluster. |
- | |
644 | retvalue = 1; // a new cluster was found. |
- | |
645 | } |
- | |
646 | } |
- | |
647 | return(retvalue); |
- | |
648 | } |
- | |
649 | - | ||
650 | //________________________________________________________________________________________________________________________________________ |
- | |
651 | // Funtion: u16 FindNextFreeCluster(void) |
- | |
652 | // |
- | |
653 | // Description: This function looks in the FAT to find the next free datacluster |
- | |
654 | // |
- | |
655 | // Returnvalue: The function returns the adress of the next free cluster found within the fAT. |
- | |
656 | //________________________________________________________________________________________________________________________________________ |
713 | Fat16Entry_t * fat; |
657 | 714 | ||
658 | u16 FindNextFreeCluster(File *file) |
715 | if((!Partition.IsValid) || (file == NULL)) return(0); |
659 | { |
716 | |
660 | u32 fat_pointer = 0; // Pointer to the first sector of the FAT. |
717 | // start searching for an empty cluster at the beginning of the fat. |
661 | u32 ul_tmp = 0; // temporary variable used to calculate a sectornumber. |
718 | fat_sector = 0; |
- | 719 | do |
|
- | 720 | { |
|
662 | u16 fat_sector_offset = 0; // index to a sector within the FAT. |
721 | curr_sector = Partition.FirstFatSector + fat_sector; // calculate sector to read |
- | 722 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
|
663 | u16 fat_entry = 0; // index to an fatentry within the actual sector (256 fatentries are possible within one sector). |
723 | if( SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector of fat from sd-card. |
664 | u16 free_cluster = 0; // a pointer to the first sector of the next free cluster. |
724 | { |
665 | 725 | Fat16_Deinit(); |
|
666 | fat_pointer = (u32) FileAllocationTable; // start searching for empty cluster at the beginning of the fat. |
726 | return(free_cluster); |
- | 727 | } |
|
- | 728 | ||
667 | // if the end of the fat is not reached yet and no free cluster has been found |
729 | fat = (Fat16Entry_t *)file->Cache; // set fat pointer to file cache |
668 | while((fat_sector_offset < SectorsPerFat) && (!free_cluster)) |
- | |
669 | { |
730 | |
670 | ul_tmp = (u32) ((u32)fat_pointer + (u32)fat_sector_offset); |
731 | for(fat_entry = 0; fat_entry < FAT16_ENTRIES_PER_SECTOR; fat_entry++) // look for an free cluster at all entries in this sector of the fat. |
Line 671... | Line -... | ||
671 | SDC_GetSector((u32)ul_tmp,file->buffer); // read next sector of FAT. |
- | |
672 | file->sector_in_buffer = ul_tmp; // remember the number of the sector in FileBuffer. |
- | |
673 | Fat = (struct FatEntry *)file->buffer; |
- | |
674 | for(fat_entry=0;fat_entry<256;fat_entry++) // look for an free cluster at all entries in this sector of the fat. |
- | |
675 | { |
- | |
676 | if(Fat[fat_entry].next_cluster == 0x0000) // empty cluster found!! |
- | |
677 | { |
- | |
Line -... | Line 732... | ||
- | 732 | { |
|
- | 733 | if(fat[fat_entry].NextCluster == FAT16_CLUSTER_FREE) // empty cluster found!! |
|
- | 734 | { |
|
- | 735 | fat[fat_entry].NextCluster = FAT16_CLUSTER_LAST_MAX; // mark this fat-entry as used |
|
- | 736 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // and save the sector at the sd-card. |
|
- | 737 | { |
|
- | 738 | Fat16_Deinit(); |
|
678 | Fat[fat_entry].next_cluster = 0xffff; // mark this fat-entry as used and save it to the device. |
739 | return(free_cluster); |
679 | SDC_PutSector((u32)file->sector_in_buffer,file->buffer); |
740 | } |
680 | free_cluster = fat_entry; // the relative position of the free cluster found in this sector of the FAT. |
- | |
681 | free_cluster += (fat_sector_offset << 8); // calculate the absolute position of the free cluster in the FAT; |
741 | free_cluster = (u16)(fat_sector * FAT16_ENTRIES_PER_SECTOR + (u32)fat_entry); |
682 | fat_entry = 256; // terminate the search for a free cluster in this sector. |
742 | fat_entry = FAT16_ENTRIES_PER_SECTOR; // terminate the search for a free cluster in this sector. |
Line 683... | Line 743... | ||
683 | } |
743 | } |
684 | } |
- | |
685 | fat_sector_offset++; |
744 | } |
686 | } |
- | |
687 | return(free_cluster); |
745 | fat_sector++; // continue the search in next fat sector |
688 | } |
746 | // repeat until the end of the fat is reached and no free cluster has been found so far |
689 | 747 | }while((fat_sector < Partition.SectorsPerFat) && (!free_cluster)); |
|
- | 748 | return(free_cluster); |
|
690 | //________________________________________________________________________________________________________________________________________ |
749 | } |
691 | // Funtion: u16 AppendCluster(File *file); |
- | |
692 | // |
- | |
693 | // Description: This function finds the next free datacluster on the disk and appends it to the specified file. |
750 | |
694 | // |
751 | |
695 | // Returnvalue: This funktion returns 1 if a cluster was appended to the specified file. |
752 | /****************************************************************************************************************************************************/ |
- | 753 | /* Function: s16 fseek_(File_t *, s32 *, u8) */ |
|
696 | //________________________________________________________________________________________________________________________________________ |
754 | /* */ |
697 | 755 | /* Description: This function sets the pointer of the stream relative to the position */ |
|
698 | u8 AppendCluster(File *file) |
- | |
699 | { |
- | |
700 | u16 free_cluster = 0; |
756 | /* specified by origin (SEEK_SET, SEEK_CUR, SEEK_END) */ |
701 | u32 fat_pointer = 0; |
- | |
Line 702... | Line -... | ||
702 | u8 retvalue = 0; |
- | |
703 | - | ||
704 | free_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
- | |
705 | if(free_cluster) retvalue = 1; // A free cluster was found and can be added to the end of the file. |
- | |
706 | fat_pointer = FileAllocationTable; // Set Pointer to the beginnig of the FAT. |
- | |
707 | fat_pointer += (u32)((u32)GetFatClusterOffset(file) >> 8); // find the sector in the FAT with 256 entries per sector. |
757 | /* Returnvalue: Is 1 if seek was successful */ |
708 | - | ||
Line 709... | Line -... | ||
709 | SDC_GetSector(fat_pointer,file->buffer); |
- | |
710 | Fat = (struct FatEntry *)file->buffer; |
- | |
711 | Fat[GetFatSectorIndex(file)].next_cluster = free_cluster; // append the free cluster to the end of the file in the FAT. |
- | |
712 | SDC_PutSector((u32)fat_pointer,file->buffer); // save the modified sector to the FAT. |
- | |
713 | - | ||
714 | fat_pointer = (u32)free_cluster; |
- | |
715 | fat_pointer -= 2; |
- | |
716 | fat_pointer *= SectorsPerCluster; |
- | |
717 | fat_pointer += FirstDataCluster; |
- | |
718 | 758 | /****************************************************************************************************************************************************/ |
|
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 | 759 | s16 fseek_(File_t *file, s32 offset, s16 origin) |
|
724 | //________________________________________________________________________________________________________________________________________ |
760 | { |
725 | // Funtion: void DeleteClusterChain(u16 startcluster); |
- | |
- | 761 | s32 fposition = 0; |
|
726 | // |
762 | s16 retvalue = 1; |
727 | // Description: This function frees all the clusters used for file from the fat. |
763 | |
728 | // |
- | |
729 | // Returnvalue: none |
764 | if((!Partition.IsValid) || (file == NULL)) return(0); |
730 | //________________________________________________________________________________________________________________________________________ |
765 | switch(origin) |
- | 766 | { |
|
- | 767 | case SEEK_SET: // Fileposition relative to the beginning of the file. |
|
731 | 768 | fposition = 0; |
|
732 | void DeleteClusterChain(u16 startcluster) |
769 | break; |
- | 770 | case SEEK_END: // Fileposition relative to the end of the file. |
|
733 | { |
771 | fposition = (s32)file->Size; |
734 | u16 fat_index = 0; |
772 | break; |
735 | u16 fat_sector_offset = 0; |
773 | case SEEK_CUR: // Fileposition relative to the current position of the file. |
736 | u32 sector_in_buffer = 0; |
774 | default: |
- | 775 | fposition = file->Position; |
|
737 | u32 ul_temp = 0; |
776 | break; |
738 | u8 buffer[512]; |
777 | } |
739 | 778 | ||
- | 779 | fposition += offset; |
|
- | 780 | ||
- | 781 | if((fposition >= 0) && (fposition <= (s32)file->Size)) // is the pointer still within the file? |
|
- | 782 | { |
|
- | 783 | // reset file position to start of the file |
|
- | 784 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
|
- | 785 | file->SectorOfCurrCluster = 0; |
|
- | 786 | file->ByteOfCurrSector = 0; |
|
740 | fat_index = (startcluster % 0x100); // Calculate the sector within the cluster. |
787 | file->Position = 0; |
741 | fat_sector_offset = (startcluster >> 8); // and the position within the sector. |
788 | |
742 | ul_temp = (u32)(FileAllocationTable + fat_sector_offset); |
789 | while(file->Position < fposition) // repeat until the current position is less than target |
- | 790 | { |
|
743 | 791 | file->Position++; // increment file position |
|
Line 744... | Line 792... | ||
744 | 792 | file->ByteOfCurrSector++; // next byte in current sector |
|
745 | do |
793 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) |
- | 794 | { |
|
- | 795 | file->ByteOfCurrSector = 0; // reading at the beginning of new sector. |
|
- | 796 | file->SectorOfCurrCluster++; // continue reading in next sector |
|
- | 797 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster) // if end of cluster is reached, the next datacluster has to be searched in the FAT. |
|
- | 798 | { |
|
746 | { |
799 | if(GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
- | 800 | { |
|
747 | if(sector_in_buffer != ul_temp) |
801 | file->SectorOfCurrCluster = 0; |
- | 802 | } |
|
- | 803 | else // the last cluster was allready reached |
|
- | 804 | { |
|
- | 805 | file->SectorOfCurrCluster--; // jump back to the ast sector in the last cluster |
|
748 | { |
806 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set ByteOfCurrSector one byte over sector end |
749 | sector_in_buffer = ul_temp; |
807 | } |
750 | SDC_GetSector(ul_temp,buffer); |
808 | } |
751 | } |
809 | } |
- | 810 | } |
|
- | 811 | } |
|
- | 812 | if(file->Position == fposition) retvalue = 0; |
|
- | 813 | return(retvalue); |
|
- | 814 | } |
|
- | 815 | ||
- | 816 | ||
752 | Fat = (struct FatEntry *)buffer; |
817 | /****************************************************************************************************************************************/ |
- | 818 | /* Function: u16 DeleteClusterChain(File *file); */ |
|
- | 819 | /* */ |
|
753 | 820 | /* Description: This function trances along a cluster chain in the fat and frees all clusters visited. */ |
|
- | 821 | /* */ |
|
- | 822 | /****************************************************************************************************************************************/ |
|
- | 823 | u8 DeleteClusterChain(u16 StartCluster) |
|
- | 824 | { |
|
- | 825 | u16 cluster; |
|
- | 826 | u32 fat_byte_offset, sector, byte; |
|
- | 827 | Fat16Entry_t * fat; |
|
- | 828 | u8 buffer[BYTES_PER_SECTOR]; |
|
754 | startcluster = Fat[fat_index].next_cluster; |
829 | u32 sector_in_buffer = 0; |
- | 830 | u8 repeat = 0; |
|
- | 831 | ||
755 | Fat[fat_index].next_cluster = 0x0000; // free the cluster within the fat. |
832 | if(!Partition.IsValid) return 0; |
- | 833 | ||
- | 834 | cluster = StartCluster; // init chain trace |
|
- | 835 | // calculate byte offset in the fat for corresponding entry |
|
756 | 836 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
|
757 | fat_index = (startcluster % 0x100); // Calculate the sector within the cluster. |
837 | // calculate the sector that contains the current cluster within the fat |
- | 838 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
|
- | 839 | // calculate byte offset of the current cluster within that fat sector |
|
- | 840 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
|
- | 841 | do |
|
- | 842 | { |
|
- | 843 | if(sector != sector_in_buffer) |
|
758 | fat_sector_offset = (startcluster >> 8); // and the position within the sector. |
844 | { |
759 | ul_temp = (u32)(FileAllocationTable + fat_sector_offset); |
845 | // read this sector to buffer |
760 | if((sector_in_buffer != ul_temp) || (startcluster == 0xffff)) |
846 | sector_in_buffer = sector; |
Line 761... | Line -... | ||
761 | { |
- | |
762 | SDC_PutSector(sector_in_buffer,buffer); |
- | |
763 | } |
- | |
764 | } |
- | |
765 | while(startcluster != 0xffff); // last cluster has been deleted. |
- | |
766 | } |
- | |
Line -... | Line 847... | ||
- | 847 | if(SD_SUCCESS != SDC_GetSector(sector_in_buffer, buffer)) return 0; // read sector from sd-card |
|
- | 848 | } |
|
- | 849 | // read the next cluster from cache |
|
- | 850 | fat = (Fat16Entry_t *)(&(buffer[byte])); |
|
- | 851 | cluster = fat->NextCluster; |
|
- | 852 | if((FAT16_CLUSTER_USED_MIN <= cluster) && (cluster <= FAT16_CLUSTER_USED_MAX) ) repeat = 1; |
|
- | 853 | else repeat = 0; |
|
767 | 854 | ||
768 | 855 | fat->NextCluster = FAT16_CLUSTER_FREE; // mark current cluster as free |
|
769 | //________________________________________________________________________________________________________________________________________ |
856 | // calculate byte offset in the fat for corresponding entry |
770 | // Funtion: u16 GetFatClusterIndex(File *file); |
- | |
771 | // |
857 | fat_byte_offset = ((u32)cluster)<<1; // two FAT bytes (16 bits) for every cluster |
772 | // Description: This function returns the clusterindex of the cluster specified by file->cluster_pointer of the specified file. |
- | |
773 | // |
- | |
774 | //________________________________________________________________________________________________________________________________________ |
858 | // calculate the sector that contains the current cluster within the fat |
775 | - | ||
776 | u16 GetFatClusterOffset(File *file) |
859 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
777 | { |
- | |
778 | u32 fat_sector_offset = 0; |
860 | // calculate byte offset of the current cluster within that fat sector |
779 | 861 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
|
780 | fat_sector_offset = ((file->cluster_pointer) - (FirstDataCluster)); // Calculate index of actual cluster in FAT. |
862 | // if new sector is not the sector in buffer or the last cluster in the chain was traced |
781 | fat_sector_offset /= SectorsPerCluster; |
- | |
782 | fat_sector_offset += 2; // In Fat16 clusterpositions have an offset of two. |
863 | if((sector != sector_in_buffer) || !repeat) |
783 | 864 | { // write sector in buffer |
|
784 | return((u16)fat_sector_offset); |
865 | if(SD_SUCCESS != SDC_PutSector(sector_in_buffer,buffer)) return 0; |
- | 866 | } |
|
785 | } |
867 | } |
786 | 868 | while(repeat); |
|
787 | //________________________________________________________________________________________________________________________________________ |
869 | |
788 | // Funtion: u16 GetFatSectorIndex(File *file); |
- | |
Line -... | Line 870... | ||
- | 870 | return 1; |
|
- | 871 | } |
|
- | 872 | ||
- | 873 | ||
- | 874 | /****************************************************************************************************************************************/ |
|
- | 875 | /* Function: u16 AppendCluster(File *file); */ |
|
- | 876 | /* */ |
|
- | 877 | /* Description: This function looks in the fat to find the next free cluster and appends it to the file. */ |
|
- | 878 | /* */ |
|
- | 879 | /* Returnvalue: The function returns the appened cluster number or 0 of no cluster was appended. */ |
|
- | 880 | /****************************************************************************************************************************************/ |
|
- | 881 | u16 AppendCluster(File_t *file) |
|
- | 882 | { |
|
- | 883 | u16 last_cluster, new_cluster = 0; |
|
- | 884 | u32 fat_byte_offset, sector, byte; |
|
- | 885 | Fat16Entry_t * fat; |
|
- | 886 | ||
- | 887 | if((!Partition.IsValid) || (file == NULL)) return(new_cluster); |
|
- | 888 | ||
- | 889 | new_cluster = FindNextFreeCluster(file); // the next free cluster found on the disk. |
|
- | 890 | if(new_cluster) |
|
- | 891 | { // A free cluster was found and can be added to the end of the file. |
|
- | 892 | fseek_(file, 0, SEEK_END); // jump to the end of the file |
|
- | 893 | last_cluster = SectorToFat16Cluster(file->FirstSectorOfCurrCluster); // determine current file cluster |
|
- | 894 | fat_byte_offset = ((u32)last_cluster)<<1; |
|
- | 895 | sector = Partition.FirstFatSector + ( fat_byte_offset / BYTES_PER_SECTOR); |
|
- | 896 | byte = fat_byte_offset % BYTES_PER_SECTOR; |
|
- | 897 | ||
- | 898 | if(file->SectorInCache != sector) |
|
- | 899 | { |
|
789 | // |
900 | file->SectorInCache = sector; // update sector stored in buffer |
790 | // Description: This function returns the sectorindex of the cluster specified by file->cluster_pointer of the specified file. |
901 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read sector from sd-card |
791 | // |
902 | { |
- | 903 | Fat16_Deinit(); |
|
- | 904 | return(0); |
|
792 | //________________________________________________________________________________________________________________________________________ |
905 | } |
793 | 906 | } |
|
794 | u16 GetFatSectorIndex(File *file) |
- | |
795 | { |
907 | fat = (Fat16Entry_t *)(&(file->Cache[byte])); |
796 | u16 fat_pointer = 0; |
- | |
797 | 908 | fat->NextCluster = new_cluster; // append the free cluster to the end of the file in the FAT. |
|
798 | fat_pointer = GetFatClusterOffset(file); |
- | |
Line 799... | Line 909... | ||
799 | fat_pointer = fat_pointer % 0x100; // Calculate the clusterposition in the fat |
909 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // save the modified sector to the FAT. |
- | 910 | { |
|
Line 800... | Line 911... | ||
800 | 911 | Fat16_Deinit(); |
|
- | 912 | return(0); |
|
- | 913 | } |
|
Line -... | Line 914... | ||
- | 914 | file->FirstSectorOfCurrCluster = Fat16ClusterToSector(new_cluster); |
|
801 | return(fat_pointer); |
915 | file->SectorOfCurrCluster = 0; |
- | 916 | file->ByteOfCurrSector = 0; |
|
802 | } |
917 | } |
803 | - | ||
804 | //________________________________________________________________________________________________________________________________________ |
- | |
805 | // Funtion: u16 CreateFileInDirectory(u8 *, File *) |
- | |
806 | // |
- | |
807 | // Description: This function looks for the next free position in the directory and creates an entry. The type of an directoryentry is |
- | |
808 | // specified by the attribute. |
- | |
809 | // bit0: unused |
- | |
810 | // bit1: archive |
- | |
811 | // bit2: read_only |
918 | return(new_cluster); |
812 | // bit3: system |
- | |
813 | // bit4: directory |
- | |
814 | // bit5: volume |
- | |
815 | //________________________________________________________________________________________________________________________________________ |
- | |
816 | - | ||
817 | u8 CreateFileInDirectory(s8 *fname, File *file) |
- | |
818 | { |
- | |
819 | u16 rootentry = 0; // index to an entry in the rootdirectory. |
- | |
820 | u16 cnt_enries_searched = 0; // count the number of rootentries which have been searched already. |
- | |
821 | u8 i = 0; |
- | |
822 | u16 sector_offset = 0; // index to the sector of the Rootentry which is searched momentarily |
- | |
823 | u8 retvalue = 0; |
919 | } |
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. |
- | |
838 | file->cluster_pointer = file->start_cluster; // start reading the file with the first sector of the first datacluster. |
- | |
839 | 920 | ||
840 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
921 | /****************************************************************************************************************************************************/ |
841 | do |
- | |
842 | { // search the next 16 rootentries in this sector of the roordirectory. |
- | |
843 | rootentry=0; |
- | |
844 | SDC_GetSector((u32)(CWD + sector_offset),file->buffer); // Read the Rootdirectory. |
922 | /* Function: DirectoryEntryExist(s8 *, u8, u8, File_t *) */ |
845 | DirectoryEntry = (struct DirEntry *)file->buffer; |
923 | /* */ |
846 | while((rootentry<16) && (!retvalue)) |
924 | /* Description: This function searches all possible dir entries until the file or directory is found or the end of the directory is reached */ |
847 | { |
925 | /* */ |
848 | if((DirectoryEntry[rootentry].attribute == 0) || (DirectoryEntry[rootentry].attribute == 0xE5)) // empty directory entry found |
926 | /* Returnvalue: This function returns 1 if the directory entry specified was found. */ |
849 | { |
927 | /****************************************************************************************************************************************************/ |
850 | for(i=0;i<11;i++) DirectoryEntry[rootentry].name[i] = name[i];// Kopie the filename and the file extension to the directoryentry. |
928 | u8 DirectoryEntryExist(s8 *dirname, u8 attribfilter, u8 attribmask, File_t *file) |
851 | DirectoryEntry[rootentry].attribute = _FILE; // Set the fileattribute to archive to reserve the directoryentry. |
929 | { |
852 | DirectoryEntry[rootentry].startcluster = cluster; // copy the location of the first datacluster to the directoryentry. |
930 | u32 dir_sector, max_dir_sector, curr_sector; |
853 | DirectoryEntry[rootentry].size = 0; // the new createted file has no content yet. |
- | |
854 | file->directory_sector = (u32) (CWD + sector_offset); |
931 | u16 dir_entry = 0; |
855 | file->directory_index = (u8) rootentry; |
- | |
856 | retvalue = 1; |
- | |
857 | SDC_PutSector((u32)(CWD + sector_offset),file->buffer); |
- | |
858 | } |
932 | |
859 | rootentry++; |
- | |
860 | cnt_enries_searched++; |
933 | u16 end_of_directory_not_reached = 0; |
861 | } |
- | |
862 | if(!retvalue) // file not found in this sector so take next sector. |
- | |
863 | { |
934 | u8 i = 0; |
864 | rootentry = 0; |
935 | u8 direntry_exist = 0; |
865 | sector_offset++; |
- | |
866 | } |
936 | DirEntry_t * dir; |
867 | } |
- | |
868 | while((cnt_enries_searched< PossibleRootEntries) && (!retvalue)); |
937 | |
869 | - | ||
870 | } |
- | |
871 | return(retvalue); // return 1 if file has been created otherwise return 0. |
- | |
872 | } |
938 | // if incomming pointers are useless return immediatly |
873 | - | ||
874 | //________________________________________________________________________________________________________________________________________ |
- | |
875 | // Funtion: u16 SeekFileInDirectory(s8 *fname, File *) |
- | |
876 | // |
- | |
877 | // Description: this function searches all possible entries withint the actual directory for the specified object. |
- | |
878 | // |
- | |
879 | // Returnvalue: This function returns 1 if the directoryentry specified was found. |
- | |
880 | //________________________________________________________________________________________________________________________________________ |
- | |
881 | - | ||
882 | u8 SeekFileInDirectory(s8 *fname, File *file) |
- | |
883 | { |
- | |
884 | u16 rootentry=0; |
- | |
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] = " "; |
- | |
890 | - | ||
891 | SeperateFileName(fname,name); |
- | |
892 | 939 | if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return(direntry_exist); |
|
893 | file->cluster_pointer = CWD; // start looking for the file in the actual directory. |
- | |
894 | 940 | ||
895 | // directory starts at sector specified by dir_sector. This can be the rootdirectory or any other directory. |
941 | // dir entries can be searched only in filesclusters that have |
896 | do |
942 | // a corresponding dir entry with adir-flag set in its attribute |
- | 943 | // or direct within the root directory area |
|
897 | { // search the next 16 rootentries in this sector of the roordirectory. |
944 | |
- | 945 | file->FirstSectorOfFirstCluster = 0; |
|
- | 946 | // no current directory exist therefore assume searching in the root |
|
- | 947 | if(file->DirectorySector == 0) |
|
898 | rootentry=0; |
948 | { |
- | 949 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
|
- | 950 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
|
- | 951 | } |
|
- | 952 | // within the root directory area we can read sectors sequentially until the end of this area |
|
- | 953 | else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector)) |
|
- | 954 | { |
|
- | 955 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
|
- | 956 | } |
|
899 | SDC_GetSector((u32) file->cluster_pointer,file->buffer); // Read the Rootdirectory. |
957 | // within the data clusters we can read sectors sequentially only within the cluster |
- | 958 | else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector)) |
|
900 | DirectoryEntry = (struct DirEntry *)file->buffer; |
959 | { |
901 | - | ||
902 | while((!retvalue)&&(rootentry<16)) |
- | |
903 | { |
- | |
904 | i=0; |
- | |
905 | if(DirectoryEntry[rootentry].name[0] != 0xe5) // ignore deleted items. |
- | |
Line 906... | Line -... | ||
906 | { |
- | |
907 | while((i<=10)&&(DirectoryEntry[rootentry].name[i] == name[i])) |
960 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
908 | { |
- | |
909 | i++; |
- | |
910 | } |
- | |
911 | } |
- | |
912 | - | ||
913 | if((i==11) && (DirectoryEntry[rootentry].attribute & _FILE)) // entry found!! -> reading startcluster of entry from offset 26. |
961 | } |
914 | { |
- | |
915 | cluster_temp = (u32)DirectoryEntry[rootentry].startcluster; |
962 | else return (direntry_exist); // bad sector range for directory sector of the file |
916 | cluster_temp -=2; // Clusterposition is ((position in FAT)-2). first two entries in FAT are reserved. |
963 | // if search area is not defined yet |
917 | cluster_temp *= (u32)SectorsPerCluster; // Calculate positon of first cluster. |
- | |
918 | file->start_cluster = (FirstDataCluster + cluster_temp); |
- | |
919 | file->directory_sector = (u32) file->cluster_pointer; |
964 | if(file->FirstSectorOfFirstCluster == 0) |
- | 965 | { |
|
920 | file->cluster_pointer = file->start_cluster; // start reading the file with the first cluster. |
966 | // check if the directory entry of current file is existent and has the dir-flag set |
921 | file->filesize = (u32) DirectoryEntry[rootentry].size; |
- | |
922 | file->directory_index = (u8) rootentry; |
967 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
923 | retvalue = 1; |
968 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
924 | } |
969 | { |
- | 970 | Fat16_Deinit(); |
|
925 | rootentry++; |
971 | return(direntry_exist); |
- | 972 | } |
|
926 | } |
973 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
927 | if(!retvalue) // file not found in this sector so take next sector. |
- | |
928 | { |
974 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
929 | end_of_directory_not_reached = GetNextCluster(file); |
975 | { |
930 | } |
976 | case SLOT_EMPTY: |
- | 977 | case SLOT_DELETED: |
|
- | 978 | // the directrory pointer of this file points to a deleted or not existen directory |
|
- | 979 | // therefore no file or subdirectory can be created |
|
931 | } |
980 | return (direntry_exist); |
932 | while((end_of_directory_not_reached) && (!retvalue)); |
- | |
933 | return(retvalue); |
981 | break; |
934 | } |
982 | default: // and is a real directory |
- | 983 | if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY) |
|
- | 984 | { // current file is not a directory therefore no file or subdirectory can be created here |
|
935 | 985 | return (direntry_exist); |
|
936 | 986 | } |
|
- | 987 | break; |
|
- | 988 | } |
|
- | 989 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[file->DirectoryIndex].StartCluster); |
|
- | 990 | } |
|
937 | 991 | ||
- | 992 | // update current file data area position to start of first cluster |
|
- | 993 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
|
- | 994 | file->SectorOfCurrCluster = 0; |
|
- | 995 | file->ByteOfCurrSector = 0; |
|
- | 996 | ||
- | 997 | do // loop over all data clusters of the current directory entry |
|
- | 998 | { |
|
- | 999 | dir_sector = 0; // reset sector counter within a new cluster |
|
- | 1000 | do // loop over all sectors of a cluster or all sectors of the root directory |
|
- | 1001 | { |
|
- | 1002 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
|
- | 1003 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
|
- | 1004 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read the sector |
|
- | 1005 | { |
|
938 | //________________________________________________________________________________________________________________________________________ |
1006 | Fat16_Deinit(); |
- | 1007 | return(direntry_exist); |
|
- | 1008 | } |
|
- | 1009 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
|
- | 1010 | // search all directory entries within that sector |
|
- | 1011 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
|
- | 1012 | { // check for existing dir entry |
|
- | 1013 | switch((u8)dir[dir_entry].Name[0]) |
|
- | 1014 | { |
|
- | 1015 | case SLOT_EMPTY: |
|
- | 1016 | case SLOT_DELETED: |
|
- | 1017 | // ignore empty or deleted dir entries |
|
- | 1018 | break; |
|
- | 1019 | default: |
|
- | 1020 | // if existing check attributes before names are compared will safe performance |
|
- | 1021 | if ((dir[dir_entry].Attribute & attribmask) != attribfilter) break; // attribute must match |
|
- | 1022 | // then compare the name to the giveb dirname (first 11 characters include 8 chars of basename and 3 chars extension.) |
|
- | 1023 | i = 0; |
|
- | 1024 | while((i < 11) && (dir[dir_entry].Name[i] == dirname[i])) i++; |
|
- | 1025 | if (i < 10) break; // names does not match |
|
- | 1026 | // if dirname and attribute have matched |
|
- | 1027 | file->Attribute = dir[dir_entry].Attribute; // store attribute of found dir entry |
|
- | 1028 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dir[dir_entry].StartCluster); // set sector of first data cluster |
|
- | 1029 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
|
- | 1030 | file->SectorOfCurrCluster = 0; |
|
- | 1031 | file->ByteOfCurrSector = 0; |
|
- | 1032 | file->DirectorySector = curr_sector; // current sector |
|
- | 1033 | file->DirectoryIndex = dir_entry; // current direntry in current sector |
|
- | 1034 | file->Size = dir[dir_entry].Size; |
|
- | 1035 | direntry_exist = 1; // mark as found |
|
- | 1036 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
|
- | 1037 | } // end of first byte of name check |
|
- | 1038 | } |
|
- | 1039 | dir_sector++; // search next sector |
|
- | 1040 | // stop if we reached the end of the cluster or the end of the root dir |
|
- | 1041 | }while((dir_sector < max_dir_sector) && (!direntry_exist)); |
|
- | 1042 | ||
- | 1043 | // if we are seaching in the data area and the file not found in this cluster so take next cluster. |
|
- | 1044 | if(!direntry_exist && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)) |
|
- | 1045 | { |
|
- | 1046 | end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster |
|
- | 1047 | } |
|
- | 1048 | }while((end_of_directory_not_reached) && (!direntry_exist)); // repeat until a next cluster exist an no |
|
- | 1049 | return(direntry_exist); |
|
- | 1050 | } |
|
- | 1051 | ||
- | 1052 | ||
- | 1053 | /****************************************************************************************************************************************/ |
|
- | 1054 | /* Function: CreateDirectoryEntry(s8 *, u16, File_t *) */ |
|
- | 1055 | /* */ |
|
- | 1056 | /* Description: This function looks for the next free position in the directory and creates an entry. */ |
|
- | 1057 | /* The type of an directory entry is specified by the file attribute. */ |
|
- | 1058 | /* */ |
|
- | 1059 | /* Returnvalue: Return 0 on error */ |
|
- | 1060 | /****************************************************************************************************************************************/ |
|
- | 1061 | u8 CreateDirectoryEntry(s8 *dirname, u8 attrib, File_t *file) |
|
- | 1062 | { |
|
- | 1063 | u32 dir_sector, max_dir_sector, curr_sector; |
|
- | 1064 | u16 dir_entry = 0; |
|
- | 1065 | u16 subdircluster, dircluster = 0; |
|
- | 1066 | u16 end_of_directory_not_reached = 0; |
|
- | 1067 | u8 i = 0; |
|
- | 1068 | u8 retvalue = 0; |
|
- | 1069 | DirEntry_t *dir; |
|
- | 1070 | ||
- | 1071 | if((!Partition.IsValid) || (file == NULL) || (dirname == NULL)) return (retvalue); |
|
- | 1072 | // It is not checked here that the dir entry that should be created is already existent! |
|
- | 1073 | ||
- | 1074 | // Dir entries can be created only in file-clusters that have |
|
- | 1075 | // the dir-flag set in its attribute or within the root directory |
|
- | 1076 | ||
- | 1077 | file->FirstSectorOfFirstCluster = 0; |
|
- | 1078 | // no current directory exist therefore assume creating in the root |
|
- | 1079 | if(file->DirectorySector == 0) |
|
- | 1080 | { |
|
- | 1081 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
|
- | 1082 | dircluster = 0; |
|
- | 1083 | file->FirstSectorOfFirstCluster = Partition.FirstRootDirSector; |
|
- | 1084 | } |
|
939 | // Funtion: void SeperateFileName(u8*); |
1085 | // within the root directory area we can read sectors sequentially until the end of this area |
940 | // |
1086 | else if((Partition.FirstRootDirSector <= file->DirectorySector) && (file->DirectorySector < Partition.FirstDataSector)) |
941 | // Description: This function seperates the filename and the fileattribute and brings them into the needed format ('test.txt' -> 'TEST TXT'); |
- | |
942 | // |
- | |
943 | //________________________________________________________________________________________________________________________________________ |
1087 | { |
- | 1088 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
|
- | 1089 | } |
|
- | 1090 | // within the data clusters we can read sectors sequentially only within the cluster |
|
- | 1091 | else if((Partition.FirstDataSector <= file->DirectorySector) && (file->DirectorySector <= Partition.LastDataSector)) |
|
944 | 1092 | { |
|
- | 1093 | max_dir_sector = Partition.SectorsPerCluster; |
|
- | 1094 | } |
|
- | 1095 | else return (retvalue); // bad sector range for directory sector of the file |
|
- | 1096 | // if search area is not defined yet |
|
- | 1097 | if(file->FirstSectorOfFirstCluster == 0) |
|
945 | void SeperateFileName(s8 *fname, s8 *name) |
1098 | { |
- | 1099 | // check if the directory entry of current file is existent and has the dir-flag set |
|
- | 1100 | file->SectorInCache = file->DirectorySector; // update the sector number of file cache. |
|
- | 1101 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
|
- | 1102 | { |
|
- | 1103 | Fat16_Deinit(); |
|
- | 1104 | return(retvalue); |
|
- | 1105 | } |
|
- | 1106 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
|
- | 1107 | switch((u8)dir[file->DirectoryIndex].Name[0]) // check if current directory exist |
|
- | 1108 | { |
|
- | 1109 | case SLOT_EMPTY: |
|
- | 1110 | case SLOT_DELETED: |
|
- | 1111 | return (retvalue); |
|
- | 1112 | break; |
|
- | 1113 | default: // and is a real directory |
|
- | 1114 | if((dir[file->DirectoryIndex].Attribute & ATTR_SUBDIRECTORY) != ATTR_SUBDIRECTORY) |
|
- | 1115 | { // current file is not a directory therefore no file or subdirectory can be created here |
|
- | 1116 | return (retvalue); |
|
- | 1117 | } |
|
- | 1118 | break; |
|
- | 1119 | } |
|
- | 1120 | dircluster = dir[file->DirectoryIndex].StartCluster; |
|
- | 1121 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(dircluster); |
|
- | 1122 | } |
|
- | 1123 | ||
- | 1124 | subdircluster = FindNextFreeCluster(file); // get the next free cluster on the disk and mark it as used. |
|
- | 1125 | if(subdircluster) |
|
- | 1126 | { |
|
- | 1127 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
|
- | 1128 | file->SectorOfCurrCluster = 0; |
|
- | 1129 | do // loop over all clusters of current directory |
|
946 | { |
1130 | { |
- | 1131 | dir_sector = 0; // reset sector counter within a new cluster |
|
- | 1132 | do // loop over all sectors of a cluster or all sectors of the root directory |
|
- | 1133 | { |
|
- | 1134 | curr_sector = file->FirstSectorOfCurrCluster + dir_sector; // calculate sector number |
|
- | 1135 | file->SectorInCache = curr_sector; // upate the sector number of file cache. |
|
- | 1136 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
|
- | 1137 | { |
|
- | 1138 | Fat16_Deinit(); |
|
- | 1139 | return(retvalue); |
|
- | 1140 | } |
|
- | 1141 | dir = (DirEntry_t *)file->Cache; // set pointer to directory |
|
- | 1142 | // search all directory entries of a sector |
|
- | 1143 | for(dir_entry = 0; dir_entry < DIRENTRIES_PER_SECTOR; dir_entry++) |
|
- | 1144 | { // check if current direntry is available |
|
- | 1145 | if(((u8)dir[dir_entry].Name[0] == SLOT_EMPTY) || ((u8)dir[dir_entry].Name[0] == SLOT_DELETED)) |
|
- | 1146 | { // a free direntry was found |
|
- | 1147 | for(i = 0; i < 11; i++) dir[dir_entry].Name[i] = dirname[i]; // Set dir name |
|
- | 1148 | dir[dir_entry].Attribute = attrib; // Set the attribute of the new directoryentry. |
|
- | 1149 | dir[dir_entry].StartCluster = subdircluster; // copy the location of the first datacluster to the directoryentry. |
|
- | 1150 | dir[dir_entry].DateTime = FileDateTime(&SystemTime); // set date/time |
|
- | 1151 | dir[dir_entry].Size = 0; // the new createted file has no content yet. |
|
- | 1152 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to card |
|
- | 1153 | { |
|
- | 1154 | Fat16_Deinit(); |
|
- | 1155 | return(retvalue); |
|
- | 1156 | } |
|
- | 1157 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(subdircluster); // Calculate absolute sectorposition of first datacluster. |
|
- | 1158 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; // Start reading the file with the first sector of the first datacluster. |
|
- | 1159 | file->SectorOfCurrCluster = 0; // reset sector of cureen cluster |
|
- | 1160 | file->ByteOfCurrSector = 0; // reset the byte location within the current sector |
|
- | 1161 | file->Attribute = attrib; // set file attribute to dir attribute |
|
- | 1162 | file->Size = 0; // new file has no size |
|
- | 1163 | file->DirectorySector = curr_sector; |
|
- | 1164 | file->DirectoryIndex = dir_entry; |
|
- | 1165 | if((attrib & ATTR_SUBDIRECTORY) == ATTR_SUBDIRECTORY) // if a new directory was created then initilize the data area |
|
- | 1166 | { |
|
- | 1167 | ClearCurrCluster(file); // fill cluster with zeros |
|
- | 1168 | file->SectorInCache = file->FirstSectorOfFirstCluster; |
|
- | 1169 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache))// read in the sector. |
|
- | 1170 | { |
|
- | 1171 | Fat16_Deinit(); |
|
- | 1172 | return(retvalue); |
|
- | 1173 | } |
|
947 | u8 readpointer = 0; |
1174 | dir = (DirEntry_t *)file->Cache; |
948 | u8 writepointer = 0; |
- | |
949 | u8 attribute = 1; |
1175 | // create direntry "." to current dir |
950 | u8 i = 0; |
1176 | dir[0].Name[0] = 0x2E; |
- | 1177 | for(i = 1; i < 11; i++) dir[0].Name[i] = ' '; |
|
- | 1178 | dir[0].Attribute = ATTR_SUBDIRECTORY; |
|
- | 1179 | dir[0].StartCluster = subdircluster; |
|
- | 1180 | dir[0].DateTime = 0; |
|
- | 1181 | dir[0].Size = 0; |
|
- | 1182 | // create direntry ".." to the upper dir |
|
- | 1183 | dir[1].Name[0] = 0x2E; |
|
- | 1184 | dir[1].Name[1] = 0x2E; |
|
- | 1185 | for(i = 2; i < 11; i++) dir[1].Name[i] = ' '; |
|
- | 1186 | dir[1].Attribute = ATTR_SUBDIRECTORY; |
|
- | 1187 | dir[1].StartCluster = dircluster; |
|
951 | 1188 | dir[1].DateTime = 0; |
|
- | 1189 | dir[1].Size = 0; |
|
- | 1190 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// read in the sector. |
|
- | 1191 | { |
|
- | 1192 | Fat16_Deinit(); |
|
- | 1193 | return(retvalue); |
|
- | 1194 | } |
|
- | 1195 | } |
|
- | 1196 | retvalue = 1; |
|
- | 1197 | dir_entry = DIRENTRIES_PER_SECTOR; // stop for-loop |
|
- | 1198 | } |
|
- | 1199 | } |
|
- | 1200 | dir_sector++; // search next sector |
|
- | 1201 | // stop if we reached the end of the cluster or the end of the root dir |
|
- | 1202 | }while((dir_sector < max_dir_sector) && (!retvalue)); |
|
- | 1203 | ||
- | 1204 | // if we are seaching in the data area and the file not found in this cluster so take next cluster. |
|
- | 1205 | if(!retvalue && ( Partition.FirstDataSector <= file->FirstSectorOfCurrCluster)) |
|
- | 1206 | { |
|
- | 1207 | end_of_directory_not_reached = GetNextCluster(file); // updates File->FirstSectorOfCurrCluster |
|
- | 1208 | } |
|
- | 1209 | }while((end_of_directory_not_reached) && (!retvalue)); |
|
- | 1210 | // Perhaps we are at the end of the last cluster of a directory file an have no free direntry found. |
|
- | 1211 | // Then we would need to add a cluster to that file and create the new direntry there. |
|
- | 1212 | // This code is not implemented yet, because its occurs only if more that 32*32=1024 direntries are |
|
- | 1213 | // within a subdirectory of root. |
|
- | 1214 | } |
|
- | 1215 | return(retvalue); // return 1 if file has been created otherwise return 0. |
|
- | 1216 | } |
|
- | 1217 | ||
- | 1218 | /********************************************************************************************************************************************/ |
|
- | 1219 | /* Function: FileExist(const s8* filename, u8 attribfilter, u8 attribmask, File_t *file); */ |
|
- | 1220 | /* */ |
|
952 | 1221 | /* Description: This function looks for the specified file including its subdirectories beginning */ |
|
- | 1222 | /* in the rootdirectory of the drive. If the file is found the Filepointer properties are */ |
|
953 | while((writepointer<=10) && (fname[readpointer]!=0)) // the rootdirectoryentry is 8bytes for filename and 3bytes for fileattribute. |
1223 | /* updated. */ |
954 | { // the filename in the rootdirectory is in the format "TEST TXT" without the dot. |
1224 | /* */ |
955 | if(fname[readpointer]=='.') // seperating filename and attribute. |
- | |
956 | { |
- | |
957 | if(attribute) // is the filename "." or ".." ? |
- | |
958 | { |
- | |
959 | name[writepointer] = fname[readpointer]; |
1225 | /* Returnvalue: 1 if file is found else 0. */ |
960 | readpointer++; |
1226 | /********************************************************************************************************************************************/ |
- | 1227 | u8 FileExist(const s8* filename, const u8 attribfilter, const u8 attribmask, File_t *file) |
|
- | 1228 | { |
|
- | 1229 | s8* path = 0; |
|
- | 1230 | s8* subpath = 0; |
|
- | 1231 | u8 af, am, file_exist = 0; |
|
- | 1232 | s8 dirname[12]; // 8+3 + temination character |
|
961 | writepointer++; |
1233 | |
- | 1234 | // if incomming pointers are useless return immediatly |
|
962 | } |
1235 | if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0; |
963 | else |
- | |
964 | { |
- | |
965 | if(fname[(readpointer-1)] != '*') |
- | |
966 | { |
- | |
967 | for(i=writepointer;i<8;i++) |
- | |
968 | { |
- | |
969 | name[i] = ' '; |
- | |
970 | } |
1236 | |
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 | { |
1237 | // trace along the filepath |
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. |
1238 | path = (s8*)filename; // start a the beginning of the filename string |
994 | } |
1239 | file->DirectorySector = 0; // start at RootDirectory with file search |
995 | else |
- | |
996 | { |
- | |
997 | name[writepointer]=fname[readpointer]; |
- | |
998 | } |
- | |
999 | readpointer++; |
- | |
1000 | writepointer++; |
- | |
1001 | attribute = 0; |
- | |
1002 | } |
- | |
1003 | } |
- | |
1004 | } |
- | |
1005 | - | ||
1006 | //________________________________________________________________________________________________________________________________________ |
- | |
1007 | // Funtion: File * ReserveFilePointer_(void); |
- | |
1008 | // |
1240 | file->DirectoryIndex = 0; |
- | 1241 | // as long as the file was not found and the remaining path is not empty |
|
- | 1242 | while((*path != 0) && !file_exist) |
|
1009 | // Description: This function looks for a free filepointer and reserves it. |
1243 | { // separate dirname and subpath from filepath string |
1010 | // |
1244 | subpath = SeperateDirName(path, dirname); |
1011 | // |
1245 | if(subpath != NULL) |
Line 1012... | Line 1246... | ||
1012 | // Return: NULL = faild to reserve a filepointer |
1246 | { |
- | 1247 | if(*subpath == 0) |
|
- | 1248 | { // empty subpath indicates last element of dir chain |
|
- | 1249 | af = attribfilter; |
|
- | 1250 | am = attribmask; |
|
- | 1251 | } |
|
- | 1252 | else // it must be a subdirectory and no volume label |
|
- | 1253 | { |
|
- | 1254 | af = ATTR_SUBDIRECTORY; |
|
1013 | // otherwise filepointer |
1255 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; |
1014 | //________________________________________________________________________________________________________________________________________ |
1256 | } |
- | 1257 | if(!DirectoryEntryExist(dirname, af, am, file)) |
|
- | 1258 | { |
|
- | 1259 | return (file_exist); // subdirectory does not exist |
|
- | 1260 | } |
|
- | 1261 | else |
|
- | 1262 | { |
|
- | 1263 | if (*subpath == 0) |
|
- | 1264 | { |
|
- | 1265 | file_exist = 1; // last element of path chain was found with the given attribute filter |
|
- | 1266 | } |
|
- | 1267 | } |
|
- | 1268 | } |
|
1015 | 1269 | else // error seperating the subpath |
|
- | 1270 | { |
|
- | 1271 | return file_exist; // bad subdir format |
|
- | 1272 | } |
|
- | 1273 | path = subpath; |
|
- | 1274 | subpath = 0; |
|
- | 1275 | } |
|
- | 1276 | return (file_exist); |
|
- | 1277 | } |
|
- | 1278 | ||
- | 1279 | ||
- | 1280 | /********************************************************************************************************************************************/ |
|
- | 1281 | /* Function: FileCreate(const s8* filename, u8 attrib, File_t *file); */ |
|
- | 1282 | /* */ |
|
- | 1283 | /* Description: This function looks for the specified file including its subdirectories beginning */ |
|
- | 1284 | /* in the rootdirectory of the partition. If the file is found the Filepointer properties are */ |
|
- | 1285 | /* updated. If file or its subdirectories are not found they will be created */ |
|
- | 1286 | /* */ |
|
- | 1287 | /* Returnvalue: 1 if file was created else 0. */ |
|
- | 1288 | /********************************************************************************************************************************************/ |
|
- | 1289 | u8 FileCreate(const s8* filename, const u8 attrib, File_t *file) |
|
- | 1290 | { |
|
- | 1291 | s8 *path = 0; |
|
- | 1292 | s8 *subpath = 0; |
|
- | 1293 | u8 af, am, file_created = 0; |
|
- | 1294 | s8 dirname[12]; |
|
- | 1295 | ||
- | 1296 | // if incomming pointers are useless return immediatly |
|
- | 1297 | if ((filename == NULL) || (file == NULL) || (!Partition.IsValid)) return 0; |
|
1016 | File * ReserveFilePointer(void) |
1298 | |
- | 1299 | // trace along the filepath |
|
- | 1300 | path = (s8*)filename; // start a the beginning of the filename string |
|
1017 | { |
1301 | file->DirectorySector = 0; // start at RootDirectory with file search |
1018 | File *file; |
1302 | file->DirectoryIndex = 0; |
1019 | file = NULL; |
1303 | // as long as the file was not created and the remaining file path is not empty |
1020 | u8 temp; |
- | |
- | 1304 | while((*path != 0) && !file_created) |
|
1021 | 1305 | { // separate dirname and subpath from filepath string |
|
- | 1306 | subpath = SeperateDirName(path, dirname); |
|
- | 1307 | if(subpath != NULL) |
|
- | 1308 | { |
|
- | 1309 | if(*subpath == 0) |
|
- | 1310 | { // empty subpath indicates last element of dir chain |
|
- | 1311 | af = ATTR_NONE; |
|
- | 1312 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; // any file that is no subdir or volume label |
|
- | 1313 | } |
|
1022 | for(temp = 0;temp<__MAX_FILES_USED;temp++) |
1314 | else // it must be a subdirectory and no volume label |
1023 | { |
1315 | { |
1024 | if(FilePointer[temp].state == _UNUSED) // free filepointer found? |
1316 | af = ATTR_SUBDIRECTORY; |
- | 1317 | am = ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL; |
|
- | 1318 | } |
|
1025 | { |
1319 | if(!DirectoryEntryExist(dirname, af, am, file)) // if subdir or file is not existent |
1026 | file = &FilePointer[temp]; |
1320 | { // try to create subdir or file |
- | 1321 | if(*subpath == 0) af = attrib; // if last element in dir chain take the given attribute |
|
- | 1322 | if(!CreateDirectoryEntry(dirname, af, file)) |
|
- | 1323 | { // could not be created |
|
- | 1324 | return(file_created); |
|
- | 1325 | } |
|
- | 1326 | else if (*subpath == 0) file_created = 1; // last element of path chain was created |
|
- | 1327 | } |
|
- | 1328 | } |
|
- | 1329 | else // error seperating the subpath |
|
- | 1330 | { |
|
- | 1331 | return file_created; // bad subdir format |
|
- | 1332 | } |
|
- | 1333 | path = subpath; |
|
- | 1334 | subpath = 0; |
|
- | 1335 | } |
|
- | 1336 | return (file_created); |
|
- | 1337 | } |
|
- | 1338 | ||
- | 1339 | ||
- | 1340 | /********************************************************************************************************************************************/ |
|
- | 1341 | /* Function: File_t * fopen_(s8* filename, s8 mode); */ |
|
- | 1342 | /* */ |
|
- | 1343 | /* Description: This function looks for the specified file in the rootdirectory of the drive. If the file is found the number of the */ |
|
- | 1344 | /* corrosponding filepointer is returned. Only modes 'r' (reading) and 'a' append are implemented yet. */ |
|
- | 1345 | /* */ |
|
- | 1346 | /* Returnvalue: The filepointer to the file or 0 if faild. */ |
|
- | 1347 | /********************************************************************************************************************************************/ |
|
- | 1348 | File_t * fopen_(const s8 *filename, const s8 mode) |
|
- | 1349 | { |
|
- | 1350 | File_t *file = 0; |
|
- | 1351 | ||
- | 1352 | if((!Partition.IsValid) || (filename == 0)) return(file); |
|
- | 1353 | ||
- | 1354 | // Look for an unused filepointer in the file pointer list? |
|
- | 1355 | file = LockFilePointer(); |
|
- | 1356 | // if no unused file pointer was found return 0 |
|
- | 1357 | if(file == NULL) return(file); |
|
- | 1358 | ||
- | 1359 | // now we have found a free filepointer and claimed it |
|
- | 1360 | // so let initiate its property values |
|
- | 1361 | file->FirstSectorOfFirstCluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
|
- | 1362 | file->FirstSectorOfCurrCluster = 0; // Pointer to the cluster which is edited at the moment. |
|
- | 1363 | file->SectorOfCurrCluster = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
|
- | 1364 | file->ByteOfCurrSector = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
|
- | 1365 | file->Mode = mode; // mode of fileoperation (read,write) |
|
- | 1366 | file->Size = 0; // the size of the opened file in bytes. |
|
- | 1367 | file->Position = 0; // pointer to a byte within the file 0 < fileposition < filesize |
|
- | 1368 | file->SectorInCache = 0; // the last sector read, wich is still in the sectorbuffer. |
|
- | 1369 | file->DirectorySector = 0; // the sectorposition where the directoryentry has been made. |
|
- | 1370 | file->DirectoryIndex = 0; // the index to the directoryentry within the specified sector. |
|
- | 1371 | file->Attribute = 0; // the attribute of the file opened. |
|
- | 1372 | ||
- | 1373 | // check if a real file (no directory) to the given filename exist |
|
- | 1374 | if(FileExist(filename, ATTR_NONE, ATTR_SUBDIRECTORY|ATTR_VOLUMELABEL, file)) |
|
- | 1375 | { // file exist |
|
- | 1376 | switch(mode) // check mode |
|
- | 1377 | { |
|
- | 1378 | case 'a': // if mode is: append to file |
|
- | 1379 | if((file->Attribute & ATTR_READONLY) == ATTR_READONLY) |
|
- | 1380 | { // file is marked as readonly --> do not open this file |
|
- | 1381 | fclose_(file); |
|
- | 1382 | file = NULL; |
|
- | 1383 | } |
|
- | 1384 | else |
|
- | 1385 | { // file is not marked as read only --> goto end of file |
|
- | 1386 | fseek_(file, 0, SEEK_END); // point to the end of the file |
|
1027 | FilePointer[temp].state = _USED; // mark as used. |
1387 | } |
1028 | FilePointer[temp].mode = 0; // type of access (read/write) not defined yet. |
1388 | break; |
1029 | FilePointer[temp].start_cluster = 0; // Sectorpointer to the first sector of the first datacluster of the file. |
1389 | case 'w': // if mode is: write to file |
- | 1390 | if((file->Attribute & ATTR_READONLY) == ATTR_READONLY) |
|
1030 | FilePointer[temp].cluster_pointer = 0; // Pointer to the cluster which is edited at the moment. |
1391 | { // file is marked as readonly --> do not open this file |
- | 1392 | fclose_(file); |
|
- | 1393 | file = NULL; |
|
- | 1394 | } |
|
- | 1395 | else |
|
- | 1396 | { // file is not marked as read only --> goto start of file |
|
- | 1397 | // free all clusters of that file |
|
- | 1398 | DeleteClusterChain(SectorToFat16Cluster(file->FirstSectorOfFirstCluster)); |
|
- | 1399 | // mar an empy cluster as the last one and store the corresponding sector |
|
- | 1400 | file->FirstSectorOfFirstCluster = Fat16ClusterToSector(FindNextFreeCluster(file)); |
|
- | 1401 | file->FirstSectorOfCurrCluster = file->FirstSectorOfFirstCluster; |
|
- | 1402 | file->SectorOfCurrCluster = 0; |
|
- | 1403 | file->ByteOfCurrSector = 0; |
|
1031 | FilePointer[temp].sector_index = 0; // The sector which is edited at the moment (cluster_pointer + sector_index). |
1404 | file->Size = 0; |
- | 1405 | file->Position = 0; |
|
1032 | FilePointer[temp].byte_index = 0; // The bytelocation within the current sector (cluster_pointer + sector_index + byte_index). |
1406 | fseek_(file, 0, SEEK_SET); |
- | 1407 | } |
|
- | 1408 | break; |
|
- | 1409 | case 'r': // if mode is: read from file |
|
- | 1410 | // goto end of file |
|
1033 | FilePointer[temp].filesize = 0; // the size of the opend file in bytes. |
1411 | fseek_(file, 0, SEEK_SET); |
Line 1034... | Line -... | ||
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. |
1412 | break; |
1036 | FilePointer[temp].directory_sector = 0; // the sectorposition where the directoryentry has been made. |
1413 | default: // other modes are not supported |
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 | } |
1414 | fclose_(file); |
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 |
1415 | file = NULL; |
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 | { |
1416 | break; |
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 | } |
1417 | } |
1067 | } |
1418 | return(file); |
1068 | - | ||
1069 | 1419 | } |
|
1070 | //________________________________________________________________________________________________________________________________________ |
1420 | else // file does not exist |
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 | //________________________________________________________________________________________________________________________________________ |
1421 | { |
1078 | - | ||
1079 | void DeleteDirectoryEntry(Find *item) |
- | |
1080 | { |
- | |
1081 | u8 buffer[512]; |
- | |
1082 | - | ||
Line 1083... | Line 1422... | ||
1083 | 1422 | switch(mode) // check mode |
|
- | 1423 | { |
|
- | 1424 | case 'a': |
|
1084 | SDC_GetSector((u32) item->cluster_pointer,buffer); // Read the Rootdirectory. |
1425 | case 'w': // if mode is write or append |
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; |
1426 | // try to create the 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 | 1427 | if(!FileCreate(filename, ATTR_ARCHIVE, file)) |
|
1127 | // -Initialise new cluster to zero-------------------------------------------------------- |
1428 | { // if it could not be created |
1128 | for(i=0;i<512;i++) |
- | |
1129 | { |
- | |
1130 | file.buffer[i] = 0; // initialise buffer to zero |
1429 | fclose_(file); |
1131 | } |
1430 | file = NULL; |
1132 | for(sector_offset=0;sector_offset<SectorsPerCluster;sector_offset++) // initialise all sectors of new cluster with buffer. |
1431 | } |
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] = '.'; |
1432 | break; |
1139 | DirectoryEntry[0].attribute = _DIRECTORY; |
1433 | case 'r': // else opened for 'r' |
1140 | DirectoryEntry[0].startcluster = cluster; |
- | |
1141 | // -Create directoryentry "." ------------------------------------------------------------- |
1434 | default: // of unsupported mode |
1142 | DirectoryEntry[1].name[0] = '.'; |
- | |
1143 | DirectoryEntry[1].name[1] = '.'; |
- | |
1144 | DirectoryEntry[1].attribute = _DIRECTORY; |
1435 | fclose_(file); |
- | 1436 | file = NULL; |
|
1145 | if(CWD == RootDirectory) |
1437 | break; |
1146 | { |
1438 | } |
1147 | DirectoryEntry[1].startcluster = 0; |
1439 | return(file); |
1148 | } |
1440 | } |
1149 | else |
1441 | // we should never come to this point |
- | 1442 | fclose_(file); |
|
- | 1443 | file = NULL; |
|
- | 1444 | return(file); |
|
- | 1445 | } |
|
- | 1446 | ||
- | 1447 | /****************************************************************************************************************************************************/ |
|
- | 1448 | /* Function: fflush_(File *); */ |
|
- | 1449 | /* */ |
|
1150 | { |
1450 | /* Description: This function writes the data already in the buffer but not yet written to the file. */ |
- | 1451 | /* */ |
|
- | 1452 | /* Returnvalue: 0 on success EOF on error */ |
|
- | 1453 | /****************************************************************************************************************************************************/ |
|
1151 | cluster_temp = (CWD - FirstDataCluster); |
1454 | s16 fflush_(File_t *file) |
- | 1455 | { |
|
Line 1152... | Line 1456... | ||
1152 | cluster_temp /= SectorsPerCluster; |
1456 | DirEntry_t *dir; |
1153 | cluster_temp -= 2; |
1457 | |
1154 | DirectoryEntry[1].startcluster = (u16) cluster_temp; |
1458 | if((file == NULL) || (!Partition.IsValid)) return (EOF); |
Line 1155... | Line -... | ||
1155 | } |
- | |
1156 | SDC_PutSector((u32) file.start_cluster,file.buffer); // save the initialised sector to the card. |
1459 | |
1157 | // -create directoryentry within the cwd -------------------------------------------------- |
1460 | switch(file->Mode) |
1158 | sector_offset = 0; |
- | |
- | 1461 | { |
|
1159 | cnt_entries_searched = 0; |
1462 | case 'a': |
1160 | do |
- | |
1161 | { // search the next 16 rootentries in this sector of the roordirectory. |
1463 | case 'w': |
- | 1464 | if(file->ByteOfCurrSector > 0) // has data been added to the file? |
|
- | 1465 | { |
|
1162 | index=0; |
1466 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache))// save the data still in the buffer |
1163 | SDC_GetSector((u32)(CWD + sector_offset),file.buffer); // Read the actual directory. |
- | |
1164 | DirectoryEntry = (struct DirEntry *)file.buffer; |
1467 | { |
1165 | while((index<16) && (!retvalue)) |
1468 | Fat16_Deinit(); |
1166 | { |
1469 | return(EOF); |
- | 1470 | } |
|
1167 | if((DirectoryEntry[index].attribute == 0) || (DirectoryEntry[index].attribute == 0xE5)) // empty directory entry found |
1471 | } |
1168 | { |
1472 | file->SectorInCache = file->DirectorySector; |
1169 | for(i=0;i<11;i++) DirectoryEntry[index].name[i] = name[i]; // Kopie the filename and the file extension to the directoryentry. |
1473 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) // read the directory entry for this file. |
- | 1474 | { |
|
- | 1475 | Fat16_Deinit(); |
|
- | 1476 | return(EOF); |
|
- | 1477 | } |
|
- | 1478 | ||
- | 1479 | dir = (DirEntry_t *)file->Cache; |
|
- | 1480 | dir[file->DirectoryIndex].Size = file->Size; // update file size |
|
- | 1481 | dir[file->DirectoryIndex].DateTime = FileDateTime(&SystemTime); // update date time |
|
1170 | DirectoryEntry[index].attribute = _DIRECTORY; // Set the fileattribute to archive to reserve the directoryentry. |
1482 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) // write back to sd-card |
- | 1483 | { |
|
- | 1484 | Fat16_Deinit(); |
|
1171 | DirectoryEntry[index].startcluster = cluster; // copy the location of the first datacluster to the directoryentry. |
1485 | return(EOF); |
1172 | DirectoryEntry[index].size = 0; // the new createted file has no content yet. |
1486 | } |
- | 1487 | break; |
|
1173 | file.directory_sector = (u32) (CWD + sector_offset); |
1488 | case 'r': |
Line 1174... | Line 1489... | ||
1174 | file.directory_index = (u8) index; |
1489 | default: |
1175 | retvalue = 1; |
1490 | return(EOF); |
1176 | SDC_PutSector((u32)(CWD + sector_offset),file.buffer); |
- | |
1177 | } |
1491 | break; |
1178 | index++; |
1492 | |
1179 | cnt_entries_searched++; |
1493 | } |
1180 | } |
- | |
1181 | if(!retvalue) // file not found in this sector so take next sector. |
- | |
1182 | { |
1494 | return(0); |
1183 | index = 0; |
1495 | } |
1184 | sector_offset++; |
1496 | |
1185 | } |
1497 | /****************************************************************************************************************************************/ |
1186 | } |
1498 | /* Function: fclose_(File *file); */ |
1187 | while((cnt_entries_searched< PossibleRootEntries) && (!retvalue)); |
1499 | /* */ |
1188 | 1500 | /* Description: This function closes the open file by writing the remaining data */ |
|
- | 1501 | /* from the buffer to the device and entering the filesize in the directory entry. */ |
|
- | 1502 | /* */ |
|
- | 1503 | /* Returnvalue: 0 on success EOF on error */ |
|
- | 1504 | /****************************************************************************************************************************************/ |
|
- | 1505 | s16 fclose_(File_t *file) |
|
- | 1506 | { |
|
- | 1507 | s16 returnvalue = EOF; |
|
- | 1508 | ||
- | 1509 | if(file == NULL) return(returnvalue); |
|
1189 | } |
1510 | returnvalue = fflush_(file); |
- | 1511 | UnlockFilePointer(file); |
|
- | 1512 | return(returnvalue); |
|
- | 1513 | } |
|
- | 1514 | ||
- | 1515 | /********************************************************************************************************************************************/ |
|
1190 | return(retvalue); // return 1 if file has been created otherwise return 0. |
1516 | /* Function: fgetc_(File *file); */ |
- | 1517 | /* */ |
|
- | 1518 | /* Description: This function reads and returns one character from the specified file. Is the end of the actual sector reached the */ |
|
- | 1519 | /* next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT. */ |
|
1191 | } |
1520 | /* */ |
- | 1521 | /* Returnvalue: The function returns the character read from the specified memorylocation as u8 casted to s16 or EOF. */ |
|
- | 1522 | /********************************************************************************************************************************************/ |
|
1192 | 1523 | s16 fgetc_(File_t *file) |
|
1193 | 1524 | { |
|
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; |
1525 | s16 c = EOF; |
1205 | u16 end_of_directory_not_reached = 0; // the directory has been read completely without a result. |
1526 | u32 curr_sector; |
1206 | u8 i = 0; |
- | |
1207 | u16 cluster_temp = 0; |
- | |
1208 | s8 name[11] = " "; |
- | |
1209 | File file; |
- | |
1210 | - | ||
1211 | SeperateFileName(fname,name); |
- | |
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 | - | ||
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. |
- | |
1219 | index=0; |
- | |
1220 | SDC_GetSector((u32) file.cluster_pointer,file.buffer); // Read the Rootdirectory. |
- | |
1221 | DirectoryEntry = (struct DirEntry *)file.buffer; |
- | |
1222 | - | ||
1223 | while((!cluster_temp)&&(index<16)) |
- | |
1224 | { |
- | |
1225 | i=0; |
- | |
1226 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
- | |
1227 | { |
- | |
1228 | while((i<=10)&&(DirectoryEntry[index].name[i] == name[i])) |
- | |
1229 | { |
- | |
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 | } |
1527 | |
1238 | index++; |
1528 | if( (!Partition.IsValid) || (file == NULL)) return(c); |
Line 1239... | Line -... | ||
1239 | } |
- | |
1240 | if(!cluster_temp) // file not found in this sector so take next sector. |
1529 | // if the end of the file is not reached, get the next character. |
- | 1530 | if((0 < file->Size) && ((file->Position+1) < file->Size) ) |
|
1241 | { |
1531 | { |
1242 | end_of_directory_not_reached = GetNextCluster(&file); |
- | |
1243 | } |
1532 | curr_sector = file->FirstSectorOfCurrCluster; // calculate the sector of the next character to be read. |
1244 | } |
- | |
1245 | while((end_of_directory_not_reached) && (!cluster_temp)); |
- | |
- | 1533 | curr_sector += file->SectorOfCurrCluster; |
|
- | 1534 | ||
1246 | return(cluster_temp); |
1535 | if(file->SectorInCache != curr_sector) |
1247 | } |
1536 | { |
1248 | - | ||
1249 | 1537 | file->SectorInCache = curr_sector; |
|
1250 | 1538 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache,file->Cache)) |
|
1251 | //________________________________________________________________________________________________________________________________________ |
1539 | { |
1252 | // Funtion: u8 mkdir_(u8 *) |
- | |
Line 1253... | Line 1540... | ||
1253 | // |
1540 | Fat16_Deinit(); |
Line 1254... | Line 1541... | ||
1254 | // Description: This function checks if the directory to be created already exists. If not the directory will be created. |
1541 | return(c); |
1255 | // |
- | |
- | 1542 | } |
|
1256 | // |
1543 | } |
1257 | // returnvalue: 1 if the directory could be created. |
1544 | c = (s16) file->Cache[file->ByteOfCurrSector]; |
1258 | //________________________________________________________________________________________________________________________________________ |
1545 | file->Position++; // increment file position |
1259 | - | ||
1260 | u8 mkdir_(s8 *fname) |
- | |
1261 | { |
1546 | file->ByteOfCurrSector++; // goto next byte in sector |
1262 | u8 retvalue = 0; |
- | |
1263 | - | ||
1264 | retvalue = SeekSubDirectory(fname); // check wether the specified directory already exists. |
1547 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) // if end of sector |
1265 | 1548 | { |
|
- | 1549 | file->ByteOfCurrSector = 0; // reset byte location |
|
- | 1550 | file->SectorOfCurrCluster++; // next sector |
|
- | 1551 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster) // if end of cluster is reached, the next datacluster has to be searched in the FAT. |
|
1266 | if(!retvalue) |
1552 | { |
- | 1553 | ||
- | 1554 | if(GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
|
- | 1555 | { |
|
1267 | { |
1556 | file->SectorOfCurrCluster = 0; // start reading new cluster at first sector of the cluster. |
1268 | CreateSubDirectory(fname); // if directory doesn't exist, create it. |
1557 | } |
- | 1558 | else // the last cluster was allready reached |
|
1269 | retvalue = 1; |
1559 | { |
Line 1270... | Line -... | ||
1270 | } |
- | |
1271 | else |
- | |
1272 | { |
- | |
1273 | retvalue = 0; |
- | |
1274 | } |
- | |
1275 | - | ||
1276 | return(retvalue); |
- | |
1277 | } |
1560 | file->SectorOfCurrCluster--; // jump back to the last sector in the last cluster |
1278 | - | ||
1279 | - | ||
1280 | //________________________________________________________________________________________________________________________________________ |
- | |
1281 | // Funtion: u8 chdir_(u8 *) |
- | |
1282 | // |
- | |
1283 | // Description: This function changes the CWD to the directory specified. |
- | |
1284 | // |
- | |
1285 | // |
1561 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set ByteOfCurrSector one byte over sector end |
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 | 1562 | } |
|
1294 | u32 ultemp = 0; |
1563 | } |
1295 | - | ||
1296 | SeperateFileName(fname,name); |
1564 | } |
1297 | - | ||
1298 | ultemp = (u32)SeekSubDirectory(name); |
1565 | } |
1299 | if(ultemp >= 2) |
- | |
1300 | { |
1566 | return(c); |
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; |
1567 | } |
- | 1568 | ||
- | 1569 | /********************************************************************************************************************************************/ |
|
1304 | CWD = ultemp; |
1570 | /* Function: fputc_( const s8 c, File *file); */ |
1305 | retvalue = 1; |
1571 | /* */ |
- | 1572 | /* Description: This function writes a byte to the specified file and takes care of writing the necessary FAT- Entries. */ |
|
- | 1573 | /* next sector of the cluster is read. If the last sector of the cluster read the next cluster will be searched in FAT. */ |
|
1306 | } |
1574 | /* */ |
1307 | else |
1575 | /* Returnvalue: The function returns the character written to the stream or EOF on error. */ |
- | 1576 | /********************************************************************************************************************************************/ |
|
- | 1577 | s16 fputc_(const s8 c, File_t *file) |
|
1308 | { |
1578 | { |
- | 1579 | u32 curr_sector = 0; |
|
- | 1580 | ||
1309 | CWD = RootDirectory; |
1581 | if((!Partition.IsValid) || (file == NULL)) return(EOF); |
1310 | retvalue = 1; |
1582 | |
1311 | } |
1583 | // If file position equals to file size, then the end of file has reached. |
1312 | - | ||
1313 | return(retvalue); |
1584 | // In this chase it has to be checked that the ByteOfCurrSector is BYTES_PER_SECTOR |
1314 | } |
1585 | // and a new cluster should be appended. |
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; |
1586 | if((file->Position >= file->Size) && (file->ByteOfCurrSector >= BYTES_PER_SECTOR)) |
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; |
1587 | { |
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; |
1588 | if(!AppendCluster(file)) return(EOF); |
1344 | - | ||
1345 | while((!retvalue)&&(index<16)) |
1589 | } |
1346 | { |
- | |
1347 | i=0; |
- | |
1348 | if(DirectoryEntry[index].name[0] != 0xe5) // ignore deleted items. |
1590 | |
1349 | { |
1591 | curr_sector = file->FirstSectorOfCurrCluster; |
Line 1350... | Line 1592... | ||
1350 | while((i<=10)&&((DirectoryEntry[index].name[i] == item->searchstring[i]) || (item->searchstring[i]=='*') || item->searchstring[i]=='?')) |
1592 | curr_sector += file->SectorOfCurrCluster; |
- | 1593 | if(file->SectorInCache != curr_sector) |
|
- | 1594 | { |
|
- | 1595 | file->SectorInCache = curr_sector; |
|
- | 1596 | if(SD_SUCCESS != SDC_GetSector(file->SectorInCache, file->Cache)) |
|
1351 | { |
1597 | { |
1352 | i++; |
- | |
1353 | } |
1598 | Fat16_Deinit(); |
- | 1599 | return(EOF); |
|
- | 1600 | } |
|
1354 | } |
1601 | } |
1355 | 1602 | ||
1356 | if((i==11) && (DirectoryEntry[index].attribute & item->attribute)) |
1603 | file->Cache[file->ByteOfCurrSector] = (u8)c; // write databyte into the buffer. The byte will be written to the device at once |
- | 1604 | if(file->Size == file->Position) file->Size++; // a character has been written to the file so the size is incremented only when the character has been added at the end of the file. |
|
- | 1605 | file->Position++; // the actual positon within the file. |
|
- | 1606 | file->ByteOfCurrSector++; // goto next byte in sector |
|
Line 1357... | Line 1607... | ||
1357 | { |
1607 | if(file->ByteOfCurrSector >= BYTES_PER_SECTOR) // if the end of this sector is reached yet |
1358 | for(readpointer=0;readpointer<=10;readpointer++) |
- | |
1359 | { |
- | |
1360 | if(DirectoryEntry[index].name[readpointer] != ' ') |
- | |
Line 1361... | Line -... | ||
1361 | { |
- | |
1362 | item->name[writepointer] = DirectoryEntry[index].name[readpointer]; // copy the name of the item found to the find_structure. |
- | |
1363 | writepointer++; |
1608 | { // save the sector to the sd-card |
1364 | } |
- | |
1365 | else if((readpointer==7) && (DirectoryEntry[index].attribute == _FILE)) // if the item found is a file |
- | |
1366 | { |
- | |
1367 | if(DirectoryEntry[index].name[readpointer] != ' ') |
- | |
Line 1368... | Line -... | ||
1368 | { |
- | |
1369 | item->name[writepointer] = DirectoryEntry[index].name[readpointer]; // copy the name of the item found to the find_structure. |
- | |
1370 | writepointer++; |
1609 | if(SD_SUCCESS != SDC_PutSector(file->SectorInCache, file->Cache)) |
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 | } |
1610 | { |
1393 | 1611 | Fat16_Deinit(); |
|
- | 1612 | return(EOF); |
|
1394 | 1613 | } |
|
- | 1614 | file->ByteOfCurrSector = 0; // reset byte location |
|
- | 1615 | file->SectorOfCurrCluster++; // next sector |
|
1395 | //________________________________________________________________________________________________________________________________________ |
1616 | if(file->SectorOfCurrCluster >= Partition.SectorsPerCluster)// if end of cluster is reached, the next datacluster has to be searched in the FAT. |
1396 | // Funtion: u8 findfirst(s8 *fname, Find *) |
1617 | { |
- | 1618 | if(!GetNextCluster(file)) // Sets the clusterpointer of the file to the next datacluster. |
|
- | 1619 | { // if current cluster was the last cluster of the file |
|
- | 1620 | if(!AppendCluster(file)) // append a new and free cluster at the end of the file. |
|
- | 1621 | { |
|
- | 1622 | file->SectorOfCurrCluster--; // jump back to last sector of last cluster |
|
- | 1623 | file->ByteOfCurrSector = BYTES_PER_SECTOR; // set byte location to 1 byte over sector len |
|
- | 1624 | return(EOF); |
|
1397 | // |
1625 | } |
1398 | // Description: finds the first item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered. |
1626 | } |
- | 1627 | else // next cluster |
|
- | 1628 | { |
|
1399 | // |
1629 | file->SectorOfCurrCluster = 0; // start reading new cluster at first sector of the cluster. |
Line -... | Line 1630... | ||
- | 1630 | } |
|
- | 1631 | } |
|
- | 1632 | } |
|
- | 1633 | return(0); |
|
- | 1634 | } |
|
- | 1635 | ||
- | 1636 | ||
- | 1637 | /****************************************************************************************************************************************/ |
|
- | 1638 | /* Function: fread_(void *buffer, s32 size, s32 count, File *File); */ |
|
- | 1639 | /* */ |
|
- | 1640 | /* Description: This function reads count objects of the specified size */ |
|
- | 1641 | /* from the actual position of the file to the specified buffer. */ |
|
- | 1642 | /* */ |
|
- | 1643 | /* Returnvalue: The function returns the number of objects (not bytes) read from the file. */ |
|
- | 1644 | /****************************************************************************************************************************************/ |
|
Line 1400... | Line -... | ||
1400 | // Returnvalue: If an item was found this function returns '1' else '0'. |
- | |
1401 | //________________________________________________________________________________________________________________________________________ |
- | |
1402 | - | ||
1403 | u8 findfirst_(s8 *fname, Find *item, u8 attribute) |
1645 | u32 fread_(void *buffer, u32 size, u32 count, File_t *file) |
1404 | { |
- | |
1405 | u8 retvalue = 0; |
- | |
1406 | u8 i = 0; |
- | |
Line 1407... | Line 1646... | ||
1407 | 1646 | { |
|
1408 | for(i=0;i<=11;i++) |
- | |
1409 | { |
- | |
1410 | item->searchstring[i] = '*'; // initialise the searchstring with wildcards. |
- | |
Line 1411... | Line -... | ||
1411 | item->name[i] = 0; |
- | |
1412 | } |
- | |
1413 | 1647 | u32 object_cnt = 0; // count the number of objects read from the file. |
|
1414 | SeperateFileName(fname,item->searchstring); |
1648 | u32 object_size = 0; // count the number of bytes read from the actual object. |
1415 | 1649 | u8 *pbuff = 0; // a pointer to the actual bufferposition. |
|
1416 | item->cluster_pointer = CWD; // findfirst_ starts at the beginning of the cwd. |
1650 | u8 success = 1; // no error occured during read operation to the file. |
1417 | item->directory_index = 0; |
- | |
1418 | item->attribute = attribute; |
- | |
1419 | 1651 | s16 c; |
|
- | 1652 | ||
1420 | retvalue = FindItem(item); |
1653 | if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0); |
- | 1654 | ||
- | 1655 | pbuff = (u8 *) buffer; // cast the void pointer to an u8 * |
|
- | 1656 | ||
- | 1657 | while((object_cnt < count) && success) |
|
1421 | 1658 | { |
|
1422 | return(retvalue); |
1659 | object_size = size; |
1423 | } |
- | |
1424 | 1660 | while((size > 0) && success) |
|
1425 | 1661 | { |
|
1426 | //________________________________________________________________________________________________________________________________________ |
1662 | c = fgetc_(file); |
1427 | // Funtion: u8 findnext(Find *) |
1663 | if(c != EOF) |
1428 | // |
1664 | { |
1429 | // Description: finds the first item (file or directory) within common working directory (cwd). Wildcards '*' or '?' will be considered. |
1665 | *pbuff = (u8)c; // read a byte from the buffer to the opened file. |
1430 | // |
1666 | pbuff++; |
1431 | // Returnvalue: If an item was found this function returns '1' else '0'. |
1667 | size--; |
Line 1432... | Line 1668... | ||
1432 | //________________________________________________________________________________________________________________________________________ |
1668 | } |
- | 1669 | else // error or end of file reached |
|
1433 | 1670 | { |
|
1434 | u8 findnext_(Find *item) |
- | |
1435 | { |
1671 | success = 0; |
1436 | u8 retvalue = 0; |
- | |
1437 | u8 i = 0; |
1672 | } |
- | 1673 | } |
|
1438 | 1674 | if(success) object_cnt++; |
|
1439 | for(i=0;i<=11;i++) // copy the name of the searched item to the findstructure. |
- | |
1440 | { |
1675 | } |
1441 | item->name[i] = 0; |
1676 | return(object_cnt); // return the number of objects succesfully read from the file |
1442 | } |
1677 | } |
1443 | 1678 | ||
Line 1444... | Line 1679... | ||
1444 | retvalue = FindItem(item); // search the item. |
1679 | |
Line 1445... | Line 1680... | ||
1445 | 1680 | /****************************************************************************************************************************************/ |
|
1446 | return(retvalue); |
1681 | /* Function: fwrite_(void *buffer, s32 size, s32 count, File *file); */ |
1447 | } |
- | |
1448 | - | ||
1449 | - | ||
1450 | - | ||
1451 | //________________________________________________________________________________________________________________________________________ |
- | |
1452 | // Funtion: u8 fdelete(s8 *fname) |
1682 | /* */ |
1453 | // |
- | |
1454 | // Description: Deletes the file specified by fname. |
- | |
1455 | // |
- | |
1456 | // Returnvalue: |
- | |
1457 | //________________________________________________________________________________________________________________________________________ |
1683 | /* Description: This function writes count objects of the specified size */ |
1458 | - | ||
1459 | u8 fdelete_(s8 *fname) |
- | |
1460 | { |
1684 | /* from the buffer pointer to the actual position in the file. */ |
1461 | u8 retvalue = 0; |
- | |
1462 | Find item; |
1685 | /* */ |
1463 | 1686 | /* Returnvalue: The function returns the number of objects (not bytes) read from the file. */ |
|
Line -... | Line 1687... | ||
- | 1687 | /****************************************************************************************************************************************/ |
|
- | 1688 | u32 fwrite_(void *buffer, u32 size, u32 count, File_t *file) |
|
- | 1689 | { |
|
- | 1690 | u32 object_cnt = 0; // count the number of objects written to the file. |
|
- | 1691 | u32 object_size = 0; // count the number of bytes written from the actual object. |
|
- | 1692 | u8 *pbuff = 0; // a pointer to the actual bufferposition. |
|
- | 1693 | u8 success = 1; // no error occured during write operation to the file. |
|
- | 1694 | s16 c; |
|
- | 1695 | ||
- | 1696 | if((!Partition.IsValid) || (file == NULL) || (buffer == NULL)) return(0); |
|
- | 1697 | ||
- | 1698 | pbuff = (u8 *) buffer; // cast the void pointer to an u8 * |
|
- | 1699 | ||
- | 1700 | while((object_cnt < count) && success) |
|
- | 1701 | { |
|
- | 1702 | object_size = size; |
|
- | 1703 | while((size > 0) && success) |
|
- | 1704 | { |
|
- | 1705 | c = fputc_(*pbuff, file); // write a byte from the buffer to the opened file. |
|
- | 1706 | if(c != EOF) |
|
- | 1707 | { |
|
- | 1708 | pbuff++; |
|
- | 1709 | size--; |
|
- | 1710 | } |
|
- | 1711 | else |
|
- | 1712 | { |
|
- | 1713 | success = 0; |
|
- | 1714 | } |
|
- | 1715 | } |
|
- | 1716 | if(success) object_cnt++; |
|
- | 1717 | } |
|
- | 1718 | ||
- | 1719 | return(object_cnt); // return the number of objects succesfully written to the file |
|
- | 1720 | } |
|
- | 1721 | ||
Line -... | Line 1722... | ||
- | 1722 | ||
- | 1723 | /****************************************************************************************************************************************/ |
|
- | 1724 | /* Function: fputs_(const s8 *string, File_t *File); */ |
|
- | 1725 | /* */ |
|
- | 1726 | /* Description: This function writes a string to the specified file. */ |
|
- | 1727 | /* */ |
|
1464 | retvalue = findfirst_(fname,&item, _FILE); // look for the item to be deleted. |
1728 | /* Returnvalue: The function returns a no negative value or EOF on error. */ |
1465 | 1729 | /****************************************************************************************************************************************/ |
|
1466 | if(retvalue); // item found? |
1730 | s16 fputs_(const s8 *string, File_t *file) |
- | 1731 | { |
|
- | 1732 | u8 i=0; |
|
- | 1733 | s16 c = 0; |
|
1467 | { |
1734 | |
- | 1735 | if((!Partition.IsValid) || (file == NULL) || (string == NULL)) return(0); |
|
- | 1736 | ||
1468 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
1737 | while((string[i] != 0)&& (c != EOF)) |
- | 1738 | { |
|
- | 1739 | c = fputc_(string[i], file); |
|
- | 1740 | i++; |
|
- | 1741 | } |
|
- | 1742 | return(c); |
|
- | 1743 | } |
|
1469 | DeleteDirectoryEntry(&item); // free the directoryentry. |
1744 | |
1470 | 1745 | /****************************************************************************************************************************************/ |
|
1471 | do |
- | |
1472 | { |
1746 | /* Function: fgets_(s8 *, s16 , File_t *); */ |
1473 | retvalue = findnext_(&item); |
1747 | /* */ |
1474 | if(retvalue) |
1748 | /* Description: This function reads a string from the file to the specifies string. */ |
1475 | { |
1749 | /* */ |
1476 | DeleteClusterChain(item.startcluster); // delete all fatentries of the item. |
1750 | /* Returnvalue: A pointer to the string read from the file or 0 on error. */ |
1477 | DeleteDirectoryEntry(&item); // free the directoryentry. |
1751 | /****************************************************************************************************************************************/ |
1478 | } |
1752 | u8 * fgets_(s8 *string, s16 length, File_t *file) |
1479 | } |
1753 | { |
1480 | while(retvalue); |
1754 | u8 *pbuff; |
1481 | } |
1755 | s16 c = 0; |
1482 | 1756 | ||
- | 1757 | if((!Partition.IsValid) || (file == NULL) || (string == NULL) || (length = 0)) return (0); |
|
- | 1758 | pbuff = string; |