|
最近在看FATFS,需要读取文件名为 汉字 的文件,调试发现:
f_readdir (
DIR *dirobj, /* Pointer to the directory object */
FILINFO *finfo /* Pointer to file information to return */
)可以正常读取 汉字 文件名;如“绿色.txt”
但打开文件时
f_open (
FIL *fp, /* Pointer to the blank file object */
const char *path, /* Pointer to the file name */
BYTE mode /* Access mode and file open mode flags */
)返回结果是 FR_INVALID_NAME ,无效的文件名
跟踪发现,make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */
const char **path, /* Pointer to the file path pointer */
char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */
) 内部判断时,认为“绿色.txt”是无效的文件名
/*-----------------------------------------------------------------------*/
/* Pick a paragraph and create the name in format of directory entry */
/*-----------------------------------------------------------------------*/
static
char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */
const char **path, /* Pointer to the file path pointer */
char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */
)
{
BYTE n, t, c, a, b;
memset(dirname, ' ', 8+3); /* Fill buffer with spaces */
a = 0; b = 0x18; /* NT flag */
n = 0; t = 8;
for (;;) {
c = *(*path)++;
if (c == '\0' || c == '/') { /* Reached to end of str or directory separator */
if (n == 0) break;
dirname[11] = _USE_NTFLAG ? (a & b) : 0;
return c;
}
if (c <= ' ' || c == 0x7F) break; /* Reject invisible chars */
if (c == '.') {
if (!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */
n = 8; t = 11; continue;
}
break;
}
if (_USE_SJIS &&
((c >= 0x81 && c <= 0x9F) || /* Accept S-JIS code */
(c >= 0xE0 && c <= 0xFC))) {
if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */
c = 0x05;
a ^= 1; goto md_l2;
}
if (c == '"') break; /* Reject " */
if (c <= ')') goto md_l1; /* Accept ! # $ % & ' ( ) */
if (c <= ',') break; /* Reject * + , */
if (c <= '9') goto md_l1; /* Accept - 0-9 */
if (c <= '?') break; /* Reject : ; < = > ? */
if (!(a & 1)) { /* These checks are not applied to S-JIS 2nd byte */
if (c == '|') break; /* Reject | */
if (c >= '[' && c <= ']') break;/* Reject [ \ ] */
if (_USE_NTFLAG && c >= 'A' && c <= 'Z')
(t == 8) ? (b &= ~0x08) : (b &= ~0x10);
if (c >= 'a' && c <= 'z') { /* Convert to upper case */
c -= 0x20;
if (_USE_NTFLAG) (t == 8) ? (a |= 0x08) : (a |= 0x10);
}
}
md_l1:
a &= ~1;
md_l2:
if (n >= t) break;
dirname[n++] = c;
}
return 1;
}
请教各位熟悉 FAT 的朋友,文件名采用的是什么编码,是unicode吗?还是GB?还是其他?
如何才能让 FATFS 正确读取 文件名 为 中文 的文件? |
|