/ ff_memfree() in ffsystem.c, need to be added to the project. */\r
\r
\r
-#define FF_LFN_UNICODE 0\r
+#ifdef CONFIG_FATFS_API_ENCODING_UTF_8\r
+#define FF_LFN_UNICODE 2\r
+#elif defined(CONFIG_FATFS_API_ENCODING_UTF_16)\r
+#define FF_LFN_UNICODE 1\r
+#else /* CONFIG_FATFS_API_ENCODING_ANSI_OEM */\r
+#define FF_LFN_UNICODE 0\r
+#endif\r
/* This option switches the character encoding on the API when LFN is enabled.\r
/\r
/ 0: ANSI/OEM in current CP (TCHAR = char)\r
/ on character encoding. When LFN is not enabled, these options have no effect. */\r
\r
\r
-#define FF_STRF_ENCODE 3\r
+#define FF_STRF_ENCODE 3\r
/* When FF_LFN_UNICODE >= 1 with LFN enabled, string I/O functions, f_gets(),\r
/ f_putc(), f_puts and f_printf() convert the character encoding in it.\r
/ This option selects assumption of character encoding ON THE FILE to be\r
#include "test_fatfs_common.h"
const char* fatfs_test_hello_str = "Hello, World!\n";
+const char* fatfs_test_hello_str_utf = "世界,你好!\n";
void test_fatfs_create_file_with_text(const char* name, const char* text)
{
TEST_ASSERT_EQUAL(0, fclose(f));
}
+void test_fatfs_read_file_utf_8(const char* filename)
+{
+ FILE* f = fopen(filename, "r");
+ TEST_ASSERT_NOT_NULL(f);
+ char buf[64] = { 0 }; //Doubled buffer size to allow for longer UTF-8 strings
+ int cb = fread(buf, 1, sizeof(buf), f);
+ TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str_utf), cb);
+ TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str_utf, buf));
+ TEST_ASSERT_EQUAL(0, fclose(f));
+}
+
void test_fatfs_open_max_files(const char* filename_prefix, size_t files_count)
{
FILE** files = calloc(files_count, sizeof(FILE*));
TEST_ASSERT_EQUAL(0, closedir(dir));
}
+void test_fatfs_opendir_readdir_rewinddir_utf_8(const char* dir_prefix)
+{
+ char name_dir_inner_file[64];
+ char name_dir_inner[64];
+ char name_dir_file3[64];
+ char name_dir_file2[64];
+ char name_dir_file1[64];
+
+ snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/内部目录/内部文件.txt", dir_prefix);
+ snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/内部目录", dir_prefix);
+ snprintf(name_dir_file3, sizeof(name_dir_file3), "%s/文件三.bin", dir_prefix);
+ snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/文件二.txt", dir_prefix);
+ snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/文件一.txt", dir_prefix);
+
+ unlink(name_dir_inner_file);
+ rmdir(name_dir_inner);
+ unlink(name_dir_file1);
+ unlink(name_dir_file2);
+ unlink(name_dir_file3);
+ rmdir(dir_prefix);
+
+ TEST_ASSERT_EQUAL(0, mkdir(dir_prefix, 0755));
+ test_fatfs_create_file_with_text(name_dir_file1, "一号\n");
+ test_fatfs_create_file_with_text(name_dir_file2, "二号\n");
+ test_fatfs_create_file_with_text(name_dir_file3, "\0一\0二\0三");
+ TEST_ASSERT_EQUAL(0, mkdir(name_dir_inner, 0755));
+ test_fatfs_create_file_with_text(name_dir_inner_file, "三号\n");
+
+ DIR* dir = opendir(dir_prefix);
+ TEST_ASSERT_NOT_NULL(dir);
+ int count = 0;
+ const char* names[4];
+ while(count < 4) {
+ struct dirent* de = readdir(dir);
+ if (!de) {
+ break;
+ }
+ printf("found '%s'\n", de->d_name);
+ if (strcasecmp(de->d_name, "文件一.txt") == 0) {
+ TEST_ASSERT_TRUE(de->d_type == DT_REG);
+ names[count] = "文件一.txt";
+ ++count;
+ } else if (strcasecmp(de->d_name, "文件二.txt") == 0) {
+ TEST_ASSERT_TRUE(de->d_type == DT_REG);
+ names[count] = "文件二.txt";
+ ++count;
+ } else if (strcasecmp(de->d_name, "内部目录") == 0) {
+ TEST_ASSERT_TRUE(de->d_type == DT_DIR);
+ names[count] = "内部目录";
+ ++count;
+ } else if (strcasecmp(de->d_name, "文件三.bin") == 0) {
+ TEST_ASSERT_TRUE(de->d_type == DT_REG);
+ names[count] = "文件三.bin";
+ ++count;
+ } else {
+ TEST_FAIL_MESSAGE("unexpected directory entry");
+ }
+ }
+ TEST_ASSERT_EQUAL(count, 4);
+
+ rewinddir(dir);
+ struct dirent* de = readdir(dir);
+ TEST_ASSERT_NOT_NULL(de);
+ TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
+ seekdir(dir, 3);
+ de = readdir(dir);
+ TEST_ASSERT_NOT_NULL(de);
+ TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
+ seekdir(dir, 1);
+ de = readdir(dir);
+ TEST_ASSERT_NOT_NULL(de);
+ TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
+ seekdir(dir, 2);
+ de = readdir(dir);
+ TEST_ASSERT_NOT_NULL(de);
+ TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
+
+ TEST_ASSERT_EQUAL(0, closedir(dir));
+}
typedef struct {
const char* filename;
vSemaphoreDelete(args4.done);
}
-
void test_fatfs_rw_speed(const char* filename, void* buf, size_t buf_size, size_t file_size, bool write)
{
const size_t buf_count = file_size / buf_size;
(write)?"Wrote":"Read", file_size, buf_size, t_s * 1e3,
file_size / (1024.0f * 1024.0f * t_s));
}
-
}
static const char* test_filename = "/sdcard/hello.txt";
+static const char* test_filename_utf_8 = "/sdcard/测试文件.txt";
TEST_CASE("Mount fails cleanly without card inserted", "[fatfs][ignore]")
{
fclose(f);
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", wl_handle));
}
+
+/*
+ * In FatFs menuconfig, set CONFIG_FATFS_API_ENCODING to UTF-8 and set the
+ * Codepage to CP936 (Simplified Chinese) in order to run the following tests.
+ * Ensure that the text editor is UTF-8 compatible when compiling these tests.
+ */
+#if defined(CONFIG_FATFS_API_ENCODING_UTF_8) && (CONFIG_FATFS_CODEPAGE == 936)
+TEST_CASE("(SD) can read file using UTF-8 encoded strings", "[fatfs][ignore]")
+{
+ test_setup();
+ test_fatfs_create_file_with_text(test_filename_utf_8, fatfs_test_hello_str_utf);
+ test_fatfs_read_file_utf_8(test_filename_utf_8);
+ test_teardown();
+}
+
+TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected using UTF-8 encoded strings", "[fatfs][ignore]")
+{
+ test_setup();
+ test_fatfs_opendir_readdir_rewinddir_utf_8("/sdcard/目录");
+ test_teardown();
+}
+#endif