ysfx_utils.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // Copyright 2021 Jean Pierre Cimalando
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #include "ysfx_utils.hpp"
  18. #include "base64/Base64.hpp"
  19. #include <system_error>
  20. #include <algorithm>
  21. #include <deque>
  22. #include <cctype>
  23. #include <string>
  24. #include <clocale>
  25. #include <cstring>
  26. #include <cassert>
  27. #if !defined(_WIN32)
  28. # include <sys/stat.h>
  29. # include <sys/types.h>
  30. # include <unistd.h>
  31. # include <dirent.h>
  32. # include <fcntl.h>
  33. #else
  34. # include <windows.h>
  35. # include <io.h>
  36. #endif
  37. namespace ysfx {
  38. #if !defined(_WIN32)
  39. static_assert(sizeof(off_t) == 8, "64-bit large file support is not enabled");
  40. #endif
  41. FILE *fopen_utf8(const char *path, const char *mode)
  42. {
  43. #if defined(_WIN32)
  44. return _wfopen(widen(path).c_str(), widen(mode).c_str());
  45. #else
  46. return fopen(path, mode);
  47. #endif
  48. }
  49. int64_t fseek_lfs(FILE *stream, int64_t off, int whence)
  50. {
  51. #if defined(_WIN32)
  52. return _fseeki64(stream, off, whence);
  53. #else
  54. return fseeko(stream, off, whence);
  55. #endif
  56. }
  57. int64_t ftell_lfs(FILE *stream)
  58. {
  59. #if defined(_WIN32)
  60. return _ftelli64(stream);
  61. #else
  62. return ftello(stream);
  63. #endif
  64. }
  65. //------------------------------------------------------------------------------
  66. namespace {
  67. struct scoped_c_locale
  68. {
  69. scoped_c_locale(int lc, const char *name);
  70. ~scoped_c_locale();
  71. c_locale_t m_loc{};
  72. scoped_c_locale(const scoped_c_locale &) = delete;
  73. scoped_c_locale &operator=(const scoped_c_locale &) = delete;
  74. };
  75. scoped_c_locale::scoped_c_locale(int lc, const char *name)
  76. {
  77. #if defined(_WIN32)
  78. m_loc = _create_locale(lc, name);
  79. #else
  80. switch (lc) {
  81. case LC_ALL:
  82. m_loc = newlocale(LC_ALL_MASK, name, c_locale_t{});
  83. break;
  84. case LC_CTYPE:
  85. m_loc = newlocale(LC_CTYPE_MASK, name, c_locale_t{});
  86. break;
  87. case LC_COLLATE:
  88. m_loc = newlocale(LC_COLLATE_MASK, name, c_locale_t{});
  89. break;
  90. case LC_MONETARY:
  91. m_loc = newlocale(LC_MONETARY_MASK, name, c_locale_t{});
  92. break;
  93. case LC_NUMERIC:
  94. m_loc = newlocale(LC_NUMERIC_MASK, name, c_locale_t{});
  95. break;
  96. case LC_TIME:
  97. m_loc = newlocale(LC_TIME_MASK, name, c_locale_t{});
  98. break;
  99. case LC_MESSAGES:
  100. m_loc = newlocale(LC_MESSAGES_MASK, name, c_locale_t{});
  101. break;
  102. default:
  103. errno = EINVAL;
  104. break;
  105. }
  106. #endif
  107. if (!m_loc)
  108. throw std::system_error(errno, std::generic_category());
  109. }
  110. scoped_c_locale::~scoped_c_locale()
  111. {
  112. if (!m_loc) return;
  113. #if !defined(_WIN32)
  114. freelocale(m_loc);
  115. #else
  116. _free_locale(m_loc);
  117. #endif
  118. }
  119. #if !defined(_WIN32)
  120. struct scoped_posix_uselocale {
  121. explicit scoped_posix_uselocale(c_locale_t loc);
  122. ~scoped_posix_uselocale();
  123. c_locale_t m_loc{};
  124. c_locale_t m_old{};
  125. scoped_posix_uselocale(const scoped_posix_uselocale &) = delete;
  126. scoped_posix_uselocale &operator=(const scoped_posix_uselocale &) = delete;
  127. };
  128. scoped_posix_uselocale::scoped_posix_uselocale(c_locale_t loc)
  129. {
  130. if (loc)
  131. {
  132. m_loc = loc;
  133. m_old = uselocale(loc);
  134. }
  135. }
  136. scoped_posix_uselocale::~scoped_posix_uselocale()
  137. {
  138. if (m_loc)
  139. uselocale(m_old);
  140. }
  141. #endif
  142. } // namespace
  143. //------------------------------------------------------------------------------
  144. c_locale_t c_numeric_locale()
  145. {
  146. static scoped_c_locale loc(LC_NUMERIC, "C");
  147. return loc.m_loc;
  148. }
  149. //------------------------------------------------------------------------------
  150. double c_atof(const char *text, c_locale_t loc)
  151. {
  152. #if defined(_WIN32)
  153. return _atof_l(text, loc);
  154. #else
  155. scoped_posix_uselocale use(loc);
  156. return atof(text);
  157. #endif
  158. }
  159. double c_strtod(const char *text, char **endp, c_locale_t loc)
  160. {
  161. #if defined(_WIN32)
  162. return _strtod_l(text, endp, loc);
  163. #else
  164. scoped_posix_uselocale use(loc);
  165. return strtod(text, endp);
  166. #endif
  167. }
  168. double dot_atof(const char *text)
  169. {
  170. return c_atof(text, c_numeric_locale());
  171. }
  172. double dot_strtod(const char *text, char **endp)
  173. {
  174. return c_strtod(text, endp, c_numeric_locale());
  175. }
  176. bool ascii_isspace(char c)
  177. {
  178. switch (c) {
  179. case ' ': case '\f': case '\n': case '\r': case '\t': case '\v':
  180. return true;
  181. default:
  182. return false;
  183. }
  184. }
  185. bool ascii_isalpha(char c)
  186. {
  187. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
  188. }
  189. char ascii_tolower(char c)
  190. {
  191. return (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c;
  192. }
  193. char ascii_toupper(char c)
  194. {
  195. return (c >= 'a' && c <= 'z') ? (c - 'a' + 'A') : c;
  196. }
  197. int ascii_casecmp(const char *a, const char *b)
  198. {
  199. for (char ca, cb; (ca = *a++) | (cb = *b++); ) {
  200. ca = ascii_tolower(ca);
  201. cb = ascii_tolower(cb);
  202. if (ca < cb) return -1;
  203. if (ca > cb) return +1;
  204. }
  205. return 0;
  206. }
  207. uint32_t latin1_toupper(uint32_t c)
  208. {
  209. if (c >= 'a' && c <= 'z')
  210. return c - 'a' + 'A';
  211. if ((c >= 0xe0 && c <= 0xf6) || (c >= 0xf8 && c <= 0xfe))
  212. return c - 0x20;
  213. return c;
  214. }
  215. uint32_t latin1_tolower(uint32_t c)
  216. {
  217. if (c >= 'A' && c <= 'Z')
  218. return c - 'A' + 'a';
  219. if ((c >= 0xc0 && c <= 0xd6) || (c >= 0xd8 && c <= 0xde))
  220. return c + 0x20;
  221. return c;
  222. }
  223. char *strdup_using_new(const char *src)
  224. {
  225. size_t len = strlen(src);
  226. char *dst = new char[len + 1];
  227. memcpy(dst, src, len + 1);
  228. return dst;
  229. }
  230. std::string trim_spaces_around_equals(const char* input) {
  231. auto output = std::string(input);
  232. for (size_t i = 0; (i = output.find('=', i)) != std::string::npos; ) {
  233. size_t start = i;
  234. while (start > 0 && std::isspace(output[start - 1])) --start;
  235. size_t end = i + 1;
  236. while (end < output.size() && std::isspace(output[end])) ++end;
  237. output.replace(start, end - start, "=");
  238. i = start + 1;
  239. }
  240. return output;
  241. }
  242. string_list split_strings_noempty(const char *input, bool(*pred)(char))
  243. {
  244. string_list list;
  245. if (input) {
  246. std::string acc;
  247. acc.reserve(256);
  248. for (char c; (c = *input++) != '\0'; ) {
  249. if (!pred(c))
  250. acc.push_back(c);
  251. else {
  252. if (!acc.empty()) {
  253. list.push_back(acc);
  254. acc.clear();
  255. }
  256. }
  257. }
  258. if (!acc.empty())
  259. list.push_back(acc);
  260. }
  261. return list;
  262. }
  263. std::string trim(const char *input, bool(*pred)(char))
  264. {
  265. const char *start = input;
  266. while (*start != '\0' && pred(*start))
  267. ++start;
  268. const char *end = start + strlen(start);
  269. while (end > start && pred(*(end - 1)))
  270. --end;
  271. return std::string(start, end);
  272. }
  273. //------------------------------------------------------------------------------
  274. void pack_u32le(uint32_t value, uint8_t data[4])
  275. {
  276. data[0] = value & 0xff;
  277. data[1] = (value >> 8) & 0xff;
  278. data[2] = (value >> 16) & 0xff;
  279. data[3] = value >> 24;
  280. }
  281. void pack_f32le(float value, uint8_t data[4])
  282. {
  283. uint32_t u;
  284. memcpy(&u, &value, 4);
  285. pack_u32le(u, data);
  286. }
  287. uint32_t unpack_u32le(const uint8_t data[4])
  288. {
  289. return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
  290. }
  291. float unpack_f32le(const uint8_t data[4])
  292. {
  293. float value;
  294. uint32_t u = unpack_u32le(data);
  295. memcpy(&value, &u, 4);
  296. return value;
  297. }
  298. //------------------------------------------------------------------------------
  299. std::vector<uint8_t> decode_base64(const char *text, size_t len)
  300. {
  301. return d_getChunkFromBase64String(text, len);
  302. }
  303. std::string encode_base64(const uint8_t *data, size_t len)
  304. {
  305. return d_getBase64StringFromChunk(data, len);
  306. }
  307. //------------------------------------------------------------------------------
  308. bool get_file_uid(const char *path, file_uid &uid)
  309. {
  310. #ifdef _WIN32
  311. HANDLE handle = CreateFileW(widen(path).c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
  312. if (handle == INVALID_HANDLE_VALUE)
  313. return false;
  314. bool success = get_handle_file_uid((void *)handle, uid);
  315. CloseHandle(handle);
  316. return success;
  317. #else
  318. int fd = open(path, O_RDONLY);
  319. if (fd == -1)
  320. return false;
  321. bool success = get_descriptor_file_uid(fd, uid);
  322. close(fd);
  323. return success;
  324. #endif
  325. }
  326. bool get_stream_file_uid(FILE *stream, file_uid &uid)
  327. {
  328. #if !defined(_WIN32)
  329. int fd = fileno(stream);
  330. if (fd == -1)
  331. return false;
  332. #else
  333. int fd = _fileno(stream);
  334. if (fd == -1)
  335. return false;
  336. #endif
  337. return get_descriptor_file_uid(fd, uid);
  338. }
  339. bool get_descriptor_file_uid(int fd, file_uid &uid)
  340. {
  341. #if !defined(_WIN32)
  342. struct stat st;
  343. if (fstat(fd, &st) != 0)
  344. return false;
  345. uid.first = (uint64_t)st.st_dev;
  346. uid.second = (uint64_t)st.st_ino;
  347. return true;
  348. #else
  349. HANDLE handle = (HANDLE)_get_osfhandle(fd);
  350. if (handle == INVALID_HANDLE_VALUE)
  351. return false;
  352. return get_handle_file_uid((void *)handle, uid);
  353. #endif
  354. }
  355. #if defined(_WIN32)
  356. bool get_handle_file_uid(void *handle, file_uid &uid)
  357. {
  358. BY_HANDLE_FILE_INFORMATION info;
  359. if (!GetFileInformationByHandle((HANDLE)handle, &info))
  360. return false;
  361. uid.first = info.dwVolumeSerialNumber;
  362. uid.second = (uint64_t)info.nFileIndexLow | ((uint64_t)info.nFileIndexHigh << 32);
  363. return true;
  364. }
  365. #endif
  366. //------------------------------------------------------------------------------
  367. bool is_path_separator(char ch)
  368. {
  369. #if !defined(_WIN32)
  370. return ch == '/';
  371. #else
  372. return ch == '/' || ch == '\\';
  373. #endif
  374. }
  375. split_path_t split_path(const char *path)
  376. {
  377. split_path_t sp;
  378. #if !defined(_WIN32)
  379. size_t npos = ~(size_t)0;
  380. size_t pos = npos;
  381. for (size_t i = 0; path[i] != '\0'; ++i) {
  382. if (is_path_separator(path[i]))
  383. pos = i;
  384. }
  385. if (pos == npos)
  386. sp.file.assign(path);
  387. else {
  388. sp.dir.assign(path, pos + 1);
  389. sp.file.assign(path + pos + 1);
  390. }
  391. #else
  392. std::wstring wpath = widen(path);
  393. std::unique_ptr<wchar_t[]> drive{new wchar_t[wpath.size() + 1]{}};
  394. std::unique_ptr<wchar_t[]> dir{new wchar_t[wpath.size() + 1]{}};
  395. std::unique_ptr<wchar_t[]> fname{new wchar_t[wpath.size() + 1]{}};
  396. std::unique_ptr<wchar_t[]> ext{new wchar_t[wpath.size() + 1]{}};
  397. _wsplitpath(wpath.c_str(), drive.get(), dir.get(), fname.get(), ext.get());
  398. sp.drive = narrow(drive.get());
  399. if (!drive[0] || dir[0] == L'/' || dir[0] == L'\\')
  400. sp.dir = narrow(dir.get());
  401. else
  402. sp.dir = narrow(L'\\' + std::wstring(dir.get()));
  403. sp.file = narrow(std::wstring(fname.get()) + std::wstring(ext.get()));
  404. #endif
  405. return sp;
  406. }
  407. std::string path_file_name(const char *path)
  408. {
  409. return split_path(path).file;
  410. }
  411. std::string path_directory(const char *path)
  412. {
  413. split_path_t sp = split_path(path);
  414. return sp.dir.empty() ? std::string("./") : (sp.drive + sp.dir);
  415. }
  416. std::string path_ensure_final_separator(const char *path)
  417. {
  418. std::string result(path);
  419. if (!result.empty() && !is_path_separator(result.back()))
  420. result.push_back('/');
  421. return result;
  422. }
  423. bool path_has_suffix(const char *path, const char *suffix)
  424. {
  425. if (*suffix == '.')
  426. ++suffix;
  427. size_t plen = strlen(path);
  428. size_t slen = strlen(suffix);
  429. if (plen < slen + 2)
  430. return false;
  431. return path[plen - slen - 1] == '.' &&
  432. ascii_casecmp(suffix, &path[plen - slen]) == 0;
  433. }
  434. bool path_is_relative(const char *path)
  435. {
  436. #if !defined(_WIN32)
  437. return !is_path_separator(path[0]);
  438. #else
  439. return !is_path_separator(split_path(path).dir.c_str()[0]);
  440. #endif
  441. }
  442. //------------------------------------------------------------------------------
  443. #if !defined(_WIN32)
  444. bool exists(const char *path)
  445. {
  446. return access(path, F_OK) == 0;
  447. }
  448. string_list list_directory(const char *path)
  449. {
  450. string_list list;
  451. DIR *dir = opendir(path);
  452. if (!dir)
  453. return list;
  454. auto dir_cleanup = defer([dir]() { closedir(dir); });
  455. list.reserve(256);
  456. std::string pathbuf;
  457. pathbuf.reserve(1024);
  458. while (dirent *ent = readdir(dir)) {
  459. const char *name = ent->d_name;
  460. if (!strcmp(name, ".") || !strcmp(name, ".."))
  461. continue;
  462. pathbuf.assign(name);
  463. if (ent->d_type == DT_DIR)
  464. pathbuf.push_back('/');
  465. list.push_back(pathbuf);
  466. }
  467. std::sort(list.begin(), list.end());
  468. return list;
  469. }
  470. #if defined(YSFX_NO_FTS)
  471. void visit_directories(const char *rootpath, bool (*visit)(const std::string &, void *), void *data)
  472. {
  473. std::deque<std::string> dirs;
  474. dirs.push_back(path_ensure_final_separator(rootpath));
  475. std::string pathbuf;
  476. pathbuf.reserve(1024);
  477. std::vector<std::string> entries;
  478. entries.reserve(256);
  479. while (!dirs.empty()) {
  480. std::string dir = std::move(dirs.front());
  481. dirs.pop_front();
  482. if (!visit(dir, data))
  483. return;
  484. DIR *dh = opendir(dir.c_str());
  485. if (!dh)
  486. continue;
  487. auto dh_cleanup = defer([dh]() { closedir(dh); });
  488. entries.clear();
  489. while (dirent *ent = readdir(dh)) {
  490. const char *name = ent->d_name;
  491. if (!strcmp(name, ".") || !strcmp(name, ".."))
  492. continue;
  493. if (ent->d_type == DT_DIR) {
  494. pathbuf.assign(dir);
  495. pathbuf.append(name);
  496. pathbuf.push_back('/');
  497. entries.push_back(pathbuf);
  498. }
  499. }
  500. std::sort(entries.begin(), entries.end());
  501. for (size_t n = entries.size(); n-- > 0; )
  502. dirs.push_front(std::move(entries[n]));
  503. }
  504. }
  505. #endif
  506. // void visit_directories(const char *rootpath, bool (*visit)(const std::string &, void *), void *data);
  507. // NOTE: implemented in separate file `ysfx_utils_fts.cpp`
  508. #else
  509. bool exists(const char *path)
  510. {
  511. return _waccess(widen(path).c_str(), 0) == 0;
  512. }
  513. string_list list_directory(const char *path)
  514. {
  515. string_list list;
  516. std::wstring pattern = widen(path) + L"\\*";
  517. WIN32_FIND_DATAW fd;
  518. HANDLE handle = FindFirstFileW(pattern.c_str(), &fd);
  519. if (handle == INVALID_HANDLE_VALUE)
  520. return list;
  521. auto handle_cleanup = defer([handle]() { FindClose(handle); });
  522. list.reserve(256);
  523. do {
  524. const wchar_t *name = fd.cFileName;
  525. if (!wcscmp(name, L".") || !wcscmp(name, L".."))
  526. continue;
  527. std::string entry = narrow(name);
  528. if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
  529. entry.push_back('/');
  530. list.push_back(std::move(entry));
  531. } while (FindNextFileW(handle, &fd));
  532. std::sort(list.begin(), list.end());
  533. return list;
  534. }
  535. void visit_directories(const char *rootpath, bool (*visit)(const std::string &, void *), void *data)
  536. {
  537. std::deque<std::wstring> dirs;
  538. dirs.push_back(widen(path_ensure_final_separator(rootpath)));
  539. std::wstring pathbuf;
  540. pathbuf.reserve(1024);
  541. std::vector<std::wstring> entries;
  542. entries.reserve(256);
  543. while (!dirs.empty()) {
  544. std::wstring dir = std::move(dirs.front());
  545. dirs.pop_front();
  546. if (!visit(narrow(dir), data))
  547. return;
  548. pathbuf.assign(dir);
  549. pathbuf.append(L"\\*");
  550. WIN32_FIND_DATAW fd;
  551. HANDLE handle = FindFirstFileW(pathbuf.c_str(), &fd);
  552. if (handle == INVALID_HANDLE_VALUE)
  553. continue;
  554. auto handle_cleanup = defer([handle]() { FindClose(handle); });
  555. entries.clear();
  556. do {
  557. if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
  558. continue;
  559. if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  560. const wchar_t *name = fd.cFileName;
  561. if (!wcscmp(name, L".") || !wcscmp(name, L".."))
  562. continue;
  563. pathbuf.assign(dir);
  564. pathbuf.append(name);
  565. pathbuf.push_back(L'\\');
  566. entries.push_back(pathbuf);
  567. }
  568. } while (FindNextFileW(handle, &fd));
  569. std::sort(entries.begin(), entries.end());
  570. for (size_t n = entries.size(); n-- > 0; )
  571. dirs.push_front(std::move(entries[n]));
  572. }
  573. }
  574. #endif
  575. int case_resolve(const char *root_, const char *fragment, std::string &result)
  576. {
  577. if (fragment[0] == '\0')
  578. return 0;
  579. std::string root = path_ensure_final_separator(root_);
  580. std::string pathbuf;
  581. pathbuf.reserve(1024);
  582. pathbuf.assign(root);
  583. pathbuf.append(fragment);
  584. if (exists(pathbuf.c_str())) {
  585. result = std::move(pathbuf);
  586. return 1;
  587. }
  588. struct Item {
  589. std::string root;
  590. string_list components;
  591. };
  592. std::deque<Item> worklist;
  593. {
  594. Item item;
  595. item.root = root;
  596. item.components = split_strings_noempty(fragment, &is_path_separator);
  597. if (item.components.empty())
  598. return 0;
  599. for (size_t i = 0; i + 1 < item.components.size(); ++i)
  600. item.components[i].push_back('/');
  601. if (is_path_separator(fragment[strlen(fragment) - 1]))
  602. item.components.back().push_back('/');
  603. worklist.push_back(std::move(item));
  604. }
  605. while (!worklist.empty()) {
  606. Item item = std::move(worklist.front());
  607. worklist.pop_front();
  608. for (const std::string &entry : list_directory(item.root.c_str())) {
  609. if (ascii_casecmp(entry.c_str(), item.components[0].c_str()) != 0)
  610. continue;
  611. if (item.components.size() == 1) {
  612. pathbuf.assign(item.root);
  613. pathbuf.append(entry);
  614. if (exists(pathbuf.c_str())) {
  615. result = std::move(pathbuf);
  616. return 2;
  617. }
  618. }
  619. else {
  620. assert(item.components.size() > 1);
  621. Item newitem;
  622. newitem.root = item.root + entry;
  623. newitem.components.assign(item.components.begin() + 1, item.components.end());
  624. worklist.push_front(std::move(newitem));
  625. }
  626. }
  627. }
  628. return 0;
  629. }
  630. //------------------------------------------------------------------------------
  631. #if defined(_WIN32)
  632. std::wstring widen(const std::string &u8str)
  633. {
  634. return widen(u8str.data(), u8str.size());
  635. }
  636. std::wstring widen(const char *u8data, size_t u8len)
  637. {
  638. if (u8len == ~(size_t)0)
  639. u8len = strlen(u8data);
  640. std::wstring wstr;
  641. int wch = MultiByteToWideChar(CP_UTF8, 0, u8data, (int)u8len, nullptr, 0);
  642. if (wch != 0) {
  643. wstr.resize((size_t)wch);
  644. MultiByteToWideChar(CP_UTF8, 0, u8data, (int)u8len, &wstr[0], wch);
  645. }
  646. return wstr;
  647. }
  648. std::string narrow(const std::wstring &wstr)
  649. {
  650. return narrow(wstr.data(), wstr.size());
  651. }
  652. std::string narrow(const wchar_t *wdata, size_t wlen)
  653. {
  654. if (wlen == ~(size_t)0)
  655. wlen = wcslen(wdata);
  656. std::string u8str;
  657. int u8ch = WideCharToMultiByte(CP_UTF8, 0, wdata, (int)wlen, nullptr, 0, nullptr, nullptr);
  658. if (u8ch != 0) {
  659. u8str.resize((size_t)u8ch);
  660. WideCharToMultiByte(CP_UTF8, 0, wdata, (int)wlen, &u8str[0], u8ch, nullptr, nullptr);
  661. }
  662. return u8str;
  663. }
  664. #endif
  665. } // namespace ysfx
  666. //------------------------------------------------------------------------------
  667. // WDL helpers
  668. // our replacement `atof` for WDL, which is unaffected by current locale
  669. extern "C" double ysfx_wdl_atof(const char *text)
  670. {
  671. return ysfx::dot_atof(text);
  672. }