📄 tff.lst
字号:
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 8
428 #endif /* _FS_MINIMIZE <= 1 */
429
430
431
432
433 /*-----------------------------------------------------------------------*/
434 /* Pick a paragraph and create the name in format of directory entry */
435 /*-----------------------------------------------------------------------*/
436
437 static
438 char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */
439 const char **path, /* Pointer to the file path pointer */
440 char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */
441 )
442 {
443 1 BYTE n, t, c, a, b;
444 1
445 1
446 1 memset(dirname, ' ', 8+3); /* Fill buffer with spaces */
447 1 a = 0; b = 0x18; /* NT flag */
448 1 n = 0; t = 8;
449 1 for (;;) {
450 2 c = *(*path)++;
451 2 if (c == '\0' || c == '/') { /* Reached to end of str or directory separator */
452 3 if (n == 0) break;
453 3 dirname[11] = _USE_NTFLAG ? (a & b) : 0;
454 3 return c;
455 3 }
456 2 if (c <= ' ' || c == 0x7F) break; /* Reject invisible chars */
457 2 if (c == '.') {
458 3 if (!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */
459 4 n = 8; t = 11; continue;
460 4 }
461 3 break;
462 3 }
463 2 if (_USE_SJIS &&
464 2 ((c >= 0x81 && c <= 0x9F) || /* Accept S-JIS code */
465 2 (c >= 0xE0 && c <= 0xFC))) {
466 3 if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */
467 3 c = 0x05;
468 3 a ^= 1; goto md_l2;
469 3 }
470 2 if (c == '"') break; /* Reject " */
471 2 if (c <= ')') goto md_l1; /* Accept ! # $ % & ' ( ) */
472 2 if (c <= ',') break; /* Reject * + , */
473 2 if (c <= '9') goto md_l1; /* Accept - 0-9 */
474 2 if (c <= '?') break; /* Reject : ; < = > ? */
475 2 if (!(a & 1)) { /* These checks are not applied to S-JIS 2nd byte */
476 3 if (c == '|') break; /* Reject | */
477 3 if (c >= '[' && c <= ']') break;/* Reject [ \ ] */
478 3 if (_USE_NTFLAG && c >= 'A' && c <= 'Z')
479 3 (t == 8) ? (b &= 0xF7) : (b &= 0xEF);
480 3 if (c >= 'a' && c <= 'z') { /* Convert to upper case */
481 4 c -= 0x20;
482 4 if (_USE_NTFLAG) (t == 8) ? (a |= 0x08) : (a |= 0x10);
483 4 }
484 3 }
485 2 md_l1:
486 2 a &= 0xFE;
487 2 md_l2:
488 2 if (n >= t) break;
489 2 dirname[n++] = c;
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 9
490 2 }
491 1 return 1;
492 1 }
493
494
495
496 /*-----------------------------------------------------------------------*/
497 /* Trace a file path */
498 /*-----------------------------------------------------------------------*/
499
500 static
501 FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */
502 DIR *dj, /* Pointer to directory object to return last directory */
503 char *fn, /* Pointer to last segment name to return */
504 const char *path, /* Full-path string to trace a file or directory */
505 BYTE **dir /* Pointer to pointer to found entry to retutn */
506 )
507 {
508 1 CLUST clust;
509 1 char ds;
510 1 BYTE *dptr = NULL;
511 1 FATFS *fs = FatFs;
512 1
513 1 /* Initialize directory object */
514 1 dj->fs = fs;
515 1 clust = fs->dirbase;
516 1 #if _FAT32
517 1 if (fs->fs_type == FS_FAT32) {
518 2 dj->clust = dj->sclust = clust;
519 2 dj->sect = clust2sect(clust);
520 2 } else
521 1 #endif
522 1 {
523 2 dj->clust = dj->sclust = 0;
524 2 dj->sect = clust;
525 2 }
526 1 dj->index = 0;
527 1
528 1 if (*path == '\0') { /* Null path means the root directory */
529 2 *dir = NULL; return FR_OK;
530 2 }
531 1
532 1 for (;;) {
533 2 ds = make_dirfile(&path, fn); /* Get a paragraph into fn[] */
534 2 if (ds == 1) return FR_INVALID_NAME;
535 2 for (;;) {
536 3 if (!move_window(dj->sect)) return FR_RW_ERROR;
537 3 dptr = &fs->win[(dj->index & 15) * 32]; /* Pointer to the directory entry */
538 3 if (dptr[DIR_Name] == 0) /* Has it reached to end of dir? */
539 3 return !ds ? FR_NO_FILE : FR_NO_PATH;
540 3 if (dptr[DIR_Name] != 0xE5 /* Matched? */
541 3 && !(dptr[DIR_Attr] & AM_VOL)
542 3 && !memcmp(&dptr[DIR_Name], fn, 8+3) ) break;
543 3 if (!next_dir_entry(dj)) /* Next directory pointer */
544 3 return !ds ? FR_NO_FILE : FR_NO_PATH;
545 3 }
546 2 if (!ds) { *dir = dptr; return FR_OK; } /* Matched with end of path */
547 2 if (!(dptr[DIR_Attr] & AM_DIR)) return FR_NO_PATH; /* Cannot trace because it is a file */
548 2 clust = /* Get cluster# of the directory */
549 2 #if _FAT32
550 2 ((DWORD)LD_WORD(&dptr[DIR_FstClusHI]) << 16) |
551 2 #endif
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 10
552 2 LD_WORD(&dptr[DIR_FstClusLO]);
553 2 dj->clust = dj->sclust = clust; /* Restart scannig with the new directory */
554 2 dj->sect = clust2sect(clust);
555 2 dj->index = 2;
556 2 }
557 1 }
558
559
560
561 /*-----------------------------------------------------------------------*/
562 /* Reserve a directory entry */
563 /*-----------------------------------------------------------------------*/
564
565 #if !_FS_READONLY
static
FRESULT reserve_direntry ( /* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occur
-ed */
DIR *dj, /* Target directory to create new entry */
BYTE **dir /* Pointer to pointer to created entry to retutn */
)
{
CLUST clust;
DWORD sector;
BYTE c, n, *dptr;
FATFS *fs = dj->fs;
/* Re-initialize directory object */
clust = dj->sclust;
if (clust != 0) { /* Dyanmic directory table */
dj->clust = clust;
dj->sect = clust2sect(clust);
} else { /* Static directory table */
dj->sect = fs->dirbase;
}
dj->index = 0;
do {
if (!move_window(dj->sect)) return FR_RW_ERROR;
dptr = &fs->win[(dj->index & 15) * 32]; /* Pointer to the directory entry */
c = dptr[DIR_Name];
if (c == 0 || c == 0xE5) { /* Found an empty entry! */
*dir = dptr; return FR_OK;
}
} while (next_dir_entry(dj)); /* Next directory pointer */
/* Reached to end of the directory table */
/* Abort when static table or could not stretch dynamic table */
if (clust == 0 || !(clust = create_chain(dj->clust))) return FR_DENIED;
if (clust == 1 || !move_window(0)) return FR_RW_ERROR;
fs->winsect = sector = clust2sect(clust); /* Cleanup the expanded table */
memset(fs->win, 0, 512U);
for (n = fs->csize; n; n--) {
if (disk_write(0, fs->win, sector, 1) != RES_OK)
return FR_RW_ERROR;
sector++;
}
fs->winflag = 1;
*dir = fs->win;
return FR_OK;
}
C51 COMPILER V8.08 TFF 10/31/2008 14:44:16 PAGE 11
#endif /* !_FS_READONLY */
614
615
616
617
618 /*-----------------------------------------------------------------------*/
619 /* Load boot record and check if it is an FAT boot record */
620 /*-----------------------------------------------------------------------*/
621
622 static
623 BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record or error
- */
624 DWORD sect /* Sector# to check if it is an FAT boot record or not */
625 )
626 {
627 1 FATFS *fs = FatFs;
628 1
629 1 if (disk_read(0, fs->win, sect, 1) != RES_OK) /* Load boot record */
630 1 return 2;
631 1 if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature */
632 1 return 2;
633 1
634 1 if (!memcmp(&fs->win[BS_FilSysType], "FAT", 3)) /* Check FAT signature */
635 1 return 0;
636 1 #if _FAT32
637 1 if (!memcmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80))
638 1 return 0;
639 1 #endif
640 1 return 1;
641 1 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -