From 847b3df322d31f1c282394e4087fcdc1275ddf82 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Wed, 8 Jul 2015 10:00:33 -0700 Subject: [PATCH] libobs/util: Add os_mkdirs Recursively creates a directory structure if one or more directories in the path structure don't exist --- libobs/util/platform.c | 36 ++++++++++++++++++++++++++++++++++++ libobs/util/platform.h | 1 + 2 files changed, 37 insertions(+) diff --git a/libobs/util/platform.c b/libobs/util/platform.c index 2c402ba70..0b07cac6b 100644 --- a/libobs/util/platform.c +++ b/libobs/util/platform.c @@ -508,3 +508,39 @@ int os_dtostr(double value, char *dst, size_t size) return (int)length; } + +static int recursive_mkdir(char *path) +{ + char *last_slash; + int ret; + + ret = os_mkdir(path); + if (ret != MKDIR_ERROR) + return ret; + + last_slash = strrchr(path, '/'); + if (!last_slash) + return MKDIR_ERROR; + + *last_slash = 0; + ret = recursive_mkdir(path); + *last_slash = '/'; + + if (ret == MKDIR_ERROR) + return MKDIR_ERROR; + + ret = os_mkdir(path); + return ret; +} + +int os_mkdirs(const char *dir) +{ + struct dstr dir_str; + int ret; + + dstr_init_copy(&dir_str, dir); + dstr_replace(&dir_str, "\\", "/"); + ret = recursive_mkdir(dir_str.array); + dstr_free(&dir_str); + return ret; +} diff --git a/libobs/util/platform.h b/libobs/util/platform.h index 24e6b1958..b299407d7 100644 --- a/libobs/util/platform.h +++ b/libobs/util/platform.h @@ -135,6 +135,7 @@ EXPORT int os_rmdir(const char *path); #define MKDIR_ERROR -1 EXPORT int os_mkdir(const char *path); +EXPORT int os_mkdirs(const char *path); EXPORT int os_rename(const char *old_path, const char *new_path); EXPORT int os_copyfile(const char *file_in, const char *file_out);