Skip to content

Commit

Permalink
Improved UTF-8 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny authored Jun 10, 2024
2 parents 74e5c15 + 84af1e1 commit e375292
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 83 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/buildThirdPartyLibrary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ jobs:
fail-fast: false
matrix:
include:
- name: 'Windows release'
os: windows-latest
build_type: Release
- name: 'Windows debug'
os: windows-latest
build_type: Debug
# - name: 'Windows release'
# os: windows-latest
# build_type: Release
# - name: 'Windows debug'
# os: windows-latest
# build_type: Debug
- name: 'Linux'
os: ubuntu-20.04
# Note: we must use ubuntu-20.04 rather than ubuntu-latest (i.e. ubuntu-22.04 at this stage). Indeed,
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ jobs:
fail-fast: false
matrix:
include:
- name: 'Windows static library'
os: windows-latest
shared_libs: OFF
- name: 'Windows shared library'
os: windows-latest
shared_libs: ON
# - name: 'Windows static library'
# os: windows-latest
# shared_libs: OFF
# - name: 'Windows shared library'
# os: windows-latest
# shared_libs: ON
- name: 'Linux static library'
os: ubuntu-latest
shared_libs: OFF
Expand Down Expand Up @@ -87,8 +87,8 @@ jobs:
fail-fast: false
matrix:
include:
- name: 'Windows Python wheels'
os: windows-latest
# - name: 'Windows Python wheels'
# os: windows-latest
- name: 'Linux Python wheels'
os: ubuntu-latest
- name: 'macOS Python wheels (Intel)'
Expand Down
90 changes: 45 additions & 45 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@ jobs:
fail-fast: false
matrix:
include:
- name: 'Windows static library'
os: windows-latest
build_type: Release
code_analysis: OFF
code_coverage: OFF
documentation: OFF
javascript_bindings: OFF
javascript_unit_testing: OFF
memory_checks: OFF
python_bindings: OFF
python_unit_testing: OFF
shared_libs: OFF
unit_testing: ON
target: unit_testing
install_uninstall_and_package: ON
- name: 'Windows shared library'
os: windows-latest
build_type: Release
code_analysis: OFF
code_coverage: OFF
documentation: OFF
javascript_bindings: OFF
javascript_unit_testing: OFF
memory_checks: OFF
python_bindings: OFF
python_unit_testing: OFF
shared_libs: ON
unit_testing: ON
target: unit_testing
install_uninstall_and_package: ON
# - name: 'Windows static library'
# os: windows-latest
# build_type: Release
# code_analysis: OFF
# code_coverage: OFF
# documentation: OFF
# javascript_bindings: OFF
# javascript_unit_testing: OFF
# memory_checks: OFF
# python_bindings: OFF
# python_unit_testing: OFF
# shared_libs: OFF
# unit_testing: ON
# target: unit_testing
# install_uninstall_and_package: ON
# - name: 'Windows shared library'
# os: windows-latest
# build_type: Release
# code_analysis: OFF
# code_coverage: OFF
# documentation: OFF
# javascript_bindings: OFF
# javascript_unit_testing: OFF
# memory_checks: OFF
# python_bindings: OFF
# python_unit_testing: OFF
# shared_libs: ON
# unit_testing: ON
# target: unit_testing
# install_uninstall_and_package: ON
- name: 'Linux static library'
os: ubuntu-latest
build_type: Release
Expand Down Expand Up @@ -147,21 +147,21 @@ jobs:
shared_libs: OFF
unit_testing: OFF
target: javascript_unit_testing
- name: 'Windows Python bindings'
os: windows-latest
build_type: Release
code_analysis: OFF
code_coverage: OFF
documentation: OFF
javascript_bindings: OFF
javascript_unit_testing: OFF
memory_checks: OFF
python_bindings: ON
python_unit_testing: ON
shared_libs: OFF
unit_testing: OFF
target: python_unit_testing
pip_install_test_and_uninstall: ON
# - name: 'Windows Python bindings'
# os: windows-latest
# build_type: Release
# code_analysis: OFF
# code_coverage: OFF
# documentation: OFF
# javascript_bindings: OFF
# javascript_unit_testing: OFF
# memory_checks: OFF
# python_bindings: ON
# python_unit_testing: ON
# shared_libs: OFF
# unit_testing: OFF
# target: python_unit_testing
# pip_install_test_and_uninstall: ON
- name: 'Linux Python bindings'
os: ubuntu-latest
build_type: Release
Expand Down
4 changes: 2 additions & 2 deletions cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ function(configure_target TARGET)

# Let the target know that we are fine with using std::codecvt_utf8() even though it has been deprecated in C++17
# (but no replacement has been provided yet).
# Note: this is so that we can convert a std::wstring to a std::string on Windows (see wideStringToString()), hence
# the WIN32 check.
# Note: this is so that we can convert a std::wstring to a std::string on Windows (see pathToString()), hence the
# WIN32 check.

if(WIN32)
target_compile_definitions(${TARGET} PRIVATE
Expand Down
11 changes: 8 additions & 3 deletions src/file/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,14 @@ void File::Impl::checkType(const FilePtr &pOwner, bool pResetType)
}
}

std::string File::Impl::fileName() const
{
return pathToString(mFilePath);
}

std::string File::Impl::path() const
{
return mUrl.empty() ? mFilePath.string() : mUrl;
return mUrl.empty() ? fileName() : mUrl;
}

#ifndef __EMSCRIPTEN__
Expand Down Expand Up @@ -176,7 +181,7 @@ FilePtr File::create(const std::string &pFileNameOrUrl)
auto file = fileManager.file(pFileNameOrUrl);

