]> git.unchartedbackwaters.co.uk Git - francis/winuae.git/commitdiff
Common file extension extraction.
authorToni Wilen <twilen@winuae.net>
Fri, 4 Aug 2023 16:15:41 +0000 (19:15 +0300)
committerToni Wilen <twilen@winuae.net>
Fri, 4 Aug 2023 16:15:41 +0000 (19:15 +0300)
include/zfile.h
zfile.cpp
zfile_archive.cpp

index 7ed1cea8fc365dd99dd996ae1829fa7222dfd63e..92fabf8f344b4a2d298f7ac7ac613f87bcc2cd4f 100644 (file)
@@ -52,6 +52,7 @@ extern uae_u8 *zfile_load_data(const TCHAR *name, const uae_u8 *data,int datalen
 extern uae_u8 *zfile_load_file(const TCHAR *name, int *outlen);
 extern struct zfile *zfile_fopen_parent(struct zfile*, const TCHAR*, uae_u64 offset, uae_u64 size);
 extern uae_u8 *zfile_get_data_pointer(struct zfile *z, size_t *len);
+extern const TCHAR *zfile_get_ext(const TCHAR *);
 
 extern int zfile_exists (const TCHAR *name);
 extern void zfile_fclose (struct zfile *z);
index e9b50d16b83024fd360a25b5346d2b3b6c725cf7..7f3689080dec02228acdcece5e24ec1c5e58dcdc 100644 (file)
--- a/zfile.cpp
+++ b/zfile.cpp
@@ -35,6 +35,22 @@ const TCHAR *uae_archive_extensions[] = { _T("zip"), _T("rar"), _T("7z"), _T("lh
 
 #define MAX_CACHE_ENTRIES 10
 
+const TCHAR *zfile_get_ext(const TCHAR *name)
+{
+       const TCHAR *sep = _tcsrchr(name, '\\');
+       if (!sep) {
+               sep = _tcsrchr(name, '/');
+       }
+       const TCHAR *ext = _tcsrchr(name, '.');
+       if (!ext) {
+               return NULL;
+       }
+       if (sep && ext < sep) {
+               return NULL;
+       }
+       return ext;
+}
+
 struct zdisktrack
 {
        void *data;
@@ -270,12 +286,12 @@ static const TCHAR *diskimages[] = { _T("adf"), _T("adz"), _T("ipf"), _T("scp"),
 int zfile_gettype (struct zfile *z)
 {
        uae_u8 buf[8];
-       TCHAR *ext;
+       const TCHAR *ext;
        bool hdf = false;
 
        if (!z || !z->name)
                return ZFILE_UNKNOWN;
-       ext = _tcsrchr (z->name, '.');
+       ext = zfile_get_ext(z->name);
        if (ext != NULL) {
                int i;
                ext++;
@@ -668,7 +684,7 @@ static struct zfile *extadf (struct zfile *z, int index, int *retcode)
        uae_u8 buffer[2 + 2 + 4 + 4];
        int outsize;
        TCHAR newname[MAX_DPATH];
-       TCHAR *ext;
+       const TCHAR *ext;
        int cantrunc = 0;
        int done = 0;
 
@@ -685,7 +701,7 @@ static struct zfile *extadf (struct zfile *z, int index, int *retcode)
        offs = 8 + 2 + 2 + tracks * (2 + 2 + 4 + 4);
 
        _tcscpy (newname, zfile_getname (z));
-       ext = _tcsrchr (newname, '.');
+       ext = zfile_get_ext(newname);
        if (ext) {
                _tcscpy (newname + _tcslen (newname) - _tcslen (ext), _T(".std.adf"));
        } else {
@@ -770,7 +786,7 @@ static struct zfile *fdi (struct zfile *z, int index, int *retcode)
        int i, j, r;
        struct zfile *zo;
        TCHAR *orgname = zfile_getname (z);
-       TCHAR *ext = _tcsrchr (orgname, '.');
+       const TCHAR *ext = zfile_get_ext(orgname);
        TCHAR newname[MAX_DPATH];
        uae_u16 *amigamfmbuffer;
        uae_u8 writebuffer_ok[32], *outbuf;
@@ -899,7 +915,7 @@ static struct zfile *ipf (struct zfile *z, int index, int *retcode)
        int i, j, r;
        struct zfile *zo;
        TCHAR *orgname = zfile_getname (z);
-       TCHAR *ext = _tcsrchr (orgname, '.');
+       const TCHAR *ext = zfile_get_ext(orgname);
        TCHAR newname[MAX_DPATH];
        uae_u16 *amigamfmbuffer;
        uae_u8 writebuffer_ok[32];
@@ -1225,7 +1241,7 @@ static struct zfile *dms (struct zfile *z, int index, int *retcode)
        int ret;
        struct zfile *zo;
        TCHAR *orgname = zfile_getname (z);
-       TCHAR *ext = _tcsrchr (orgname, '.');
+       const TCHAR *ext = zfile_get_ext(orgname);
        TCHAR newname[MAX_DPATH];
        static int recursive;
        int i;
@@ -1302,7 +1318,7 @@ int zfile_is_ignore_ext (const TCHAR *name)
        int i;
        const TCHAR *ext;
 
-       ext = _tcsrchr (name, '.');
+       ext = zfile_get_ext(name);
        if (!ext)
                return 0;
        for (i = 0; uae_ignoreextensions[i]; i++) {
@@ -1316,9 +1332,10 @@ int zfile_is_diskimage (const TCHAR *name)
 {
        int i;
 
-       const TCHAR *ext = _tcsrchr (name, '.');
-       if (!ext)
+       const TCHAR *ext = zfile_get_ext(name);
+       if (!ext) {
                return 0;
+       }
        i = 0;
        while (uae_diskimageextensions[i]) {
                if (!strcasecmp (ext, uae_diskimageextensions[i]))
@@ -1353,7 +1370,7 @@ static const int plugins_7z_m[] = {
 static int iszip (struct zfile *z, int mask)
 {
        TCHAR *name = z->name;
-       TCHAR *ext = _tcsrchr (name, '.');
+       const TCHAR *ext = zfile_get_ext(name);
        uae_u8 header[32];
 
        if (!ext)
@@ -1435,7 +1452,7 @@ int iszip (struct zfile *z)
 struct zfile *zuncompress (struct znode *parent, struct zfile *z, int dodefault, int mask, int *retcode, int index)
 {
        TCHAR *name = z->name;
-       TCHAR *ext = NULL;
+       const TCHAR *ext = NULL;
        uae_u8 header[32];
 
        if (retcode)
@@ -1443,7 +1460,7 @@ struct zfile *zuncompress (struct znode *parent, struct zfile *z, int dodefault,
        if (!mask)
                return NULL;
        if (name) {
-               ext = _tcsrchr (name, '.');
+               ext = zfile_get_ext(name);
                if (ext)
                        ext++;
        }
@@ -2760,7 +2777,7 @@ static struct zvolume *zfile_fopen_archive_ext (struct znode *parent, struct zfi
 {
        struct zvolume *zv = NULL;
        TCHAR *name = zfile_getname (zf);
-       TCHAR *ext;
+       const TCHAR *ext;
        uae_u8 header[7];
 
        if (!name)
@@ -2771,7 +2788,7 @@ static struct zvolume *zfile_fopen_archive_ext (struct znode *parent, struct zfi
        zfile_fread (header, sizeof (header), 1, zf);
        zfile_fseek (zf, 0, SEEK_SET);
 
-       ext = _tcsrchr (name, '.');
+       ext = zfile_get_ext(name);
        if (ext != NULL) {
                ext++;
                if (flags & ZFD_ARCHIVE) {
@@ -2894,7 +2911,7 @@ static int zfile_fopen_archive_recurse (struct zvolume *zv, int flags)
        while (zn) {
                int done = 0;
                struct zfile *z;
-               TCHAR *ext = _tcsrchr (zn->name, '.');
+               const TCHAR *ext = zfile_get_ext(zn->name);
                if (ext && !zn->vchild && zn->type == ZNODE_FILE) {
                        for (i = 0; !done && archive_extensions[i]; i++) {
                                if (!strcasecmp (ext + 1, archive_extensions[i])) {
index 0581907e200f5d6540817e6134a0f3686c48601e..1314dc3c86c70e3d9420020f49d0a13774818764 100644 (file)
@@ -168,7 +168,7 @@ struct zfile *archive_access_select (struct znode *parent, struct zfile *zf, uns
                                select = -1;
                        if (select && we_have_file < 10) {
                                struct zfile *zt = NULL;
-                               TCHAR *ext = _tcsrchr (zn->fullname, '.');
+                               const TCHAR *ext = zfile_get_ext(zn->fullname);
                                int whf = 1;
                                int ft = 0;
                                if (mask & ZFD_CD) {