Branch data TLA Line data Source code
1 : : // Copyright (c) 2017-2022 Thomas Fussell
2 : : // Copyright (c) 2024-2025 xlnt-community
3 : : //
4 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : : // of this software and associated documentation files (the "Software"), to deal
6 : : // in the Software without restriction, including without limitation the rights
7 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : : // copies of the Software, and to permit persons to whom the Software is
9 : : // furnished to do so, subject to the following conditions:
10 : : //
11 : : // The above copyright notice and this permission notice shall be included in
12 : : // all copies or substantial portions of the Software.
13 : : //
14 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : : // THE SOFTWARE
21 : : //
22 : : // @license: http://www.opensource.org/licenses/mit-license.php
23 : : // @author: see AUTHORS file
24 : :
25 : : #include <xlnt/utils/path.hpp>
26 : : #include <detail/serialization/open_stream.hpp>
27 : :
28 : : #if XLNT_HAS_INCLUDE(<filesystem>)
29 : : #include <filesystem>
30 : : #endif
31 : :
32 : : namespace xlnt {
33 : : namespace detail {
34 : :
35 :CBC 70 : void open_stream(std::ifstream &stream, const std::string &path)
36 : : {
37 : : #ifdef _MSC_VER
38 : : open_stream(stream, xlnt::path(path).wstring());
39 : : #else
40 : 70 : stream.open(path, std::ios::binary);
41 : : #endif
42 : 70 : }
43 : :
44 : 31 : void open_stream(std::ofstream &stream, const std::string &path)
45 : : {
46 : : #ifdef _MSC_VER
47 : : open_stream(stream, xlnt::path(path).wstring());
48 : : #else
49 : 31 : stream.open(path, std::ios::binary);
50 : : #endif
51 : 31 : }
52 : :
53 : : #ifdef _MSC_VER
54 : : void open_stream(std::ifstream &stream, const std::wstring &path)
55 : : {
56 : : stream.open(path, std::ios::binary);
57 : : }
58 : :
59 : : void open_stream(std::ofstream &stream, const std::wstring &path)
60 : : {
61 : : stream.open(path, std::ios::binary);
62 : : }
63 : : #endif
64 : :
65 : : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
66 :UBC 0 : void open_stream(std::ifstream &stream, std::u8string_view path)
67 : : {
68 : : #ifdef _MSC_VER
69 : : open_stream(stream, xlnt::path(path).wstring());
70 : : #elif XLNT_HAS_FEATURE(FILESYSTEM)
71 [ # # ]: 0 : stream.open(std::filesystem::path(path), std::ios::binary);
72 : : #else
73 : : // TODO: this cannot work if the user's locale is not UTF-8. In such cases we cannot ensure
74 : : // that this always works - however, in such cases we can still attempt to do a conversion to
75 : : // the locale encoding, which will still work if the string can be represented
76 : : // with the user's locale encoding.
77 : : // NOTE: this code will only run if C++17 is only partially implemented,
78 : : // but C++17 string_view and C++20 char8_t are implemented, while C++17 filesystem is not.
79 : : stream.open(to_char_ptr(path.data()), std::ios::binary);
80 : : #endif
81 : 0 : }
82 : :
83 : 0 : void open_stream(std::ofstream &stream, std::u8string_view path)
84 : : {
85 : : #ifdef _MSC_VER
86 : : open_stream(stream, xlnt::path(path).wstring());
87 : : #elif XLNT_HAS_FEATURE(FILESYSTEM)
88 [ # # ]: 0 : stream.open(std::filesystem::path(path), std::ios::binary);
89 : : #else
90 : : // TODO: this cannot work if the user's locale is not UTF-8. In such cases we cannot ensure
91 : : // that this always works - however, in such cases we can still attempt to do a conversion to
92 : : // the locale encoding, which will still work if the string can be represented
93 : : // with the user's locale encoding.
94 : : // NOTE: this code will only run if C++17 is only partially implemented,
95 : : // but C++17 string_view and C++20 char8_t are implemented, while C++17 filesystem is not.
96 : : stream.open(to_char_ptr(path.data()), std::ios::binary);
97 : : #endif
98 : 0 : }
99 : : #endif
100 : :
101 : : } // namespace detail
102 : : } // namespace xlnt
|