mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
move compatibility code for file access from audio.c to compat.h
This commit is contained in:
39
src/util/compat.h
Normal file
39
src/util/compat.h
Normal 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
|
||||||
|
}
|
||||||
@@ -7,17 +7,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.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 "../error.h"
|
||||||
|
#include "../util/compat.h"
|
||||||
|
|
||||||
static void audiomixer(void* const userdata, uint8_t* const stream, int const len) {
|
static void audiomixer(void* const userdata, uint8_t* const stream, int const len) {
|
||||||
memset(stream, 0, len); // clear the playing audio
|
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);
|
debug("loading audio file '%s'...", fpath);
|
||||||
|
|
||||||
if (!fexists(fpath)) {
|
if (!faccess(fpath, FA_R)) {
|
||||||
error("%s:%u couldn't find audio file '%s'!", __FILE_NAME__, __LINE__, fpath);
|
error("%s:%u audio file either isn't readable or doesn't exist. path: '%s'!", __FILE_NAME__, __LINE__, fpath);
|
||||||
return (audiodata){0};
|
return (audiodata){0};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user