Skip to content

Commit

Permalink
detectImportOptions, readcell, readtable functions (#1298)
Browse files Browse the repository at this point in the history
- Implement detectImportOptions for CSV import configuration
- Add readtable function for importing tabular data
- Enhance readcell with new import options
- Update ArrayOf to support table data type
- Implement optimized join() function for string concatenation
- Add strjust() implementation with performance improvements
- Fix #1292 - Refactor table display functionality
  • Loading branch information
Nelson-numerical-software authored Dec 8, 2024
1 parent e17e2ed commit c3bf24b
Show file tree
Hide file tree
Showing 75 changed files with 3,953 additions and 472 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `detectImportOptions`: Generate import options from the file's content.
- `readcell`: Read cell array from file.
- `readtable`: Read table from file.
- `writetable`: Write table to file.
- `writecell`: write cell array to file.
- `writematrix`: write matrix to file.
Expand All @@ -17,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `dlmread`: Read ASCII-delimited file of numeric data into matrix.
- `realmin`: Smallest normalized floating-point number.
- [#1288](http://github.com/nelson-lang/nelson/issues/1288) `mustBeMatrix`, `mustBeRow`, `mustBeColumn` validator functions.
- `join`: Combine strings.
- [#1292](http://github.com/nelson-lang/nelson/issues/1292) Large Table Display.

### Changed

Expand All @@ -25,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `title`: `Visible` property is inherited from the parent if not explicitly defined.
- i18n: migration PO files to JSON.
- `dlmwrite`: rework the function to be more fast and robust.
- `strjust`: rework the function to be more fast and robust.

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<copyright>SAME AS NELSON SOFTWARE</copyright>

<language>en_US</language>
<keyword>actxGetRunningSrv</keyword>
<keyword>actxGetRunningServer</keyword>
<short_description
>Handle to running instance of Automation server.</short_description>

<syntax>
<syntax_item>h = actxGetRunningSrv(progid)</syntax_item>
<syntax_item>h = actxGetRunningServer(progid)</syntax_item>
</syntax>

<param_input>
Expand All @@ -30,7 +30,7 @@

<description>
<p><b
>h = actxGetRunningSrv(progid)</b> gets a reference to a running instance of the OLE/COM Automation server.</p>
>h = actxGetRunningServer(progid)</b> gets a reference to a running instance of the OLE/COM Automation server.</p>
<p><b
>progid</b> is the programmatic identifier of the Automation server object and <b
>h</b> is the handle to the default interface of the server object.</p>
Expand Down
10 changes: 5 additions & 5 deletions modules/core/src/cpp/Banner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ void
Banner()
{
NelsonPrint(L"\n");
NelsonPrint(L" __ _ __\n");
NelsonPrint(L" /\\ \\ \\___| |/ _\\ ___ _ __\n");
NelsonPrint(L" / \\/ / _ | |\\ \\ / _ \\| '_ \\\n");
NelsonPrint(L"/ /\\ | __| |_\\ | (_) | | | |\n");
NelsonPrint(L"\\_\\ \\/ \\___|_|\\__/\\___/|_| |_|\n");
NelsonPrint(L" _ __ __\n");
NelsonPrint(L" / | / /__ / /________ ____\n");
NelsonPrint(L" / |/ / _ \\/ / ___/ __ \\/ __ \\\n");
NelsonPrint(L" / /| / __/ (__ ) /_/ / / / /\n");
NelsonPrint(L"/_/ |_/\\___/_/____/\\____/_/ /_/\n");
}
//=============================================================================
} // namespace Nelson
Expand Down
4 changes: 2 additions & 2 deletions modules/data_structures/help/en_US/xml/cellfun.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ f = str2func('size');
<example_item_data
><![CDATA[function r = fun1(x, y)
r = x > y;
endfunction
end
function result = errorfun(S, varargin)
disp(nargin())
Expand All @@ -91,7 +91,7 @@ function result = errorfun(S, varargin)
disp(varargin{1})
disp(varargin{2})
result = false;
endfunction]]>
end]]>
</example_item_data>
</example_item>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

<language>en_US</language>
<keyword>num2cell</keyword>
<short_description>Convert array to cell array with consistently sized cells.</short_description>
<short_description
>Convert array to cell array with consistently sized cells.</short_description>

<syntax>
<syntax_item>C = num2cell(A)</syntax_item>
Expand All @@ -19,7 +20,8 @@

<param_input_item>
<param_name>dim</param_name>
<param_description>positive integer value or positive vector of integers.</param_description>
<param_description
>positive integer value or positive vector of integers.</param_description>
</param_input_item>

