platform.c 15 KB

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