From 1c50e0dc3afe4e8c079bf0fd84876ee90d53d378 Mon Sep 17 00:00:00 2001 From: sorgelig Date: Fri, 1 Sep 2017 20:47:17 +0800 Subject: [PATCH] Support for memory files. --- file_io.c | 97 ++++++++++++++++++++++++++++++------------------------- file_io.h | 11 ++++++- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/file_io.c b/file_io.c index 4f52d7b..0710c84 100644 --- a/file_io.c +++ b/file_io.c @@ -10,40 +10,51 @@ #include #include #include +#include #include #include "osd.h" #include "fpga_io.h" #include "menu.h" #include "errno.h" +#include "DiskImage.h" int nDirEntries = 0; struct dirent DirItem[1000]; int iSelectedEntry = 0; // selected entry index int iFirstEntry = 0; +static char full_path[1200]; + void FileClose(fileTYPE *file) { if (file->fd > 0) { //printf("closing %d\n", file->fd); close(file->fd); + if (file->type == 1) + { + if (file->name[0] == '/') + { + shm_unlink(file->name); + } + file->type = 0; + } } file->fd = -1; } -static char full_path[1200]; - int FileOpenEx(fileTYPE *file, const char *name, int mode) { - sprintf(full_path, "%s/%s", getRootDir(), name); + sprintf(full_path, "%s/%s", (mode == -1) ? "" : getRootDir(), name); FileClose(file); file->mode = 0; + file->type = 0; char *p = strrchr(full_path, '/'); - strcpy(file->name, p+1); + strcpy(file->name, (mode == -1) ? full_path : p+1); - file->fd = open(full_path, mode); + file->fd = (mode == -1) ? shm_open("/vtrd", O_CREAT | O_RDWR | O_TRUNC, 0777) : open(full_path, mode); if (file->fd <= 0) { printf("FileOpenEx(open) File:%s, error: %d.\n", full_path, file->fd); @@ -51,18 +62,28 @@ int FileOpenEx(fileTYPE *file, const char *name, int mode) return 0; } - struct stat64 st; - int ret = fstat64(file->fd, &st); - if ( ret < 0) + if (mode == -1) { - printf("FileOpenEx(fstat) File:%s, error: %d.\n", full_path, ret); - file->fd = -1; - return 0; + file->type = 1; + file->size = 0; + file->offset = 0; + file->mode = O_CREAT | O_RDWR | O_TRUNC; } + else + { + struct stat64 st; + int ret = fstat64(file->fd, &st); + if (ret < 0) + { + printf("FileOpenEx(fstat) File:%s, error: %d.\n", full_path, ret); + FileClose(file); + return 0; + } - file->size = st.st_size; - file->offset = 0; - file->mode = mode; + file->size = st.st_size; + file->offset = 0; + file->mode = mode; + } //printf("opened %s, size %lu\n", full_path, file->size); return 1; @@ -298,34 +319,6 @@ int FileCanWrite(char *name) return ((st.st_mode & S_IWUSR) != 0); } -char *make_name(char *short_name) -{ - static char name[16]; - memset(name, 0, sizeof(name)); - memcpy(name, short_name, 8); - memcpy(name + 10, short_name + 8, 3); - - for (int i = 7; i >= 0; i--) - { - if (name[i] <= 0x20) name[i] = 0; - else break; - } - - for (int i = 12; i >= 10; i--) - { - if (name[i] <= 0x20) name[i] = 0; - else break; - } - - if (strlen(name + 10)) - { - strcat(name, "."); - strcat(name, name + 10); - } - - return name; -} - static int device = 0; static int usbnum = 0; char *getRootDir() @@ -336,6 +329,12 @@ char *getRootDir() return dev; } +char *getFullPath(char *name) +{ + sprintf(full_path, "%s/%s", getRootDir(), name); + return full_path; +} + void setStorage(int dev) { device = 0; @@ -531,7 +530,16 @@ void AdjustDirectory(char *path) int ScanDirectory(char* path, int mode, char *extension, int options) { + int has_trd = 0; + char *ext = extension; + while (*ext) + { + if (!strncasecmp(ext, "TRD", 3)) has_trd = 1; + ext += 3; + } + int extlen = strlen(extension); + //printf("scan dir\n"); if (mode == SCAN_INIT) @@ -576,8 +584,9 @@ int ScanDirectory(char* path, int mode, char *extension, int options) { int len = strlen(de->d_name); char *ext = extension; - int found = 0; - while(*ext) + int found = (has_trd && x2trd_ext_supp(de->d_name)); + + while(!found && *ext) { char e[5]; memcpy(e+1, ext, 3); diff --git a/file_io.h b/file_io.h index 3949f99..b68cbfe 100644 --- a/file_io.h +++ b/file_io.h @@ -6,10 +6,15 @@ #include #include "spi.h" +#ifdef __cplusplus +extern "C" { +#endif + typedef struct { int fd; int mode; + int type; __off64_t size; __off64_t offset; char name[261]; @@ -69,7 +74,11 @@ int FileLoadConfig(char *name, void *pBuffer, int size); // supply pBuffer = 0 t void AdjustDirectory(char *path); int ScanDirectory(char* path, int mode, char *extension, int options); -char *make_name(char *short_name); char *getRootDir(); +char *getFullPath(char *name); + +#ifdef __cplusplus +} +#endif #endif