📄 file.lst
字号:
250 3 fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
251 3 fs_initClusterChain(fs,&(file->Cache),sec);
252 3 fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
253 3 file_setAttr(file,FILE_STATUS_OPEN,1);
254 3 file_setAttr(file,FILE_STATUS_WRITE,1);
255 3 return(0);
256 3 }
257 2 else
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 5
258 2 {
259 3 return(-3);
260 3 }
261 2 break;
262 2 case MODE_APPEND:
263 2 if(fs_findFile(fs,filename,&loc,0)==1) /* File exists */
264 2 {
265 3 dir_getFileStructure(fs,&(file->DirEntry), &loc);
266 3 file_initFile(file,fs,&loc);
267 3 if(file->Cache.FirstCluster==0){
268 4 sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
269 4 dir_setFirstCluster(file->fs,&(file->Location),sec);
270 4 fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
271 4 fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
272 4 file_initFile(file,fs,&loc);
273 4 }
274 3 file_setpos(file,file->FileSize);
275 3 file_setAttr(file,FILE_STATUS_OPEN,1);
276 3 file_setAttr(file,FILE_STATUS_WRITE,1);
277 3 }
278 2 else /* File does not excist */
279 2 {
280 3 if(fs_findFreeFile(fs,filename,&loc,0))
281 3 {
282 4 dir_createDefaultEntry(fs,&wtmp,fatfilename);
283 4 dir_createDirectoryEntry(fs,&wtmp,&loc);
284 4 memCpy(&wtmp,&(file->DirEntry),sizeof(wtmp));
285 4 file_initFile(file,fs,&loc);
286 4 sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
287 4 dir_setFirstCluster(file->fs,&(file->Location),sec);
288 4 fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
289 4 fs_initClusterChain(fs,&(file->Cache),sec);
290 4 fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
291 4 file_setAttr(file,FILE_STATUS_OPEN,1);
292 4 file_setAttr(file,FILE_STATUS_WRITE,1);
293 4 }
294 3 else
295 3 {
296 4 return(-3);
297 4 }
298 3 }
299 2 return(0);
300 2 break;
301 2 default:
302 2 return(-4);
303 2 break;
304 2 }
305 1 return(-5);
306 1 }
307 /*****************************************************************************/
308
309 /* ****************************************************************************
310 * esint8 file_fclose(File *file)
311 * Description: This function closes a file, by clearing the object.
312 * Return value: 0 on success.
313 */
314 esint8 file_fclose(File *file)
315 {
316 1 if(fs_hasTimeSupport()){
317 2 file->DirEntry.AccessDate = time_getDate();
318 2 if(file_getAttr(file,FILE_STATUS_WRITE)){
319 3 file->DirEntry.FileSize = file->FileSize;
320 3 file->DirEntry.WriteDate = file->DirEntry.AccessDate;
321 3 file->DirEntry.WriteTime = time_getTime();
322 3 }
323 2 dir_updateDirectoryEntry(file->fs,&(file->DirEntry),&(file->Location));
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 6
324 2 }else{
325 2 if(file_getAttr(file,FILE_STATUS_WRITE)){
326 3 dir_setFileSize(file->fs,&(file->Location),file->FileSize);
327 3 }
328 2 }
329 1
330 1 memClr(file,sizeof(*file));
331 1 file_setAttr(file,FILE_STATUS_OPEN,0);
332 1 file_setAttr(file,FILE_STATUS_WRITE,0);
333 1 return(0);
334 1 }
335
336
337 /* ****************************************************************************
338 * void file_initFile(File *file, FileSystem *fs, FileLocation *loc)
339 * Description: This function initialises a new file object, by filling in
340 * the fs pointer, filesize (note, that DirEntry must already be filled in)
341 * and known cache parameters.
342 * Return value: void
343 */
344 void file_initFile(File *file, FileSystem *fs, FileLocation *loc)
345 {
346 1 file->fs=fs;
347 1 file->FileSize=file->DirEntry.FileSize;
348 1 file->FilePtr=0;
349 1 file->Location.Sector=loc->Sector;
350 1 file->Location.Offset=loc->Offset;
351 1 file->Cache.Linear=0;
352 1 file->Cache.FirstCluster=(((euint32)file->DirEntry.FirstClusterHigh)<<16)+
353 1 file->DirEntry.FirstClusterLow;
354 1 file->Cache.LastCluster=0;
355 1 file->Cache.LogicCluster=0;
356 1 file->Cache.DiscCluster=file->Cache.FirstCluster;
357 1 }
358 /*****************************************************************************/
359
360 /* ****************************************************************************
361 * euint8* file_normalToFatName(eint8* filename,eint8* fatfilename)
362 * Description: This function converts a human readable filename (limited to
363 * 8.3 eint8 character) to a valid FAT (not VFAT) filename. Invalid characters are
364 * changed to capital X and only the first 11 characters are used.
365 * Furthermore all letters are capitalised.
366 * Return value: pointer after the filename
367 */
368 eint8* file_normalToFatName(eint8* filename,eint8* fatfilename)
369 {
370 1 euint8 c,dot=0,vc=0;
371 1
372 1 for(c=0;c<11;c++)fatfilename[c]=' ';
373 1
374 1 c=0;
375 1
376 1 if(*filename == '.'){
377 2 fatfilename[0]='.';
378 2 vc++;
379 2 if(*(filename+1) == '.'){
380 3 fatfilename[1]='.';
381 3 filename+=2;
382 3 }else{
383 3 filename++;
384 3 }
385 2 }else{
386 2 while(*filename != '\0' && *filename != ' ' && *filename != '/'){
387 3 if(*filename=='.' && !dot){
388 4 dot=1;
389 4 c=8;
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 7
390 4 }else{
391 4 if(dot){
392 5 if(c<=10){
393 6 fatfilename[c]=file_validateChar(*filename);
394 6 c++;
395 6 }
396 5 }else{
397 5 if(c<=7){
398 6 fatfilename[c]=file_validateChar(*filename);
399 6 c++; vc++;
400 6 }
401 5 }
402 4 }
403 3 filename++;
404 3 }
405 2 }
406 1
407 1 if(vc>0){
408 2 if(*filename=='\0'){
409 3 return(filename);
410 3 }else{
411 3 return(filename+1);
412 3 }
413 2 }else{
414 2 return(0);
415 2 }
416 1 }
417 /*****************************************************************************/
418
419 /* ****************************************************************************
420 *
421 * Description: This function takes the character c, and if it is not a *
422 * valid FAT Filename character returns X. If it is a lowercase letter the *
423 * uppercase equivalent is returned. The remaining characters are returned *
424 * as they are.
425 * Return value: The validated char
426 */
427 euint8 file_validateChar(euint8 c)
428 {
429 1 if( (c<0x20) || (c>0x20&&c<0x30&&c!='-') || (c>0x39&&c<0x41) || (c>0x5A&&c<0x61&&c!='_') || (c>0x7A&&
-c!='~') )
430 1 return(0x58);
431 1 if( c>=0x61 && c<=0x7A )
432 1 return(c-32);
433 1
434 1 return(c);
435 1 }
436 /*****************************************************************************/
437
438 /* ****************************************************************************
439 * void ioman_setAttr(IOManager *ioman,euint16 bufplace,euint8 attribute,euint8 val)
440 * Description: This sets the attribute of 'bufplace' to the given value (binary).
441 *
442 * Return value: void
443 */
444 void file_setAttr(File* file,euint8 attribute,euint8 val)
445 {
446 1 if(val){
447 2 file->FileStatus|=1<<attribute;
448 2 }else{
449 2 file->FileStatus&=~(1<<attribute);
450 2 }
451 1 }
452 /*****************************************************************************/
453
454 /* ****************************************************************************
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 8
455 * euint8 ioman_getAttr(IOManager *ioman,euint16 bufplace,euint8 attribute)
456 * Description: This function retrieves an attribute from the bufstat array.
457 * It returns nonzero when it attribute is true and 0 when it is false.
458 * Please note, I said "nonzero", not 1.
459 * Return value: Attribute.
460 */
461 euint8 file_getAttr(File* file,euint8 attribute)
462 {
463 1 return(file->FileStatus&(1<<attribute));
464 1 }
465 /*****************************************************************************/
466
467 euint32 file_requiredCluster(File *file,euint32 offset, euint32 size)
468 {
469 1 euint32 clusters_required,clustersize;
470 1 euint32 hc;
471 1
472 1 if((offset+size)>file->FileSize){
473 2 if(file->Cache.ClusterCount==0){ /* Number of cluster unknown */
474 3 hc = fat_countClustersInChain(file->fs,file->Cache.FirstCluster);
475 3 file->Cache.ClusterCount = hc;
476 3 }else{
477 3 hc = file->Cache.ClusterCount; /* This better be right */
478 3 }
479 2 clustersize = file->fs->volumeId.BytesPerSector * file->fs->volumeId.SectorsPerCluster;
480 2 if((size-file->FileSize+offset)>
481 2 ((hc-((file->FileSize+clustersize-1)/clustersize))*clustersize)){
482 3 clusters_required = (((offset+size)-(hc*clustersize))+clustersize-1)/clustersize;
483 3 }else{
484 3 clusters_required = 0;
485 3 }
486 2 }else{
487 2 clusters_required = 0;
488 2 }
489 1 return(clusters_required);
490 1 }
ARM COMPILER V2.42, file 27/03/06 10:45:50 PAGE 9
ASSEMBLY LISTING OF GENERATED OBJECT CODE
*** EXTERNALS:
EXTERN CODE16 (fs_hasTimeSupport?T)
EXTERN CODE16 (memCpy?T)
EXTERN CODE16 (memClr?T)
EXTERN CODE16 (part_getSect?T)
EXTERN CODE16 (part_relSect?T)
EXTERN CODE16 (part_directSectorRead?T)
EXTERN CODE16 (part_directSectorWrite?T)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -