Skip to content

Commit

Permalink
fix(files_folders_functions): correct RemoveDirectory implementation
Browse files Browse the repository at this point in the history
- Updated RemoveDirectory.cpp to fix directory removal issues
- Modified filewriteBuiltin.cpp to improve file writing functionality
- Added tests in test_filewrite.m to cover new changes
  • Loading branch information
Nelson-numerical-software committed Dec 22, 2024
1 parent 44e8578 commit 3047ac2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 73 deletions.
7 changes: 4 additions & 3 deletions modules/files_folders_functions/src/cpp/RemoveDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ RemoveDirectory(const std::wstring& folderName, bool bSubfolder, std::wstring& m
FileSystemWrapper::Path p(folderName);
std::string errorMessage;
if (bSubfolder) {
if (!FileSystemWrapper::Path::remove_all(p, errorMessage)) {
res = false;
res = FileSystemWrapper::Path::remove_all(p, errorMessage);
if (!res) {
message = utf8_to_wstring(errorMessage);
return res;
}
} else {
res = FileSystemWrapper::Path::remove(p, errorMessage);
}
res = FileSystemWrapper::Path::remove(p, errorMessage);
if (!res) {
message = utf8_to_wstring(errorMessage);
}
Expand Down
141 changes: 71 additions & 70 deletions modules/stream_manager/builtin/cpp/filewriteBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,64 @@ using namespace Nelson;
//=============================================================================
// filewrite(filename, txt [, eol, encoding])
// eol == 'native' (system default), 'pc' ("\r\n"), 'unix' ("\n")
//=============================================================================
namespace {
//=============================================================================
static std::pair<std::wstring, std::string>
getEol(const std::wstring& str)
{
if (str == L"pc") {
return { L"\r\n", "\r\n" };
} else if (str == L"unix") {
return { L"\n", "\n" };
} else if (str == L"native") {
#ifdef _MSC_VER
return { L"\r\n", "\r\n" };
#else
return { L"\n", "\n" };
#endif
} else {
Error(_W("Wrong value for #3 argument."));
return { L"", "" }; // This line will never be reached
}
}
//=============================================================================
static void
writeFile(const std::wstring& filename, const wstringVector& lines, const std::wstring& weol,
const std::string& eol, const std::string& encoding)
{
#ifdef _MSC_VER
std::ofstream of(filename, std::ios::trunc | std::ios::binary);
#else
std::ofstream of(wstring_to_utf8(filename), std::ios::trunc | std::ios::binary);
#endif
if (!of.is_open()) {
Error(_W("Cannot open file."));
}

for (size_t k = 0; k < lines.size(); ++k) {
std::wstring line = lines[k];
StringHelpers::replace_all(line, L"\r\n", L"\n");
StringHelpers::replace_all(line, L"\n", weol);
std::string data;
if (encoding == "UTF-8") {
data = wstring_to_utf8(line);
} else {
std::string asUtf8 = wstring_to_utf8(line);
if (!utf8ToCharsetConverter(asUtf8, data, encoding)) {
of.flush();
Error(_W("Encoding not supported."));
}
}
of << data;
if (!StringHelpers::ends_with(data, eol) && k != lines.size() - 1) {
of << eol;
}
}
of.flush();
}
}
//=============================================================================
ArrayOfVector
Nelson::StreamGateway::filewriteBuiltin(int nLhs, const ArrayOfVector& argIn)
{
Expand All @@ -33,30 +91,20 @@ Nelson::StreamGateway::filewriteBuiltin(int nLhs, const ArrayOfVector& argIn)
std::wstring weol;
std::string eol;
std::string encoding = "UTF-8";

if (argIn.size() >= 3) {
ArrayOf param3 = argIn[2];
std::tie(weol, eol) = getEol(param3.getContentAsWideString());
} else {
#ifdef _MSC_VER
weol = L"\r\n";
eol = "\r\n";
weol = L"\r\n";
eol = "\r\n";
#else
weol = L"\n";
eol = "\n";
weol = L"\n";
eol = "\n";
#endif
if (argIn.size() == 3) {
ArrayOf param3 = argIn[2];
std::wstring str = param3.getContentAsWideString();
if (str == L"native" || str == L"pc" || str == L"unix") {
if (str == L"pc") {
weol = L"\r\n";
eol = "\r\n";
}
if (str == L"unix") {
weol = L"\n";
eol = "\n";
}
} else {
Error(_W("Wrong value for #3 argument."));
}
}
wstringVector lines = param2.getContentAsWideStringVector(false);

if (argIn.size() == 4) { //-V112
ArrayOf param4 = argIn[3];
encoding = param4.getContentAsCString();
Expand All @@ -65,56 +113,9 @@ Nelson::StreamGateway::filewriteBuiltin(int nLhs, const ArrayOfVector& argIn)
}
}

if (encoding == "UTF-8") {
#ifdef _MSC_VER
std::wofstream wof(filename, std::ios::trunc | std::ios::binary);
#else
std::wofstream wof(wstring_to_utf8(filename), std::ios::trunc | std::ios::binary);
#endif
if (!wof.is_open()) {
Error(_W("Cannot open file."));
}
for (size_t k = 0; k < lines.size(); ++k) {
std::wstring line = lines[k];
StringHelpers::replace_all(line, L"\r\n", L"\n");
StringHelpers::replace_all(line, L"\n", weol);
wof << line;
if (!StringHelpers::ends_with(line, weol)) {
if (k != lines.size() - 1) {
wof << weol;
}
}
}
wof.flush();
} else {
#ifdef _MSC_VER
std::ofstream of(filename, std::ios::trunc | std::ios::binary);
#else
std::ofstream of(wstring_to_utf8(filename), std::ios::trunc | std::ios::binary);
#endif
if (!of.is_open()) {
Error(_W("Cannot open file."));
}
for (size_t k = 0; k < lines.size(); ++k) {
std::string asUtf8 = wstring_to_utf8(lines[k]);
StringHelpers::replace_all(asUtf8, "\r\n", "\n");
StringHelpers::replace_all(asUtf8, "\n", eol);
std::string data;
if (utf8ToCharsetConverter(asUtf8, data, encoding)) {
of << data;
if (!StringHelpers::ends_with(data, eol)) {
if (k != lines.size() - 1) {
of << eol;
}
}
} else {
of.flush();
Error(_W("Encoding not supported."));
}
}
of.flush();
// close made by destructor of ofstream
}
wstringVector lines = param2.getContentAsWideStringVector(false);
writeFile(filename, lines, weol, eol, encoding);

return retval;
}
//=============================================================================
2 changes: 2 additions & 0 deletions modules/stream_manager/tests/test_filewrite.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@
end
str = 'живете зело, земля, и иже и како люди';
filewrite(dest, str, 'native', 'windows-1251');
R = fileread(dest,'char','native','windows-1251');
assert_isequal(R, str);
%=============================================================================

0 comments on commit 3047ac2

Please sign in to comment.