platform.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  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. #define _FILE_OFFSET_BITS 64
  17. #include <time.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <inttypes.h>
  21. #include <locale.h>
  22. #include "c99defs.h"
  23. #include "platform.h"
  24. #include "bmem.h"
  25. #include "utf8.h"
  26. #include "dstr.h"
  27. #include "obs.h"
  28. #include "threading.h"
  29. FILE *os_wfopen(const wchar_t *path, const char *mode)
  30. {
  31. FILE *file = NULL;
  32. if (path) {
  33. #ifdef _MSC_VER
  34. wchar_t *wcs_mode;
  35. os_utf8_to_wcs_ptr(mode, 0, &wcs_mode);
  36. file = _wfopen(path, wcs_mode);
  37. bfree(wcs_mode);
  38. #else
  39. char *mbs_path;
  40. os_wcs_to_utf8_ptr(path, 0, &mbs_path);
  41. file = fopen(mbs_path, mode);
  42. bfree(mbs_path);
  43. #endif
  44. }
  45. return file;
  46. }
  47. FILE *os_fopen(const char *path, const char *mode)
  48. {
  49. #ifdef _WIN32
  50. wchar_t *wpath = NULL;
  51. FILE *file = NULL;
  52. if (path) {
  53. os_utf8_to_wcs_ptr(path, 0, &wpath);
  54. file = os_wfopen(wpath, mode);
  55. bfree(wpath);
  56. }
  57. return file;
  58. #else
  59. return path ? fopen(path, mode) : NULL;
  60. #endif
  61. }
  62. int64_t os_fgetsize(FILE *file)
  63. {
  64. int64_t cur_offset = os_ftelli64(file);
  65. int64_t size;
  66. int errval = 0;
  67. if (fseek(file, 0, SEEK_END) == -1)
  68. return -1;
  69. size = os_ftelli64(file);
  70. if (size == -1)
  71. errval = errno;
  72. if (os_fseeki64(file, cur_offset, SEEK_SET) != 0 && errval != 0)
  73. errno = errval;
  74. return size;
  75. }
  76. #ifdef _WIN32
  77. int os_stat(const char *file, struct stat *st)
  78. {
  79. if (file) {
  80. wchar_t w_file[512];
  81. size_t size = os_utf8_to_wcs(file, 0, w_file, sizeof(w_file));
  82. if (size > 0) {
  83. struct _stat st_w32;
  84. int ret = _wstat(w_file, &st_w32);
  85. if (ret == 0) {
  86. st->st_dev = st_w32.st_dev;
  87. st->st_ino = st_w32.st_ino;
  88. st->st_mode = st_w32.st_mode;
  89. st->st_nlink = st_w32.st_nlink;
  90. st->st_uid = st_w32.st_uid;
  91. st->st_gid = st_w32.st_gid;
  92. st->st_rdev = st_w32.st_rdev;
  93. st->st_size = st_w32.st_size;
  94. st->st_atime = st_w32.st_atime;
  95. st->st_mtime = st_w32.st_mtime;
  96. st->st_ctime = st_w32.st_ctime;
  97. }
  98. return ret;
  99. }
  100. }
  101. return -1;
  102. }
  103. #endif
  104. int os_fseeki64(FILE *file, int64_t offset, int origin)
  105. {
  106. #ifdef _MSC_VER
  107. return _fseeki64(file, offset, origin);
  108. #else
  109. return fseeko(file, offset, origin);
  110. #endif
  111. }
  112. int64_t os_ftelli64(FILE *file)
  113. {
  114. #ifdef _MSC_VER
  115. return _ftelli64(file);
  116. #else
  117. return ftello(file);
  118. #endif
  119. }
  120. size_t os_fread_mbs(FILE *file, char **pstr)
  121. {
  122. size_t size = 0;
  123. size_t len = 0;
  124. fseek(file, 0, SEEK_END);
  125. size = (size_t)os_ftelli64(file);
  126. *pstr = NULL;
  127. if (size > 0) {
  128. char *mbstr = bmalloc(size + 1);
  129. fseek(file, 0, SEEK_SET);
  130. size = fread(mbstr, 1, size, file);
  131. if (size == 0) {
  132. bfree(mbstr);
  133. return 0;
  134. }
  135. mbstr[size] = 0;
  136. len = os_mbs_to_utf8_ptr(mbstr, size, pstr);
  137. bfree(mbstr);
  138. }
  139. return len;
  140. }
  141. size_t os_fread_utf8(FILE *file, char **pstr)
  142. {
  143. size_t size = 0;
  144. size_t len = 0;
  145. *pstr = NULL;
  146. fseek(file, 0, SEEK_END);
  147. size = (size_t)os_ftelli64(file);
  148. if (size > 0) {
  149. char bom[3];
  150. char *utf8str;
  151. off_t offset;
  152. bom[0] = 0;
  153. bom[1] = 0;
  154. bom[2] = 0;
  155. /* remove the ghastly BOM if present */
  156. fseek(file, 0, SEEK_SET);
  157. size_t size_read = fread(bom, 1, 3, file);
  158. (void)size_read;
  159. offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;
  160. size -= offset;
  161. if (size == 0)
  162. return 0;
  163. utf8str = bmalloc(size + 1);
  164. fseek(file, offset, SEEK_SET);
  165. size = fread(utf8str, 1, size, file);
  166. if (size == 0) {
  167. bfree(utf8str);
  168. return 0;
  169. }
  170. utf8str[size] = 0;
  171. *pstr = utf8str;
  172. }
  173. return len;
  174. }
  175. char *os_quick_read_mbs_file(const char *path)
  176. {
  177. FILE *f = os_fopen(path, "rb");
  178. char *file_string = NULL;
  179. if (!f)
  180. return NULL;
  181. os_fread_mbs(f, &file_string);
  182. fclose(f);
  183. return file_string;
  184. }
  185. char *os_quick_read_utf8_file(const char *path)
  186. {
  187. FILE *f = os_fopen(path, "rb");
  188. char *file_string = NULL;
  189. if (!f)
  190. return NULL;
  191. os_fread_utf8(f, &file_string);
  192. fclose(f);
  193. return file_string;
  194. }
  195. bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
  196. {
  197. FILE *f = os_fopen(path, "wb");
  198. char *mbs = NULL;
  199. size_t mbs_len = 0;
  200. if (!f)
  201. return false;
  202. mbs_len = os_utf8_to_mbs_ptr(str, len, &mbs);
  203. if (mbs_len)
  204. fwrite(mbs, 1, mbs_len, f);
  205. bfree(mbs);
  206. fflush(f);
  207. fclose(f);
  208. return true;
  209. }
  210. bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
  211. bool marker)
  212. {
  213. FILE *f = os_fopen(path, "wb");
  214. if (!f)
  215. return false;
  216. if (marker) {
  217. if (fwrite("\xEF\xBB\xBF", 3, 1, f) != 1) {
  218. fclose(f);
  219. return false;
  220. }
  221. }
  222. if (len) {
  223. if (fwrite(str, len, 1, f) != 1) {
  224. fclose(f);
  225. return false;
  226. }
  227. }
  228. fflush(f);
  229. fclose(f);
  230. return true;
  231. }
  232. bool os_quick_write_utf8_file_safe(const char *path, const char *str,
  233. size_t len, bool marker,
  234. const char *temp_ext, const char *backup_ext)
  235. {
  236. struct dstr backup_path = {0};
  237. struct dstr temp_path = {0};
  238. bool success = false;
  239. if (!temp_ext || !*temp_ext) {
  240. blog(LOG_ERROR, "os_quick_write_utf8_file_safe: invalid "
  241. "temporary extension specified");
  242. return false;
  243. }
  244. dstr_copy(&temp_path, path);
  245. if (*temp_ext != '.')
  246. dstr_cat(&temp_path, ".");
  247. dstr_cat(&temp_path, temp_ext);
  248. if (!os_quick_write_utf8_file(temp_path.array, str, len, marker)) {
  249. blog(LOG_ERROR,
  250. "os_quick_write_utf8_file_safe: failed to "
  251. "write to %s",
  252. temp_path.array);
  253. goto cleanup;
  254. }
  255. if (backup_ext && *backup_ext) {
  256. dstr_copy(&backup_path, path);
  257. if (*backup_ext != '.')
  258. dstr_cat(&backup_path, ".");
  259. dstr_cat(&backup_path, backup_ext);
  260. }
  261. if (os_safe_replace(path, temp_path.array, backup_path.array) == 0)
  262. success = true;
  263. cleanup:
  264. dstr_free(&backup_path);
  265. dstr_free(&temp_path);
  266. return success;
  267. }
  268. int64_t os_get_file_size(const char *path)
  269. {
  270. FILE *f = os_fopen(path, "rb");
  271. if (!f)
  272. return -1;
  273. int64_t sz = os_fgetsize(f);
  274. fclose(f);
  275. return sz;
  276. }
  277. size_t os_mbs_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
  278. {
  279. UNUSED_PARAMETER(len);
  280. size_t out_len;
  281. if (!str)
  282. return 0;
  283. out_len = dst ? (dst_size - 1) : mbstowcs(NULL, str, 0);
  284. if (dst) {
  285. if (!dst_size)
  286. return 0;
  287. if (out_len)
  288. out_len = mbstowcs(dst, str, out_len + 1);
  289. dst[out_len] = 0;
  290. }
  291. return out_len;
  292. }
  293. size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
  294. size_t dst_size)
  295. {
  296. size_t in_len;
  297. size_t out_len;
  298. if (!str)
  299. return 0;
  300. in_len = len ? len : strlen(str);
  301. out_len = dst ? (dst_size - 1) : utf8_to_wchar(str, in_len, NULL, 0, 0);
  302. if (dst) {
  303. if (!dst_size)
  304. return 0;
  305. if (out_len)
  306. out_len =
  307. utf8_to_wchar(str, in_len, dst, out_len + 1, 0);
  308. dst[out_len] = 0;
  309. }
  310. return out_len;
  311. }
  312. size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size)
  313. {
  314. UNUSED_PARAMETER(len);
  315. size_t out_len;
  316. if (!str)
  317. return 0;
  318. out_len = dst ? (dst_size - 1) : wcstombs(NULL, str, 0);
  319. if (dst) {
  320. if (!dst_size)
  321. return 0;
  322. if (out_len)
  323. out_len = wcstombs(dst, str, out_len + 1);
  324. dst[out_len] = 0;
  325. }
  326. return out_len;
  327. }
  328. size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
  329. size_t dst_size)
  330. {
  331. size_t in_len;
  332. size_t out_len;
  333. if (!str)
  334. return 0;
  335. in_len = (len != 0) ? len : wcslen(str);
  336. out_len = dst ? (dst_size - 1) : wchar_to_utf8(str, in_len, NULL, 0, 0);
  337. if (dst) {
  338. if (!dst_size)
  339. return 0;
  340. if (out_len)
  341. out_len = wchar_to_utf8(str, in_len, dst, out_len, 0);
  342. dst[out_len] = 0;
  343. }
  344. return out_len;
  345. }
  346. size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  347. {
  348. if (str) {
  349. size_t out_len = os_mbs_to_wcs(str, len, NULL, 0);
  350. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  351. return os_mbs_to_wcs(str, len, *pstr, out_len + 1);
  352. } else {
  353. *pstr = NULL;
  354. return 0;
  355. }
  356. }
  357. size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  358. {
  359. if (str) {
  360. size_t out_len = os_utf8_to_wcs(str, len, NULL, 0);
  361. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  362. return os_utf8_to_wcs(str, len, *pstr, out_len + 1);
  363. } else {
  364. *pstr = NULL;
  365. return 0;
  366. }
  367. }
  368. size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr)
  369. {
  370. if (str) {
  371. size_t out_len = os_wcs_to_mbs(str, len, NULL, 0);
  372. *pstr = bmalloc((out_len + 1) * sizeof(char));
  373. return os_wcs_to_mbs(str, len, *pstr, out_len + 1);
  374. } else {
  375. *pstr = NULL;
  376. return 0;
  377. }
  378. }
  379. size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
  380. {
  381. if (str) {
  382. size_t out_len = os_wcs_to_utf8(str, len, NULL, 0);
  383. *pstr = bmalloc((out_len + 1) * sizeof(char));
  384. return os_wcs_to_utf8(str, len, *pstr, out_len + 1);
  385. } else {
  386. *pstr = NULL;
  387. return 0;
  388. }
  389. }
  390. size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
  391. {
  392. char *dst = NULL;
  393. size_t out_len = 0;
  394. if (str) {
  395. wchar_t *wstr = NULL;
  396. size_t wlen = os_utf8_to_wcs_ptr(str, len, &wstr);
  397. out_len = os_wcs_to_mbs_ptr(wstr, wlen, &dst);
  398. bfree(wstr);
  399. }
  400. *pstr = dst;
  401. return out_len;
  402. }
  403. size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
  404. {
  405. char *dst = NULL;
  406. size_t out_len = 0;
  407. if (str) {
  408. wchar_t *wstr = NULL;
  409. size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
  410. out_len = os_wcs_to_utf8_ptr(wstr, wlen, &dst);
  411. bfree(wstr);
  412. }
  413. *pstr = dst;
  414. return out_len;
  415. }
  416. /* locale independent double conversion from jansson, credit goes to them */
  417. static inline void to_locale(char *str)
  418. {
  419. const char *point;
  420. char *pos;
  421. point = localeconv()->decimal_point;
  422. if (*point == '.') {
  423. /* No conversion needed */
  424. return;
  425. }
  426. pos = strchr(str, '.');
  427. if (pos)
  428. *pos = *point;
  429. }
  430. static inline void from_locale(char *buffer)
  431. {
  432. const char *point;
  433. char *pos;
  434. point = localeconv()->decimal_point;
  435. if (*point == '.') {
  436. /* No conversion needed */
  437. return;
  438. }
  439. pos = strchr(buffer, *point);
  440. if (pos)
  441. *pos = '.';
  442. }
  443. double os_strtod(const char *str)
  444. {
  445. char buf[64];
  446. strncpy(buf, str, sizeof(buf) - 1);
  447. buf[sizeof(buf) - 1] = 0;
  448. to_locale(buf);
  449. return strtod(buf, NULL);
  450. }
  451. int os_dtostr(double value, char *dst, size_t size)
  452. {
  453. int ret;
  454. char *start, *end;
  455. size_t length;
  456. ret = snprintf(dst, size, "%.17g", value);
  457. if (ret < 0)
  458. return -1;
  459. length = (size_t)ret;
  460. if (length >= size)
  461. return -1;
  462. from_locale(dst);
  463. /* Make sure there's a dot or 'e' in the output. Otherwise
  464. a real is converted to an integer when decoding */
  465. if (strchr(dst, '.') == NULL && strchr(dst, 'e') == NULL) {
  466. if (length + 3 >= size) {
  467. /* No space to append ".0" */
  468. return -1;
  469. }
  470. dst[length] = '.';
  471. dst[length + 1] = '0';
  472. dst[length + 2] = '\0';
  473. length += 2;
  474. }
  475. /* Remove leading '+' from positive exponent. Also remove leading
  476. zeros from exponents (added by some printf() implementations) */
  477. start = strchr(dst, 'e');
  478. if (start) {
  479. start++;
  480. end = start + 1;
  481. if (*start == '-')
  482. start++;
  483. while (*end == '0')
  484. end++;
  485. if (end != start) {
  486. memmove(start, end, length - (size_t)(end - dst));
  487. length -= (size_t)(end - start);
  488. }
  489. }
  490. return (int)length;
  491. }
  492. static int recursive_mkdir(char *path)
  493. {
  494. char *last_slash;
  495. int ret;
  496. ret = os_mkdir(path);
  497. if (ret != MKDIR_ERROR)
  498. return ret;
  499. last_slash = strrchr(path, '/');
  500. if (!last_slash)
  501. return MKDIR_ERROR;
  502. *last_slash = 0;
  503. ret = recursive_mkdir(path);
  504. *last_slash = '/';
  505. if (ret == MKDIR_ERROR)
  506. return MKDIR_ERROR;
  507. ret = os_mkdir(path);
  508. return ret;
  509. }
  510. int os_mkdirs(const char *dir)
  511. {
  512. struct dstr dir_str;
  513. int ret;
  514. dstr_init_copy(&dir_str, dir);
  515. dstr_replace(&dir_str, "\\", "/");
  516. ret = recursive_mkdir(dir_str.array);
  517. dstr_free(&dir_str);
  518. return ret;
  519. }
  520. const char *os_get_path_extension(const char *path)
  521. {
  522. for (size_t pos = strlen(path); pos > 0; pos--) {
  523. switch (path[pos - 1]) {
  524. case '.':
  525. return path + pos - 1;
  526. case '/':
  527. case '\\':
  528. return NULL;
  529. }
  530. }
  531. return NULL;
  532. }
  533. static inline bool valid_string(const char *str)
  534. {
  535. while (str && *str) {
  536. if (*(str++) != ' ')
  537. return true;
  538. }
  539. return false;
  540. }
  541. static void replace_text(struct dstr *str, size_t pos, size_t len,
  542. const char *new_text)
  543. {
  544. struct dstr front = {0};
  545. struct dstr back = {0};
  546. dstr_left(&front, str, pos);
  547. dstr_right(&back, str, pos + len);
  548. dstr_copy_dstr(str, &front);
  549. dstr_cat(str, new_text);
  550. dstr_cat_dstr(str, &back);
  551. dstr_free(&front);
  552. dstr_free(&back);
  553. }
  554. static void erase_ch(struct dstr *str, size_t pos)
  555. {
  556. struct dstr new_str = {0};
  557. dstr_left(&new_str, str, pos);
  558. dstr_cat(&new_str, str->array + pos + 1);
  559. dstr_free(str);
  560. *str = new_str;
  561. }
  562. char *os_generate_formatted_filename(const char *extension, bool space,
  563. const char *format)
  564. {
  565. time_t now = time(0);
  566. struct tm *cur_time;
  567. cur_time = localtime(&now);
  568. struct obs_video_info ovi;
  569. obs_get_video_info(&ovi);
  570. const size_t spec_count = 23;
  571. static const char *spec[][2] = {
  572. {"%CCYY", "%Y"}, {"%YY", "%y"}, {"%MM", "%m"}, {"%DD", "%d"},
  573. {"%hh", "%H"}, {"%mm", "%M"}, {"%ss", "%S"}, {"%%", "%%"},
  574. {"%a", ""}, {"%A", ""}, {"%b", ""}, {"%B", ""},
  575. {"%d", ""}, {"%H", ""}, {"%I", ""}, {"%m", ""},
  576. {"%M", ""}, {"%p", ""}, {"%S", ""}, {"%y", ""},
  577. {"%Y", ""}, {"%z", ""}, {"%Z", ""},
  578. };
  579. char convert[128] = {0};
  580. struct dstr sf;
  581. struct dstr c = {0};
  582. size_t pos = 0;
  583. dstr_init_copy(&sf, format);
  584. while (pos < sf.len) {
  585. const char *cmp = sf.array + pos;
  586. for (size_t i = 0; i < spec_count && !convert[0]; i++) {
  587. size_t len = strlen(spec[i][0]);
  588. if (astrcmp_n(cmp, spec[i][0], len) == 0) {
  589. if (strlen(spec[i][1]))
  590. strftime(convert, sizeof(convert),
  591. spec[i][1], cur_time);
  592. else
  593. strftime(convert, sizeof(convert),
  594. spec[i][0], cur_time);
  595. dstr_copy(&c, convert);
  596. if (c.len && valid_string(c.array))
  597. replace_text(&sf, pos, len, convert);
  598. }
  599. }
  600. if (!convert[0]) {
  601. if (astrcmp_n(cmp, "%FPS", 4) == 0) {
  602. if (ovi.fps_den <= 1) {
  603. snprintf(convert, sizeof(convert), "%u",
  604. ovi.fps_num);
  605. } else {
  606. const double obsFPS =
  607. (double)ovi.fps_num /
  608. (double)ovi.fps_den;
  609. snprintf(convert, sizeof(convert),
  610. "%.2f", obsFPS);
  611. }
  612. replace_text(&sf, pos, 4, convert);
  613. } else if (astrcmp_n(cmp, "%CRES", 5) == 0) {
  614. snprintf(convert, sizeof(convert), "%ux%u",
  615. ovi.base_width, ovi.base_height);
  616. replace_text(&sf, pos, 5, convert);
  617. } else if (astrcmp_n(cmp, "%ORES", 5) == 0) {
  618. snprintf(convert, sizeof(convert), "%ux%u",
  619. ovi.output_width, ovi.output_height);
  620. replace_text(&sf, pos, 5, convert);
  621. } else if (astrcmp_n(cmp, "%VF", 3) == 0) {
  622. strcpy(convert, get_video_format_name(
  623. ovi.output_format));
  624. replace_text(&sf, pos, 3, convert);
  625. } else if (astrcmp_n(cmp, "%s", 2) == 0) {
  626. snprintf(convert, sizeof(convert), "%" PRId64,
  627. (int64_t)now);
  628. replace_text(&sf, pos, 2, convert);
  629. }
  630. }
  631. if (convert[0]) {
  632. pos += strlen(convert);
  633. convert[0] = 0;
  634. } else if (!convert[0] && sf.array[pos] == '%') {
  635. erase_ch(&sf, pos);
  636. } else {
  637. pos++;
  638. }
  639. }
  640. if (!space)
  641. dstr_replace(&sf, " ", "_");
  642. if (extension && *extension) {
  643. dstr_cat_ch(&sf, '.');
  644. dstr_cat(&sf, extension);
  645. }
  646. dstr_free(&c);
  647. if (sf.len > 255)
  648. dstr_mid(&sf, &sf, 0, 255);
  649. return sf.array;
  650. }
  651. static struct {
  652. struct timespec ts;
  653. bool ts_valid;
  654. uint64_t timestamp;
  655. } timespec_offset = {0};
  656. static void init_timespec_offset(void)
  657. {
  658. timespec_offset.ts_valid =
  659. timespec_get(&timespec_offset.ts, TIME_UTC) == TIME_UTC;
  660. timespec_offset.timestamp = os_gettime_ns();
  661. }
  662. struct timespec *os_nstime_to_timespec(uint64_t timestamp,
  663. struct timespec *storage)
  664. {
  665. static pthread_once_t once = PTHREAD_ONCE_INIT;
  666. pthread_once(&once, init_timespec_offset);
  667. if (!storage || !timespec_offset.ts_valid) {
  668. return NULL;
  669. }
  670. *storage = timespec_offset.ts;
  671. static const int64_t nsecs_per_sec = 1000000000;
  672. int64_t nsecs = 0;
  673. int64_t secs = 0;
  674. if (timestamp >= timespec_offset.timestamp) {
  675. uint64_t offset = timestamp - timespec_offset.timestamp;
  676. nsecs = storage->tv_nsec + offset % nsecs_per_sec;
  677. secs = storage->tv_sec + offset / nsecs_per_sec;
  678. } else {
  679. uint64_t offset = timespec_offset.timestamp - timestamp;
  680. int64_t nsec_offset = offset % nsecs_per_sec;
  681. int64_t sec_offset = offset / nsecs_per_sec;
  682. int64_t tv_nsec = storage->tv_nsec;
  683. if (nsec_offset > tv_nsec) {
  684. storage->tv_sec -= 1;
  685. tv_nsec += nsecs_per_sec;
  686. }
  687. nsecs = tv_nsec - nsec_offset;
  688. secs = storage->tv_sec - sec_offset;
  689. }
  690. if (nsecs > nsecs_per_sec) {
  691. nsecs -= nsecs_per_sec;
  692. secs += 1;
  693. }
  694. storage->tv_nsec = (long)nsecs;
  695. storage->tv_sec = (time_t)secs;
  696. return storage;
  697. }