浏览代码

libobs/util: Add function to get path extension

jp9000 9 年之前
父节点
当前提交
9948eee6c2
共有 2 个文件被更改,包括 25 次插入0 次删除
  1. 23 0
      libobs/util/platform.c
  2. 2 0
      libobs/util/platform.h

+ 23 - 0
libobs/util/platform.c

@@ -633,3 +633,26 @@ int os_mkdirs(const char *dir)
 	dstr_free(&dir_str);
 	return ret;
 }
+
+const char *os_get_path_extension(const char *path)
+{
+	struct dstr temp;
+	size_t pos = 0;
+	char *period;
+	char *slash;
+
+	dstr_init_copy(&temp, path);
+	dstr_replace(&temp, "\\", "/");
+
+	slash = strrchr(temp.array, '/');
+	period = strrchr(temp.array, '.');
+	if (period)
+		pos = (size_t)(period - temp.array);
+
+	dstr_free(&temp);
+
+	if (!period || slash > period)
+		return NULL;
+
+	return path + pos;
+}

+ 2 - 0
libobs/util/platform.h

@@ -113,6 +113,8 @@ EXPORT bool os_file_exists(const char *path);
 EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size);
 EXPORT char *os_get_abs_path_ptr(const char *path);
 
+EXPORT const char *os_get_path_extension(const char *path);
+
 struct os_dir;
 typedef struct os_dir os_dir_t;