Slight refactor to move common ".gz" extension handling code to interface module

This commit is contained in:
Aaron Culliney 2015-11-29 12:59:24 -08:00
parent 12b2103a56
commit ff204a4300
5 changed files with 28 additions and 45 deletions

View File

@ -90,22 +90,6 @@ static void _init_disk6(void) {
}
}
static inline void cut_gz(char *name) {
size_t len = strlen(name);
if (len <= _GZLEN) {
return;
}
*(name+len-_GZLEN) = '\0';
}
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) {
@ -1081,7 +1065,7 @@ bool disk6_loadState(StateHelper_s *helper) {
break;
}
snprintf(namebuf+namelen, gzlen, "%s", DISK_EXT_GZ);
snprintf(namebuf+namelen, gzlen, "%s", EXT_GZ);
namebuf[namelen+gzlen] = '\0';
LOG("LOAD disk[%lu] : (%u) %s", i, namelen, namebuf);
disk6_insert(i, namebuf, disk6.disk[i].is_protected);

View File

@ -47,8 +47,6 @@
#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 {
char *file_name;

View File

@ -222,28 +222,7 @@ void c_interface_print_submenu_centered( char *submenu, const int message_cols,
/* ------------------------------------------------------------------------- */
static int c_interface_cut_name(char *name)
{
char *p = name + strlen(name) - 1;
int is_gz = 0;
if (p >= name && *p == 'z')
{
p--;
if (p >= name && *p == 'g')
{
p--;
if (p >= name && *p == '.')
{
*p-- = '\0';
is_gz = 1;
}
}
}
return is_gz;
}
#warning TODO FIXME : file selection and extension management should be made generic (merge similar code from disk.[hc] and possible Mac/iOS target) ...
static int disk_select(const struct dirent *e) {
char cmp[PATH_MAX] = { 0 };
@ -469,8 +448,9 @@ void c_interface_select_diskette( int drive )
namelist[ ent_no ]->d_name );
}
if (c_interface_cut_name(temp))
if (is_gz(temp))
{
cut_gz(temp);
strncat(temp, " <gz>", PATH_MAX-1);
}
/* write protected disk in drive? */

View File

@ -89,5 +89,26 @@ extern void (*interface_setTouchMenuEnabled)(bool enabled);
extern void (*interface_setTouchMenuVisibility)(float alpha);
#endif
#define EXT_GZ ".gz"
#define _GZLEN (sizeof(EXT_GZ)-1)
// ----------------------------------------------------------------------------
// file extension handling
static inline bool is_gz(const char * const name) {
size_t len = strlen(name);
if (len <= _GZLEN) {
return false;
}
return strncmp(name+len-_GZLEN, EXT_GZ, _GZLEN) == 0;
}
static inline void cut_gz(char *name) {
size_t len = strlen(name);
if (len <= _GZLEN) {
return;
}
*(name+len-_GZLEN) = '\0';
}
#endif

View File

@ -76,7 +76,7 @@ const char *zlib_deflate(const char* const src, const int expected_bytecount) {
break;
}
snprintf(dst, PATH_MAX-1, "%s%s", src, ".gz");
snprintf(dst, PATH_MAX-1, "%s%s", src, EXT_GZ);
gzdest = gzopen(dst, "wb");
if (gzdest == NULL) {
@ -167,8 +167,8 @@ const char *zlib_inflate(const char* const src, const int expected_bytecount) {
size_t len = strlen(src);
snprintf(dst, PATH_MAX-1, "%s", src);
if (! ( (dst[len-3] == '.') && (dst[len-2] == 'g') && (dst[len-1] == 'z') ) ) {
ERRLOG("Expected filename ending in .gz");
if (!is_gz(dst)) {
ERRLOG("Expected filename ending in %s", EXT_GZ);
break;
}
dst[len-3] = '\0';