src/utilities/core/Path.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Path.hpp" | ||
| 7 | #include "String.hpp" | ||
| 8 | |||
| 9 | #if (defined(_WIN32) || defined(_WIN64)) | ||
| 10 | # include <locale> | ||
| 11 | # include <codecvt> | ||
| 12 | # include "Logger.hpp" | ||
| 13 | #endif | ||
| 14 | |||
| 15 | namespace openstudio { | ||
| 16 | |||
| 17 | /* | ||
| 18 | | Class | Internal | Platform | | ||
| 19 | | std::string | UTF-8, char | All | | ||
| 20 | | path | UTF-8, char | Unix, Mac | | ||
| 21 | | path | UTF-16, wchar | Windows | | ||
| 22 | | QString | UTF-16, wchar | All | | ||
| 23 | |||
| 24 | */ | ||
| 25 | |||
| 26 | // allow path to be written to cout on Windows | ||
| 27 | 5267 | std::ostream& operator<<(std::ostream& os, const path& p) { | |
| 28 |
2/2✓ Branch 1 taken 5267 times.
✓ Branch 4 taken 5267 times.
|
5267 | os << toString(p); |
| 29 | 5267 | return os; | |
| 30 | } | ||
| 31 | |||
| 32 | /** path to a temporary directory. */ | ||
| 33 | 9 | path tempDir() { | |
| 34 | 9 | return openstudio::filesystem::temp_directory_path(); | |
| 35 | } | ||
| 36 | |||
| 37 | /** path to UTF-8 encoding. */ | ||
| 38 | 16091 | std::string toString(const path& p) { | |
| 39 | #if (defined(_WIN32) || defined(_WIN64)) | ||
| 40 | std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter; | ||
| 41 | std::string s = converter.to_bytes(p.generic_wstring()); | ||
| 42 | return s; | ||
| 43 | #endif | ||
| 44 | |||
| 45 | // cppcheck-suppress duplicateBreak | ||
| 46 | 16091 | return p.generic_string(); | |
| 47 | } | ||
| 48 | |||
| 49 | /** UTF-8 encoded char* to path*/ | ||
| 50 | 39554 | path toPath(const char* s) { | |
| 51 |
2/2✓ Branch 1 taken 39554 times.
✓ Branch 4 taken 39554 times.
|
39554 | return toPath(toString(s)); |
| 52 | } | ||
| 53 | |||
| 54 | /** UTF-8 encoded std::string to path*/ | ||
| 55 | 49778 | path toPath(const std::string& s) { | |
| 56 | #if (defined(_WIN32) || defined(_WIN64)) | ||
| 57 | std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; | ||
| 58 | try { | ||
| 59 | std::wstring wstr = converter.from_bytes(s); | ||
| 60 | return path(wstr); | ||
| 61 | } catch (std::range_error& e) { | ||
| 62 | // Fallback to basically decoding latin-1 | ||
| 63 | // Masking with 0xFF reduces any negative values into the range 0-255. | ||
| 64 | // TODO: another option is to write a codecvt_latin1 class | ||
| 65 | // cf: https://stackoverflow.com/questions/49669048/why-mask-a-char-with-0xff-when-converting-narrow-string-to-wide-string#49669048 | ||
| 66 | size_t length = s.length(); | ||
| 67 | std::wstring wstr; | ||
| 68 | wstr.reserve(length); | ||
| 69 | for (size_t i = 0; i < length; ++i) { | ||
| 70 | wstr.push_back(s[i] & 0xFF); | ||
| 71 | } | ||
| 72 | LOG_FREE(Debug, "Path", | ||
| 73 | "An error occurred trying to convert the input to a wide string." << "\n Reason: " << e.what() << "\n Input =" << s | ||
| 74 | << "\n Result=" << wstr); | ||
| 75 | return path(wstr); | ||
| 76 | } | ||
| 77 | #endif | ||
| 78 | |||
| 79 | // else | ||
| 80 | 49778 | return path(s); | |
| 81 | } | ||
| 82 | |||
| 83 | 338 | path toPath(std::string_view s) { | |
| 84 |
2/2✓ Branch 1 taken 338 times.
✓ Branch 4 taken 338 times.
|
676 | return toPath(std::string(s)); |
| 85 | } | ||
| 86 | |||
| 87 | #ifdef _WIN32 | ||
| 88 | /** UTF-16 encoded std::wstring for opening fstreams*/ | ||
| 89 | std::wstring toSystemFilename(const path& p) { | ||
| 90 | return p.wstring(); | ||
| 91 | } | ||
| 92 | #else | ||
| 93 | /** UTF-8 encoded std::string for opening fstreams*/ | ||
| 94 | 701 | std::string toSystemFilename(const path& p) { | |
| 95 | 701 | return p.string(); | |
| 96 | } | ||
| 97 | #endif | ||
| 98 | |||
| 99 | } // namespace openstudio | ||
| 100 |