libobs/util: Remove dup. code, add os_opendir
Just use platform-nix.c code for general stuff that mac is compliant with, and put a define around everything else. Take that code out of platform-cocoa.m. Added os_opendir, os_readdir, and os_closedir to be able to query available files within a directory.
This commit is contained in:
parent
4bdcbe0600
commit
4f9e4a27d5
@ -30,6 +30,7 @@ elseif(APPLE)
|
||||
set(libobs_PLATFORM_SOURCES
|
||||
obs-cocoa.c
|
||||
util/threading-posix.c
|
||||
util/platform-nix.c
|
||||
util/platform-cocoa.m)
|
||||
|
||||
set_source_files_properties(${libobs_PLATFORM_SOURCES}
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Ruwen Hahn <palana@stunned.de>
|
||||
* Copyright (c) 2013-2014 Ruwen Hahn <palana@stunned.de>
|
||||
* Hugh "Jim" Bailey <obs.jim@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
@ -22,72 +23,12 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <CoreServices/CoreServices.h>
|
||||
#include <mach/mach.h>
|
||||
#include <mach/mach_time.h>
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
void *os_dlopen(const char *path)
|
||||
{
|
||||
struct dstr dylib_name;
|
||||
|
||||
if (!path)
|
||||
return NULL;
|
||||
|
||||
dstr_init_copy(&dylib_name, path);
|
||||
if (!dstr_find(&dylib_name, ".so"))
|
||||
dstr_cat(&dylib_name, ".so");
|
||||
|
||||
void *res = dlopen(dylib_name.array, RTLD_LAZY);
|
||||
if (!res)
|
||||
blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
|
||||
path, dylib_name.array, dlerror());
|
||||
|
||||
dstr_free(&dylib_name);
|
||||
return res;
|
||||
}
|
||||
|
||||
void *os_dlsym(void *module, const char *func)
|
||||
{
|
||||
return dlsym(module, func);
|
||||
}
|
||||
|
||||
void os_dlclose(void *module)
|
||||
{
|
||||
dlclose(module);
|
||||
}
|
||||
|
||||
bool os_sleepto_ns(uint64_t time_target)
|
||||
{
|
||||
uint64_t current = os_gettime_ns();
|
||||
if (time_target < current)
|
||||
return false;
|
||||
|
||||
time_target -= current;
|
||||
|
||||
struct timespec req, remain;
|
||||
memset(&req, 0, sizeof(req));
|
||||
memset(&remain, 0, sizeof(remain));
|
||||
req.tv_sec = time_target/1000000000;
|
||||
req.tv_nsec = time_target%1000000000;
|
||||
|
||||
while (nanosleep(&req, &remain)) {
|
||||
req = remain;
|
||||
memset(&remain, 0, sizeof(remain));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void os_sleep_ms(uint32_t duration)
|
||||
{
|
||||
usleep(duration*1000);
|
||||
}
|
||||
|
||||
|
||||
/* clock function selection taken from libc++ */
|
||||
static uint64_t ns_time_simple()
|
||||
{
|
||||
@ -152,19 +93,3 @@ char *os_get_config_path(const char *name)
|
||||
dstr_cat(&path, name);
|
||||
return path.array;
|
||||
}
|
||||
|
||||
bool os_file_exists(const char *path)
|
||||
{
|
||||
return access(path, F_OK) == 0;
|
||||
}
|
||||
|
||||
int os_mkdir(const char *path)
|
||||
{
|
||||
if(!mkdir(path, 0777))
|
||||
return MKDIR_SUCCESS;
|
||||
|
||||
if(errno == EEXIST)
|
||||
return MKDIR_EXISTS;
|
||||
|
||||
return MKDIR_ERROR;
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
#include <unistd.h>
|
||||
@ -82,6 +83,8 @@ void os_sleep_ms(uint32_t duration)
|
||||
usleep(duration*1000);
|
||||
}
|
||||
|
||||
#if !defined(__APPLE__)
|
||||
|
||||
uint64_t os_gettime_ns(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
@ -103,15 +106,78 @@ char *os_get_config_path(const char *name)
|
||||
return path.array;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
bool os_file_exists(const char *path)
|
||||
{
|
||||
return access(path, F_OK) == 0;
|
||||
}
|
||||
|
||||
struct os_dir {
|
||||
const char *path;
|
||||
DIR *dir;
|
||||
struct dirent *cur_dirent;
|
||||
struct os_dirent out;
|
||||
};
|
||||
|
||||
os_dir_t os_opendir(const char *path)
|
||||
{
|
||||
struct os_dir *dir;
|
||||
DIR *dir_val;
|
||||
|
||||
dir_val = opendir(path);
|
||||
if (!dir_val)
|
||||
return NULL;
|
||||
|
||||
dir = bzalloc(sizeof(struct os_dir));
|
||||
dir->dir = dir_val;
|
||||
return dir;
|
||||
}
|
||||
|
||||
struct os_dirent *os_readdir(os_dir_t dir)
|
||||
{
|
||||
struct dstr file_path = {0};
|
||||
struct stat stat_info;
|
||||
|
||||
if (!dir) return NULL;
|
||||
|
||||
dir->cur_dirent = readdir(dir->dir);
|
||||
if (!dir->cur_dirent)
|
||||
return NULL;
|
||||
|
||||
strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
|
||||
|
||||
dstr_copy(&file_path, dir->path);
|
||||
dstr_cat(&file_path, "/");
|
||||
dstr_cat(&file_path, dir->out.d_name);
|
||||
|
||||
if (stat(file_path.array, &stat_info) == 0)
|
||||
dir->out.directory = !!S_ISDIR(stat_info.st_mode);
|
||||
else
|
||||
blog(LOG_DEBUG, FILE_LINE "stat for %s failed, errno: %d",
|
||||
file_path.array, errno);
|
||||
|
||||
dstr_free(&file_path);
|
||||
|
||||
return &dir->out;
|
||||
}
|
||||
|
||||
void os_closedir(os_dir_t dir)
|
||||
{
|
||||
if (dir) {
|
||||
closedir(dir->dir);
|
||||
bfree(dir);
|
||||
}
|
||||
}
|
||||
|
||||
int os_unlink(const char *path)
|
||||
{
|
||||
return unlink(path);
|
||||
}
|
||||
|
||||
int os_mkdir(const char *path)
|
||||
{
|
||||
int errorcode = mkdir(path, S_IRWXU);
|
||||
if (errorcode == 0)
|
||||
if (mkdir(path, 0777) == 0)
|
||||
return MKDIR_SUCCESS;
|
||||
|
||||
return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
|
||||
|
@ -167,6 +167,83 @@ bool os_file_exists(const char *path)
|
||||
return hFind != INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
struct os_dir {
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATA wfd;
|
||||
bool first;
|
||||
struct os_dirent out;
|
||||
};
|
||||
|
||||
os_dir_t os_opendir(const char *path)
|
||||
{
|
||||
struct dstr path_str = {0};
|
||||
struct os_dir *dir = NULL;
|
||||
WIN32_FIND_DATA wfd;
|
||||
HANDLE handle;
|
||||
wchar_t *w_path;
|
||||
|
||||
dstr_copy(&path_str, path);
|
||||
dstr_cat(&path_str, "/*.*");
|
||||
|
||||
if (os_utf8_to_wcs_ptr(path_str.array, path_str.len, &w_path) > 0) {
|
||||
handle = FindFirstFileW(w_path, &wfd);
|
||||
if (handle != INVALID_HANDLE_VALUE) {
|
||||
dir = bzalloc(sizeof(struct os_dir));
|
||||
dir->handle = handle;
|
||||
dir->first = true;
|
||||
dir->wfd = wfd;
|
||||
}
|
||||
|
||||
bfree(w_path);
|
||||
}
|
||||
|
||||
dstr_free(&path_str);
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
struct os_dirent *os_readdir(os_dir_t dir)
|
||||
{
|
||||
if (!dir)
|
||||
return NULL;
|
||||
|
||||
if (dir->first) {
|
||||
dir->first = false;
|
||||
} else {
|
||||
if (!FindNextFileW(dir->handle, &dir->wfd))
|
||||
return NULL;
|
||||
}
|
||||
|
||||
os_wcs_to_utf8(dir->wfd.cFileName, 255, dir->out.d_name);
|
||||
dir->out.directory =
|
||||
!!(dir->wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
||||
|
||||
return &dir->out;
|
||||
}
|
||||
|
||||
void os_closedir(os_dir_t dir)
|
||||
{
|
||||
if (dir) {
|
||||
FindClose(dir->handle);
|
||||
bfree(dir);
|
||||
}
|
||||
}
|
||||
|
||||
int os_unlink(const char *path)
|
||||
{
|
||||
wchar_t *w_path;
|
||||
bool success;
|
||||
|
||||
os_utf8_to_wcs_ptr(path, 0, &w_path);
|
||||
if (!w_path)
|
||||
return -1;
|
||||
|
||||
success = !!DeleteFileW(w_path);
|
||||
bfree(w_path);
|
||||
|
||||
return success ? 0 : -1;
|
||||
}
|
||||
|
||||
int os_mkdir(const char *path)
|
||||
{
|
||||
wchar_t *path_utf16;
|
||||
|
@ -76,6 +76,20 @@ EXPORT char *os_get_config_path(const char *name);
|
||||
|
||||
EXPORT bool os_file_exists(const char *path);
|
||||
|
||||
struct os_dir;
|
||||
typedef struct os_dir *os_dir_t;
|
||||
|
||||
struct os_dirent {
|
||||
char d_name[256];
|
||||
bool directory;
|
||||
};
|
||||
|
||||
EXPORT os_dir_t os_opendir(const char *path);
|
||||
EXPORT struct os_dirent *os_readdir(os_dir_t dir);
|
||||
EXPORT void os_closedir(os_dir_t dir);
|
||||
|
||||
EXPORT int os_unlink(const char *path);
|
||||
|
||||
#define MKDIR_EXISTS 1
|
||||
#define MKDIR_SUCCESS 0
|
||||
#define MKDIR_ERROR -1
|
||||
|
Loading…
x
Reference in New Issue
Block a user