</param_input>
Expand All @@ -33,8 +35,10 @@
</param_output>

<description>
<p><b>num2cell</b> function converts a numeric array into a cell array, where each element of the numeric array is placed in its own cell in the resulting cell array.</p>
<p>If <b>A</b> is a character array, num2cell will convert each row of the array into a separate cell in the resulting cell array.</p>
<p><b
>num2cell</b> function converts a numeric array into a cell array, where each element of the numeric array is placed in its own cell in the resulting cell array.</p>
<p>If <b
>A</b> is a character array, num2cell will convert each row of the array into a separate cell in the resulting cell array.</p>
</description>


Expand Down
18 changes: 8 additions & 10 deletions modules/display_format/functions/formattedDisplayText.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
% LICENCE_BLOCK_END
%=============================================================================
function result = formattedDisplayText(varargin)
nbArgsValid = (nargin >= 1) && (mod(nargin, 2) == 1);
nbArgsValid = mod(nargin, 2) == 1;
if ~nbArgsValid
error(_('Wrong number of input arguments.'));
end
Expand All @@ -25,23 +25,21 @@
validField = false;
name = lower(args{i});
value = args{i + 1};
if strcmp(name, 'numericformat')
switch name
case 'numericformat'
validateNumericFormat(value, i + 1);
newFormat.NumericFormat = value;
validField = true;
end
if strcmp(name, 'linespacing')

case 'linespacing'
validateLineSpacing(value, i + 1);
newFormat.LineSpacing = value;
validField = true;
end
if strcmp(name, 'suppressmarkup')
% not managed -> ignored
validField = true;
end
if strcmp(name, 'usetruefalseforlogical')

case {'suppressmarkup', 'usetruefalseforlogical'}
% not managed -> ignored
validField = true;

