platform.c 15 KB

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