Open Broadcaster Software
Free, open source software for live streaming and recording
platform.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #pragma once
18 
19 #include <stdio.h>
20 #include <wchar.h>
21 #include <sys/types.h>
22 #include "c99defs.h"
23 
24 /*
25  * Platform-independent functions for Accessing files, encoding, DLLs,
26  * sleep, timer, and timing.
27  */
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 EXPORT FILE *os_wfopen(const wchar_t *path, const char *mode);
34 EXPORT FILE *os_fopen(const char *path, const char *mode);
35 EXPORT int64_t os_fgetsize(FILE *file);
36 
37 #ifdef _WIN32
38 EXPORT int os_stat(const char *file, struct stat *st);
39 #else
40 #define os_stat stat
41 #endif
42 
43 EXPORT int os_fseeki64(FILE *file, int64_t offset, int origin);
44 EXPORT int64_t os_ftelli64(FILE *file);
45 
46 EXPORT size_t os_fread_mbs(FILE *file, char **pstr);
47 EXPORT size_t os_fread_utf8(FILE *file, char **pstr);
48 
49 /* functions purely for convenience */
50 EXPORT char *os_quick_read_utf8_file(const char *path);
51 EXPORT bool os_quick_write_utf8_file(const char *path, const char *str,
52  size_t len, bool marker);
53 EXPORT bool os_quick_write_utf8_file_safe(const char *path, const char *str,
54  size_t len, bool marker,
55  const char *temp_ext,
56  const char *backup_ext);
57 EXPORT char *os_quick_read_mbs_file(const char *path);
58 EXPORT bool os_quick_write_mbs_file(const char *path, const char *str,
59  size_t len);
60 
61 EXPORT int64_t os_get_file_size(const char *path);
62 EXPORT int64_t os_get_free_space(const char *path);
63 
64 EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst,
65  size_t dst_size);
66 EXPORT size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
67  size_t dst_size);
68 EXPORT size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst,
69  size_t dst_size);
70 EXPORT size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
71  size_t dst_size);
72 
73 EXPORT size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
74 EXPORT size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
75 EXPORT size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr);
76 EXPORT size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr);
77 
78 EXPORT size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr);
79 EXPORT size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr);
80 
81 EXPORT double os_strtod(const char *str);
82 EXPORT int os_dtostr(double value, char *dst, size_t size);
83 
84 EXPORT void *os_dlopen(const char *path);
85 EXPORT void *os_dlsym(void *module, const char *func);
86 EXPORT void os_dlclose(void *module);
87 
88 struct os_cpu_usage_info;
89 typedef struct os_cpu_usage_info os_cpu_usage_info_t;
90 
94 
95 typedef const void os_performance_token_t;
98 
104 EXPORT bool os_sleepto_ns(uint64_t time_target);
105 EXPORT void os_sleep_ms(uint32_t duration);
106 
107 EXPORT uint64_t os_gettime_ns(void);
108 
109 EXPORT int os_get_config_path(char *dst, size_t size, const char *name);
110 EXPORT char *os_get_config_path_ptr(const char *name);
111 
112 EXPORT int os_get_program_data_path(char *dst, size_t size, const char *name);
113 EXPORT char *os_get_program_data_path_ptr(const char *name);
114 
115 EXPORT char *os_get_executable_path_ptr(const char *name);
116 
117 EXPORT bool os_file_exists(const char *path);
118 
119 EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size);
120 EXPORT char *os_get_abs_path_ptr(const char *path);
121 
122 EXPORT const char *os_get_path_extension(const char *path);
123 
124 struct os_dir;
125 typedef struct os_dir os_dir_t;
126 
127 struct os_dirent {
128  char d_name[256];
129  bool directory;
130 };
131 
132 EXPORT os_dir_t *os_opendir(const char *path);
135 
136 struct os_globent {
137  char *path;
138  bool directory;
139 };
140 
141 struct os_glob_info {
142  size_t gl_pathc;
144 };
145 
146 typedef struct os_glob_info os_glob_t;
147 
148 /* currently no flags available */
149 
150 EXPORT int os_glob(const char *pattern, int flags, os_glob_t **pglob);
152 
153 EXPORT int os_unlink(const char *path);
154 EXPORT int os_rmdir(const char *path);
155 
156 EXPORT char *os_getcwd(char *path, size_t size);
157 EXPORT int os_chdir(const char *path);
158 
159 EXPORT uint64_t os_get_free_disk_space(const char *dir);
160 
161 #define MKDIR_EXISTS 1
162 #define MKDIR_SUCCESS 0
163 #define MKDIR_ERROR -1
164 
165 EXPORT int os_mkdir(const char *path);
166 EXPORT int os_mkdirs(const char *path);
167 EXPORT int os_rename(const char *old_path, const char *new_path);
168 EXPORT int os_copyfile(const char *file_in, const char *file_out);
169 EXPORT int os_safe_replace(const char *target_path, const char *from_path,
170  const char *backup_path);
171 
172 EXPORT char *os_generate_formatted_filename(const char *extension, bool space,
173  const char *format);
174 
175 struct os_inhibit_info;
176 typedef struct os_inhibit_info os_inhibit_t;
177 
181 
183 
186 
188 
190  uint64_t resident_size;
191  uint64_t virtual_size;
192 };
194 
198 
199 #ifdef _MSC_VER
200 #define strtoll _strtoi64
201 #if _MSC_VER < 1900
202 #define snprintf _snprintf
203 #endif
204 #endif
205 
206 /* clang-format off */
207 #ifdef __APPLE__
208 # define ARCH_BITS 64
209 #else
210 # ifdef _WIN32
211 # ifdef _WIN64
212 # define ARCH_BITS 64
213 # else
214 # define ARCH_BITS 32
215 # endif
216 # else
217 # ifdef __LP64__
218 # define ARCH_BITS 64
219 # else
220 # define ARCH_BITS 32
221 # endif
222 # endif
223 #endif
224 /* clang-format on */
225 
226 #ifdef __cplusplus
227 }
228 #endif
os_quick_read_mbs_file
EXPORT char * os_quick_read_mbs_file(const char *path)
os_get_free_disk_space
EXPORT uint64_t os_get_free_disk_space(const char *dir)
os_get_path_extension
EXPORT const char * os_get_path_extension(const char *path)
os_readdir
EXPORT struct os_dirent * os_readdir(os_dir_t *dir)
os_unlink
EXPORT int os_unlink(const char *path)
os_get_program_data_path
EXPORT int os_get_program_data_path(char *dst, size_t size, const char *name)
os_glob_info::gl_pathv
struct os_globent * gl_pathv
Definition: platform.h:143
os_safe_replace
EXPORT int os_safe_replace(const char *target_path, const char *from_path, const char *backup_path)
os_wcs_to_utf8_ptr
EXPORT size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
os_get_file_size
EXPORT int64_t os_get_file_size(const char *path)
os_stat
#define os_stat
Definition: platform.h:40
os_get_logical_cores
EXPORT int os_get_logical_cores(void)
os_utf8_to_wcs
EXPORT size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
os_dirent::directory
bool directory
Definition: platform.h:129
os_mbs_to_wcs_ptr
EXPORT size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
os_end_high_performance
EXPORT void os_end_high_performance(os_performance_token_t *)
EXPORT
#define EXPORT
Definition: c99defs.h:37
os_inhibit_sleep_set_active
EXPORT bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
os_quick_read_utf8_file
EXPORT char * os_quick_read_utf8_file(const char *path)
os_fgetsize
EXPORT int64_t os_fgetsize(FILE *file)
os_get_config_path_ptr
EXPORT char * os_get_config_path_ptr(const char *name)
os_mbs_to_utf8_ptr
EXPORT size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
os_get_physical_cores
EXPORT int os_get_physical_cores(void)
os_fread_mbs
EXPORT size_t os_fread_mbs(FILE *file, char **pstr)
os_get_sys_free_size
EXPORT uint64_t os_get_sys_free_size(void)
os_mbs_to_wcs
EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst, size_t dst_size)
os_utf8_to_wcs_ptr
EXPORT size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
os_quick_write_mbs_file
EXPORT bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
os_fread_utf8
EXPORT size_t os_fread_utf8(FILE *file, char **pstr)
os_rmdir
EXPORT int os_rmdir(const char *path)
os_generate_formatted_filename
EXPORT char * os_generate_formatted_filename(const char *extension, bool space, const char *format)
os_dirent::d_name
char d_name[256]
Definition: platform.h:128
os_get_abs_path_ptr
EXPORT char * os_get_abs_path_ptr(const char *path)
os_sleep_ms
EXPORT void os_sleep_ms(uint32_t duration)
os_breakpoint
EXPORT void os_breakpoint(void)
os_strtod
EXPORT double os_strtod(const char *str)
c99defs.h
os_globent::directory
bool directory
Definition: platform.h:138
os_quick_write_utf8_file
EXPORT bool os_quick_write_utf8_file(const char *path, const char *str, size_t len, bool marker)
os_opendir
EXPORT os_dir_t * os_opendir(const char *path)
os_globent::path
char * path
Definition: platform.h:137
os_get_executable_path_ptr
EXPORT char * os_get_executable_path_ptr(const char *name)
os_cpu_usage_info_query
EXPORT double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
os_cpu_usage_info_t
struct os_cpu_usage_info os_cpu_usage_info_t
Definition: platform.h:89
os_dlopen
EXPORT void * os_dlopen(const char *path)
os_chdir
EXPORT int os_chdir(const char *path)
os_get_proc_resident_size
EXPORT uint64_t os_get_proc_resident_size(void)
os_dlclose
EXPORT void os_dlclose(void *module)
os_get_program_data_path_ptr
EXPORT char * os_get_program_data_path_ptr(const char *name)
os_closedir
EXPORT void os_closedir(os_dir_t *dir)
os_glob_info
Definition: platform.h:141
os_glob_info::gl_pathc
size_t gl_pathc
Definition: platform.h:142
os_cpu_usage_info_start
EXPORT os_cpu_usage_info_t * os_cpu_usage_info_start(void)
os_quick_write_utf8_file_safe
EXPORT bool os_quick_write_utf8_file_safe(const char *path, const char *str, size_t len, bool marker, const char *temp_ext, const char *backup_ext)
os_proc_memory_usage
Definition: platform.h:189
os_getcwd
EXPORT char * os_getcwd(char *path, size_t size)
os_globfree
EXPORT void os_globfree(os_glob_t *pglob)
os_ftelli64
EXPORT int64_t os_ftelli64(FILE *file)
os_wcs_to_utf8
EXPORT size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst, size_t dst_size)
os_glob
EXPORT int os_glob(const char *pattern, int flags, os_glob_t **pglob)
os_wcs_to_mbs
EXPORT size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size)
os_inhibit_sleep_destroy
EXPORT void os_inhibit_sleep_destroy(os_inhibit_t *info)
os_sleepto_ns
EXPORT bool os_sleepto_ns(uint64_t time_target)
os_performance_token_t
const void os_performance_token_t
Definition: platform.h:95
os_get_proc_memory_usage
EXPORT bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
os_get_abs_path
EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size)
os_request_high_performance
EXPORT os_performance_token_t * os_request_high_performance(const char *reason)
os_copyfile
EXPORT int os_copyfile(const char *file_in, const char *file_out)
os_dirent
Definition: platform.h:127
os_inhibit_sleep_create
EXPORT os_inhibit_t * os_inhibit_sleep_create(const char *reason)
os_fopen
EXPORT FILE * os_fopen(const char *path, const char *mode)
os_file_exists
EXPORT bool os_file_exists(const char *path)
os_mkdirs
EXPORT int os_mkdirs(const char *path)
os_wfopen
EXPORT FILE * os_wfopen(const wchar_t *path, const char *mode)
os_fseeki64
EXPORT int os_fseeki64(FILE *file, int64_t offset, int origin)
os_wcs_to_mbs_ptr
EXPORT size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr)
os_get_proc_virtual_size
EXPORT uint64_t os_get_proc_virtual_size(void)
os_utf8_to_mbs_ptr
EXPORT size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
os_dlsym
EXPORT void * os_dlsym(void *module, const char *func)
os_inhibit_t
struct os_inhibit_info os_inhibit_t
Definition: platform.h:176
os_dtostr
EXPORT int os_dtostr(double value, char *dst, size_t size)
os_rename
EXPORT int os_rename(const char *old_path, const char *new_path)
os_proc_memory_usage::virtual_size
uint64_t virtual_size
Definition: platform.h:191
os_globent
Definition: platform.h:136
os_proc_memory_usage::resident_size
uint64_t resident_size
Definition: platform.h:190
os_dir_t
struct os_dir os_dir_t
Definition: platform.h:125
os_gettime_ns
EXPORT uint64_t os_gettime_ns(void)
os_cpu_usage_info_destroy
EXPORT void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
os_get_config_path
EXPORT int os_get_config_path(char *dst, size_t size, const char *name)
os_mkdir
EXPORT int os_mkdir(const char *path)
os_get_free_space
EXPORT int64_t os_get_free_space(const char *path)