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 
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);
133 EXPORT struct os_dirent *os_readdir(os_dir_t *dir);
134 EXPORT void os_closedir(os_dir_t *dir);
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);
151 EXPORT void os_globfree(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 
178 EXPORT os_inhibit_t *os_inhibit_sleep_create(const char *reason);
179 EXPORT bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active);
181 
182 EXPORT void os_breakpoint(void);
183 
184 EXPORT int os_get_physical_cores(void);
185 EXPORT int os_get_logical_cores(void);
186 
188 
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
EXPORT bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
EXPORT void os_closedir(os_dir_t *dir)
struct os_globent * gl_pathv
Definition: platform.h:143
EXPORT size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
EXPORT void * os_dlsym(void *module, const char *func)
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)
EXPORT FILE * os_wfopen(const wchar_t *path, const char *mode)
EXPORT int os_fseeki64(FILE *file, int64_t offset, int origin)
EXPORT size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size)
EXPORT char * os_getcwd(char *path, size_t size)
struct os_inhibit_info os_inhibit_t
Definition: platform.h:176
EXPORT int os_get_config_path(char *dst, size_t size, const char *name)
EXPORT size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
unsigned uint32_t
Definition: vc_stdint.h:31
EXPORT int os_mkdirs(const char *path)
EXPORT int64_t os_get_free_space(const char *path)
EXPORT os_performance_token_t * os_request_high_performance(const char *reason)
bool directory
Definition: platform.h:129
EXPORT uint64_t os_get_free_disk_space(const char *dir)
EXPORT char * os_get_config_path_ptr(const char *name)
struct os_dir os_dir_t
Definition: platform.h:125
EXPORT FILE * os_fopen(const char *path, const char *mode)
EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size)
EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst, size_t dst_size)
unsigned __int64 uint64_t
Definition: vc_stdint.h:33
EXPORT void os_inhibit_sleep_destroy(os_inhibit_t *info)
EXPORT char * os_get_abs_path_ptr(const char *path)
uint64_t virtual_size
Definition: platform.h:191
EXPORT char * os_get_executable_path_ptr(const char *name)
EXPORT void os_sleep_ms(uint32_t duration)
Definition: platform.h:127
EXPORT os_dir_t * os_opendir(const char *path)
EXPORT int os_mkdir(const char *path)
EXPORT size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst, size_t dst_size)
EXPORT int64_t os_get_file_size(const char *path)
EXPORT uint64_t os_gettime_ns(void)
#define EXPORT
Definition: c99defs.h:49
EXPORT int os_unlink(const char *path)
EXPORT int os_get_program_data_path(char *dst, size_t size, const char *name)
EXPORT int os_safe_replace(const char *target_path, const char *from_path, const char *backup_path)
EXPORT int os_chdir(const char *path)
char d_name[256]
Definition: platform.h:128
EXPORT size_t os_fread_mbs(FILE *file, char **pstr)
EXPORT void os_breakpoint(void)
EXPORT os_cpu_usage_info_t * os_cpu_usage_info_start(void)
char * path
Definition: platform.h:137
Definition: platform.h:141
EXPORT int os_dtostr(double value, char *dst, size_t size)
EXPORT char * os_quick_read_utf8_file(const char *path)
EXPORT bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
Definition: platform.h:189
EXPORT void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
EXPORT double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
EXPORT double os_strtod(const char *str)
EXPORT uint64_t os_get_sys_free_size(void)
EXPORT bool os_quick_write_utf8_file(const char *path, const char *str, size_t len, bool marker)
bool directory
Definition: platform.h:138
EXPORT int os_rename(const char *old_path, const char *new_path)
EXPORT int64_t os_fgetsize(FILE *file)
EXPORT size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
Definition: platform.h:136
EXPORT void * os_dlopen(const char *path)
EXPORT os_inhibit_t * os_inhibit_sleep_create(const char *reason)
EXPORT uint64_t os_get_proc_virtual_size(void)
EXPORT size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr)
EXPORT size_t os_fread_utf8(FILE *file, char **pstr)
EXPORT char * os_quick_read_mbs_file(const char *path)
EXPORT char * os_generate_formatted_filename(const char *extension, bool space, const char *format)
size_t gl_pathc
Definition: platform.h:142
EXPORT size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
EXPORT bool os_sleepto_ns(uint64_t time_target)
EXPORT size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
EXPORT uint64_t os_get_proc_resident_size(void)
EXPORT void os_end_high_performance(os_performance_token_t *)
EXPORT int os_get_logical_cores(void)
EXPORT size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
struct os_cpu_usage_info os_cpu_usage_info_t
Definition: platform.h:89
EXPORT struct os_dirent * os_readdir(os_dir_t *dir)
const void os_performance_token_t
Definition: platform.h:95
#define os_stat
Definition: platform.h:40
EXPORT int os_get_physical_cores(void)
EXPORT void os_dlclose(void *module)
EXPORT const char * os_get_path_extension(const char *path)
EXPORT char * os_get_program_data_path_ptr(const char *name)
EXPORT int64_t os_ftelli64(FILE *file)
EXPORT int os_copyfile(const char *file_in, const char *file_out)
__int64 int64_t
Definition: vc_stdint.h:32
uint64_t resident_size
Definition: platform.h:190
EXPORT bool os_file_exists(const char *path)
EXPORT void os_globfree(os_glob_t *pglob)
EXPORT int os_glob(const char *pattern, int flags, os_glob_t **pglob)
EXPORT bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
EXPORT int os_rmdir(const char *path)