blob: 4803f1cecbad14d7dbe7f1b86bdb09abcbdd699d [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>
26
27namespace string_utils {
28std::wstring
29to_wstring(const std::string& str) {
30 int codePage = CP_UTF8;
31 int srcLength = (int) str.length();
32 int requiredSize = MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, nullptr, 0);
33 if (!requiredSize) {
34 throw std::runtime_error("Can't convert string to wstring");
35 }
36 std::wstring result((size_t) requiredSize, 0);
37 if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) {
38 throw std::runtime_error("Can't convert string to wstring");
39 }
40 return result;
41}
42} // namespace string_utils
43#endif // WIN32
Aline Gondim Santos9bd153e2022-08-25 10:49:23 -030044
45namespace file_utils {
46
47void
48openStream(std::ifstream& file, const std::string& path)
49{
50#ifdef _WIN32
51 file = std::ifstream(string_utils::to_wstring(path));
52#else
53 file = std::ifstream(path);
54#endif
55}
56} // namespace file_utils