瀏覽代碼

libobs/util: Add os_mkdirs

Recursively creates a directory structure if one or more directories in
the path structure don't exist
jp9000 10 年之前
父節點
當前提交
847b3df322
共有 2 個文件被更改,包括 37 次插入0 次删除
  1. 36 0
      libobs/util/platform.c
  2. 1 0
      libobs/util/platform.h

+ 36 - 0
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;
+}

+ 1 - 0
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);