From 52d74aa43cc69175a7f161e3afe10912bfdb3110 Mon Sep 17 00:00:00 2001 From: Aaron Culliney Date: Fri, 9 Oct 2015 21:32:04 -0700 Subject: [PATCH] Improved disk prefix utility functions --- src/disk.c | 57 ++++++++++++++++++++++++++++++------------------------ src/disk.h | 11 +++++++++++ 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/disk.c b/src/disk.c index 8473a891..9862040a 100644 --- a/src/disk.c +++ b/src/disk.c @@ -79,41 +79,48 @@ static void _initialize_reverse_translate(void) { } } -static void cut_gz(char *name) { - char *p = name + strlen(name) - 1; - p--; - p--; - *p = '\0'; -} - -static bool is_gz(const char * const name) { - size_t len = strlen( name ); - if (len > 3) { - return (strcmp(name+len-3, ".gz") == 0); +static inline void cut_gz(char *name) { + size_t len = strlen(name); + if (len <= _GZLEN) { + return; } - return false; + *(name+len-_GZLEN) = '\0'; } -static bool is_nib(const char * const name) { - size_t len = strlen( name ); +static inline bool is_gz(const char * const name) { + size_t len = strlen(name); + if (len <= _GZLEN) { + return false; + } + return strncmp(name+len-_GZLEN, DISK_EXT_GZ, _GZLEN) == 0; +} + +static inline bool is_nib(const char * const name) { + size_t len = strlen(name); + if (len <= _NIBLEN) { + return false; + } if (is_gz(name)) { - len -= 3; + if (len <= _NIBLEN+_GZLEN) { + return false; + } + len -= _GZLEN; } - if (!strncmp(name + len - 4, ".nib", 4)) { - return true; - } - return false; + return strncmp(name+len-_NIBLEN, DISK_EXT_NIB, _NIBLEN) == 0; } -static bool is_po(const char * const name) { +static inline bool is_po(const char * const name) { size_t len = strlen( name ); + if (len <= _POLEN) { + return false; + } if (is_gz(name)) { - len -= 3; + if (len <= _POLEN+_GZLEN) { + return false; + } + len -= _GZLEN; } - if (!strncmp(name + len - 3, ".po", 3)) { - return true; - } - return false; + return strncmp(name+len-_POLEN, DISK_EXT_PO, _POLEN) == 0; } #define SIXBIT_MASK 0x3F // 111111 diff --git a/src/disk.h b/src/disk.h index 77fc45aa..c9b87bb9 100644 --- a/src/disk.h +++ b/src/disk.h @@ -36,6 +36,17 @@ #define DSK_VOLUME 254 #define FILE_NAME_SZ (PATH_MAX>>2) +#define DISK_EXT_DSK ".dsk" +#define _DSKLEN (sizeof(DISK_EXT_DSK)-1) +#define DISK_EXT_DO ".do" +#define _DOLEN (sizeof(DISK_EXT_DO)-1) +#define DISK_EXT_PO ".po" +#define _POLEN (sizeof(DISK_EXT_PO)-1) +#define DISK_EXT_NIB ".nib" +#define _NIBLEN (sizeof(DISK_EXT_NIB)-1) +#define DISK_EXT_GZ ".gz" +#define _GZLEN (sizeof(DISK_EXT_GZ)-1) + typedef struct diskette_t { uint8_t track_image[NIB_TRACK_SIZE]; char file_name[FILE_NAME_SZ];