reference-libobs-util-platform.rst 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. Platform Helpers
  2. ================
  3. These functions/structures/types are used to perform actions that
  4. typically don't have shared functions across different operating systems
  5. and platforms.
  6. .. code:: cpp
  7. #include <util/platform.h>
  8. File Functions
  9. --------------
  10. .. function:: FILE *os_wfopen(const wchar_t *path, const char *mode)
  11. Opens a file with a wide string path.
  12. ----------------------
  13. .. function:: FILE *os_fopen(const char *path, const char *mode)
  14. Opens a file with a UTF8 string path.
  15. ----------------------
  16. .. function:: int64_t os_fgetsize(FILE *file)
  17. Returns a file's size.
  18. ----------------------
  19. .. function:: int os_stat(const char *file, struct stat *st)
  20. Equivalent to the posix *stat* function.
  21. ----------------------
  22. .. function:: int os_fseeki64(FILE *file, int64_t offset, int origin)
  23. Equivalent to fseek.
  24. ----------------------
  25. .. function:: int64_t os_ftelli64(FILE *file)
  26. Gets the current file position.
  27. ----------------------
  28. .. function:: size_t os_fread_utf8(FILE *file, char **pstr)
  29. Reads a UTF8 encoded file and allocates a pointer to the UTF8 string.
  30. ----------------------
  31. .. function:: char *os_quick_read_utf8_file(const char *path)
  32. Reads a UTF8 encoded file and returns an allocated pointer to the
  33. string.
  34. ----------------------
  35. .. function:: bool os_quick_write_utf8_file(const char *path, const char *str, size_t len, bool marker)
  36. Writes a UTF8 encoded file.
  37. ----------------------
  38. .. function:: 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)
  39. Writes a UTF8 encoded file with overwrite corruption prevention.
  40. ----------------------
  41. .. function:: int64_t os_get_file_size(const char *path)
  42. Gets a file's size.
  43. ----------------------
  44. .. function:: int64_t os_get_free_space(const char *path)
  45. Gets free space of a specific file path.
  46. ---------------------
  47. String Conversion Functions
  48. ---------------------------
  49. .. function:: size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
  50. Converts a UTF8 string to a wide string.
  51. ----------------------
  52. .. function:: size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst, size_t dst_size)
  53. Converts a wide string to a UTF8 string.
  54. ----------------------
  55. .. function:: size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  56. Gets an bmalloc-allocated wide string converted from a UTF8 string.
  57. ----------------------
  58. .. function:: size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
  59. Gets an bmalloc-allocated UTF8 string converted from a wide string.
  60. ---------------------
  61. Number/String Conversion Functions
  62. ----------------------------------
  63. .. function:: double os_strtod(const char *str)
  64. Converts a string to a double.
  65. ----------------------
  66. .. function:: int os_dtostr(double value, char *dst, size_t size)
  67. Converts a double to a string.
  68. ---------------------
  69. Dynamic Link Library Functions
  70. ------------------------------
  71. These functions are roughly equivalent to dlopen/dlsym/dlclose.
  72. .. function:: void *os_dlopen(const char *path)
  73. Opens a dynamic library.
  74. ----------------------
  75. .. function:: void *os_dlsym(void *module, const char *func)
  76. Returns a symbol from a dynamic library.
  77. ----------------------
  78. .. function:: void os_dlclose(void *module)
  79. Closes a dynamic library.
  80. ---------------------
  81. CPU Usage Functions
  82. -------------------
  83. .. function:: os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  84. Creates a CPU usage information object.
  85. ----------------------
  86. .. function:: double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  87. Queries the current CPU usage.
  88. ----------------------
  89. .. function:: void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  90. Destroys a CPU usage information object.
  91. ---------------------
  92. Sleep/Time Functions
  93. --------------------
  94. .. function:: bool os_sleepto_ns(uint64_t time_target)
  95. Sleeps to a specific time with high precision, in nanoseconds.
  96. ---------------------
  97. .. function:: void os_sleep_ms(uint32_t duration)
  98. Sleeps for a specific number of milliseconds.
  99. ---------------------
  100. .. function:: uint64_t os_gettime_ns(void)
  101. Gets the current high-precision system time, in nanoseconds.
  102. ---------------------
  103. Other Path/File Functions
  104. -------------------------
  105. .. function:: int os_get_config_path(char *dst, size_t size, const char *name)
  106. char *os_get_config_path_ptr(const char *name)
  107. Gets the user-specific application configuration data path.
  108. ---------------------
  109. .. function:: int os_get_program_data_path(char *dst, size_t size, const char *name)
  110. char *os_get_program_data_path_ptr(const char *name)
  111. Gets the application configuration data path.
  112. ---------------------
  113. .. function:: bool os_file_exists(const char *path)
  114. Returns true if a file/directory exists, false otherwise.
  115. ---------------------
  116. .. function:: size_t os_get_abs_path(const char *path, char *abspath, size_t size)
  117. char *os_get_abs_path_ptr(const char *path)
  118. Converts a relative path to an absolute path.
  119. ---------------------
  120. .. function:: const char *os_get_path_extension(const char *path)
  121. Returns the extension portion of a path string.
  122. ---------------------
  123. .. type:: typedef struct os_dir os_dir_t
  124. A directory object.
  125. .. type:: struct os_dirent
  126. A directory entry record.
  127. .. member:: char os_dirent.d_name[256]
  128. The directory entry name.
  129. .. member:: bool os_dirent.directory
  130. *true* if the entry is a directory.
  131. ---------------------
  132. .. function:: os_dir_t *os_opendir(const char *path)
  133. Opens a directory object to enumerate files within the directory.
  134. ---------------------
  135. .. function:: struct os_dirent *os_readdir(os_dir_t *dir)
  136. Returns the linked list of directory entries.
  137. ---------------------
  138. .. function:: void os_closedir(os_dir_t *dir)
  139. Closes a directory object.
  140. ---------------------
  141. .. type:: struct os_globent
  142. A glob entry.
  143. .. member:: char *os_globent.path
  144. The full path to the glob entry.
  145. .. member:: bool os_globent.directory
  146. *true* if the glob entry is a directory, *false* otherwise.
  147. .. type:: struct os_glob_info
  148. A glob object.
  149. .. member:: size_t os_glob_info.gl_pathc
  150. Number of glob entries.
  151. .. member:: struct os_globent *os_glob_info.gl_pathv
  152. Array of glob entries.
  153. .. type:: typedef struct os_glob_info os_glob_t
  154. ---------------------
  155. .. function:: int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  156. Enumerates files based upon a glob string.
  157. ---------------------
  158. .. function:: void os_globfree(os_glob_t *pglob)
  159. Frees a glob object.
  160. ---------------------
  161. .. function:: int os_unlink(const char *path)
  162. Deletes a file.
  163. ---------------------
  164. .. function:: int os_rmdir(const char *path)
  165. Deletes a directory.
  166. ---------------------
  167. .. function:: char *os_getcwd(char *path, size_t size)
  168. Returns a new bmalloc-allocated path to the current working
  169. directory.
  170. ---------------------
  171. .. function:: int os_chdir(const char *path)
  172. Changes the current working directory.
  173. ---------------------
  174. .. function:: int os_mkdir(const char *path)
  175. Creates a directory.
  176. ---------------------
  177. .. function:: int os_mkdirs(const char *path)
  178. Creates a full directory path if it doesn't exist.
  179. ---------------------
  180. .. function:: int os_rename(const char *old_path, const char *new_path)
  181. Renames a file.
  182. ---------------------
  183. .. function:: int os_copyfile(const char *file_in, const char *file_out)
  184. Copys a file.
  185. ---------------------
  186. .. function:: int os_safe_replace(const char *target_path, const char *from_path, const char *backup_path)
  187. Safely replaces a file.
  188. ---------------------
  189. .. function:: char *os_generate_formatted_filename(const char *extension, bool space, const char *format)
  190. Returns a new bmalloc-allocated filename generated from specific
  191. formatting.
  192. ---------------------
  193. Sleep-Inhibition Functions
  194. --------------------------
  195. These functions/types are used to inhibit the computer from going to
  196. sleep.
  197. .. type:: struct os_inhibit_info
  198. .. type:: typedef struct os_inhibit_info os_inhibit_t
  199. ---------------------
  200. .. function:: os_inhibit_t *os_inhibit_sleep_create(const char *reason)
  201. Creates a sleep inhibition object.
  202. ---------------------
  203. .. function:: bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
  204. Activates/deactivates a sleep inhibition object.
  205. ---------------------
  206. .. function:: void os_inhibit_sleep_destroy(os_inhibit_t *info)
  207. Destroys a sleep inhibition object. If the sleep inhibition object
  208. was active, it will be deactivated.
  209. ---------------------
  210. Other Functions
  211. ---------------
  212. .. function:: void os_breakpoint(void)
  213. Triggers a debugger breakpoint (or crashes the program if no debugger
  214. present).
  215. ---------------------
  216. .. function:: int os_get_physical_cores(void)
  217. Returns the number of physical cores available.
  218. ---------------------
  219. .. function:: int os_get_logical_cores(void)
  220. Returns the number of logical cores available.
  221. ---------------------
  222. .. function:: uint64_t os_get_sys_free_size(void)
  223. Returns the amount of memory available.
  224. ---------------------
  225. .. type:: struct os_proc_memory_usage
  226. Memory usage structure.
  227. .. member:: uint64_t os_proc_memory_usage.resident_size
  228. Resident size.
  229. .. member:: uint64_t os_proc_memory_usage.virtual_size
  230. Virtual size.
  231. .. type:: typedef struct os_proc_memory_usage os_proc_memory_usage_t
  232. ---------------------
  233. .. function:: bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
  234. Gets memory usage of the current process.
  235. ---------------------
  236. .. function:: uint64_t os_get_proc_resident_size(void)
  237. Returns the resident memory size of the current process.
  238. ---------------------
  239. .. function:: uint64_t os_get_proc_virtual_size(void)
  240. Returns the virtual memory size of the current process.