if (file != nullptr) {
return file->shared_from_this();
return file;
}

auto res = FilePtr {new File {pFileNameOrUrl}};
Expand All @@ -195,7 +200,7 @@ File::Type File::type() const

std::string File::fileName() const
{
return pimpl()->mFilePath.string();
return pimpl()->fileName();
}

std::string File::url() const
Expand Down
1 change: 1 addition & 0 deletions src/file/file_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class File::Impl: public Logger::Impl

void checkType(const FilePtr &pOwner, bool pResetType = false);

std::string fileName() const;
std::string path() const;

#ifndef __EMSCRIPTEN__
Expand Down
34 changes: 18 additions & 16 deletions src/misc/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ bool fuzzyCompare(double pNb1, double pNb2)
}

#ifdef BUILDING_USING_MSVC
std::string wideStringToString(const std::wstring &pString)
{
return std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(pString);
}

std::string forwardSlashPath(const std::string &pPath)
{
static constexpr auto BACKSLASH = "\\\\";
Expand All @@ -67,6 +62,15 @@ std::filesystem::path stringToPath(const std::string &pString)
return std::u8string {pString.begin(), pString.end()};
}

std::string pathToString(const std::filesystem::path &pPath)
{
#if defined(BUILDING_USING_MSVC)
return std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(pPath.wstring());
#else
return pPath.string();
#endif
}

namespace {

#ifdef BUILDING_USING_MSVC
Expand All @@ -87,23 +91,19 @@ std::string canonicalFileName(const std::string &pFileName)

std::filesystem::current_path(FORWARD_SLASH);

#if defined(BUILDING_USING_MSVC)
auto res = wideStringToString(std::filesystem::weakly_canonical(stringToPath(pFileName)).wstring());
#elif defined(BUILDING_USING_GNU) || defined(BUILDING_USING_CLANG)
auto res = std::filesystem::weakly_canonical(stringToPath(pFileName)).string();
#else
#ifdef __EMSCRIPTEN__
static constexpr auto DUMMY_FOLDER = "/dummy";

auto res = pFileName;

res = DUMMY_FOLDER + std::string((res.find(FORWARD_SLASH) != 0) ? FORWARD_SLASH : "") + res;
res = std::filesystem::weakly_canonical(stringToPath(res)).string();
res = DUMMY_FOLDER + std::string(res.starts_with(FORWARD_SLASH) ? "" : FORWARD_SLASH) + res;
res = pathToString(std::filesystem::weakly_canonical(stringToPath(res)));

res.erase(0, strlen(DUMMY_FOLDER));
#else
auto res = pathToString(std::filesystem::weakly_canonical(stringToPath(pFileName)));
#endif

std::filesystem::current_path(currentPath);

#if defined(BUILDING_USING_MSVC)
// Replace "\"s with "/"s, if needed.

Expand All @@ -121,6 +121,8 @@ std::string canonicalFileName(const std::string &pFileName)
}
#endif

std::filesystem::current_path(currentPath);

// Return the canonical version of the file name.

return res;
Expand Down Expand Up @@ -187,7 +189,7 @@ std::filesystem::path canonicalPath(const std::string &pPath)

std::string relativePath(const std::string &pPath, const std::string &pBasePath)
{
return std::filesystem::relative(canonicalPath(pPath), canonicalPath(pBasePath)).string();
return pathToString(std::filesystem::relative(canonicalPath(pPath), canonicalPath(pBasePath)));
}

std::string urlPath(const std::string &pPath)
Expand Down Expand Up @@ -250,7 +252,7 @@ std::filesystem::path uniqueFilePath()
static const std::string LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const uint64_t LETTERS_SIZE = LETTERS.size();

static auto testFile = (std::filesystem::temp_directory_path() / "libOpenCOR_XXXXXX.tmp").string();
static auto testFile = pathToString(std::filesystem::temp_directory_path() / "libOpenCOR_XXXXXX.tmp");

static const size_t XXXXXX_POS = testFile.size() - 6 - 4;
static constexpr uint64_t MICROSECONDS_SHIFT = 16U;
Expand Down
3 changes: 1 addition & 2 deletions src/misc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ using StringStringMap = std::map<std::string, std::string>;
bool LIBOPENCOR_UNIT_TESTING_EXPORT fuzzyCompare(double pNb1, double pNb2);

#ifdef BUILDING_USING_MSVC
std::string wideStringToString(const std::wstring &pString);

std::string forwardSlashPath(const std::string &pPath);
#endif

std::filesystem::path stringToPath(const std::string &pString);
std::string pathToString(const std::filesystem::path &pPath);

std::tuple<bool, std::string> retrieveFileInfo(const std::string &pFileNameOrUrl);
std::string relativePath(const std::string &pPath, const std::string &pBasePath);
Expand Down
2 changes: 1 addition & 1 deletion src/support/cellml/cellmlfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CellmlFile::Impl::Impl(const FilePtr &pFile, const libcellml::ModelPtr &pModel,
if (mModel->hasUnresolvedImports()) {
auto importer = libcellml::Importer::create(pStrict);

if (importer->resolveImports(mModel, stringToPath(pFile->path()).parent_path().string())) {
if (importer->resolveImports(mModel, pathToString(stringToPath(pFile->path()).parent_path()))) {
mModel = importer->flattenModel(mModel);
} else {
addIssues(importer);
Expand Down

0 comments on commit e375292

Please sign in to comment.