blob: c9534b13107a49f455640958aa159cd94b8a2087 [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;
62 for (size_t i = 0; i < str.size(); i ++) {
63 switch (str[i]) {
64 case '\'':
65 newStr.append("ยด");
66 break;
67 case '%':
68 newStr.append("\\%");
69 break;
70 case ':':
71 newStr.append("\\:");
72 break;
73 case '\\':
74 newStr.append("\\\\");
75 break;
76 default:
77 newStr.insert(newStr.end(), str[i]);
78 break;
79 }
80 }
81 std::swap(newStr, str);
82}
83
Aline Gondim Santos4b62db12022-10-06 18:20:07 -030084std::string ffmpegFormatString(const std::string& str)
85{
86 std::string ret = str;
87 ffmpegFormatStringInline(ret);
88 return ret;
89}
Aline Gondim Santos329f8622022-11-08 08:04:22 -030090
91std::string ffmpegScapeString(const std::string& str)
92{
93 std::string ret = str;
94 ffmpegScapeStringInline(ret);
95 return ret;
96}
Aline Gondim Santos676d1c32022-12-05 21:13:25 -030097
98std::vector<std::string>
99getWords(const std::string& text, const std::string& splits)
100{
101 std::vector<std::string> words;
102 auto token = std::strtok (const_cast<char*>(text.c_str()), const_cast<char*>(splits.c_str()));
103 while (token != NULL) {
104 words.emplace_back(token);
105 token = std::strtok (NULL, const_cast<char*>(splits.c_str()));
106 }
107 return words;
108}
Aline Gondim Santos4b62db12022-10-06 18:20:07 -0300109} // namespace string_utils
110
Aline Gondim Santos9bd153e2022-08-25 10:49:23 -0300111namespace file_utils {
112
113void
114openStream(std::ifstream& file, const std::string& path)
115{
116#ifdef _WIN32
117 file = std::ifstream(string_utils::to_wstring(path));
118#else
119 file = std::ifstream(path);
120#endif
121}
122} // namespace file_utils