Skip to content

Commit

Permalink
Fileparts compatibility (#759)
Browse files Browse the repository at this point in the history
* fix #755 boost 1.80 support

* build as debug by default

* fileparts is more compatible
  • Loading branch information
Nelson-numerical-software committed Oct 16, 2022
1 parent 0896635 commit 0cd88cc
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- [#755](http://github.com/Nelson-numerical-software/nelson/issues/755): Boost 1.80 support (default on Windows)

- fileparts builtin updated to be more compatible.

## 0.6.9 (2022-09-28)

### Added
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -137,7 +137,7 @@ if(POLICY CMP0068)
cmake_policy(SET CMP0068 NEW)
endif()
# ==============================================================================
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake/")
set(BUILD_SHARED_LIBS "true")
# ==============================================================================
Expand Down
134 changes: 101 additions & 33 deletions modules/files_folders_functions/src/cpp/FileParts.cpp
Expand Up @@ -7,64 +7,132 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#include <string>
#include "FileParts.hpp"
#include <boost/filesystem.hpp>
//=============================================================================
namespace Nelson {
//=============================================================================
std::wstring
FilePartsPath(const std::wstring& fullpath)
{
std::wstring res;
boost::filesystem::path pathToSplit = fullpath;
if (pathToSplit.has_parent_path()) {
res = pathToSplit.parent_path().generic_wstring();
}
if (res.length() > 1 && res.back() == L':') {
res = res + L"/";
}
return res;
std::wstring path = L"";
std::wstring filename = L"";
std::wstring extension = L"";
FileParts(fullpath, path, filename, extension);
return path;
}
//=============================================================================
std::wstring
FilePartsFilename(const std::wstring& fullpath)
{
std::wstring res;
boost::filesystem::path pathToSplit = fullpath;
if (pathToSplit.has_filename()) {
res = pathToSplit.stem().generic_wstring();
}
if (res == L".") {
res = L"";
}
if (res == L"/" || res == L"\\") {
res = L"";
}
return res;
}
//=============================================================================
std::wstring path = L"";
std::wstring filename = L"";
std::wstring extension = L"";
FileParts(fullpath, path, filename, extension);
return filename;
} //=============================================================================
std::wstring
FilePartsExtension(const std::wstring& fullpath)
{
std::wstring res;
boost::filesystem::path pathToSplit = fullpath;
if (pathToSplit.has_extension()) {
res = pathToSplit.extension().generic_wstring();
std::wstring path = L"";
std::wstring filename = L"";
std::wstring extension = L"";
FileParts(fullpath, path, filename, extension);
return extension;
}
//=============================================================================
static inline size_t
findLastFileSeparator(const std::wstring& fullpath)
{
size_t indexSlash = fullpath.rfind(L'/');
size_t indexBackslash = fullpath.rfind(L'\\');
size_t indexFileSeparator = std::wstring::npos;

if (indexSlash != std::wstring::npos && indexBackslash != std::wstring::npos) {
indexFileSeparator = std::max(indexSlash, indexBackslash);
} else {
if (indexSlash != std::wstring::npos) {
indexFileSeparator = indexSlash;

} else {
indexFileSeparator = indexBackslash;
}
}
return res;
return indexFileSeparator;
}
//=============================================================================
void
FileParts(const std::wstring& fullpath, std::wstring& path, std::wstring& filename,
std::wstring& extension)
{
path = FilePartsPath(fullpath);
filename = FilePartsFilename(fullpath);
if (path == L"" && (filename.size() > 1 && filename[1] == L':')) {
path = L"";
filename = L"";
extension = L"";

size_t indexFileSeparator = findLastFileSeparator(fullpath);
size_t indexDot = fullpath.rfind(L'.');

std::wstring fullpathWithoutExtension;
if (indexDot != std::wstring::npos) {
if (indexFileSeparator != std::wstring::npos) {
if (indexDot > indexFileSeparator) {
fullpathWithoutExtension = L"";
extension.append(fullpath.begin() + indexDot, fullpath.end());
fullpathWithoutExtension.append(fullpath.begin(), fullpath.begin() + indexDot);
} else {
extension = L"";
fullpathWithoutExtension = fullpath;
}
} else {
path = L"";
extension.append(fullpath.begin() + indexDot, fullpath.end());
filename.append(fullpath.begin(), fullpath.begin() + indexDot);
return;
}
} else {
extension = L"";
if (indexFileSeparator != std::wstring::npos) {
path.append(fullpath.begin(), fullpath.begin() + indexFileSeparator);
filename.append(fullpath.begin() + indexFileSeparator + 1, fullpath.end());
#ifdef _MSC_VER
if (filename.empty() && path.length() > 1 && (path.back() == L':')) {
path = fullpath;
filename = L"";
}
#endif
} else {
path = L"";
filename = fullpath;
#ifdef _MSC_VER
if (filename.length() > 1 && (filename.back() == L':')) {
path = fullpath;
filename = L"";
}
#endif
}
return;
}
if (indexFileSeparator != std::wstring::npos) {
if (indexFileSeparator == 0) {
path.append(fullpathWithoutExtension.begin(), fullpathWithoutExtension.begin() + 1);
filename.append(fullpathWithoutExtension.begin() + 1, fullpathWithoutExtension.end());
} else {
path.append(fullpathWithoutExtension.begin(),
fullpathWithoutExtension.begin() + indexFileSeparator);
filename.append(fullpathWithoutExtension.begin() + indexFileSeparator + 1,
fullpathWithoutExtension.end());
}
} else {
path = L"";
filename = fullpathWithoutExtension;
}
#ifdef _MSC_VER
if (path.empty() && extension.empty() && (filename.length() > 1) && (filename.back() == L':')) {
path = filename;
filename = L"";
extension = L"";
}
extension = FilePartsExtension(fullpath);
#endif
}
//=============================================================================
} // namespace Nelson
Expand Down
14 changes: 10 additions & 4 deletions modules/files_folders_functions/tests/bug_github_issue_#78.m
Expand Up @@ -13,7 +13,13 @@
% [p,f,e]=fileparts('c:/') did not return the good result
%=============================================================================
[p, f, e] = fileparts('c:/');
assert_isequal(p, 'c:/');
assert_isequal(f, '');
assert_isequal(e, '');
%=============================================================================
if ispc()
assert_isequal(p, 'c:/');
assert_isequal(f, '');
assert_isequal(e, '');
else
assert_isequal('c:', p);
assert_isequal('', f);
assert_isequal('', e);
end
%=============================================================================
32 changes: 25 additions & 7 deletions modules/files_folders_functions/tests/test_fileparts.m
Expand Up @@ -66,7 +66,7 @@
assert_isequal('', e);
%=============================================================================
[p,f,e] = fileparts('c:/Windows//');
assert_isequal('c:/Windows', p);
assert_isequal('c:/Windows/', p);
assert_isequal('', f);
assert_isequal('', e);
%=============================================================================
Expand All @@ -76,12 +76,30 @@
assert_isequal('', e);
%=============================================================================
[p,f,e] = fileparts('c:');
assert_isequal('c:', p);
assert_isequal('', f);
assert_isequal('', e);
if ispc()
assert_isequal('c:', p);
assert_isequal('', f);
assert_isequal('', e);
else
assert_isequal('', p);
assert_isequal('c:', f);
assert_isequal('', e);
end
%=============================================================================
[p,f,e] = fileparts('c:/');
assert_isequal('c:/', p);
assert_isequal('', f);
assert_isequal('', e);
if ispc()
assert_isequal('c:/', p);
assert_isequal('', f);
assert_isequal('', e);
else
assert_isequal('c:', p);
assert_isequal('', f);
assert_isequal('', e);
end
%=============================================================================
url = char([104 116 116 112 115 58 47 47 104 111 111 107 115 46 115 108 97 99 107 46 99 111 109 47 115 101 114 118 105 99 101 115 47 84 77 82 71 56 82 72 68 50 47 66 77 83 48 76 72 65 65 67 47 81 54 52 97 52 49 84 83 76 104 105 78 71 81 108 100 51 115 76 50 86 109 74 71]);
[p,f,e] = fileparts(url);
assert_isequal(f , 'Q64a41TSLhiNGQld3sL2VmJG');
assert_isequal(e , '');
assert_isequal(length(p) , 52);
%=============================================================================

0 comments on commit 0cd88cc

Please sign in to comment.