diff --git a/src/util/compat.h b/src/util/compat.h new file mode 100644 index 0000000..644a14a --- /dev/null +++ b/src/util/compat.h @@ -0,0 +1,39 @@ +#pragma once + +#if defined __unix__ +# include +#elif defined _WIN32 +# include +#else +# error platform not supported! +#endif + +// define the constants if they haven't been +#ifndef F_OK +# define F_OK 0 +#endif +#ifndef X_OK +# define X_OK 1 +#endif +#ifndef W_OK +# define W_OK 2 +#endif +#ifndef R_OK +# define R_OK 4 +#endif + +enum faccess_perms { + FA_F = F_OK, // test for file's existence + FA_X = X_OK, // test for executing permission + FA_W = W_OK, // test for write permissions + FA_R = R_OK, // test for read permissions +}; + +// tests a files access with F_OK, X_OK, R_OK, W_OK OR'd together +static inline int faccess(char const* restrict fname, int perms) { +#if defined __unix__ + return !access(fname, perms); +#elif defined _WIN32 + return !_access(fname, perms); +#endif +} diff --git a/src/window/audio.c b/src/window/audio.c index 30de20a..e680de5 100644 --- a/src/window/audio.c +++ b/src/window/audio.c @@ -7,17 +7,8 @@ #include #include -#ifdef __unix__ -# include -# define fexists(fname) (access(fname, F_OK) == 0) -#elif _WIN32 -# include -# define fexists(fname) (_access(fname, 0) == 0) -#else -# error platform not supported! -#endif - #include "../error.h" +#include "../util/compat.h" static void audiomixer(void* const userdata, uint8_t* const stream, int const len) { memset(stream, 0, len); // clear the playing audio @@ -174,8 +165,8 @@ audiodata audio_wav_load(audiodevice const* dev, char const* fpath) { debug("loading audio file '%s'...", fpath); - if (!fexists(fpath)) { - error("%s:%u couldn't find audio file '%s'!", __FILE_NAME__, __LINE__, fpath); + if (!faccess(fpath, FA_R)) { + error("%s:%u audio file either isn't readable or doesn't exist. path: '%s'!", __FILE_NAME__, __LINE__, fpath); return (audiodata){0}; }