Aline Gondim Santos | 607635b | 2022-08-22 15:40:59 -0300 | [diff] [blame] | 1 | /** |
| 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 Santos | 4b62db1 | 2022-10-06 18:20:07 -0300 | [diff] [blame] | 26 | #endif // WIN32 |
Aline Gondim Santos | 607635b | 2022-08-22 15:40:59 -0300 | [diff] [blame] | 27 | |
Aline Gondim Santos | 676d1c3 | 2022-12-05 21:13:25 -0300 | [diff] [blame^] | 28 | #include <cstring> |
| 29 | |
Aline Gondim Santos | 607635b | 2022-08-22 15:40:59 -0300 | [diff] [blame] | 30 | namespace string_utils { |
Aline Gondim Santos | 4b62db1 | 2022-10-06 18:20:07 -0300 | [diff] [blame] | 31 | |
| 32 | #ifdef WIN32 |
Aline Gondim Santos | 607635b | 2022-08-22 15:40:59 -0300 | [diff] [blame] | 33 | std::wstring |
| 34 | to_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 Santos | 607635b | 2022-08-22 15:40:59 -0300 | [diff] [blame] | 47 | #endif // WIN32 |
Aline Gondim Santos | 9bd153e | 2022-08-25 10:49:23 -0300 | [diff] [blame] | 48 | |
Aline Gondim Santos | 4b62db1 | 2022-10-06 18:20:07 -0300 | [diff] [blame] | 49 | void 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 Santos | 329f862 | 2022-11-08 08:04:22 -0300 | [diff] [blame] | 59 | void 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 Santos | 4b62db1 | 2022-10-06 18:20:07 -0300 | [diff] [blame] | 84 | std::string ffmpegFormatString(const std::string& str) |
| 85 | { |
| 86 | std::string ret = str; |
| 87 | ffmpegFormatStringInline(ret); |
| 88 | return ret; |
| 89 | } |
Aline Gondim Santos | 329f862 | 2022-11-08 08:04:22 -0300 | [diff] [blame] | 90 | |
| 91 | std::string ffmpegScapeString(const std::string& str) |
| 92 | { |
| 93 | std::string ret = str; |
| 94 | ffmpegScapeStringInline(ret); |
| 95 | return ret; |
| 96 | } |
Aline Gondim Santos | 676d1c3 | 2022-12-05 21:13:25 -0300 | [diff] [blame^] | 97 | |
| 98 | std::vector<std::string> |
| 99 | getWords(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 Santos | 4b62db1 | 2022-10-06 18:20:07 -0300 | [diff] [blame] | 109 | } // namespace string_utils |
| 110 | |
Aline Gondim Santos | 9bd153e | 2022-08-25 10:49:23 -0300 | [diff] [blame] | 111 | namespace file_utils { |
| 112 | |
| 113 | void |
| 114 | openStream(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 |