end
if ~validField
msg = sprintf(_('Invalid name-value argument: %s.'), args{i});
Expand Down
10 changes: 10 additions & 0 deletions modules/display_format/src/cpp/FormatHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ template <class T>
static std::wstring
formatIntegerReal(T val, const FormatDisplayInformation& formatInfo)
{
if (std::isnan(val)) {
return L"NaN";
}
if (std::isinf(val)) {
if (val > 0) {
return L"Inf";
} else {
return L"-Inf";
}
}
if (formatInfo.decimalsReal != 0) {
return fmt::sprintf(
formatInfo.formatReal, formatInfo.widthReal, formatInfo.decimalsReal, val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
function varargout = strjust(varargin)
narginchk(1, 2);
nargoutchk(0, 1);

if nargin == 1
justify = 'right';
else
justify = lower(varargin{2});
end
mustBeMember(justify, ["left", "right", "center"], 2);

str = varargin{1};
result = str;
for k = 1:numel(str)
if ischar(str{k})
result{k} = strjust(str{k}, justify);
else
error(_('String, cell of chars or characters vector expected.'));
end
end
varargout{1} = result;
end
% <--ENGLISH IMPOSED-->
%=============================================================================
st = [];
st.A = [1 Inf];
st.B = [2 -Inf];
st.C = [3 NaN];
R = evalc('st');
REF = '
st =
struct with fields:
A: [1 Inf]
B: [2 -Inf]
C: [3 NaN]
';
assert_isequal(R, REF);
%=============================================================================
2 changes: 1 addition & 1 deletion modules/interpreter/help/en_US/xml/switch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
otherwise
c = 'not sure';
end
endfunction
end
]]>
</example_item_data>
</example_item>
Expand Down
2 changes: 1 addition & 1 deletion modules/memory_manager/help/en_US/xml/global.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
><![CDATA[function myfun()
global y;
y = 1;
endfunction
end
myfun()
who
Expand Down
4 changes: 4 additions & 0 deletions modules/spreadsheet/builtin/c/nlsSpreadsheet_builtin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,22 @@
<ResourceCompile Include="resources.rc" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\cpp\detectImportOptionsBuiltin.cpp" />
<ClCompile Include="..\cpp\dlmreadlBuiltin.cpp" />
<ClCompile Include="..\cpp\dlmwriteBuiltin.cpp" />
<ClCompile Include="..\cpp\Gateway.cpp" />
<ClCompile Include="..\cpp\dllMain.cpp" />
<ClCompile Include="..\cpp\readcellBuiltin.cpp" />
<ClCompile Include="..\cpp\readtableBuiltin.cpp" />
<ClCompile Include="..\cpp\writetableBuiltin.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\detectImportOptionsBuiltin.hpp" />
<ClInclude Include="..\include\dlmreadBuiltin.hpp" />
<ClInclude Include="..\include\dlmwriteBuiltin.hpp" />
<ClInclude Include="..\include\nlsSpreadsheet_builtin_exports.h" />
<ClInclude Include="..\include\readcellBuiltin.hpp" />
<ClInclude Include="..\include\readtableBuiltin.hpp" />
<ClInclude Include="..\include\writetableBuiltin.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
<ClCompile Include="..\cpp\writetableBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\detectImportOptionsBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\readtableBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\nlsSpreadsheet_builtin_exports.h">
Expand All @@ -64,5 +70,11 @@
<ClInclude Include="..\include\writetableBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\detectImportOptionsBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\include\readtableBuiltin.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions modules/spreadsheet/builtin/cpp/Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@
//=============================================================================
#include "NelsonGateway.hpp"
#include "readcellBuiltin.hpp"
#include "readtableBuiltin.hpp"
#include "dlmreadBuiltin.hpp"
#include "dlmwriteBuiltin.hpp"
#include "writetableBuiltin.hpp"
#include "detectImportOptionsBuiltin.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
const std::wstring gatewayName = L"spreadsheet";
//=============================================================================
static const nlsGateway gateway[] = {
{ "readcell", (ptrBuiltin)Nelson::SpreadsheetGateway::readcellBuiltin, 1, 1 },
{ "readtable", (ptrBuiltin)Nelson::SpreadsheetGateway::readtableBuiltin, 1, 1 },
{ "dlmread", (ptrBuiltin)Nelson::SpreadsheetGateway::dlmreadBuiltin, 1, 4 },
{ "dlmwrite", (ptrBuiltin)Nelson::SpreadsheetGateway::dlmwriteBuiltin, 0, -3,
CPP_BUILTIN_WITH_EVALUATOR },
{ "writetable", (ptrBuiltin)Nelson::SpreadsheetGateway::writetableBuiltin, 0, 4 },
{ "detectImportOptions", (ptrBuiltin)Nelson::SpreadsheetGateway::detectImportOptionsBuiltin, 1,
-1 },

};
//=============================================================================
NLSGATEWAYFUNC(gateway)
Expand Down
53 changes: 53 additions & 0 deletions modules/spreadsheet/builtin/cpp/detectImportOptionsBuiltin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#include "detectImportOptionsBuiltin.hpp"
#include "Error.hpp"
#include "InputOutputArgumentsCheckers.hpp"
#include "DetectImportOptions.hpp"
//=============================================================================
using namespace Nelson;
//=============================================================================
ArrayOfVector
Nelson::SpreadsheetGateway::detectImportOptionsBuiltin(int nLhs, const ArrayOfVector& argIn)
{
ArrayOfVector retval;
nargoutcheck(nLhs, 0, 1);
nargincheck(argIn, 1, 1);

std::wstring filename = argIn[0].getContentAsWideString();

detectImportOptions options;
initializeDetectImportOptions(options);

std::string errorMessage;
analyzeFileFormatImportOptions(filename, 4096, options, errorMessage);
if (!errorMessage.empty()) {
Error(errorMessage);
}

stringVector fieldnames = { "Delimiter", "LineEnding", "CommentStyle", "EmptyLineRule",
"VariableNamesLine", "VariableNames", "RowNamesColumn", "DataLines" };
ArrayOfVector fieldvalues;

fieldvalues << ArrayOf::toCellArrayOfCharacterRowVectors(options.Delimiter);
fieldvalues << ArrayOf::toCellArrayOfCharacterRowVectors(options.LineEnding);
fieldvalues << ArrayOf::toCellArrayOfCharacterRowVectors(options.CommentStyle);
fieldvalues << ArrayOf::characterArrayConstructor(options.EmptyLineRule);
fieldvalues << ArrayOf::doubleConstructor(options.VariableNamesLine);
fieldvalues << ArrayOf::toCellArrayOfCharacterRowVectors(options.VariableNames);
fieldvalues << ArrayOf::doubleConstructor(options.RowNamesColumn);
fieldvalues << ArrayOf::doubleVectorConstructor(options.DataLines);

ArrayOf importOptions
= ArrayOf::classConstructor("DelimitedTextImportOptions", fieldnames, fieldvalues);
retval << importOptions;
return retval;
}
//=============================================================================
Loading

0 comments on commit c3bf24b

Please sign in to comment.