📄 fat16test.cpp
字号:
continue;
/* change directory */
if(find_file_in_dir(fs, dd, command, &subdir_entry))
{
dd_new = fat16_open_dir(fs, &subdir_entry);
if(dd_new)
{
fat16_close_dir(dd);
dd = dd_new;
continue;
}
}
uart_puts_p(PSTR("directory not found: "));
uart_puts(command);
uart_putc('\n');
}
else if(strcmp(command, PSTR("ls")) == 0)
{
/* print directory listing */
struct fat16_dir_entry_struct dir_entry;
while(fat16_read_dir(dd, &dir_entry))
{
uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4;
uart_puts(dir_entry.long_name);
uart_putc(dir_entry.attributes & FAT16_ATTRIB_DIR ? '/' : ' ');
while(spaces--)
uart_putc(' ');
uart_putdw_dec(dir_entry.file_size);
uart_putc('\n');
}
}
else if(strncmp(command, PSTR("cat "), 4) == 0)
{
command += 4;
if(command[0] == '\0')
continue;
/* search file in current directory and open it */
struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command);
if(!fd)
{
uart_puts_p(PSTR("error opening "));
uart_puts(command);
uart_putc('\n');
continue;
}
/* print file contents */
uint8_t buffer[8];
uint32_t offset = 0;
while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0)
{
uart_putdw_hex(offset);
uart_putc(':');
for(uint8_t i = 0; i < 8; ++i)
{
uart_putc(' ');
uart_putc_hex(buffer[i]);
}
uart_putc('\n');
offset += 8;
}
fat16_close_file(fd);
}
else if(strcmp(command, PSTR("disk")) == 0)
{
if(!print_disk_info(fs))
uart_puts_p(PSTR("error reading disk info\n"));
}
#if FAT16_WRITE_SUPPORT
else if(strncmp(command, PSTR("rm "), 3) == 0)
{
command += 3;
if(command[0] == '\0')
continue;
struct fat16_dir_entry_struct file_entry;
if(find_file_in_dir(fs, dd, command, &file_entry))
{
if(fat16_delete_file(fs, &file_entry))
continue;
}
uart_puts_p(PSTR("error deleting file: "));
uart_puts(command);
uart_putc('\n');
}
else if(strncmp(command, PSTR("touch "), 6) == 0)
{
command += 6;
if(command[0] == '\0')
continue;
struct fat16_dir_entry_struct file_entry;
if(!fat16_create_file(dd, command, &file_entry))
{
uart_puts_p(PSTR("error creating file: "));
uart_puts(command);
uart_putc('\n');
}
}
else if(strncmp(command, PSTR("write "), 6) == 0)
{
command += 6;
if(command[0] == '\0')
continue;
char* offset_value = command;
while(*offset_value != ' ' && *offset_value != '\0')
++offset_value;
if(*offset_value == ' ')
*offset_value++ = '\0';
else
continue;
/* search file in current directory and open it */
struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command);
if(!fd)
{
uart_puts_p(PSTR("error opening "));
uart_puts(command);
uart_putc('\n');
continue;
}
int32_t offset = strtolong(offset_value);
if(!fat16_seek_file(fd, &offset, FAT16_SEEK_SET))
{
uart_puts_p(PSTR("error seeking on "));
uart_puts(command);
uart_putc('\n');
fat16_close_file(fd);
continue;
}
/* read text from the shell and write it to the file */
uint8_t data_len;
while(1)
{
/* give a different prompt */
uart_putc('<');
uart_putc(' ');
/* read one line of text */
data_len = read_line(buffer, sizeof(buffer));
if(!data_len)
break;
/* write text to file */
if(fat16_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
{
uart_puts_p(PSTR("error writing to file\n"));
break;
}
}
fat16_close_file(fd);
}
else if(strncmp(command, PSTR("mkdir "), 6) == 0)
{
command += 6;
if(command[0] == '\0')
continue;
struct fat16_dir_entry_struct dir_entry;
if(!fat16_create_dir(dd, command, &dir_entry))
{
uart_puts_p(PSTR("error creating directory: "));
uart_puts(command);
uart_putc('\n');
}
}
#endif
//#if BlkMdd_WRITE_BUFFERING
// else if(strcmp_P(command, PSTR("sync")) == 0)
// {
// if(!BlkMdd_sync())
// uart_puts_p(PSTR("error syncing disk\n"));
// }
//#endif
else
{
uart_puts_p(PSTR("unknown command: "));
uart_puts(command);
uart_putc('\n');
}
}
/* close file system */
fat16_close(fs);
/* close partition */
partition_close(partition);
BlkMdd_Cleanup();
return 0;
}
uint8_t read_line(char* buffer, uint8_t buffer_length)
{
memset(buffer, 0, buffer_length);
uint8_t read_length = 0;
while(read_length < buffer_length - 1)
{
uint8_t c = uart_getc();
if(c == 0x08 || c == 0x7f)
{
if(read_length < 1)
continue;
--read_length;
buffer[read_length] = '\0';
uart_putc(0x08);
uart_putc(' ');
uart_putc(0x08);
continue;
}
uart_putc(c);
if(c == 13 ) // '\n'
{
buffer[read_length] = '\0';
uart_putc('\n');
break;
}
else
{
buffer[read_length] = c;
++read_length;
}
}
return read_length;
}
uint32_t strtolong(const char* str)
{
uint32_t l = 0;
while(*str >= '0' && *str <= '9')
l = l * 10 + (*str++ - '0');
return l;
}
uint8_t find_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name, struct fat16_dir_entry_struct* dir_entry)
{
while(fat16_read_dir(dd, dir_entry))
{
if(strcmp(dir_entry->long_name, name) == 0)
{
fat16_reset_dir(dd);
return 1;
}
}
return 0;
}
struct fat16_file_struct* open_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name)
{
struct fat16_dir_entry_struct file_entry;
if(!find_file_in_dir(fs, dd, name, &file_entry))
return 0;
return fat16_open_file(fs, &file_entry);
}
uint8_t print_disk_info(const struct fat16_fs_struct* fs)
{
uart_puts_p("16MB virtual disk, FAT 16");
return 1;
}
void myget_datetime(uint16_t* year, uint8_t* month, uint8_t* day, uint8_t* hour, uint8_t* min, uint8_t* sec)
{
*year = 2007;
*month = 1;
*day = 1;
*hour = 0;
*min = 0;
*sec = 0;
}
} //extern "C" {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -