move compatibility code for file access from audio.c to compat.h

This commit is contained in:
2025-04-18 15:42:26 +02:00
parent 5145e43eaa
commit da59b64a0b
2 changed files with 42 additions and 12 deletions

39
src/util/compat.h Normal file
View File

@@ -0,0 +1,39 @@
#pragma once
#if defined __unix__
# include <unistd.h>
#elif defined _WIN32
# include <io.h>
#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
}

View File

@@ -7,17 +7,8 @@
#include <stdlib.h>
#include <string.h>
#ifdef __unix__
# include <unistd.h>
# define fexists(fname) (access(fname, F_OK) == 0)
#elif _WIN32
# include <io.h>
# 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};
}