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, const char *temp_ext,
55  const char *backup_ext);
56 EXPORT char *os_quick_read_mbs_file(const char *path);
57 EXPORT bool os_quick_write_mbs_file(const char *path, const char *str,
58  size_t len);
59 
60 EXPORT int64_t os_get_file_size(const char *path);
61 EXPORT int64_t os_get_free_space(const char *path);
62 
63 EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst,
64  size_t dst_size);
65 EXPORT size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
66  size_t dst_size);
67 EXPORT size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst,
68  size_t dst_size);
69 EXPORT size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
70  size_t dst_size);
71 
72 EXPORT size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
73 EXPORT size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr);
74 EXPORT size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr);
75 EXPORT size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr);
76 
77 EXPORT size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr);
78 EXPORT size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr);
79 
80 EXPORT double os_strtod(const char *str);
81 EXPORT int os_dtostr(double value, char *dst, size_t size);
82 
83 EXPORT void *os_dlopen(const char *path);
84 EXPORT void *os_dlsym(void *module, const char *func);
85 EXPORT void os_dlclose(void *module);
86 
87 struct os_cpu_usage_info;
88 typedef struct os_cpu_usage_info os_cpu_usage_info_t;
89 
93 
94 typedef const void os_performance_token_t;
97 
103 EXPORT bool os_sleepto_ns(uint64_t time_target);
104 EXPORT void os_sleep_ms(uint32_t duration);
105 
107 
108 EXPORT int os_get_config_path(char *dst, size_t size, const char *name);
109 EXPORT char *os_get_config_path_ptr(const char *name);
110 
111 EXPORT int os_get_program_data_path(char *dst, size_t size, const char *name);
112 EXPORT char *os_get_program_data_path_ptr(const char *name);
113 
114 EXPORT bool os_file_exists(const char *path);
115 
116 EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size);
117 EXPORT char *os_get_abs_path_ptr(const char *path);
118 
119 EXPORT const char *os_get_path_extension(const char *path);
120 
121 struct os_dir;
122 typedef struct os_dir os_dir_t;
123 
124 struct os_dirent {
125  char d_name[256];
126  bool directory;
127 };
128 
129 EXPORT os_dir_t *os_opendir(const char *path);
130 EXPORT struct os_dirent *os_readdir(os_dir_t *dir);
131 EXPORT void os_closedir(os_dir_t *dir);
132 
133 struct os_globent {
134  char *path;
135  bool directory;
136 };
137 
138 struct os_glob_info {
139  size_t gl_pathc;
141 };
142 
143 typedef struct os_glob_info os_glob_t;
144 
145 /* currently no flags available */
146 
147 EXPORT int os_glob(const char *pattern, int flags, os_glob_t **pglob);
148 EXPORT void os_globfree(os_glob_t *pglob);
149 
150 EXPORT int os_unlink(const char *path);
151 EXPORT int os_rmdir(const char *path);
152 
153 EXPORT char *os_getcwd(char *path, size_t size);
154 EXPORT int os_chdir(const char *path);
155 
156 EXPORT uint64_t os_get_free_disk_space(const char *dir);
157 
158 #define MKDIR_EXISTS 1
159 #define MKDIR_SUCCESS 0
160 #define MKDIR_ERROR -1
161 
162 EXPORT int os_mkdir(const char *path);
163 EXPORT int os_mkdirs(const char *path);
164 EXPORT int os_rename(const char *old_path, const char *new_path);
165 EXPORT int os_copyfile(const char *file_in, const char *file_out);
166 EXPORT int os_safe_replace(const char *target_path, const char *from_path,
167  const char *backup_path);
168 
169 EXPORT char *os_generate_formatted_filename(const char *extension, bool space,
170  const char *format);
171 
172 struct os_inhibit_info;
173 typedef struct os_inhibit_info os_inhibit_t;
174 
175 EXPORT os_inhibit_t *os_inhibit_sleep_create(const char *reason);
176 EXPORT bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active);
178 
179 EXPORT void os_breakpoint(void);
180 
181 EXPORT int os_get_physical_cores(void);
182 EXPORT int os_get_logical_cores(void);
183 
185 
189 };
191 
195 
196 #ifdef _MSC_VER
197 #define strtoll _strtoi64
198 #if _MSC_VER < 1900
199 #define snprintf _snprintf
200 #endif
201 #endif
202 
203 #ifdef __APPLE__
204 # define ARCH_BITS 64
205 #else
206 # ifdef _WIN32
207 # ifdef _WIN64
208 # define ARCH_BITS 64
209 # else
210 # define ARCH_BITS 32
211 # endif
212 # else
213 # ifdef __LP64__
214 # define ARCH_BITS 64
215 # else
216 # define ARCH_BITS 32
217 # endif
218 # endif
219 #endif
220 
221 #ifdef __cplusplus
222 }
223 #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:140
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:173
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:126
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:122
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:188
EXPORT void os_sleep_ms(uint32_t duration)
Definition: platform.h:124
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:125
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:134
Definition: platform.h:138
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:186
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:135
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:133
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:139
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:88
EXPORT struct os_dirent * os_readdir(os_dir_t *dir)
const void os_performance_token_t
Definition: platform.h:94
#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:187
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)