blob: 0eeff59b68ff6d239a68052c3396fd28def86e88 [file] [log] [blame]
Aline Gondim Santos607635b2022-08-22 15:40:59 -03001/**
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#include "common.h"
22
23#ifdef WIN32
24#include <WTypes.h>
25#include <stdexcept>
Aline Gondim Santos4b62db12022-10-06 18:20:07 -030026#endif // WIN32
Aline Gondim Santos607635b2022-08-22 15:40:59 -030027
Aline Gondim Santos676d1c32022-12-05 21:13:25 -030028#include <cstring>
29
Aline Gondim Santos607635b2022-08-22 15:40:59 -030030namespace string_utils {
Aline Gondim Santos4b62db12022-10-06 18:20:07 -030031
32#ifdef WIN32
Aline Gondim Santos607635b2022-08-22 15:40:59 -030033std::wstring
34to_wstring(const std::string& str) {
35 int codePage = CP_UTF8;
36 int srcLength = (int) str.length();
37 int requiredSize = MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, nullptr, 0);
38 if (!requiredSize) {
39 throw std::runtime_error("Can't convert string to wstring");
40 }
41 std::wstring result((size_t) requiredSize, 0);
42 if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) {
43 throw std::runtime_error("Can't convert string to wstring");
44 }
45 return result;
46}
Aline Gondim Santos607635b2022-08-22 15:40:59 -030047#endif // WIN32
Aline Gondim Santos9bd153e2022-08-25 10:49:23 -030048
Aline Gondim Santos4b62db12022-10-06 18:20:07 -030049void ffmpegFormatStringInline(std::string& str)
50{
51#ifdef WIN32
52 for (int i = str.size(); i > 0; i--)
53 if (str[i] == '\\')
54 str.replace(str.begin() + i, str.begin() + i + 1, "/");
55 str = str.insert(1, "\\");
56#endif
57}
58
Aline Gondim Santos329f8622022-11-08 08:04:22 -030059void ffmpegScapeStringInline(std::string& str)
60{
61 std::string newStr;
Adrien Beraud087d5592023-03-06 11:22:33 -050062 newStr.reserve(str.size());
Aline Gondim Santos329f8622022-11-08 08:04:22 -030063 for (size_t i = 0; i < str.size(); i ++) {
64 switch (str[i]) {
65 case '\'':
66 newStr.append("ยด");
67 break;
68 case '%':
69 newStr.append("\\%");
70 break;
71 case ':':
72 newStr.append("\\:");
73 break;
74 case '\\':
75 newStr.append("\\\\");
76 break;
77 default:
78 newStr.insert(newStr.end(), str[i]);
79 break;
80 }
81 }
82 std::swap(newStr, str);
83}
84
Aline Gondim Santos4b62db12022-10-06 18:20:07 -030085std::string ffmpegFormatString(const std::string& str)
86{
87 std::string ret = str;
88 ffmpegFormatStringInline(ret);
89 return ret;
90}
Aline Gondim Santos329f8622022-11-08 08:04:22 -030091
92std::string ffmpegScapeString(const std::string& str)
93{
94 std::string ret = str;
95 ffmpegScapeStringInline(ret);
96 return ret;
97}
Aline Gondim Santos676d1c32022-12-05 21:13:25 -030098
99std::vector<std::string>
100getWords(const std::string& text, const std::string& splits)
101{
102 std::vector<std::string> words;
103 auto token = std::strtok (const_cast<char*>(text.c_str()), const_cast<char*>(splits.c_str()));
104 while (token != NULL) {
105 words.emplace_back(token);
106 token = std::strtok (NULL, const_cast<char*>(splits.c_str()));
107 }
108 return words;
109}
Aline Gondim Santos4b62db12022-10-06 18:20:07 -0300110} // namespace string_utils
111
Aline Gondim Santos9bd153e2022-08-25 10:49:23 -0300112namespace file_utils {
113
114void
115openStream(std::ifstream& file, const std::string& path)
116{
117#ifdef _WIN32
118 file = std::ifstream(string_utils::to_wstring(path));
119#else
120 file = std::ifstream(path);
121#endif
122}
123} // namespace file_utils