Compare commits
No commits in common. "003186b3b1a7398c9c6634a763ae9c23a93fbe3d" and "cc3bdc000ce8682b9e75caaaf470a22ac774c66b" have entirely different histories.
003186b3b1
...
cc3bdc000c
248
.editorconfig
248
.editorconfig
@ -1,248 +0,0 @@
|
|||||||
# Remove the line below if you want to inherit .editorconfig settings from higher directories
|
|
||||||
root = true
|
|
||||||
|
|
||||||
# C# files
|
|
||||||
[*.cs]
|
|
||||||
|
|
||||||
dotnet_analyzer_diagnostic.category-Design.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Documentation.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Globalization.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Interoperability.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Maintainability.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Naming.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Performance.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Reliability.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Security.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Style.severity = warning
|
|
||||||
dotnet_analyzer_diagnostic.category-Usage.severity = warning
|
|
||||||
|
|
||||||
dotnet_diagnostic.IDE0010.severity = none
|
|
||||||
dotnet_diagnostic.IDE0072.severity = none
|
|
||||||
dotnet_diagnostic.CA1707.severity = none
|
|
||||||
dotnet_diagnostic.CA1861.severity = none
|
|
||||||
|
|
||||||
#### Core EditorConfig Options ####
|
|
||||||
|
|
||||||
# Indentation and spacing
|
|
||||||
indent_size = 4
|
|
||||||
indent_style = space
|
|
||||||
tab_width = 4
|
|
||||||
|
|
||||||
# New line preferences
|
|
||||||
end_of_line = crlf
|
|
||||||
insert_final_newline = false
|
|
||||||
|
|
||||||
#### .NET Coding Conventions ####
|
|
||||||
|
|
||||||
# Organize usings
|
|
||||||
dotnet_separate_import_directive_groups = false
|
|
||||||
dotnet_sort_system_directives_first = false
|
|
||||||
file_header_template = unset
|
|
||||||
|
|
||||||
# this. and Me. preferences
|
|
||||||
dotnet_style_qualification_for_event = false
|
|
||||||
dotnet_style_qualification_for_field = false
|
|
||||||
dotnet_style_qualification_for_method = false
|
|
||||||
dotnet_style_qualification_for_property = false
|
|
||||||
|
|
||||||
# Language keywords vs BCL types preferences
|
|
||||||
dotnet_style_predefined_type_for_locals_parameters_members = true
|
|
||||||
dotnet_style_predefined_type_for_member_access = true
|
|
||||||
|
|
||||||
# Parentheses preferences
|
|
||||||
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
|
|
||||||
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
|
|
||||||
dotnet_style_parentheses_in_other_operators = never_if_unnecessary
|
|
||||||
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
|
|
||||||
|
|
||||||
# Modifier preferences
|
|
||||||
dotnet_style_require_accessibility_modifiers = for_non_interface_members
|
|
||||||
|
|
||||||
# Expression-level preferences
|
|
||||||
dotnet_style_coalesce_expression = true
|
|
||||||
dotnet_style_collection_initializer = true
|
|
||||||
dotnet_style_explicit_tuple_names = true
|
|
||||||
dotnet_style_namespace_match_folder = true
|
|
||||||
dotnet_style_null_propagation = true
|
|
||||||
dotnet_style_object_initializer = true
|
|
||||||
dotnet_style_operator_placement_when_wrapping = beginning_of_line
|
|
||||||
dotnet_style_prefer_auto_properties = true
|
|
||||||
dotnet_style_prefer_collection_expression = false
|
|
||||||
dotnet_style_prefer_compound_assignment = true
|
|
||||||
dotnet_style_prefer_conditional_expression_over_assignment = false
|
|
||||||
dotnet_style_prefer_conditional_expression_over_return = false
|
|
||||||
dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed
|
|
||||||
dotnet_style_prefer_inferred_anonymous_type_member_names = true
|
|
||||||
dotnet_style_prefer_inferred_tuple_names = true
|
|
||||||
dotnet_style_prefer_is_null_check_over_reference_equality_method = true
|
|
||||||
dotnet_style_prefer_simplified_boolean_expressions = true
|
|
||||||
dotnet_style_prefer_simplified_interpolation = true
|
|
||||||
|
|
||||||
# Field preferences
|
|
||||||
dotnet_style_readonly_field = true
|
|
||||||
|
|
||||||
# Parameter preferences
|
|
||||||
dotnet_code_quality_unused_parameters = all:silent
|
|
||||||
|
|
||||||
# Suppression preferences
|
|
||||||
dotnet_remove_unnecessary_suppression_exclusions = none
|
|
||||||
|
|
||||||
# New line preferences
|
|
||||||
dotnet_style_allow_multiple_blank_lines_experimental = true
|
|
||||||
dotnet_style_allow_statement_immediately_after_block_experimental = true
|
|
||||||
|
|
||||||
#### C# Coding Conventions ####
|
|
||||||
|
|
||||||
# var preferences
|
|
||||||
csharp_style_var_elsewhere = true
|
|
||||||
csharp_style_var_for_built_in_types = true
|
|
||||||
csharp_style_var_when_type_is_apparent = true
|
|
||||||
|
|
||||||
# Expression-bodied members
|
|
||||||
csharp_style_expression_bodied_accessors = true
|
|
||||||
csharp_style_expression_bodied_constructors = false
|
|
||||||
csharp_style_expression_bodied_indexers = true
|
|
||||||
csharp_style_expression_bodied_lambdas = true
|
|
||||||
csharp_style_expression_bodied_local_functions = false
|
|
||||||
csharp_style_expression_bodied_methods = true:none
|
|
||||||
csharp_style_expression_bodied_operators = false
|
|
||||||
csharp_style_expression_bodied_properties = true
|
|
||||||
|
|
||||||
# Pattern matching preferences
|
|
||||||
csharp_style_pattern_matching_over_as_with_null_check = true
|
|
||||||
csharp_style_pattern_matching_over_is_with_cast_check = true
|
|
||||||
csharp_style_prefer_extended_property_pattern = true
|
|
||||||
csharp_style_prefer_not_pattern = true
|
|
||||||
csharp_style_prefer_pattern_matching = true
|
|
||||||
csharp_style_prefer_switch_expression = true
|
|
||||||
|
|
||||||
# Null-checking preferences
|
|
||||||
csharp_style_conditional_delegate_call = true
|
|
||||||
|
|
||||||
# Modifier preferences
|
|
||||||
csharp_prefer_static_local_function = true
|
|
||||||
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async
|
|
||||||
csharp_style_prefer_readonly_struct = true
|
|
||||||
csharp_style_prefer_readonly_struct_member = true
|
|
||||||
|
|
||||||
# Code-block preferences
|
|
||||||
csharp_prefer_braces = true
|
|
||||||
csharp_prefer_simple_using_statement = true
|
|
||||||
csharp_style_namespace_declarations = file_scoped:none
|
|
||||||
csharp_style_prefer_method_group_conversion = true
|
|
||||||
csharp_style_prefer_primary_constructors = false
|
|
||||||
csharp_style_prefer_top_level_statements = true
|
|
||||||
|
|
||||||
# Expression-level preferences
|
|
||||||
csharp_prefer_simple_default_expression = true
|
|
||||||
csharp_style_deconstructed_variable_declaration = true
|
|
||||||
csharp_style_implicit_object_creation_when_type_is_apparent = true
|
|
||||||
csharp_style_inlined_variable_declaration = true
|
|
||||||
csharp_style_prefer_index_operator = true
|
|
||||||
csharp_style_prefer_local_over_anonymous_function = true
|
|
||||||
csharp_style_prefer_null_check_over_type_check = true
|
|
||||||
csharp_style_prefer_range_operator = true
|
|
||||||
csharp_style_prefer_tuple_swap = true
|
|
||||||
csharp_style_prefer_utf8_string_literals = true
|
|
||||||
csharp_style_throw_expression = true
|
|
||||||
csharp_style_unused_value_assignment_preference = discard_variable
|
|
||||||
csharp_style_unused_value_expression_statement_preference = discard_variable
|
|
||||||
|
|
||||||
# 'using' directive preferences
|
|
||||||
csharp_using_directive_placement = outside_namespace
|
|
||||||
|
|
||||||
# New line preferences
|
|
||||||
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true
|
|
||||||
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true
|
|
||||||
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true
|
|
||||||
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true
|
|
||||||
csharp_style_allow_embedded_statements_on_same_line_experimental = true
|
|
||||||
|
|
||||||
#### C# Formatting Rules ####
|
|
||||||
|
|
||||||
# New line preferences
|
|
||||||
csharp_new_line_before_catch = true
|
|
||||||
csharp_new_line_before_else = true
|
|
||||||
csharp_new_line_before_finally = true
|
|
||||||
csharp_new_line_before_members_in_anonymous_types = true
|
|
||||||
csharp_new_line_before_members_in_object_initializers = true
|
|
||||||
csharp_new_line_before_open_brace = all
|
|
||||||
csharp_new_line_between_query_expression_clauses = true
|
|
||||||
|
|
||||||
# Indentation preferences
|
|
||||||
csharp_indent_block_contents = true
|
|
||||||
csharp_indent_braces = false
|
|
||||||
csharp_indent_case_contents = true
|
|
||||||
csharp_indent_case_contents_when_block = true
|
|
||||||
csharp_indent_labels = one_less_than_current
|
|
||||||
csharp_indent_switch_labels = true
|
|
||||||
|
|
||||||
# Space preferences
|
|
||||||
csharp_space_after_cast = false
|
|
||||||
csharp_space_after_colon_in_inheritance_clause = true
|
|
||||||
csharp_space_after_comma = true
|
|
||||||
csharp_space_after_dot = false
|
|
||||||
csharp_space_after_keywords_in_control_flow_statements = true
|
|
||||||
csharp_space_after_semicolon_in_for_statement = true
|
|
||||||
csharp_space_around_binary_operators = before_and_after
|
|
||||||
csharp_space_around_declaration_statements = false
|
|
||||||
csharp_space_before_colon_in_inheritance_clause = true
|
|
||||||
csharp_space_before_comma = false
|
|
||||||
csharp_space_before_dot = false
|
|
||||||
csharp_space_before_open_square_brackets = false
|
|
||||||
csharp_space_before_semicolon_in_for_statement = false
|
|
||||||
csharp_space_between_empty_square_brackets = false
|
|
||||||
csharp_space_between_method_call_empty_parameter_list_parentheses = false
|
|
||||||
csharp_space_between_method_call_name_and_opening_parenthesis = false
|
|
||||||
csharp_space_between_method_call_parameter_list_parentheses = false
|
|
||||||
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
|
|
||||||
csharp_space_between_method_declaration_name_and_open_parenthesis = false
|
|
||||||
csharp_space_between_method_declaration_parameter_list_parentheses = false
|
|
||||||
csharp_space_between_parentheses = false
|
|
||||||
csharp_space_between_square_brackets = false
|
|
||||||
|
|
||||||
# Wrapping preferences
|
|
||||||
csharp_preserve_single_line_blocks = true
|
|
||||||
csharp_preserve_single_line_statements = true
|
|
||||||
|
|
||||||
#### Naming styles ####
|
|
||||||
|
|
||||||
# Naming rules
|
|
||||||
|
|
||||||
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
|
|
||||||
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
|
|
||||||
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
|
|
||||||
|
|
||||||
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
|
|
||||||
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
|
|
||||||
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
|
|
||||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
|
|
||||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
|
|
||||||
|
|
||||||
# Symbol specifications
|
|
||||||
|
|
||||||
dotnet_naming_symbols.interface.applicable_kinds = interface
|
|
||||||
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.interface.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
|
|
||||||
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.types.required_modifiers =
|
|
||||||
|
|
||||||
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
|
|
||||||
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
|
||||||
dotnet_naming_symbols.non_field_members.required_modifiers =
|
|
||||||
|
|
||||||
# Naming styles
|
|
||||||
|
|
||||||
dotnet_naming_style.pascal_case.required_prefix =
|
|
||||||
dotnet_naming_style.pascal_case.required_suffix =
|
|
||||||
dotnet_naming_style.pascal_case.word_separator =
|
|
||||||
dotnet_naming_style.pascal_case.capitalization = pascal_case
|
|
||||||
|
|
||||||
dotnet_naming_style.begins_with_i.required_prefix = I
|
|
||||||
dotnet_naming_style.begins_with_i.required_suffix =
|
|
||||||
dotnet_naming_style.begins_with_i.word_separator =
|
|
||||||
dotnet_naming_style.begins_with_i.capitalization = pascal_case
|
|
68
MatFileHandler.Tests/AbstractTestDataFactory.cs
Executable file
68
MatFileHandler.Tests/AbstractTestDataFactory.cs
Executable file
@ -0,0 +1,68 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Abstract factory of test data.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TTestData">Type of test data.</typeparam>
|
||||||
|
public abstract class AbstractTestDataFactory<TTestData>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="AbstractTestDataFactory{TTestData}"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataDirectory">Directory with test files.</param>
|
||||||
|
/// <param name="testFilenameConvention">A convention used to filter test files.</param>
|
||||||
|
protected AbstractTestDataFactory(string dataDirectory, ITestFilenameConvention testFilenameConvention)
|
||||||
|
{
|
||||||
|
DataDirectory = dataDirectory;
|
||||||
|
TestFilenameConvention = testFilenameConvention;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string DataDirectory { get; }
|
||||||
|
|
||||||
|
private ITestFilenameConvention TestFilenameConvention { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get test data set by name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataSet">Name of the data set.</param>
|
||||||
|
/// <returns>Test data.</returns>
|
||||||
|
public TTestData this[string dataSet] =>
|
||||||
|
ReadTestData(FixPath(TestFilenameConvention.ConvertTestNameToFilename(dataSet)));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a sequence of all test data sets in the factory.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A sequence of data sets.</returns>
|
||||||
|
public IEnumerable<TTestData> GetAllTestData()
|
||||||
|
{
|
||||||
|
var files = Directory.EnumerateFiles(DataDirectory).Where(TestFilenameConvention.FilterFile);
|
||||||
|
foreach (var filename in files)
|
||||||
|
{
|
||||||
|
yield return ReadTestData(filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read test data from a stream.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">Input stream.</param>
|
||||||
|
/// <returns>Test data.</returns>
|
||||||
|
protected abstract TTestData ReadDataFromStream(Stream stream);
|
||||||
|
|
||||||
|
private string FixPath(string filename) => Path.Combine(DataDirectory, filename);
|
||||||
|
|
||||||
|
private TTestData ReadTestData(string filename)
|
||||||
|
{
|
||||||
|
using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
|
||||||
|
{
|
||||||
|
return ReadDataFromStream(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -15,48 +17,50 @@ namespace MatFileHandler.Tests
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing various things.
|
/// Test writing various things.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bytes">Bytes to write.</param>
|
/// <param name="action"></param>
|
||||||
[Theory]
|
[Theory]
|
||||||
[MemberData(nameof(TestData))]
|
[MemberData(nameof(TestData))]
|
||||||
public void Test(byte[] bytes)
|
public void Test(Action<Stream> action)
|
||||||
{
|
{
|
||||||
using var stream = new MemoryStream();
|
using var stream = new MemoryStream();
|
||||||
var sut = new ChecksumCalculatingStream(stream);
|
var sut = new ChecksumCalculatingStream(stream);
|
||||||
sut.Write(bytes, 0, bytes.Length);
|
action(sut);
|
||||||
var actual = sut.GetCrc();
|
var actual = sut.GetCrc();
|
||||||
|
var expected = ReferenceCalculation(action);
|
||||||
var expected = ReferenceCalculation(bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test data for <see cref="Test"/>.
|
/// Test data for <see cref="Test"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Test data.</returns>
|
/// <returns>Test data.</returns>
|
||||||
public static TheoryData<byte[]> TestData()
|
public static IEnumerable<object[]> TestData()
|
||||||
{
|
{
|
||||||
var empty = new byte[1234];
|
foreach (var data in TestData_Typed())
|
||||||
var nonEmpty = new byte[12345];
|
|
||||||
for (var i = 0; i < 1234; i++)
|
|
||||||
{
|
{
|
||||||
nonEmpty[i] = (byte)((i * i) % 256);
|
yield return new object[] { data };
|
||||||
}
|
}
|
||||||
return new TheoryData<byte[]>()
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<Action<Stream>> TestData_Typed()
|
||||||
{
|
{
|
||||||
new byte[] { 0x00 },
|
yield return BinaryWriterAction(w => w.Write(true));
|
||||||
new byte[] { 0x01 },
|
yield return BinaryWriterAction(w => w.Write(false));
|
||||||
new byte[] { 0xff },
|
yield return BinaryWriterAction(w => w.Write(byte.MinValue));
|
||||||
new byte[] { 0xff, 0xff },
|
yield return BinaryWriterAction(w => w.Write(byte.MaxValue));
|
||||||
new byte[] { 0xff, 0xff, 0xff },
|
yield return BinaryWriterAction(w => w.Write(short.MinValue));
|
||||||
new byte[] { 0xff, 0xff, 0xff, 0xff },
|
yield return BinaryWriterAction(w => w.Write(short.MaxValue));
|
||||||
new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff },
|
yield return BinaryWriterAction(w => w.Write(int.MinValue));
|
||||||
new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
|
yield return BinaryWriterAction(w => w.Write(int.MaxValue));
|
||||||
new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
|
yield return BinaryWriterAction(w => w.Write(long.MinValue));
|
||||||
new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
|
yield return BinaryWriterAction(w => w.Write(long.MaxValue));
|
||||||
new byte[] { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
|
yield return BinaryWriterAction(w => w.Write(decimal.MinValue));
|
||||||
new byte[] { 0x02, 0x03, 0x05, 0x07, 0x0b, 0x0d, 0x11, 0x13, 0x17, 0x1d },
|
yield return BinaryWriterAction(w => w.Write(decimal.MaxValue));
|
||||||
empty,
|
yield return BinaryWriterAction(w => w.Write(double.MinValue));
|
||||||
nonEmpty,
|
yield return BinaryWriterAction(w => w.Write(double.MaxValue));
|
||||||
};
|
yield return BinaryWriterAction(w => w.Write(double.PositiveInfinity));
|
||||||
|
yield return BinaryWriterAction(w => w.Write(double.NaN));
|
||||||
|
yield return BinaryWriterAction(w => w.Write(new byte[] { 1, 2, 3, 4, 5, 6, 7 }));
|
||||||
|
yield return BinaryWriterAction(w => w.Write(Enumerable.Range(0, 255).SelectMany(x => Enumerable.Range(0, 255)).Select(x => (byte)x).ToArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Action<Stream> BinaryWriterAction(Action<BinaryWriter> action)
|
private static Action<Stream> BinaryWriterAction(Action<BinaryWriter> action)
|
||||||
@ -68,15 +72,15 @@ namespace MatFileHandler.Tests
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static uint ReferenceCalculation(byte[] bytes)
|
private uint ReferenceCalculation(Action<Stream> action)
|
||||||
{
|
{
|
||||||
using var stream = new MemoryStream();
|
using var stream = new MemoryStream();
|
||||||
stream.Write(bytes, 0, bytes.Length);
|
action(stream);
|
||||||
stream.Position = 0;
|
stream.Position = 0;
|
||||||
return CalculateAdler32Checksum(stream);
|
return CalculateAdler32Checksum(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static uint CalculateAdler32Checksum(MemoryStream stream)
|
private static uint CalculateAdler32Checksum(Stream stream)
|
||||||
{
|
{
|
||||||
uint s1 = 1;
|
uint s1 = 1;
|
||||||
uint s2 = 0;
|
uint s2 = 0;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler.Tests
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Data used in reading/writing tests.
|
/// Data used in reading/writing tests.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using Xunit;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
namespace MatFileHandler.Tests
|
namespace MatFileHandler.Tests
|
||||||
{
|
{
|
||||||
|
43
MatFileHandler.Tests/ExtensionTestFilenameConvention.cs
Executable file
43
MatFileHandler.Tests/ExtensionTestFilenameConvention.cs
Executable file
@ -0,0 +1,43 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A filename convention based on file extensions.
|
||||||
|
/// </summary>
|
||||||
|
internal class ExtensionTestFilenameConvention : ITestFilenameConvention
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="ExtensionTestFilenameConvention"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extension">File extension.</param>
|
||||||
|
public ExtensionTestFilenameConvention(string extension)
|
||||||
|
{
|
||||||
|
Extension = extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string Extension { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert test name to filename by adding the extension.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="testName">Test name.</param>
|
||||||
|
/// <returns>The corresponding filename.</returns>
|
||||||
|
public string ConvertTestNameToFilename(string testName)
|
||||||
|
{
|
||||||
|
return Path.ChangeExtension(testName, Extension);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Compare file's extension to the one specified during initialization.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">Filename.</param>
|
||||||
|
/// <returns>True iff the file has the extension stored in the class.</returns>
|
||||||
|
public bool FilterFile(string filename)
|
||||||
|
{
|
||||||
|
return Path.GetExtension(filename) == "." + Extension;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
MatFileHandler.Tests/ITestFilenameConvention.cs
Executable file
24
MatFileHandler.Tests/ITestFilenameConvention.cs
Executable file
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Interface for handling filtering tests based on filenames.
|
||||||
|
/// </summary>
|
||||||
|
public interface ITestFilenameConvention
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Convert test name to a filename (e.g., by adding an appropriate extension).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="testName">Name of a test.</param>
|
||||||
|
/// <returns>Filename.</returns>
|
||||||
|
string ConvertTestNameToFilename(string testName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decide if a file contains a test based on the filename.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filename">A filename.</param>
|
||||||
|
/// <returns>True iff the file should contain a test.</returns>
|
||||||
|
bool FilterFile(string filename);
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,10 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net461;net472;net8.0</TargetFrameworks>
|
<TargetFrameworks>net461;net472;net8.0</TargetFrameworks>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>10.0</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
<CodeAnalysisRuleSet>..\MatFileHandler.ruleset</CodeAnalysisRuleSet>
|
||||||
<DocumentationFile>bin\Debug\net5.0\MatFileHandler.Tests.xml</DocumentationFile>
|
<DocumentationFile>bin\Debug\net5.0\MatFileHandler.Tests.xml</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
using System.Collections.Generic;
|
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
@ -18,9 +19,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading all files in a given test set.
|
/// Test reading all files in a given test set.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestReader(MatFileReadingMethod method)
|
public void TestReader(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
foreach (var matFile in ReadAllTestFiles(method))
|
foreach (var matFile in testFactory.GetAllTestData())
|
||||||
{
|
{
|
||||||
Assert.NotEmpty(matFile.Variables);
|
Assert.NotEmpty(matFile.Variables);
|
||||||
}
|
}
|
||||||
@ -30,9 +31,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading lower and upper limits of integer data types.
|
/// Test reading lower and upper limits of integer data types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestLimits(MatFileReadingMethod method)
|
public void TestLimits(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("limits", method);
|
var matFile = testFactory["limits"];
|
||||||
IArray array;
|
IArray array;
|
||||||
array = matFile["int8_"].Value;
|
array = matFile["int8_"].Value;
|
||||||
CheckLimits(array as IArrayOf<sbyte>, CommonData.Int8Limits);
|
CheckLimits(array as IArrayOf<sbyte>, CommonData.Int8Limits);
|
||||||
@ -64,9 +65,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test writing lower and upper limits of integer-based complex data types.
|
/// Test writing lower and upper limits of integer-based complex data types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestComplexLimits(MatFileReadingMethod method)
|
public void TestComplexLimits(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("limits_complex", method);
|
var matFile = testFactory["limits_complex"];
|
||||||
IArray array;
|
IArray array;
|
||||||
array = matFile["int8_complex"].Value;
|
array = matFile["int8_complex"].Value;
|
||||||
CheckComplexLimits(array as IArrayOf<ComplexOf<sbyte>>, CommonData.Int8Limits);
|
CheckComplexLimits(array as IArrayOf<ComplexOf<sbyte>>, CommonData.Int8Limits);
|
||||||
@ -100,9 +101,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading an ASCII-encoded string.
|
/// Test reading an ASCII-encoded string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestAscii(MatFileReadingMethod method)
|
public void TestAscii(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("ascii", method);
|
var matFile = testFactory["ascii"];
|
||||||
var arrayAscii = matFile["s"].Value as ICharArray;
|
var arrayAscii = matFile["s"].Value as ICharArray;
|
||||||
Assert.NotNull(arrayAscii);
|
Assert.NotNull(arrayAscii);
|
||||||
Assert.Equal(new[] { 1, 3 }, arrayAscii.Dimensions);
|
Assert.Equal(new[] { 1, 3 }, arrayAscii.Dimensions);
|
||||||
@ -114,9 +115,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a Unicode string.
|
/// Test reading a Unicode string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestUnicode(MatFileReadingMethod method)
|
public void TestUnicode(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("unicode", method);
|
var matFile = testFactory["unicode"];
|
||||||
var arrayUnicode = matFile["s"].Value as ICharArray;
|
var arrayUnicode = matFile["s"].Value as ICharArray;
|
||||||
Assert.NotNull(arrayUnicode);
|
Assert.NotNull(arrayUnicode);
|
||||||
Assert.Equal(new[] { 1, 2 }, arrayUnicode.Dimensions);
|
Assert.Equal(new[] { 1, 2 }, arrayUnicode.Dimensions);
|
||||||
@ -129,9 +130,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a wide Unicode string.
|
/// Test reading a wide Unicode string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestUnicodeWide(MatFileReadingMethod method)
|
public void TestUnicodeWide(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("unicode-wide", method);
|
var matFile = testFactory["unicode-wide"];
|
||||||
var arrayUnicodeWide = matFile["s"].Value as ICharArray;
|
var arrayUnicodeWide = matFile["s"].Value as ICharArray;
|
||||||
Assert.NotNull(arrayUnicodeWide);
|
Assert.NotNull(arrayUnicodeWide);
|
||||||
Assert.Equal(new[] { 1, 2 }, arrayUnicodeWide.Dimensions);
|
Assert.Equal(new[] { 1, 2 }, arrayUnicodeWide.Dimensions);
|
||||||
@ -142,9 +143,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test converting a structure array to a Double array.
|
/// Test converting a structure array to a Double array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestConvertToDoubleArray(MatFileReadingMethod method)
|
public void TestConvertToDoubleArray(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("struct", method);
|
var matFile = testFactory["struct"];
|
||||||
var array = matFile.Variables[0].Value;
|
var array = matFile.Variables[0].Value;
|
||||||
Assert.Null(array.ConvertToDoubleArray());
|
Assert.Null(array.ConvertToDoubleArray());
|
||||||
}
|
}
|
||||||
@ -154,9 +155,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>Should return null.</returns>
|
/// <returns>Should return null.</returns>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestConvertToComplexArray(MatFileReadingMethod method)
|
public void TestConvertToComplexArray(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("struct", method);
|
var matFile = testFactory["struct"];
|
||||||
var array = matFile.Variables[0].Value;
|
var array = matFile.Variables[0].Value;
|
||||||
Assert.Null(array.ConvertToComplexArray());
|
Assert.Null(array.ConvertToComplexArray());
|
||||||
}
|
}
|
||||||
@ -165,9 +166,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading an enumeration.
|
/// Test reading an enumeration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestEnum(MatFileReadingMethod method)
|
public void TestEnum(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("enum", method);
|
var matFile = testFactory["enum"];
|
||||||
var days = matFile["days"].Value;
|
var days = matFile["days"].Value;
|
||||||
var enumeration = new EnumAdapter(days);
|
var enumeration = new EnumAdapter(days);
|
||||||
Assert.Equal(5, enumeration.Values.Count);
|
Assert.Equal(5, enumeration.Values.Count);
|
||||||
@ -182,9 +183,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a structure array.
|
/// Test reading a structure array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestStruct(MatFileReadingMethod method)
|
public void TestStruct(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("struct", method);
|
var matFile = testFactory["struct"];
|
||||||
var structure = matFile["struct_"].Value as IStructureArray;
|
var structure = matFile["struct_"].Value as IStructureArray;
|
||||||
Assert.NotNull(structure);
|
Assert.NotNull(structure);
|
||||||
Assert.Equal(new[] { "x", "y" }, structure.FieldNames);
|
Assert.Equal(new[] { "x", "y" }, structure.FieldNames);
|
||||||
@ -236,9 +237,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a sparse array.
|
/// Test reading a sparse array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestSparse(MatFileReadingMethod method)
|
public void TestSparse(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("sparse", method);
|
var matFile = testFactory["sparse"];
|
||||||
var sparseArray = matFile["sparse_"].Value as ISparseArrayOf<double>;
|
var sparseArray = matFile["sparse_"].Value as ISparseArrayOf<double>;
|
||||||
Assert.NotNull(sparseArray);
|
Assert.NotNull(sparseArray);
|
||||||
Assert.Equal(new[] { 4, 5 }, sparseArray.Dimensions);
|
Assert.Equal(new[] { 4, 5 }, sparseArray.Dimensions);
|
||||||
@ -266,9 +267,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a logical array.
|
/// Test reading a logical array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestLogical(MatFileReadingMethod method)
|
public void TestLogical(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("logical", method);
|
var matFile = testFactory["logical"];
|
||||||
var array = matFile["logical_"].Value;
|
var array = matFile["logical_"].Value;
|
||||||
var logicalArray = array as IArrayOf<bool>;
|
var logicalArray = array as IArrayOf<bool>;
|
||||||
Assert.NotNull(logicalArray);
|
Assert.NotNull(logicalArray);
|
||||||
@ -284,9 +285,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a sparse logical array.
|
/// Test reading a sparse logical array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestSparseLogical(MatFileReadingMethod method)
|
public void TestSparseLogical(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("sparse_logical", method);
|
var matFile = testFactory["sparse_logical"];
|
||||||
var array = matFile["sparse_logical"].Value;
|
var array = matFile["sparse_logical"].Value;
|
||||||
var sparseArray = array as ISparseArrayOf<bool>;
|
var sparseArray = array as ISparseArrayOf<bool>;
|
||||||
Assert.NotNull (sparseArray);
|
Assert.NotNull (sparseArray);
|
||||||
@ -303,9 +304,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a global variable.
|
/// Test reading a global variable.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestGlobal(MatFileReadingMethod method)
|
public void TestGlobal(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("global", method);
|
var matFile = testFactory["global"];
|
||||||
var variable = matFile.Variables.First();
|
var variable = matFile.Variables.First();
|
||||||
Assert.True(variable.IsGlobal);
|
Assert.True(variable.IsGlobal);
|
||||||
}
|
}
|
||||||
@ -314,9 +315,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a sparse complex array.
|
/// Test reading a sparse complex array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TextSparseComplex(MatFileReadingMethod method)
|
public void TextSparseComplex(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("sparse_complex", method);
|
var matFile = testFactory["sparse_complex"];
|
||||||
var array = matFile["sparse_complex"].Value;
|
var array = matFile["sparse_complex"].Value;
|
||||||
var sparseArray = array as ISparseArrayOf<Complex>;
|
var sparseArray = array as ISparseArrayOf<Complex>;
|
||||||
Assert.NotNull(sparseArray);
|
Assert.NotNull(sparseArray);
|
||||||
@ -330,9 +331,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading an object.
|
/// Test reading an object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestObject(MatFileReadingMethod method)
|
public void TestObject(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("object", method);
|
var matFile = testFactory["object"];
|
||||||
var obj = matFile["object_"].Value as IMatObject;
|
var obj = matFile["object_"].Value as IMatObject;
|
||||||
Assert.NotNull(obj);
|
Assert.NotNull(obj);
|
||||||
Assert.Equal("Point", obj.ClassName);
|
Assert.Equal("Point", obj.ClassName);
|
||||||
@ -347,9 +348,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading another object.
|
/// Test reading another object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestObject2(MatFileReadingMethod method)
|
public void TestObject2(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("object2", method);
|
var matFile = testFactory["object2"];
|
||||||
var obj = matFile["object2"].Value as IMatObject;
|
var obj = matFile["object2"].Value as IMatObject;
|
||||||
Assert.NotNull(obj);
|
Assert.NotNull(obj);
|
||||||
Assert.Equal("Point", obj.ClassName);
|
Assert.Equal("Point", obj.ClassName);
|
||||||
@ -370,9 +371,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test reading a table.
|
/// Test reading a table.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestTable(MatFileReadingMethod method)
|
public void TestTable(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("table", method);
|
var matFile = testFactory["table"];
|
||||||
var obj = matFile["table_"].Value as IMatObject;
|
var obj = matFile["table_"].Value as IMatObject;
|
||||||
var table = new TableAdapter(obj);
|
var table = new TableAdapter(obj);
|
||||||
Assert.Equal(3, table.NumberOfRows);
|
Assert.Equal(3, table.NumberOfRows);
|
||||||
@ -387,37 +388,13 @@ namespace MatFileHandler.Tests
|
|||||||
Assert.Equal(new[] { 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 }, variable2.ConvertToDoubleArray());
|
Assert.Equal(new[] { 1.0, 3.0, 5.0, 2.0, 4.0, 6.0 }, variable2.ConvertToDoubleArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Test reading a deeply nested table.
|
|
||||||
/// </summary>
|
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
|
||||||
public void TestDeepTable(MatFileReadingMethod method)
|
|
||||||
{
|
|
||||||
var matFile = ReadTestFile("table-deep", method);
|
|
||||||
var obj = matFile["t"].Value as IMatObject;
|
|
||||||
var table = new TableAdapter(obj);
|
|
||||||
Assert.Equal(1, table.NumberOfRows);
|
|
||||||
Assert.Equal(2, table.NumberOfVariables);
|
|
||||||
Assert.Equal(new[] { "s", "another" }, table.VariableNames);
|
|
||||||
var s = table["s"] as IStructureArray;
|
|
||||||
Assert.Equal(new[] { "a", "b", "c" }, s.FieldNames);
|
|
||||||
var c = s["c", 0];
|
|
||||||
var internalTable = new TableAdapter(c);
|
|
||||||
Assert.Equal(2, internalTable.NumberOfRows);
|
|
||||||
Assert.Equal(2, internalTable.NumberOfVariables);
|
|
||||||
Assert.Equal(new[] { "x", "y" }, internalTable.VariableNames);
|
|
||||||
var y = new StringAdapter(internalTable["y"]);
|
|
||||||
Assert.Equal("3", y[0]);
|
|
||||||
Assert.Equal("abc", y[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test reading a table with strings
|
/// Test reading a table with strings
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestTableWithStrings(MatFileReadingMethod method)
|
public void TestTableWithStrings(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("table-with-strings", method);
|
var matFile = testFactory["table-with-strings"];
|
||||||
var obj = matFile["t"].Value as IMatObject;
|
var obj = matFile["t"].Value as IMatObject;
|
||||||
var table = new TableAdapter(obj);
|
var table = new TableAdapter(obj);
|
||||||
Assert.Equal(5, table.NumberOfRows);
|
Assert.Equal(5, table.NumberOfRows);
|
||||||
@ -440,9 +417,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test subobjects within objects.
|
/// Test subobjects within objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestSubobjects(MatFileReadingMethod method)
|
public void TestSubobjects(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("pointWithSubpoints", method);
|
var matFile = testFactory["pointWithSubpoints"];
|
||||||
var p = matFile["p"].Value as IMatObject;
|
var p = matFile["p"].Value as IMatObject;
|
||||||
Assert.Equal("Point", p.ClassName);
|
Assert.Equal("Point", p.ClassName);
|
||||||
var x = p["x"] as IMatObject;
|
var x = p["x"] as IMatObject;
|
||||||
@ -463,9 +440,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test nested objects.
|
/// Test nested objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestNestedObjects(MatFileReadingMethod method)
|
public void TestNestedObjects(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("subsubPoint", method);
|
var matFile = testFactory["subsubPoint"];
|
||||||
var p = matFile["p"].Value as IMatObject;
|
var p = matFile["p"].Value as IMatObject;
|
||||||
Assert.Equal("Point", p.ClassName);
|
Assert.Equal("Point", p.ClassName);
|
||||||
Assert.Equal(new[] { 1.0 }, p["x"].ConvertToDoubleArray());
|
Assert.Equal(new[] { 1.0 }, p["x"].ConvertToDoubleArray());
|
||||||
@ -481,9 +458,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test datetime objects.
|
/// Test datetime objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestDatetime(MatFileReadingMethod method)
|
public void TestDatetime(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("datetime", method);
|
var matFile = testFactory["datetime"];
|
||||||
var d = matFile["d"].Value as IMatObject;
|
var d = matFile["d"].Value as IMatObject;
|
||||||
var datetime = new DatetimeAdapter(d);
|
var datetime = new DatetimeAdapter(d);
|
||||||
Assert.Equal(new[] { 1, 2 }, datetime.Dimensions);
|
Assert.Equal(new[] { 1, 2 }, datetime.Dimensions);
|
||||||
@ -495,9 +472,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Another test for datetime objects.
|
/// Another test for datetime objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestDatetime2(MatFileReadingMethod method)
|
public void TestDatetime2(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("datetime2", method);
|
var matFile = testFactory["datetime2"];
|
||||||
var d = matFile["d"].Value as IMatObject;
|
var d = matFile["d"].Value as IMatObject;
|
||||||
var datetime = new DatetimeAdapter(d);
|
var datetime = new DatetimeAdapter(d);
|
||||||
Assert.Equal(new[] { 1, 1 }, datetime.Dimensions);
|
Assert.Equal(new[] { 1, 1 }, datetime.Dimensions);
|
||||||
@ -510,9 +487,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test string objects.
|
/// Test string objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestString(MatFileReadingMethod method)
|
public void TestString(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("string", method);
|
var matFile = testFactory["string"];
|
||||||
var s = matFile["s"].Value as IMatObject;
|
var s = matFile["s"].Value as IMatObject;
|
||||||
var str = new StringAdapter(s);
|
var str = new StringAdapter(s);
|
||||||
Assert.Equal(new[] { 4, 1 }, str.Dimensions);
|
Assert.Equal(new[] { 4, 1 }, str.Dimensions);
|
||||||
@ -526,9 +503,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test duration objects.
|
/// Test duration objects.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestDuration(MatFileReadingMethod method)
|
public void TestDuration(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("duration", method);
|
var matFile = testFactory["duration"];
|
||||||
var d = matFile["d"].Value as IMatObject;
|
var d = matFile["d"].Value as IMatObject;
|
||||||
var duration = new DurationAdapter(d);
|
var duration = new DurationAdapter(d);
|
||||||
Assert.Equal(new[] { 1, 3 }, duration.Dimensions);
|
Assert.Equal(new[] { 1, 3 }, duration.Dimensions);
|
||||||
@ -541,9 +518,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test unrepresentable datetime.
|
/// Test unrepresentable datetime.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void TestDatetime_Unrepresentable(MatFileReadingMethod method)
|
public void TestDatetime_Unrepresentable(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("datetime-unrepresentable", method);
|
var matFile = testFactory["datetime-unrepresentable"];
|
||||||
var obj = matFile["d"].Value as IMatObject;
|
var obj = matFile["d"].Value as IMatObject;
|
||||||
var datetime = new DatetimeAdapter(obj);
|
var datetime = new DatetimeAdapter(obj);
|
||||||
var d0 = datetime[0];
|
var d0 = datetime[0];
|
||||||
@ -554,9 +531,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test 3-dimensional arrays.
|
/// Test 3-dimensional arrays.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void Test_3DArrays(MatFileReadingMethod method)
|
public void Test_3DArrays(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("issue20", method);
|
var matFile = testFactory["issue20.mat"];
|
||||||
var obj = matFile["a3d"].Value;
|
var obj = matFile["a3d"].Value;
|
||||||
var values = obj.ConvertToDoubleArray();
|
var values = obj.ConvertToDoubleArray();
|
||||||
Assert.Equal(Enumerable.Range(1, 24).Select(x => (double)x).ToArray(), values);
|
Assert.Equal(Enumerable.Range(1, 24).Select(x => (double)x).ToArray(), values);
|
||||||
@ -589,9 +566,9 @@ namespace MatFileHandler.Tests
|
|||||||
/// Test four-dimensional arrays.
|
/// Test four-dimensional arrays.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(TestDataFactories))]
|
[Theory, MemberData(nameof(TestDataFactories))]
|
||||||
public void Test4DArrays(MatFileReadingMethod method)
|
public void Test_4DArrays(AbstractTestDataFactory<IMatFile> testFactory)
|
||||||
{
|
{
|
||||||
var matFile = ReadTestFile("issue20", method);
|
var matFile = testFactory["issue20.mat"];
|
||||||
var obj = matFile["a4d"].Value;
|
var obj = matFile["a4d"].Value;
|
||||||
Assert.Equal(Enumerable.Range(1, 120).Select(x => (double)x).ToArray(), obj.ConvertToDoubleArray());
|
Assert.Equal(Enumerable.Range(1, 120).Select(x => (double)x).ToArray(), obj.ConvertToDoubleArray());
|
||||||
Assert.Null(obj.ConvertTo2dDoubleArray());
|
Assert.Null(obj.ConvertTo2dDoubleArray());
|
||||||
@ -600,36 +577,19 @@ namespace MatFileHandler.Tests
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the factories that provide test data in various configurations.
|
/// Returns the factories that provide test data in various configurations.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static TheoryData<MatFileReadingMethod> TestDataFactories
|
public static TheoryData<AbstractTestDataFactory<IMatFile>> TestDataFactories
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return new TheoryData<MatFileReadingMethod>
|
return new TheoryData<AbstractTestDataFactory<IMatFile>>
|
||||||
{
|
{
|
||||||
MatFileReadingMethod.NormalStream,
|
new MatTestDataFactory(Path.Combine(TestDirectory, "good")),
|
||||||
MatFileReadingMethod.PartialStream,
|
new PartialReadMatTestDataFactory(Path.Combine(TestDirectory, "good")),
|
||||||
MatFileReadingMethod.UnalignedStream,
|
new UnalignedMatTestDataFactory(Path.Combine(TestDirectory, "good")),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IMatFile ReadTestFile(string fileName, MatFileReadingMethod method)
|
|
||||||
{
|
|
||||||
var fullFileName = Path.Combine("test-data", "good", $"{fileName}.mat");
|
|
||||||
return MatFileReadingMethods.ReadMatFile(method, fullFileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<IMatFile> ReadAllTestFiles(MatFileReadingMethod method)
|
|
||||||
{
|
|
||||||
foreach (var fileName in Directory.EnumerateFiles(
|
|
||||||
Path.Combine("test-data", "good"),
|
|
||||||
"*.mat"))
|
|
||||||
{
|
|
||||||
var fullFileName = fileName;
|
|
||||||
yield return MatFileReadingMethods.ReadMatFile(method, fullFileName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void CheckLimits<T>(IArrayOf<T> array, T[] limits)
|
private static void CheckLimits<T>(IArrayOf<T> array, T[] limits)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
namespace MatFileHandler.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Method of reading .mat files for testing.
|
|
||||||
/// </summary>
|
|
||||||
public enum MatFileReadingMethod
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Undefined.
|
|
||||||
/// </summary>
|
|
||||||
Undefined = 0,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Normal stream (like memory or file stream).
|
|
||||||
/// </summary>
|
|
||||||
NormalStream,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Partial stream (only is capable of reading one byte at a time).
|
|
||||||
/// </summary>
|
|
||||||
PartialStream,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Unaligned stream (what happens if the data don't start at the beginning?).
|
|
||||||
/// </summary>
|
|
||||||
UnalignedStream,
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace MatFileHandler.Tests;
|
|
||||||
|
|
||||||
internal static class MatFileReadingMethods
|
|
||||||
{
|
|
||||||
public static IMatFile ReadMatFile(MatFileReadingMethod method, string fullFileName)
|
|
||||||
{
|
|
||||||
using var stream = File.OpenRead(fullFileName);
|
|
||||||
switch (method)
|
|
||||||
{
|
|
||||||
case MatFileReadingMethod.NormalStream:
|
|
||||||
return ReadFromStream(stream);
|
|
||||||
case MatFileReadingMethod.PartialStream:
|
|
||||||
{
|
|
||||||
using var wrapper = new PartialUnseekableReadStream(stream);
|
|
||||||
return ReadFromStream(wrapper);
|
|
||||||
}
|
|
||||||
case MatFileReadingMethod.UnalignedStream:
|
|
||||||
{
|
|
||||||
using var ms = new MemoryStream();
|
|
||||||
ms.Seek(3, SeekOrigin.Begin);
|
|
||||||
stream.CopyTo(ms);
|
|
||||||
ms.Seek(3, SeekOrigin.Begin);
|
|
||||||
return ReadFromStream(ms);
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IMatFile ReadFromStream(Stream stream)
|
|
||||||
{
|
|
||||||
var reader = new MatFileReader(stream);
|
|
||||||
return reader.Read();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
namespace MatFileHandler.Tests;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Options to give to MatFileWriter constructor for testing it.
|
|
||||||
/// </summary>
|
|
||||||
public enum MatFileWriterOptionsForTests
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Undefined.
|
|
||||||
/// </summary>
|
|
||||||
Undefined = 0,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// No options.
|
|
||||||
/// </summary>
|
|
||||||
None,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Option to always use compression.
|
|
||||||
/// </summary>
|
|
||||||
Always,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Option to never use compression.
|
|
||||||
/// </summary>
|
|
||||||
Never,
|
|
||||||
}
|
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
@ -15,8 +17,8 @@ namespace MatFileHandler.Tests
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing a simple Double array.
|
/// Test writing a simple Double array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestWrite(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestWrite(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var array = builder.NewArray<double>(1, 2);
|
var array = builder.NewArray<double>(1, 2);
|
||||||
@ -24,7 +26,7 @@ namespace MatFileHandler.Tests
|
|||||||
array[1] = 17.0;
|
array[1] = 17.0;
|
||||||
var variable = builder.NewVariable("test", array);
|
var variable = builder.NewVariable("test", array);
|
||||||
var actual = builder.NewFile(new[] { variable });
|
var actual = builder.NewFile(new[] { variable });
|
||||||
MatCompareWithTestData("good", "double-array", actual, method, options);
|
MatCompareWithTestData("good", "double-array", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -49,8 +51,8 @@ namespace MatFileHandler.Tests
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing lower and upper limits of integer data types.
|
/// Test writing lower and upper limits of integer data types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestLimits(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestLimits(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var int8 = builder.NewVariable("int8_", builder.NewArray(CommonData.Int8Limits, 1, 2));
|
var int8 = builder.NewVariable("int8_", builder.NewArray(CommonData.Int8Limits, 1, 2));
|
||||||
@ -62,14 +64,14 @@ namespace MatFileHandler.Tests
|
|||||||
var int64 = builder.NewVariable("int64_", builder.NewArray(CommonData.Int64Limits, 1, 2));
|
var int64 = builder.NewVariable("int64_", builder.NewArray(CommonData.Int64Limits, 1, 2));
|
||||||
var uint64 = builder.NewVariable("uint64_", builder.NewArray(CommonData.UInt64Limits, 1, 2));
|
var uint64 = builder.NewVariable("uint64_", builder.NewArray(CommonData.UInt64Limits, 1, 2));
|
||||||
var actual = builder.NewFile(new[] { int16, int32, int64, int8, uint16, uint32, uint64, uint8 });
|
var actual = builder.NewFile(new[] { int16, int32, int64, int8, uint16, uint32, uint64, uint8 });
|
||||||
MatCompareWithTestData("good", "limits", actual, method, options);
|
MatCompareWithTestData("good", "limits", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing lower and upper limits of integer-based complex data types.
|
/// Test writing lower and upper limits of integer-based complex data types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestLimitsComplex(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestLimitsComplex(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var int8Complex = builder.NewVariable(
|
var int8Complex = builder.NewVariable(
|
||||||
@ -101,26 +103,26 @@ namespace MatFileHandler.Tests
|
|||||||
int16Complex, int32Complex, int64Complex, int8Complex,
|
int16Complex, int32Complex, int64Complex, int8Complex,
|
||||||
uint16Complex, uint32Complex, uint64Complex, uint8Complex,
|
uint16Complex, uint32Complex, uint64Complex, uint8Complex,
|
||||||
});
|
});
|
||||||
MatCompareWithTestData("good", "limits_complex", actual, method, options);
|
MatCompareWithTestData("good", "limits_complex", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing a wide-Unicode symbol.
|
/// Test writing a wide-Unicode symbol.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestUnicodeWide(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestUnicodeWide(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var s = builder.NewVariable("s", builder.NewCharArray("🍆"));
|
var s = builder.NewVariable("s", builder.NewCharArray("🍆"));
|
||||||
var actual = builder.NewFile(new[] { s });
|
var actual = builder.NewFile(new[] { s });
|
||||||
MatCompareWithTestData("good", "unicode-wide", actual, method, options);
|
MatCompareWithTestData("good", "unicode-wide", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing a sparse array.
|
/// Test writing a sparse array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestSparseArray(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestSparseArray(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var sparseArray = builder.NewSparseArray<double>(4, 5);
|
var sparseArray = builder.NewSparseArray<double>(4, 5);
|
||||||
@ -130,14 +132,14 @@ namespace MatFileHandler.Tests
|
|||||||
sparseArray[2, 3] = 4;
|
sparseArray[2, 3] = 4;
|
||||||
var sparse = builder.NewVariable("sparse_", sparseArray);
|
var sparse = builder.NewVariable("sparse_", sparseArray);
|
||||||
var actual = builder.NewFile(new[] { sparse });
|
var actual = builder.NewFile(new[] { sparse });
|
||||||
MatCompareWithTestData("good", "sparse", actual, method, options);
|
MatCompareWithTestData("good", "sparse", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing a structure array.
|
/// Test writing a structure array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestStructure(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestStructure(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var structure = builder.NewStructureArray(new[] { "x", "y" }, 2, 3);
|
var structure = builder.NewStructureArray(new[] { "x", "y" }, 2, 3);
|
||||||
@ -158,27 +160,27 @@ namespace MatFileHandler.Tests
|
|||||||
structure["y", 1, 2] = builder.NewEmpty();
|
structure["y", 1, 2] = builder.NewEmpty();
|
||||||
var struct_ = builder.NewVariable("struct_", structure);
|
var struct_ = builder.NewVariable("struct_", structure);
|
||||||
var actual = builder.NewFile(new[] { struct_ });
|
var actual = builder.NewFile(new[] { struct_ });
|
||||||
MatCompareWithTestData("good", "struct", actual, method, options);
|
MatCompareWithTestData("good", "struct", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing a logical array.
|
/// Test writing a logical array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestLogical(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestLogical(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var logical = builder.NewArray(new[] { true, false, true, true, false, true }, 2, 3);
|
var logical = builder.NewArray(new[] { true, false, true, true, false, true }, 2, 3);
|
||||||
var logicalVariable = builder.NewVariable("logical_", logical);
|
var logicalVariable = builder.NewVariable("logical_", logical);
|
||||||
var actual = builder.NewFile(new[] { logicalVariable });
|
var actual = builder.NewFile(new[] { logicalVariable });
|
||||||
MatCompareWithTestData("good", "logical", actual, method, options);
|
MatCompareWithTestData("good", "logical", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing a sparse logical array.
|
/// Test writing a sparse logical array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestSparseLogical(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestSparseLogical(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var array = builder.NewSparseArray<bool>(2, 3);
|
var array = builder.NewSparseArray<bool>(2, 3);
|
||||||
@ -188,14 +190,14 @@ namespace MatFileHandler.Tests
|
|||||||
array[1, 2] = true;
|
array[1, 2] = true;
|
||||||
var sparseLogical = builder.NewVariable("sparse_logical", array);
|
var sparseLogical = builder.NewVariable("sparse_logical", array);
|
||||||
var actual = builder.NewFile(new[] { sparseLogical });
|
var actual = builder.NewFile(new[] { sparseLogical });
|
||||||
MatCompareWithTestData("good", "sparse_logical", actual, method, options);
|
MatCompareWithTestData("good", "sparse_logical", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing a sparse complex array.
|
/// Test writing a sparse complex array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestSparseComplex(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestSparseComplex(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var array = builder.NewSparseArray<Complex>(2, 2);
|
var array = builder.NewSparseArray<Complex>(2, 2);
|
||||||
@ -204,48 +206,48 @@ namespace MatFileHandler.Tests
|
|||||||
array[1, 1] = 0.5 + Complex.ImaginaryOne;
|
array[1, 1] = 0.5 + Complex.ImaginaryOne;
|
||||||
var sparseComplex = builder.NewVariable("sparse_complex", array);
|
var sparseComplex = builder.NewVariable("sparse_complex", array);
|
||||||
var actual = builder.NewFile(new[] { sparseComplex });
|
var actual = builder.NewFile(new[] { sparseComplex });
|
||||||
MatCompareWithTestData("good", "sparse_complex", actual, method, options);
|
MatCompareWithTestData("good", "sparse_complex", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test writing a global variable.
|
/// Test writing a global variable.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Theory, MemberData(nameof(MatFileWritingTestData))]
|
[Theory, MemberData(nameof(MatFileWritingMethods))]
|
||||||
public void TestGlobal(MatFileWritingMethod method, MatFileWriterOptionsForTests options)
|
public void TestGlobal(MatFileWritingMethod method)
|
||||||
{
|
{
|
||||||
var builder = new DataBuilder();
|
var builder = new DataBuilder();
|
||||||
var array = builder.NewArray(new double[] { 1, 3, 5 }, 1, 3);
|
var array = builder.NewArray(new double[] { 1, 3, 5 }, 1, 3);
|
||||||
var global = builder.NewVariable("global_", array, true);
|
var global = builder.NewVariable("global_", array, true);
|
||||||
var actual = builder.NewFile(new[] { global });
|
var actual = builder.NewFile(new[] { global });
|
||||||
MatCompareWithTestData("good", "global", actual, method, options);
|
MatCompareWithTestData("good", "global", actual, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Various writing methods for testing writing of .mat files.
|
/// Various writing methods for testing writing of .mat files.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static TheoryData<MatFileWritingMethod, MatFileWriterOptionsForTests> MatFileWritingTestData
|
public static TheoryData<MatFileWritingMethod> MatFileWritingMethods
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
var always = new MatFileWriterOptions { UseCompression = CompressionUsage.Always};
|
return new TheoryData<MatFileWritingMethod>
|
||||||
var never = new MatFileWriterOptions { UseCompression = CompressionUsage.Never };
|
|
||||||
var data = new TheoryData<MatFileWritingMethod, MatFileWriterOptionsForTests>
|
|
||||||
{
|
{
|
||||||
{ MatFileWritingMethod.NormalStream, MatFileWriterOptionsForTests.None },
|
new MatFileWritingToMemoryStream(null),
|
||||||
{ MatFileWritingMethod.NormalStream, MatFileWriterOptionsForTests.Always },
|
new MatFileWritingToMemoryStream(new MatFileWriterOptions { UseCompression = CompressionUsage.Always }),
|
||||||
{ MatFileWritingMethod.NormalStream, MatFileWriterOptionsForTests.Never },
|
new MatFileWritingToMemoryStream(new MatFileWriterOptions { UseCompression = CompressionUsage.Never }),
|
||||||
{ MatFileWritingMethod.UnseekableStream, MatFileWriterOptionsForTests.None },
|
new MatFileWritingToUnseekableStream(null),
|
||||||
{ MatFileWritingMethod.UnseekableStream, MatFileWriterOptionsForTests.Always },
|
new MatFileWritingToUnseekableStream(new MatFileWriterOptions { UseCompression = CompressionUsage.Always }),
|
||||||
{ MatFileWritingMethod.UnseekableStream, MatFileWriterOptionsForTests.Never },
|
new MatFileWritingToUnseekableStream(new MatFileWriterOptions { UseCompression = CompressionUsage.Never }),
|
||||||
{ MatFileWritingMethod.UnalignedStream, MatFileWriterOptionsForTests.None },
|
new MatFileWritingToUnalignedMemoryStream(null),
|
||||||
{ MatFileWritingMethod.UnalignedStream, MatFileWriterOptionsForTests.Always },
|
new MatFileWritingToUnalignedMemoryStream(new MatFileWriterOptions { UseCompression = CompressionUsage.Always }),
|
||||||
{ MatFileWritingMethod.UnalignedStream, MatFileWriterOptionsForTests.Never },
|
new MatFileWritingToUnalignedMemoryStream(new MatFileWriterOptions { UseCompression = CompressionUsage.Never }),
|
||||||
};
|
};
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CompareSparseArrays<T>(ISparseArrayOf<T> expected, ISparseArrayOf<T> actual)
|
private static AbstractTestDataFactory<IMatFile> GetMatTestData(string factoryName) =>
|
||||||
|
new MatTestDataFactory(Path.Combine(TestDirectory, factoryName));
|
||||||
|
|
||||||
|
private void CompareSparseArrays<T>(ISparseArrayOf<T> expected, ISparseArrayOf<T> actual)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
Assert.NotNull(actual);
|
Assert.NotNull(actual);
|
||||||
@ -253,7 +255,7 @@ namespace MatFileHandler.Tests
|
|||||||
Assert.Equal(expected.Data, actual.Data);
|
Assert.Equal(expected.Data, actual.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CompareStructureArrays(IStructureArray expected, IStructureArray actual)
|
private void CompareStructureArrays(IStructureArray expected, IStructureArray actual)
|
||||||
{
|
{
|
||||||
Assert.NotNull(actual);
|
Assert.NotNull(actual);
|
||||||
Assert.Equal(expected.Dimensions, actual.Dimensions);
|
Assert.Equal(expected.Dimensions, actual.Dimensions);
|
||||||
@ -267,7 +269,7 @@ namespace MatFileHandler.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CompareCellArrays(ICellArray expected, ICellArray actual)
|
private void CompareCellArrays(ICellArray expected, ICellArray actual)
|
||||||
{
|
{
|
||||||
Assert.NotNull(actual);
|
Assert.NotNull(actual);
|
||||||
Assert.Equal(expected.Dimensions, actual.Dimensions);
|
Assert.Equal(expected.Dimensions, actual.Dimensions);
|
||||||
@ -277,21 +279,21 @@ namespace MatFileHandler.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CompareNumericalArrays<T>(IArrayOf<T> expected, IArrayOf<T> actual)
|
private void CompareNumericalArrays<T>(IArrayOf<T> expected, IArrayOf<T> actual)
|
||||||
{
|
{
|
||||||
Assert.NotNull(actual);
|
Assert.NotNull(actual);
|
||||||
Assert.Equal(expected.Dimensions, actual.Dimensions);
|
Assert.Equal(expected.Dimensions, actual.Dimensions);
|
||||||
Assert.Equal(expected.Data, actual.Data);
|
Assert.Equal(expected.Data, actual.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CompareCharArrays(ICharArray expected, ICharArray actual)
|
private void CompareCharArrays(ICharArray expected, ICharArray actual)
|
||||||
{
|
{
|
||||||
Assert.NotNull(actual);
|
Assert.NotNull(actual);
|
||||||
Assert.Equal(expected.Dimensions, actual.Dimensions);
|
Assert.Equal(expected.Dimensions, actual.Dimensions);
|
||||||
Assert.Equal(expected.String, actual.String);
|
Assert.Equal(expected.String, actual.String);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CompareMatArrays(IArray expected, IArray actual)
|
private void CompareMatArrays(IArray expected, IArray actual)
|
||||||
{
|
{
|
||||||
switch (expected)
|
switch (expected)
|
||||||
{
|
{
|
||||||
@ -382,7 +384,7 @@ namespace MatFileHandler.Tests
|
|||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CompareMatFiles(IMatFile expected, IMatFile actual)
|
private void CompareMatFiles(IMatFile expected, IMatFile actual)
|
||||||
{
|
{
|
||||||
Assert.Equal(expected.Variables.Length, actual.Variables.Length);
|
Assert.Equal(expected.Variables.Length, actual.Variables.Length);
|
||||||
for (var i = 0; i < expected.Variables.Length; i++)
|
for (var i = 0; i < expected.Variables.Length; i++)
|
||||||
@ -395,25 +397,21 @@ namespace MatFileHandler.Tests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void MatCompareWithTestData(
|
private void MatCompareWithTestData(
|
||||||
string factoryName,
|
string factoryName,
|
||||||
string testName,
|
string testName,
|
||||||
IMatFile actual,
|
IMatFile actual,
|
||||||
MatFileWritingMethod method,
|
MatFileWritingMethod method)
|
||||||
MatFileWriterOptionsForTests options)
|
|
||||||
{
|
{
|
||||||
var fullFileName = Path.Combine("test-data", "good", $"{testName}.mat");
|
var expected = GetMatTestData(factoryName)[testName];
|
||||||
var expected = MatFileReadingMethods.ReadMatFile(
|
var buffer = method.WriteMatFile(actual);
|
||||||
MatFileReadingMethod.NormalStream,
|
|
||||||
fullFileName);
|
|
||||||
var buffer = MatFileWritingMethods.WriteMatFile(method, options, actual);
|
|
||||||
using var stream = new MemoryStream(buffer);
|
using var stream = new MemoryStream(buffer);
|
||||||
var reader = new MatFileReader(stream);
|
var reader = new MatFileReader(stream);
|
||||||
var actualRead = reader.Read();
|
var actualRead = reader.Read();
|
||||||
CompareMatFiles(expected, actualRead);
|
CompareMatFiles(expected, actualRead);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ComplexOf<T>[] CreateComplexLimits<T>(T[] limits)
|
private ComplexOf<T>[] CreateComplexLimits<T>(T[] limits)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
return new[] { new ComplexOf<T>(limits[0], limits[1]), new ComplexOf<T>(limits[1], limits[0]) };
|
return new[] { new ComplexOf<T>(limits[0], limits[1]), new ComplexOf<T>(limits[1], limits[0]) };
|
||||||
|
@ -1,27 +1,17 @@
|
|||||||
namespace MatFileHandler.Tests;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
/// <summary>
|
namespace MatFileHandler.Tests
|
||||||
/// Method of writing .mat files for testing.
|
|
||||||
/// </summary>
|
|
||||||
public enum MatFileWritingMethod
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Undefined.
|
/// A method of writing IMatFile into a byte buffer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Undefined = 0,
|
public abstract class MatFileWritingMethod
|
||||||
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Normal stream (like memory or file stream).
|
/// Write an IMatFile into a byte buffer.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
NormalStream,
|
/// <param name="matFile"></param>
|
||||||
|
/// <returns></returns>
|
||||||
/// <summary>
|
public abstract byte[] WriteMatFile(IMatFile matFile);
|
||||||
/// A stream that cannot be seeked (like a deflate stream).
|
}
|
||||||
/// </summary>
|
|
||||||
UnseekableStream,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Unaligned stream (what happens if the data don't start at the beginning?).
|
|
||||||
/// </summary>
|
|
||||||
UnalignedStream,
|
|
||||||
}
|
}
|
@ -1,58 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace MatFileHandler.Tests;
|
|
||||||
|
|
||||||
internal static class MatFileWritingMethods
|
|
||||||
{
|
|
||||||
public static byte[] WriteMatFile(MatFileWritingMethod method, MatFileWriterOptionsForTests options, IMatFile matFile)
|
|
||||||
{
|
|
||||||
switch (method)
|
|
||||||
{
|
|
||||||
case MatFileWritingMethod.NormalStream:
|
|
||||||
{
|
|
||||||
using var memoryStream = new MemoryStream();
|
|
||||||
var matFileWriter = CreateWriter(options, memoryStream);
|
|
||||||
matFileWriter.Write(matFile);
|
|
||||||
return memoryStream.ToArray();
|
|
||||||
}
|
|
||||||
case MatFileWritingMethod.UnseekableStream:
|
|
||||||
{
|
|
||||||
using var memoryStream = new MemoryStream();
|
|
||||||
using var unseekableStream = new UnseekableWriteStream(memoryStream);
|
|
||||||
var matFileWriter = CreateWriter(options, unseekableStream);
|
|
||||||
matFileWriter.Write(matFile);
|
|
||||||
return memoryStream.ToArray();
|
|
||||||
}
|
|
||||||
case MatFileWritingMethod.UnalignedStream:
|
|
||||||
{
|
|
||||||
using var memoryStream = new MemoryStream();
|
|
||||||
memoryStream.Seek(3, SeekOrigin.Begin);
|
|
||||||
var matFileWriter = CreateWriter(options, memoryStream);
|
|
||||||
matFileWriter.Write(matFile);
|
|
||||||
var fullArray = memoryStream.ToArray();
|
|
||||||
var length = fullArray.Length - 3;
|
|
||||||
var result = new byte[length];
|
|
||||||
Buffer.BlockCopy(fullArray, 3, result, 0, length);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static MatFileWriter CreateWriter(MatFileWriterOptionsForTests options, Stream stream)
|
|
||||||
{
|
|
||||||
return options switch
|
|
||||||
{
|
|
||||||
MatFileWriterOptionsForTests.None => new MatFileWriter(stream),
|
|
||||||
MatFileWriterOptionsForTests.Always => new MatFileWriter(
|
|
||||||
stream,
|
|
||||||
new MatFileWriterOptions { UseCompression = CompressionUsage.Always }),
|
|
||||||
MatFileWriterOptionsForTests.Never => new MatFileWriter(
|
|
||||||
stream,
|
|
||||||
new MatFileWriterOptions { UseCompression = CompressionUsage.Never }),
|
|
||||||
_ => throw new NotImplementedException(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
37
MatFileHandler.Tests/MatFileWritingToMemoryStream.cs
Normal file
37
MatFileHandler.Tests/MatFileWritingToMemoryStream.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A method of writing an IMatFile into a MemoryStream.
|
||||||
|
/// </summary>
|
||||||
|
public class MatFileWritingToMemoryStream : MatFileWritingMethod
|
||||||
|
{
|
||||||
|
private readonly MatFileWriterOptions? _maybeOptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="MatFileWritingToMemoryStream"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="maybeOptions">Options for the <see cref="MatFileWriter"/>.</param>
|
||||||
|
public MatFileWritingToMemoryStream(MatFileWriterOptions? maybeOptions)
|
||||||
|
{
|
||||||
|
_maybeOptions = maybeOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override byte[] WriteMatFile(IMatFile matFile)
|
||||||
|
{
|
||||||
|
using var memoryStream = new MemoryStream();
|
||||||
|
var matFileWriter = _maybeOptions switch
|
||||||
|
{
|
||||||
|
{ } options => new MatFileWriter(memoryStream, options),
|
||||||
|
_ => new MatFileWriter(memoryStream),
|
||||||
|
};
|
||||||
|
|
||||||
|
matFileWriter.Write(matFile);
|
||||||
|
return memoryStream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A method of writing an IMatFile into a stream that is "unaligned".
|
||||||
|
/// </summary>
|
||||||
|
public class MatFileWritingToUnalignedMemoryStream : MatFileWritingMethod
|
||||||
|
{
|
||||||
|
private readonly MatFileWriterOptions? _maybeOptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="MatFileWritingToUnalignedMemoryStream"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="maybeOptions">Options for the <see cref="MatFileWriter"/>.</param>
|
||||||
|
public MatFileWritingToUnalignedMemoryStream(MatFileWriterOptions? maybeOptions)
|
||||||
|
{
|
||||||
|
_maybeOptions = maybeOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override byte[] WriteMatFile(IMatFile matFile)
|
||||||
|
{
|
||||||
|
using var memoryStream = new MemoryStream();
|
||||||
|
memoryStream.Seek(3, SeekOrigin.Begin);
|
||||||
|
var matFileWriter = _maybeOptions switch
|
||||||
|
{
|
||||||
|
{ } options => new MatFileWriter(memoryStream, options),
|
||||||
|
_ => new MatFileWriter(memoryStream),
|
||||||
|
};
|
||||||
|
|
||||||
|
matFileWriter.Write(matFile);
|
||||||
|
var fullArray = memoryStream.ToArray();
|
||||||
|
var length = fullArray.Length - 3;
|
||||||
|
var result = new byte[length];
|
||||||
|
Buffer.BlockCopy(fullArray, 3, result, 0, length);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
38
MatFileHandler.Tests/MatFileWritingToUnseekableStream.cs
Normal file
38
MatFileHandler.Tests/MatFileWritingToUnseekableStream.cs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A method of writing an IMatFile into a stream that is not seekable.
|
||||||
|
/// </summary>
|
||||||
|
public class MatFileWritingToUnseekableStream : MatFileWritingMethod
|
||||||
|
{
|
||||||
|
private readonly MatFileWriterOptions? _maybeOptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="MatFileWritingToUnseekableStream"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="maybeOptions">Options for the <see cref="MatFileWriter"/>.</param>
|
||||||
|
public MatFileWritingToUnseekableStream(MatFileWriterOptions? maybeOptions)
|
||||||
|
{
|
||||||
|
_maybeOptions = maybeOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override byte[] WriteMatFile(IMatFile matFile)
|
||||||
|
{
|
||||||
|
using var memoryStream = new MemoryStream();
|
||||||
|
using var unseekableStream = new UnseekableWriteStream(memoryStream);
|
||||||
|
var matFileWriter = _maybeOptions switch
|
||||||
|
{
|
||||||
|
{ } options => new MatFileWriter(unseekableStream, options),
|
||||||
|
_ => new MatFileWriter(unseekableStream),
|
||||||
|
};
|
||||||
|
|
||||||
|
matFileWriter.Write(matFile);
|
||||||
|
return memoryStream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
MatFileHandler.Tests/MatTestDataFactory.cs
Executable file
35
MatFileHandler.Tests/MatTestDataFactory.cs
Executable file
@ -0,0 +1,35 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Factory providing the parsed contents of .mat files.
|
||||||
|
/// </summary>
|
||||||
|
public class MatTestDataFactory : AbstractTestDataFactory<IMatFile>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="MatTestDataFactory"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="testDirectory">Directory containing test files.</param>
|
||||||
|
public MatTestDataFactory(string testDirectory)
|
||||||
|
: base(
|
||||||
|
testDirectory,
|
||||||
|
new ExtensionTestFilenameConvention("mat"))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read and parse data from a .mat file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">Input stream.</param>
|
||||||
|
/// <returns>Parsed contents of the file.</returns>
|
||||||
|
protected override IMatFile ReadDataFromStream(Stream stream)
|
||||||
|
{
|
||||||
|
var matFileReader = new MatFileReader(stream);
|
||||||
|
var matFile = matFileReader.Read();
|
||||||
|
return matFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
MatFileHandler.Tests/PartialReadMatTestDataFactory.cs
Normal file
35
MatFileHandler.Tests/PartialReadMatTestDataFactory.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Factory providing the parsed contents of .mat files,
|
||||||
|
/// wrapped in a <see cref="PartialUnseekableReadStream"/>.
|
||||||
|
/// </summary>
|
||||||
|
public class PartialReadMatTestDataFactory : MatTestDataFactory
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="PartialReadMatTestDataFactory"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="testDirectory">Directory containing test files.</param>
|
||||||
|
public PartialReadMatTestDataFactory(string testDirectory)
|
||||||
|
: base(testDirectory)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read and parse data from a .mat file.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">Input stream.</param>
|
||||||
|
/// <returns>Parsed contents of the file.</returns>
|
||||||
|
protected override IMatFile ReadDataFromStream(Stream stream)
|
||||||
|
{
|
||||||
|
using (var wrapper = new PartialUnseekableReadStream(stream))
|
||||||
|
{
|
||||||
|
return base.ReadDataFromStream(wrapper);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace MatFileHandler.Tests
|
namespace MatFileHandler.Tests
|
||||||
@ -7,7 +9,7 @@ namespace MatFileHandler.Tests
|
|||||||
/// A stream which wraps another stream and only reads one byte at a time,
|
/// A stream which wraps another stream and only reads one byte at a time,
|
||||||
/// while forbidding seeking in it.
|
/// while forbidding seeking in it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal sealed class PartialUnseekableReadStream : Stream
|
internal class PartialUnseekableReadStream : Stream
|
||||||
{
|
{
|
||||||
private readonly Stream _baseStream;
|
private readonly Stream _baseStream;
|
||||||
|
|
||||||
|
37
MatFileHandler.Tests/UnalignedMatTestDataFactory.cs
Normal file
37
MatFileHandler.Tests/UnalignedMatTestDataFactory.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace MatFileHandler.Tests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Factory providing the parsed contents of .mat files,
|
||||||
|
/// which start at a non-8 byte-aligned offset in the stream.
|
||||||
|
/// </summary>
|
||||||
|
public class UnalignedMatTestDataFactory : MatTestDataFactory
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="PartialReadMatTestDataFactory"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="testDirectory">Directory containing test files.</param>
|
||||||
|
public UnalignedMatTestDataFactory(string testDirectory)
|
||||||
|
: base(testDirectory)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
protected override IMatFile ReadDataFromStream(Stream stream)
|
||||||
|
{
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
ms.Seek(3, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
stream.CopyTo(ms);
|
||||||
|
|
||||||
|
ms.Seek(3, SeekOrigin.Begin);
|
||||||
|
|
||||||
|
return base.ReadDataFromStream(ms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace MatFileHandler.Tests
|
namespace MatFileHandler.Tests
|
||||||
@ -6,7 +8,7 @@ namespace MatFileHandler.Tests
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A stream which wraps another stream and forbids seeking in it.
|
/// A stream which wraps another stream and forbids seeking in it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal sealed class UnseekableWriteStream : Stream
|
internal class UnseekableWriteStream : Stream
|
||||||
{
|
{
|
||||||
public UnseekableWriteStream(Stream baseStream)
|
public UnseekableWriteStream(Stream baseStream)
|
||||||
{
|
{
|
||||||
|
Binary file not shown.
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
@ -11,8 +13,8 @@ namespace MatFileHandler
|
|||||||
{
|
{
|
||||||
private const uint BigPrime = 0xFFF1;
|
private const uint BigPrime = 0xFFF1;
|
||||||
private readonly Stream _stream;
|
private readonly Stream _stream;
|
||||||
private uint s1;
|
private uint s1 = 1;
|
||||||
private uint s2;
|
private uint s2 = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ChecksumCalculatingStream"/> class.
|
/// Initializes a new instance of the <see cref="ChecksumCalculatingStream"/> class.
|
||||||
@ -21,8 +23,6 @@ namespace MatFileHandler
|
|||||||
public ChecksumCalculatingStream(Stream stream)
|
public ChecksumCalculatingStream(Stream stream)
|
||||||
{
|
{
|
||||||
_stream = stream;
|
_stream = stream;
|
||||||
s1 = 1;
|
|
||||||
s2 = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
@ -6,7 +8,7 @@ namespace MatFileHandler
|
|||||||
/// A structure representing a complex number where real and imaginary parts are of type T.
|
/// A structure representing a complex number where real and imaginary parts are of type T.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">Type of real and imaginary parts.</typeparam>
|
/// <typeparam name="T">Type of real and imaginary parts.</typeparam>
|
||||||
public readonly struct ComplexOf<T> : IEquatable<ComplexOf<T>>
|
public struct ComplexOf<T> : IEquatable<ComplexOf<T>>
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -69,11 +71,10 @@ namespace MatFileHandler
|
|||||||
/// <returns>True iff another object is a complex number equal to this.</returns>
|
/// <returns>True iff another object is a complex number equal to this.</returns>
|
||||||
public override bool Equals(object obj)
|
public override bool Equals(object obj)
|
||||||
{
|
{
|
||||||
if (obj is null)
|
if (ReferenceEquals(null, obj))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj is ComplexOf<T> other && Equals(other);
|
return obj is ComplexOf<T> other && Equals(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Describes compression usage strategy for writing files.
|
/// Describes compression usage strategy for writing files.
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
#pragma warning disable CA1822
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -51,7 +52,7 @@ namespace MatFileHandler
|
|||||||
{
|
{
|
||||||
if (data.Length != dimensions.NumberOfElements())
|
if (data.Length != dimensions.NumberOfElements())
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Data size does not match the specified dimensions", nameof(data));
|
throw new ArgumentException("Data size does not match the specified dimensions", "data");
|
||||||
}
|
}
|
||||||
return new MatNumericalArrayOf<T>(GetStandardFlags<T>(), dimensions, string.Empty, data);
|
return new MatNumericalArrayOf<T>(GetStandardFlags<T>(), dimensions, string.Empty, data);
|
||||||
}
|
}
|
||||||
@ -156,7 +157,7 @@ namespace MatFileHandler
|
|||||||
return new MatFile(variables);
|
return new MatFile(variables);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ArrayFlags ConstructArrayFlags(ArrayType class_, bool isComplex = false, bool isLogical = false)
|
private ArrayFlags ConstructArrayFlags(ArrayType class_, bool isComplex = false, bool isLogical = false)
|
||||||
{
|
{
|
||||||
return new ArrayFlags
|
return new ArrayFlags
|
||||||
{
|
{
|
||||||
@ -166,7 +167,7 @@ namespace MatFileHandler
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ArrayFlags GetStandardFlags<T>()
|
private ArrayFlags GetStandardFlags<T>()
|
||||||
{
|
{
|
||||||
if (typeof(T) == typeof(sbyte))
|
if (typeof(T) == typeof(sbyte))
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Base class for all data elements in .mat files.
|
/// Base class for all data elements in .mat files.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
@ -69,21 +71,16 @@ namespace MatFileHandler
|
|||||||
{
|
{
|
||||||
throw new NotSupportedException("Only 2-dimensional sparse arrays are supported");
|
throw new NotSupportedException("Only 2-dimensional sparse arrays are supported");
|
||||||
}
|
}
|
||||||
if (data is null)
|
if (data == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Null data found.", nameof(data));
|
throw new ArgumentException("Null data found.", "data");
|
||||||
}
|
}
|
||||||
|
var elements =
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var maybeElements =
|
|
||||||
ConvertDataToSparseProperType<T>(data, flags.ArrayFlags.Variable.HasFlag(Variable.IsLogical));
|
ConvertDataToSparseProperType<T>(data, flags.ArrayFlags.Variable.HasFlag(Variable.IsLogical));
|
||||||
if (maybeElements is not { } elements)
|
if (elements == null)
|
||||||
{
|
{
|
||||||
throw new HandlerException("Couldn't read sparse array.");
|
throw new HandlerException("Couldn't read sparse array.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataDictionary =
|
var dataDictionary =
|
||||||
ConvertMatlabSparseToDictionary(rowIndex, columnIndex, j => elements[j]);
|
ConvertMatlabSparseToDictionary(rowIndex, columnIndex, j => elements[j]);
|
||||||
return new MatSparseArrayOf<T>(flags, dimensions, name, dataDictionary);
|
return new MatSparseArrayOf<T>(flags, dimensions, name, dataDictionary);
|
||||||
@ -120,12 +117,15 @@ namespace MatFileHandler
|
|||||||
switch (flags.Class)
|
switch (flags.Class)
|
||||||
{
|
{
|
||||||
case ArrayType.MxChar:
|
case ArrayType.MxChar:
|
||||||
return realData switch
|
switch (realData)
|
||||||
{
|
{
|
||||||
MiNum<byte> dataByte => ConvertToMatCharArray(flags, dimensions, name, dataByte),
|
case MiNum<byte> dataByte:
|
||||||
MiNum<ushort> dataUshort => ConvertToMatCharArray(flags, dimensions, name, dataUshort),
|
return ConvertToMatCharArray(flags, dimensions, name, dataByte);
|
||||||
_ => throw new NotSupportedException("Only utf8, utf16 or ushort char arrays are supported."),
|
case MiNum<ushort> dataUshort:
|
||||||
};
|
return ConvertToMatCharArray(flags, dimensions, name, dataUshort);
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException("Only utf8, utf16 or ushort char arrays are supported.");
|
||||||
|
}
|
||||||
case ArrayType.MxDouble:
|
case ArrayType.MxDouble:
|
||||||
case ArrayType.MxSingle:
|
case ArrayType.MxSingle:
|
||||||
case ArrayType.MxInt8:
|
case ArrayType.MxInt8:
|
||||||
@ -184,12 +184,13 @@ namespace MatFileHandler
|
|||||||
{
|
{
|
||||||
return DataExtraction.GetDataAsUInt8(data).Select(x => x != 0).ToArray() as T[];
|
return DataExtraction.GetDataAsUInt8(data).Select(x => x != 0).ToArray() as T[];
|
||||||
}
|
}
|
||||||
|
switch (data)
|
||||||
return data switch
|
|
||||||
{
|
{
|
||||||
MiNum<double> => DataExtraction.GetDataAsDouble(data) as T[],
|
case MiNum<double> _:
|
||||||
_ => throw new NotSupportedException(),
|
return DataExtraction.GetDataAsDouble(data) as T[];
|
||||||
};
|
default:
|
||||||
|
throw new NotSupportedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MatCharArrayOf<ushort> ConvertToMatCharArray(
|
private static MatCharArrayOf<ushort> ConvertToMatCharArray(
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
@ -28,33 +30,54 @@ namespace MatFileHandler
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="reader">Input reader.</param>
|
/// <param name="reader">Input reader.</param>
|
||||||
/// <returns>Data element.</returns>
|
/// <returns>Data element.</returns>
|
||||||
public DataElement? Read(BinaryReader reader)
|
public DataElement Read(BinaryReader reader)
|
||||||
{
|
{
|
||||||
var maybeTagPair = ReadTag(reader);
|
var (dataReader, tag) = ReadTag(reader);
|
||||||
if (maybeTagPair is not { } tagPair)
|
DataElement result;
|
||||||
|
switch (tag.Type)
|
||||||
{
|
{
|
||||||
return null;
|
case DataType.MiInt8:
|
||||||
|
result = ReadNum<sbyte>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiUInt8:
|
||||||
|
case DataType.MiUtf8:
|
||||||
|
result = ReadNum<byte>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiInt16:
|
||||||
|
result = ReadNum<short>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiUInt16:
|
||||||
|
case DataType.MiUtf16:
|
||||||
|
result = ReadNum<ushort>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiInt32:
|
||||||
|
result = ReadNum<int>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiUInt32:
|
||||||
|
result = ReadNum<uint>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiSingle:
|
||||||
|
result = ReadNum<float>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiDouble:
|
||||||
|
result = ReadNum<double>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiInt64:
|
||||||
|
result = ReadNum<long>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiUInt64:
|
||||||
|
result = ReadNum<ulong>(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiMatrix:
|
||||||
|
result = ReadMatrix(tag, dataReader);
|
||||||
|
break;
|
||||||
|
case DataType.MiCompressed:
|
||||||
|
result = ReadCompressed(tag, dataReader);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException("Unknown element.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var (dataReader, tag) = tagPair;
|
|
||||||
|
|
||||||
var result = tag.Type switch
|
|
||||||
{
|
|
||||||
DataType.MiInt8 => ReadNum<sbyte>(tag, dataReader),
|
|
||||||
DataType.MiUInt8 or DataType.MiUtf8 => ReadNum<byte>(tag, dataReader),
|
|
||||||
DataType.MiInt16 => ReadNum<short>(tag, dataReader),
|
|
||||||
DataType.MiUInt16 or DataType.MiUtf16 => ReadNum<ushort>(tag, dataReader),
|
|
||||||
DataType.MiInt32 => ReadNum<int>(tag, dataReader),
|
|
||||||
DataType.MiUInt32 => ReadNum<uint>(tag, dataReader),
|
|
||||||
DataType.MiSingle => ReadNum<float>(tag, dataReader),
|
|
||||||
DataType.MiDouble => ReadNum<double>(tag, dataReader),
|
|
||||||
DataType.MiInt64 => ReadNum<long>(tag, dataReader),
|
|
||||||
DataType.MiUInt64 => ReadNum<ulong>(tag, dataReader),
|
|
||||||
DataType.MiMatrix => ReadMatrix(tag, dataReader),
|
|
||||||
DataType.MiCompressed => ReadCompressed(tag, dataReader),
|
|
||||||
_ => throw new NotSupportedException("Unknown element."),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (tag.Type != DataType.MiCompressed)
|
if (tag.Type != DataType.MiCompressed)
|
||||||
{
|
{
|
||||||
var position = reader.BaseStream.Position;
|
var position = reader.BaseStream.Position;
|
||||||
@ -171,31 +194,9 @@ namespace MatFileHandler
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int? TryReadInt32(BinaryReader reader)
|
private static (BinaryReader reader, Tag tag) ReadTag(BinaryReader reader)
|
||||||
{
|
{
|
||||||
var buffer = new byte[4];
|
var type = reader.ReadInt32();
|
||||||
var position = 0;
|
|
||||||
while (position < 4)
|
|
||||||
{
|
|
||||||
var actually = reader.BaseStream.Read(buffer, position, 4 - position);
|
|
||||||
if (actually == 0)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
position += actually;
|
|
||||||
}
|
|
||||||
|
|
||||||
return BitConverter.ToInt32(buffer, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static (BinaryReader reader, Tag tag)? ReadTag(BinaryReader reader)
|
|
||||||
{
|
|
||||||
var maybeType = TryReadInt32(reader);
|
|
||||||
if (maybeType is not int type)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var typeHi = type >> 16;
|
var typeHi = type >> 16;
|
||||||
if (typeHi == 0)
|
if (typeHi == 0)
|
||||||
{
|
{
|
||||||
@ -205,13 +206,13 @@ namespace MatFileHandler
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var length = typeHi;
|
var length = typeHi;
|
||||||
type &= 0xffff;
|
type = type & 0xffff;
|
||||||
var smallReader = new BinaryReader(new MemoryStream(reader.ReadBytes(4)));
|
var smallReader = new BinaryReader(new MemoryStream(reader.ReadBytes(4)));
|
||||||
return (smallReader, new Tag((DataType)type, length));
|
return (smallReader, new Tag((DataType)type, length));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MatCellArray ContinueReadingCellArray(
|
private DataElement ContinueReadingCellArray(
|
||||||
BinaryReader reader,
|
BinaryReader reader,
|
||||||
ArrayFlags flags,
|
ArrayFlags flags,
|
||||||
int[] dimensions,
|
int[] dimensions,
|
||||||
@ -240,7 +241,7 @@ namespace MatFileHandler
|
|||||||
var classNameElement = Read(reader) as MiNum<sbyte> ??
|
var classNameElement = Read(reader) as MiNum<sbyte> ??
|
||||||
throw new HandlerException("Unexpected type in class name.");
|
throw new HandlerException("Unexpected type in class name.");
|
||||||
var className = ReadName(classNameElement);
|
var className = ReadName(classNameElement);
|
||||||
var dataElement = Read(reader) ?? throw new HandlerException("Missing opaque data element.");
|
var dataElement = Read(reader);
|
||||||
var data = ReadData(dataElement);
|
var data = ReadData(dataElement);
|
||||||
if (data is MatNumericalArrayOf<uint> linkElement)
|
if (data is MatNumericalArrayOf<uint> linkElement)
|
||||||
{
|
{
|
||||||
@ -257,11 +258,11 @@ namespace MatFileHandler
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return new Opaque(name, typeDescription, className, Array.Empty<int>(), data, subsystemData);
|
return new Opaque(name, typeDescription, className, new int[] { }, data, subsystemData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MatArray ContinueReadingSparseArray(
|
private DataElement ContinueReadingSparseArray(
|
||||||
BinaryReader reader,
|
BinaryReader reader,
|
||||||
DataElement firstElement,
|
DataElement firstElement,
|
||||||
int[] dimensions,
|
int[] dimensions,
|
||||||
@ -272,7 +273,7 @@ namespace MatFileHandler
|
|||||||
throw new HandlerException("Unexpected type in row indices of a sparse array.");
|
throw new HandlerException("Unexpected type in row indices of a sparse array.");
|
||||||
var columnIndex = Read(reader) as MiNum<int> ??
|
var columnIndex = Read(reader) as MiNum<int> ??
|
||||||
throw new HandlerException("Unexpected type in column indices of a sparse array.");
|
throw new HandlerException("Unexpected type in column indices of a sparse array.");
|
||||||
var data = Read(reader) ?? throw new HandlerException("Missing sparse array data.");
|
var data = Read(reader);
|
||||||
if (sparseArrayFlags.ArrayFlags.Variable.HasFlag(Variable.IsLogical))
|
if (sparseArrayFlags.ArrayFlags.Variable.HasFlag(Variable.IsLogical))
|
||||||
{
|
{
|
||||||
return DataElementConverter.ConvertToMatSparseArrayOf<bool>(
|
return DataElementConverter.ConvertToMatSparseArrayOf<bool>(
|
||||||
@ -286,7 +287,7 @@ namespace MatFileHandler
|
|||||||
|
|
||||||
if (sparseArrayFlags.ArrayFlags.Variable.HasFlag(Variable.IsComplex))
|
if (sparseArrayFlags.ArrayFlags.Variable.HasFlag(Variable.IsComplex))
|
||||||
{
|
{
|
||||||
var imaginaryData = Read(reader) ?? throw new HandlerException("Missing imaginary part of sparse array data.");
|
var imaginaryData = Read(reader);
|
||||||
return DataElementConverter.ConvertToMatSparseArrayOfComplex(
|
return DataElementConverter.ConvertToMatSparseArrayOfComplex(
|
||||||
sparseArrayFlags,
|
sparseArrayFlags,
|
||||||
dimensions,
|
dimensions,
|
||||||
@ -297,20 +298,22 @@ namespace MatFileHandler
|
|||||||
imaginaryData);
|
imaginaryData);
|
||||||
}
|
}
|
||||||
|
|
||||||
return data switch
|
switch (data)
|
||||||
{
|
{
|
||||||
MiNum<double> => DataElementConverter.ConvertToMatSparseArrayOf<double>(
|
case MiNum<double> _:
|
||||||
|
return DataElementConverter.ConvertToMatSparseArrayOf<double>(
|
||||||
sparseArrayFlags,
|
sparseArrayFlags,
|
||||||
dimensions,
|
dimensions,
|
||||||
name,
|
name,
|
||||||
rowIndex.Data,
|
rowIndex.Data,
|
||||||
columnIndex.Data,
|
columnIndex.Data,
|
||||||
data),
|
data);
|
||||||
_ => throw new NotSupportedException("Only double and logical sparse arrays are supported."),
|
default:
|
||||||
};
|
throw new NotSupportedException("Only double and logical sparse arrays are supported.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MatStructureArray ContinueReadingStructure(
|
private DataElement ContinueReadingStructure(
|
||||||
BinaryReader reader,
|
BinaryReader reader,
|
||||||
ArrayFlags flags,
|
ArrayFlags flags,
|
||||||
int[] dimensions,
|
int[] dimensions,
|
||||||
@ -353,7 +356,7 @@ namespace MatFileHandler
|
|||||||
using (var positionTrackingStream = new PositionTrackingStream(bufferedStream))
|
using (var positionTrackingStream = new PositionTrackingStream(bufferedStream))
|
||||||
using (var innerReader = new BinaryReader(positionTrackingStream))
|
using (var innerReader = new BinaryReader(positionTrackingStream))
|
||||||
{
|
{
|
||||||
element = Read(innerReader) ?? throw new HandlerException("Missing compressed data.");
|
element = Read(innerReader);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (substream.Position != substream.Length)
|
if (substream.Position != substream.Length)
|
||||||
@ -376,7 +379,7 @@ namespace MatFileHandler
|
|||||||
return MatArray.Empty();
|
return MatArray.Empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
var element1 = Read(reader) ?? throw new HandlerException("Missing matrix data.");
|
var element1 = Read(reader);
|
||||||
var flags = ReadArrayFlags(element1);
|
var flags = ReadArrayFlags(element1);
|
||||||
if (flags.Class == ArrayType.MxOpaque)
|
if (flags.Class == ArrayType.MxOpaque)
|
||||||
{
|
{
|
||||||
@ -398,12 +401,12 @@ namespace MatFileHandler
|
|||||||
return ContinueReadingSparseArray(reader, element1, dimensions, name);
|
return ContinueReadingSparseArray(reader, element1, dimensions, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
var element4 = Read(reader) ?? throw new HandlerException("Missing matrix data.");
|
var element4 = Read(reader);
|
||||||
var data = ReadData(element4);
|
var data = ReadData(element4);
|
||||||
DataElement? imaginaryData = null;
|
DataElement? imaginaryData = null;
|
||||||
if (flags.Variable.HasFlag(Variable.IsComplex))
|
if (flags.Variable.HasFlag(Variable.IsComplex))
|
||||||
{
|
{
|
||||||
var element5 = Read(reader) ?? throw new HandlerException("Missing complex matrix data.");
|
var element5 = Read(reader);
|
||||||
imaginaryData = ReadData(element5);
|
imaginaryData = ReadData(element5);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -418,23 +421,26 @@ namespace MatFileHandler
|
|||||||
switch (flags.Class)
|
switch (flags.Class)
|
||||||
{
|
{
|
||||||
case ArrayType.MxChar:
|
case ArrayType.MxChar:
|
||||||
return data switch
|
switch (data)
|
||||||
{
|
{
|
||||||
MiNum<byte> => DataElementConverter.ConvertToMatNumericalArrayOf<byte>(
|
case MiNum<byte> _:
|
||||||
|
return DataElementConverter.ConvertToMatNumericalArrayOf<byte>(
|
||||||
flags,
|
flags,
|
||||||
dimensions,
|
dimensions,
|
||||||
name,
|
name,
|
||||||
data,
|
data,
|
||||||
imaginaryData),
|
imaginaryData);
|
||||||
MiNum<ushort> => DataElementConverter.ConvertToMatNumericalArrayOf<ushort>(
|
case MiNum<ushort> _:
|
||||||
|
return DataElementConverter.ConvertToMatNumericalArrayOf<ushort>(
|
||||||
flags,
|
flags,
|
||||||
dimensions,
|
dimensions,
|
||||||
name,
|
name,
|
||||||
data,
|
data,
|
||||||
imaginaryData),
|
imaginaryData);
|
||||||
_ => throw new NotSupportedException(
|
default:
|
||||||
$"This type of char array ({data.GetType()}) is not supported."),
|
throw new NotSupportedException(
|
||||||
};
|
$"This type of char array ({data.GetType()}) is not supported.");
|
||||||
|
}
|
||||||
case ArrayType.MxInt8:
|
case ArrayType.MxInt8:
|
||||||
return DataElementConverter.ConvertToMatNumericalArrayOf<sbyte>(
|
return DataElementConverter.ConvertToMatNumericalArrayOf<sbyte>(
|
||||||
flags,
|
flags,
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
@ -14,21 +16,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to Double.</returns>
|
/// <returns>Contents of the elements, converted to Double.</returns>
|
||||||
public static double[] GetDataAsDouble(DataElement element)
|
public static double[] GetDataAsDouble(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToDouble(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToDouble(byteElement.Data),
|
return SbyteToDouble(sbyteElement.Data);
|
||||||
MiNum<int> intElement => IntToDouble(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToDouble(uintElement.Data),
|
return ByteToDouble(byteElement.Data);
|
||||||
MiNum<short> shortElement => ShortToDouble(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToDouble(ushortElement.Data),
|
return IntToDouble(intElement.Data);
|
||||||
MiNum<long> longElement => LongToDouble(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToDouble(ulongElement.Data),
|
return UintToDouble(uintElement.Data);
|
||||||
MiNum<float> floatElement => FloatToDouble(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => doubleElement.Data,
|
return ShortToDouble(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to double, found {element.GetType()}."),
|
return UshortToDouble(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToDouble(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToDouble(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return FloatToDouble(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return doubleElement.Data;
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to double, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -38,21 +50,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to Single.</returns>
|
/// <returns>Contents of the elements, converted to Single.</returns>
|
||||||
public static float[] GetDataAsSingle(DataElement element)
|
public static float[] GetDataAsSingle(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToSingle(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToSingle(byteElement.Data),
|
return SbyteToSingle(sbyteElement.Data);
|
||||||
MiNum<int> intElement => IntToSingle(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToSingle(uintElement.Data),
|
return ByteToSingle(byteElement.Data);
|
||||||
MiNum<short> shortElement => ShortToSingle(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToSingle(ushortElement.Data),
|
return IntToSingle(intElement.Data);
|
||||||
MiNum<long> longElement => LongToSingle(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToSingle(ulongElement.Data),
|
return UintToSingle(uintElement.Data);
|
||||||
MiNum<float> floatElement => floatElement.Data,
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToSingle(doubleElement.Data),
|
return ShortToSingle(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to float, found {element.GetType()}."),
|
return UshortToSingle(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToSingle(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToSingle(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return floatElement.Data;
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToSingle(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to float, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -62,21 +84,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to Int8.</returns>
|
/// <returns>Contents of the elements, converted to Int8.</returns>
|
||||||
public static sbyte[] GetDataAsInt8(DataElement element)
|
public static sbyte[] GetDataAsInt8(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => sbyteElement.Data,
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToSByte(byteElement.Data),
|
return sbyteElement.Data;
|
||||||
MiNum<int> intElement => IntToSByte(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToSByte(uintElement.Data),
|
return ByteToSByte(byteElement.Data);
|
||||||
MiNum<short> shortElement => ShortToSByte(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToSByte(ushortElement.Data),
|
return IntToSByte(intElement.Data);
|
||||||
MiNum<long> longElement => LongToSByte(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToSByte(ulongElement.Data),
|
return UintToSByte(uintElement.Data);
|
||||||
MiNum<float> floatElement => SingleToSByte(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToSByte(doubleElement.Data),
|
return ShortToSByte(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to int8, found {element.GetType()}."),
|
return UshortToSByte(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToSByte(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToSByte(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return SingleToSByte(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToSByte(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to int8, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -86,21 +118,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to UInt8.</returns>
|
/// <returns>Contents of the elements, converted to UInt8.</returns>
|
||||||
public static byte[] GetDataAsUInt8(DataElement element)
|
public static byte[] GetDataAsUInt8(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToByte(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => byteElement.Data,
|
return SbyteToByte(sbyteElement.Data);
|
||||||
MiNum<int> intElement => IntToByte(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToByte(uintElement.Data),
|
return byteElement.Data;
|
||||||
MiNum<short> shortElement => ShortToByte(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToByte(ushortElement.Data),
|
return IntToByte(intElement.Data);
|
||||||
MiNum<long> longElement => LongToByte(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToByte(ulongElement.Data),
|
return UintToByte(uintElement.Data);
|
||||||
MiNum<float> floatElement => SingleToByte(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToByte(doubleElement.Data),
|
return ShortToByte(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to uint8, found {element.GetType()}."),
|
return UshortToByte(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToByte(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToByte(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return SingleToByte(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToByte(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to uint8, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -110,21 +152,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to Int16.</returns>
|
/// <returns>Contents of the elements, converted to Int16.</returns>
|
||||||
public static short[] GetDataAsInt16(DataElement element)
|
public static short[] GetDataAsInt16(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToInt16(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToInt16(byteElement.Data),
|
return SbyteToInt16(sbyteElement.Data);
|
||||||
MiNum<int> intElement => IntToInt16(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToInt16(uintElement.Data),
|
return ByteToInt16(byteElement.Data);
|
||||||
MiNum<short> shortElement => shortElement.Data,
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToInt16(ushortElement.Data),
|
return IntToInt16(intElement.Data);
|
||||||
MiNum<long> longElement => LongToInt16(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToInt16(ulongElement.Data),
|
return UintToInt16(uintElement.Data);
|
||||||
MiNum<float> floatElement => SingleToInt16(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToInt16(doubleElement.Data),
|
return shortElement.Data;
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to int16, found {element.GetType()}."),
|
return UshortToInt16(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToInt16(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToInt16(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return SingleToInt16(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToInt16(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to int16, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -134,21 +186,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to UInt16.</returns>
|
/// <returns>Contents of the elements, converted to UInt16.</returns>
|
||||||
public static ushort[] GetDataAsUInt16(DataElement element)
|
public static ushort[] GetDataAsUInt16(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToUInt16(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToUInt16(byteElement.Data),
|
return SbyteToUInt16(sbyteElement.Data);
|
||||||
MiNum<int> intElement => IntToUInt16(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToUInt16(uintElement.Data),
|
return ByteToUInt16(byteElement.Data);
|
||||||
MiNum<short> shortElement => ShortToUInt16(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => ushortElement.Data,
|
return IntToUInt16(intElement.Data);
|
||||||
MiNum<long> longElement => LongToUInt16(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToUInt16(ulongElement.Data),
|
return UintToUInt16(uintElement.Data);
|
||||||
MiNum<float> floatElement => SingleToUInt16(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToUInt16(doubleElement.Data),
|
return ShortToUInt16(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to uint16, found {element.GetType()}."),
|
return ushortElement.Data;
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToUInt16(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToUInt16(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return SingleToUInt16(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToUInt16(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to uint16, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -158,21 +220,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to Int32.</returns>
|
/// <returns>Contents of the elements, converted to Int32.</returns>
|
||||||
public static int[] GetDataAsInt32(DataElement element)
|
public static int[] GetDataAsInt32(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToInt32(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToInt32(byteElement.Data),
|
return SbyteToInt32(sbyteElement.Data);
|
||||||
MiNum<int> intElement => intElement.Data,
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToInt32(uintElement.Data),
|
return ByteToInt32(byteElement.Data);
|
||||||
MiNum<short> shortElement => ShortToInt32(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToInt32(ushortElement.Data),
|
return intElement.Data;
|
||||||
MiNum<long> longElement => LongToInt32(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToInt32(ulongElement.Data),
|
return UintToInt32(uintElement.Data);
|
||||||
MiNum<float> floatElement => SingleToInt32(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToInt32(doubleElement.Data),
|
return ShortToInt32(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to int32, found {element.GetType()}."),
|
return UshortToInt32(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToInt32(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToInt32(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return SingleToInt32(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToInt32(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to int32, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -182,21 +254,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to UInt32.</returns>
|
/// <returns>Contents of the elements, converted to UInt32.</returns>
|
||||||
public static uint[] GetDataAsUInt32(DataElement element)
|
public static uint[] GetDataAsUInt32(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToUInt32(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToUInt32(byteElement.Data),
|
return SbyteToUInt32(sbyteElement.Data);
|
||||||
MiNum<int> intElement => IntToUInt32(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => uintElement.Data,
|
return ByteToUInt32(byteElement.Data);
|
||||||
MiNum<short> shortElement => ShortToUInt32(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToUInt32(ushortElement.Data),
|
return IntToUInt32(intElement.Data);
|
||||||
MiNum<long> longElement => LongToUInt32(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToUInt32(ulongElement.Data),
|
return uintElement.Data;
|
||||||
MiNum<float> floatElement => SingleToUInt32(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToUInt32(doubleElement.Data),
|
return ShortToUInt32(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to uint32, found {element.GetType()}."),
|
return UshortToUInt32(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToUInt32(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToUInt32(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return SingleToUInt32(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToUInt32(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to uint32, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -206,21 +288,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to Int64.</returns>
|
/// <returns>Contents of the elements, converted to Int64.</returns>
|
||||||
public static long[] GetDataAsInt64(DataElement element)
|
public static long[] GetDataAsInt64(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToInt64(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToInt64(byteElement.Data),
|
return SbyteToInt64(sbyteElement.Data);
|
||||||
MiNum<int> intElement => IntToInt64(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToInt64(uintElement.Data),
|
return ByteToInt64(byteElement.Data);
|
||||||
MiNum<short> shortElement => ShortToInt64(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToInt64(ushortElement.Data),
|
return IntToInt64(intElement.Data);
|
||||||
MiNum<long> longElement => longElement.Data,
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => UlongToInt64(ulongElement.Data),
|
return UintToInt64(uintElement.Data);
|
||||||
MiNum<float> floatElement => SingleToInt64(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToInt64(doubleElement.Data),
|
return ShortToInt64(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to int64, found {element.GetType()}."),
|
return UshortToInt64(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return longElement.Data;
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return UlongToInt64(ulongElement.Data);
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return SingleToInt64(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToInt64(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to int64, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -230,21 +322,31 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the elements, converted to UInt64.</returns>
|
/// <returns>Contents of the elements, converted to UInt64.</returns>
|
||||||
public static ulong[] GetDataAsUInt64(DataElement element)
|
public static ulong[] GetDataAsUInt64(DataElement element)
|
||||||
{
|
{
|
||||||
return element switch
|
switch (element)
|
||||||
{
|
{
|
||||||
MiNum<sbyte> sbyteElement => SbyteToUInt64(sbyteElement.Data),
|
case MiNum<sbyte> sbyteElement:
|
||||||
MiNum<byte> byteElement => ByteToUInt64(byteElement.Data),
|
return SbyteToUInt64(sbyteElement.Data);
|
||||||
MiNum<int> intElement => IntToUInt64(intElement.Data),
|
case MiNum<byte> byteElement:
|
||||||
MiNum<uint> uintElement => UintToUInt64(uintElement.Data),
|
return ByteToUInt64(byteElement.Data);
|
||||||
MiNum<short> shortElement => ShortToUInt64(shortElement.Data),
|
case MiNum<int> intElement:
|
||||||
MiNum<ushort> ushortElement => UshortToUInt64(ushortElement.Data),
|
return IntToUInt64(intElement.Data);
|
||||||
MiNum<long> longElement => LongToUInt64(longElement.Data),
|
case MiNum<uint> uintElement:
|
||||||
MiNum<ulong> ulongElement => ulongElement.Data,
|
return UintToUInt64(uintElement.Data);
|
||||||
MiNum<float> floatElement => SingleToUInt64(floatElement.Data),
|
case MiNum<short> shortElement:
|
||||||
MiNum<double> doubleElement => DoubleToUInt64(doubleElement.Data),
|
return ShortToUInt64(shortElement.Data);
|
||||||
_ => throw new HandlerException(
|
case MiNum<ushort> ushortElement:
|
||||||
$"Expected data element that would be convertible to uint64, found {element.GetType()}."),
|
return UshortToUInt64(ushortElement.Data);
|
||||||
};
|
case MiNum<long> longElement:
|
||||||
|
return LongToUInt64(longElement.Data);
|
||||||
|
case MiNum<ulong> ulongElement:
|
||||||
|
return ulongElement.Data;
|
||||||
|
case MiNum<float> floatElement:
|
||||||
|
return SingleToUInt64(floatElement.Data);
|
||||||
|
case MiNum<double> doubleElement:
|
||||||
|
return DoubleToUInt64(doubleElement.Data);
|
||||||
|
}
|
||||||
|
throw new HandlerException(
|
||||||
|
$"Expected data element that would be convertible to uint64, found {element.GetType()}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// * to double
|
// * to double
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
@ -14,21 +16,41 @@ namespace MatFileHandler
|
|||||||
/// <returns>Size in bytes.</returns>
|
/// <returns>Size in bytes.</returns>
|
||||||
public static int Size(this DataType type)
|
public static int Size(this DataType type)
|
||||||
{
|
{
|
||||||
return type switch
|
switch (type)
|
||||||
{
|
{
|
||||||
DataType.MiInt8 or DataType.MiUInt8 => 1,
|
case DataType.MiInt8:
|
||||||
DataType.MiInt16 or DataType.MiUInt16 => 2,
|
return 1;
|
||||||
DataType.MiInt32 or DataType.MiUInt32 => 4,
|
case DataType.MiUInt8:
|
||||||
DataType.MiSingle => 4,
|
return 1;
|
||||||
DataType.MiDouble => 8,
|
case DataType.MiInt16:
|
||||||
DataType.MiInt64 or DataType.MiUInt64 => 8,
|
return 2;
|
||||||
DataType.MiMatrix => 0,
|
case DataType.MiUInt16:
|
||||||
DataType.MiCompressed => 0,
|
return 2;
|
||||||
DataType.MiUtf8 => 1,
|
case DataType.MiInt32:
|
||||||
DataType.MiUtf16 => 2,
|
return 4;
|
||||||
DataType.MiUtf32 => 4,
|
case DataType.MiUInt32:
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null),
|
return 4;
|
||||||
};
|
case DataType.MiSingle:
|
||||||
|
return 4;
|
||||||
|
case DataType.MiDouble:
|
||||||
|
return 8;
|
||||||
|
case DataType.MiInt64:
|
||||||
|
return 8;
|
||||||
|
case DataType.MiUInt64:
|
||||||
|
return 8;
|
||||||
|
case DataType.MiMatrix:
|
||||||
|
return 0;
|
||||||
|
case DataType.MiCompressed:
|
||||||
|
return 0;
|
||||||
|
case DataType.MiUtf8:
|
||||||
|
return 1;
|
||||||
|
case DataType.MiUtf16:
|
||||||
|
return 2;
|
||||||
|
case DataType.MiUtf32:
|
||||||
|
return 4;
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(type), type, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
@ -10,6 +12,8 @@ namespace MatFileHandler
|
|||||||
public class DatetimeAdapter
|
public class DatetimeAdapter
|
||||||
{
|
{
|
||||||
private readonly double[] data;
|
private readonly double[] data;
|
||||||
|
private readonly double[] data2;
|
||||||
|
private readonly int[] dimensions;
|
||||||
|
|
||||||
private readonly DateTimeOffset epoch;
|
private readonly DateTimeOffset epoch;
|
||||||
|
|
||||||
@ -32,13 +36,15 @@ namespace MatFileHandler
|
|||||||
case IArrayOf<double> dataArray:
|
case IArrayOf<double> dataArray:
|
||||||
data = dataArray.ConvertToDoubleArray()
|
data = dataArray.ConvertToDoubleArray()
|
||||||
?? throw new HandlerException("Cannot extract data for the datetime adapter.");
|
?? throw new HandlerException("Cannot extract data for the datetime adapter.");
|
||||||
Dimensions = dataArray.Dimensions;
|
data2 = new double[data.Length];
|
||||||
|
dimensions = dataArray.Dimensions;
|
||||||
break;
|
break;
|
||||||
case IArrayOf<Complex> dataComplex:
|
case IArrayOf<Complex> dataComplex:
|
||||||
var complexData = dataComplex.ConvertToComplexArray()
|
var complexData = dataComplex.ConvertToComplexArray()
|
||||||
?? throw new HandlerException("Cannot extract data for the datetime adapter.");
|
?? throw new HandlerException("Cannot extract data for the datetime adapter.");
|
||||||
data = complexData.Select(c => c.Real).ToArray();
|
data = complexData.Select(c => c.Real).ToArray();
|
||||||
Dimensions = dataComplex.Dimensions;
|
data2 = complexData.Select(c => c.Imaginary).ToArray();
|
||||||
|
dimensions = dataComplex.Dimensions;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new HandlerException("Datetime data not found.");
|
throw new HandlerException("Datetime data not found.");
|
||||||
@ -48,7 +54,7 @@ namespace MatFileHandler
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets datetime array dimensions.
|
/// Gets datetime array dimensions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int[] Dimensions { get; }
|
public int[] Dimensions => dimensions;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets values of datetime object at given position in the array converted to <see cref="DateTimeOffset"/>.
|
/// Gets values of datetime object at given position in the array converted to <see cref="DateTimeOffset"/>.
|
||||||
@ -60,11 +66,11 @@ namespace MatFileHandler
|
|||||||
get
|
get
|
||||||
{
|
{
|
||||||
var milliseconds = data[Dimensions.DimFlatten(list)];
|
var milliseconds = data[Dimensions.DimFlatten(list)];
|
||||||
return milliseconds switch
|
if (milliseconds < -62_135_596_800_000.0 || milliseconds > 253_402_300_799_999.0)
|
||||||
{
|
{
|
||||||
< -62_135_596_800_000.0 or > 253_402_300_799_999.0 => null,
|
return null;
|
||||||
_ => epoch.AddMilliseconds(milliseconds),
|
}
|
||||||
};
|
return epoch.AddMilliseconds(milliseconds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
@ -7,6 +9,7 @@ namespace MatFileHandler
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class DurationAdapter
|
public class DurationAdapter
|
||||||
{
|
{
|
||||||
|
private readonly int[] dimensions;
|
||||||
private readonly double[] data;
|
private readonly double[] data;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -24,13 +27,13 @@ namespace MatFileHandler
|
|||||||
var dataObject = matObject["millis", 0];
|
var dataObject = matObject["millis", 0];
|
||||||
data = dataObject.ConvertToDoubleArray()
|
data = dataObject.ConvertToDoubleArray()
|
||||||
?? throw new HandlerException("Cannot extract data for the duration adapter.");
|
?? throw new HandlerException("Cannot extract data for the duration adapter.");
|
||||||
Dimensions = dataObject.Dimensions;
|
dimensions = dataObject.Dimensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets duration array dimensions.
|
/// Gets duration array dimensions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int[] Dimensions { get; }
|
public int[] Dimensions => dimensions;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets duration object at given position.
|
/// Gets duration object at given position.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A better interface for using enum adapter.
|
/// A better interface for using enum adapter.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
@ -200,27 +202,32 @@ namespace MatFileHandler
|
|||||||
WriteDataElement(buffer);
|
WriteDataElement(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static unsafe int GetLengthOfByteArray<T>(int dataLength)
|
private unsafe int GetLengthOfByteArray<T>(int dataLength)
|
||||||
where T : unmanaged
|
where T : unmanaged
|
||||||
{
|
{
|
||||||
return dataLength * sizeof(T);
|
return dataLength * sizeof(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static unsafe int GetLengthOfPairOfByteArrays<T>(ComplexOf<T>[] data)
|
private unsafe int GetLengthOfPairOfByteArrays<T>(ComplexOf<T>[] data)
|
||||||
where T : unmanaged
|
where T : unmanaged
|
||||||
{
|
{
|
||||||
return data.Length * sizeof(T);
|
return data.Length * sizeof(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static unsafe int GetLengthOfPairOfByteArrays(Complex[] data)
|
private unsafe int GetLengthOfPairOfByteArrays(Complex[] data)
|
||||||
{
|
{
|
||||||
return data.Length * sizeof(double);
|
return data.Length * sizeof(double);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int CalculatePadding(int length)
|
private int CalculatePadding(int length)
|
||||||
{
|
{
|
||||||
var rem = length % 8;
|
var rem = length % 8;
|
||||||
return rem == 0 ? 0 : 8 - rem;
|
if (rem == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 8 - rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteDataElement(int dataLength)
|
private void WriteDataElement(int dataLength)
|
||||||
@ -268,7 +275,7 @@ namespace MatFileHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (int rowIndexLength, int columnIndexLength, int dataLength, uint nonZero) PrepareSparseArrayData<T>(
|
private (int rowIndexLength, int columnIndexLength, int dataLength, uint nonZero) PrepareSparseArrayData<T>(
|
||||||
ISparseArrayOf<T> array)
|
ISparseArrayOf<T> array)
|
||||||
where T : struct, IEquatable<T>
|
where T : struct, IEquatable<T>
|
||||||
{
|
{
|
||||||
@ -286,7 +293,7 @@ namespace MatFileHandler
|
|||||||
private void WriteFieldNames(IEnumerable<string> fieldNames)
|
private void WriteFieldNames(IEnumerable<string> fieldNames)
|
||||||
{
|
{
|
||||||
var fieldNamesArray = fieldNames.Select(name => Encoding.ASCII.GetBytes(name)).ToArray();
|
var fieldNamesArray = fieldNames.Select(name => Encoding.ASCII.GetBytes(name)).ToArray();
|
||||||
var maxFieldName = fieldNamesArray.Max(name => name.Length) + 1;
|
var maxFieldName = fieldNamesArray.Select(name => name.Length).Max() + 1;
|
||||||
WriteDataElement(GetLengthOfByteArray<int>(1));
|
WriteDataElement(GetLengthOfByteArray<int>(1));
|
||||||
var buffer = new byte[fieldNamesArray.Length * maxFieldName];
|
var buffer = new byte[fieldNamesArray.Length * maxFieldName];
|
||||||
var startPosition = 0;
|
var startPosition = 0;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
#if !NET461
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
@ -72,9 +72,11 @@ namespace MatFileHandler
|
|||||||
var version = reader.ReadInt16();
|
var version = reader.ReadInt16();
|
||||||
var endian = reader.ReadInt16();
|
var endian = reader.ReadInt16();
|
||||||
var isLittleEndian = endian == 19785;
|
var isLittleEndian = endian == 19785;
|
||||||
return isLittleEndian
|
if (!isLittleEndian)
|
||||||
? new Header(text, subsystemDataOffset, version)
|
{
|
||||||
: throw new NotSupportedException("Big-endian files are not supported.");
|
throw new NotSupportedException("Big-endian files are not supported.");
|
||||||
|
}
|
||||||
|
return new Header(text, subsystemDataOffset, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetOperatingSystem()
|
private static string GetOperatingSystem()
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An interface providing access to array's contents.
|
/// An interface providing access to array's contents.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Matlab's cell array.
|
/// Matlab's cell array.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Matlab's character array.
|
/// Matlab's character array.
|
||||||
@ -8,8 +10,6 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the contained string.
|
/// Gets the contained string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
#pragma warning disable CA1716, CA1720
|
|
||||||
string String { get; }
|
string String { get; }
|
||||||
#pragma warning restore CA1716, CA1720
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An interface for accessing the contents of .mat files.
|
/// An interface for accessing the contents of .mat files.
|
||||||
@ -25,6 +27,6 @@
|
|||||||
/// <param name="name">The name of the variable to get.</param>
|
/// <param name="name">The name of the variable to get.</param>
|
||||||
/// <param name="variable">When this method returns, contains the variable with the specified name, if it is found; otherwise, null.</param>
|
/// <param name="variable">When this method returns, contains the variable with the specified name, if it is found; otherwise, null.</param>
|
||||||
/// <returns>True if the file contains a variable with the specified name; otherwise, false.</returns>
|
/// <returns>True if the file contains a variable with the specified name; otherwise, false.</returns>
|
||||||
bool TryGetVariable(string name, out IVariable? variable);
|
public bool TryGetVariable(string name, out IVariable? variable);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An interface for accessing the variable contents.
|
/// An interface for accessing the variable contents.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
@ -49,7 +51,7 @@ namespace MatFileHandler
|
|||||||
/// <returns>Empty array.</returns>
|
/// <returns>Empty array.</returns>
|
||||||
public static MatArray Empty()
|
public static MatArray Empty()
|
||||||
{
|
{
|
||||||
return new MatArray(new ArrayFlags { Class = ArrayType.MxCell, Variable = 0 }, Array.Empty<int>(), string.Empty);
|
return new MatArray(new ArrayFlags { Class = ArrayType.MxCell, Variable = 0 }, new int[] { }, string.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Character array.
|
/// Character array.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
@ -16,7 +18,11 @@ namespace MatFileHandler
|
|||||||
/// <param name="variables">List of variables.</param>
|
/// <param name="variables">List of variables.</param>
|
||||||
public MatFile(IEnumerable<IVariable> variables)
|
public MatFile(IEnumerable<IVariable> variables)
|
||||||
{
|
{
|
||||||
_variables = variables.ToDictionary(v => v.Name, v => v);
|
_variables = new Dictionary<string, IVariable>();
|
||||||
|
foreach (var variable in variables)
|
||||||
|
{
|
||||||
|
_variables[variable.Name] = variable;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -1,44 +1,52 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netstandard2.0;net461;net472</TargetFrameworks>
|
<TargetFrameworks>netstandard2.0;net461;net472</TargetFrameworks>
|
||||||
<PackageVersion>1.4.0-beta6</PackageVersion>
|
<PackageVersion>1.4.0-beta5</PackageVersion>
|
||||||
<PackageId>MatFileHandler</PackageId>
|
<PackageId>MatFileHandler</PackageId>
|
||||||
<Title>A library for reading and writing MATLAB .mat files.</Title>
|
<Title>A library for reading and writing MATLAB .mat files.</Title>
|
||||||
<Authors>Alexander Luzgarev</Authors>
|
<Authors>Alexander Luzgarev</Authors>
|
||||||
<Description>MatFileHandler provides a simple interface for reading and writing MATLAB .mat files (of so-called "Level 5") and extracting the contents of numerical arrays, logical arrays, sparse arrays, char arrays, cell arrays and structure arrays.</Description>
|
<Description>MatFileHandler provides a simple interface for reading and writing MATLAB .mat files (of so-called "Level 5") and extracting the contents of numerical arrays, logical arrays, sparse arrays, char arrays, cell arrays and structure arrays.</Description>
|
||||||
<Copyright>Copyright 2017-2020 Alexander Luzgarev</Copyright>
|
<Copyright>Copyright 2017-2020 Alexander Luzgarev</Copyright>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PackageProjectUrl>https://git.mahalex.net/mahalex/MatFileHandler</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/mahalex/MatFileHandler</PackageProjectUrl>
|
||||||
<PackageReleaseNotes>https://git.mahalex.net/mahalex/MatFileHandler/releases/</PackageReleaseNotes>
|
<PackageReleaseNotes>https://github.com/mahalex/MatFileHandler/releases/tag/v$(PackageVersion)</PackageReleaseNotes>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<PackageTags>Matlab</PackageTags>
|
<PackageTags>Matlab</PackageTags>
|
||||||
<RepositoryUrl>https://git.mahalex.net/mahalex/MatFileHandler</RepositoryUrl>
|
<RepositoryUrl>https://github.com/mahalex/MatFileHandler</RepositoryUrl>
|
||||||
<OutputPath>bin\$(Configuration)\</OutputPath>
|
<OutputPath>bin\$(Configuration)\</OutputPath>
|
||||||
<DocumentationFile>$(OutputPath)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
<DocumentationFile>$(OutputPath)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
|
<LangVersion>10.0</LangVersion>
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||||
<IncludeSymbols>true</IncludeSymbols>
|
<IncludeSymbols>true</IncludeSymbols>
|
||||||
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<AnalysisLevel>latest-Recommended</AnalysisLevel>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||||
|
<CodeAnalysisRuleSet>..\MatFileHandler.ruleset</CodeAnalysisRuleSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
<CodeAnalysisRuleSet>..\MatFileHandler.ruleset</CodeAnalysisRuleSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<AdditionalFiles Include="..\stylecop.json" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.66">
|
||||||
|
<PrivateAssets>All</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="System.ValueTuple" Version="4.4.0" Condition="'$(TargetFramework)' == 'net461'" />
|
<PackageReference Include="System.ValueTuple" Version="4.4.0" Condition="'$(TargetFramework)' == 'net461'" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.SourceLink.Gitea" Version="8.0.0" PrivateAssets="All"/>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\README.md" Pack="true" PackagePath="\"/>
|
<None Include="..\README.md" Pack="true" PackagePath="\"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<InternalsVisibleTo Include="MatFileHandler.Tests" />
|
<InternalsVisibleTo Include="MatFileHandler.Tests" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
@ -25,9 +28,11 @@ namespace MatFileHandler
|
|||||||
/// <returns>Contents of the file.</returns>
|
/// <returns>Contents of the file.</returns>
|
||||||
public IMatFile Read()
|
public IMatFile Read()
|
||||||
{
|
{
|
||||||
using var reader = new BinaryReader(new PositionTrackingStream(Stream));
|
using (var reader = new BinaryReader(new PositionTrackingStream(Stream)))
|
||||||
|
{
|
||||||
return Read(reader);
|
return Read(reader);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read a sequence of raw variables from .mat file.
|
/// Read a sequence of raw variables from .mat file.
|
||||||
@ -45,14 +50,11 @@ namespace MatFileHandler
|
|||||||
var variables = new List<RawVariable>();
|
var variables = new List<RawVariable>();
|
||||||
var dataElementReader = new DataElementReader(subsystemData);
|
var dataElementReader = new DataElementReader(subsystemData);
|
||||||
while (true)
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var position = reader.BaseStream.Position;
|
var position = reader.BaseStream.Position;
|
||||||
var dataElement = dataElementReader.Read(reader);
|
var dataElement = dataElementReader.Read(reader);
|
||||||
if (dataElement is null)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (position == subsystemDataOffset)
|
if (position == subsystemDataOffset)
|
||||||
{
|
{
|
||||||
var subsystemDataElement = dataElement as IArrayOf<byte>
|
var subsystemDataElement = dataElement as IArrayOf<byte>
|
||||||
@ -65,6 +67,11 @@ namespace MatFileHandler
|
|||||||
variables.Add(new RawVariable(position, dataElement));
|
variables.Add(new RawVariable(position, dataElement));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (EndOfStreamException)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return variables;
|
return variables;
|
||||||
}
|
}
|
||||||
@ -81,22 +88,24 @@ namespace MatFileHandler
|
|||||||
return ReadRawVariables(reader, subsystemDataOffset, subsystemData);
|
return ReadRawVariables(reader, subsystemDataOffset, subsystemData);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MatFile Read(BinaryReader reader)
|
private static IMatFile Read(BinaryReader reader)
|
||||||
{
|
{
|
||||||
var header = ReadHeader(reader);
|
var header = ReadHeader(reader);
|
||||||
var rawVariables = ReadRawVariables(reader, header.SubsystemDataOffset);
|
var rawVariables = ReadRawVariables(reader, header.SubsystemDataOffset);
|
||||||
var variables = new List<IVariable>();
|
var variables = new List<IVariable>();
|
||||||
foreach (var variable in rawVariables)
|
foreach (var variable in rawVariables)
|
||||||
{
|
{
|
||||||
if (variable.DataElement is MatArray array)
|
var array = variable.DataElement as MatArray;
|
||||||
|
if (array is null)
|
||||||
{
|
{
|
||||||
variables.Add(
|
continue;
|
||||||
new MatVariable(
|
}
|
||||||
|
|
||||||
|
variables.Add(new MatVariable(
|
||||||
array,
|
array,
|
||||||
array.Name,
|
array.Name,
|
||||||
array.Flags.Variable.HasFlag(Variable.IsGlobal)));
|
array.Flags.Variable.HasFlag(Variable.IsGlobal)));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return new MatFile(variables);
|
return new MatFile(variables);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
@ -45,7 +47,8 @@ namespace MatFileHandler
|
|||||||
public void Write(IMatFile file)
|
public void Write(IMatFile file)
|
||||||
{
|
{
|
||||||
var header = Header.CreateNewHeader();
|
var header = Header.CreateNewHeader();
|
||||||
using var writer = new BinaryWriter(Stream);
|
using (var writer = new BinaryWriter(Stream))
|
||||||
|
{
|
||||||
WriteHeader(writer, header);
|
WriteHeader(writer, header);
|
||||||
foreach (var variable in file.Variables)
|
foreach (var variable in file.Variables)
|
||||||
{
|
{
|
||||||
@ -66,7 +69,8 @@ namespace MatFileHandler
|
|||||||
WriteVariable(writer, variable);
|
WriteVariable(writer, variable);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new NotImplementedException();
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,7 +98,7 @@ namespace MatFileHandler
|
|||||||
return (s2 << 16) | s1;
|
return (s2 << 16) | s1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteHeader(BinaryWriter writer, Header header)
|
private void WriteHeader(BinaryWriter writer, Header header)
|
||||||
{
|
{
|
||||||
writer.Write(Encoding.UTF8.GetBytes(header.Text));
|
writer.Write(Encoding.UTF8.GetBytes(header.Text));
|
||||||
writer.Write(header.SubsystemDataOffset);
|
writer.Write(header.SubsystemDataOffset);
|
||||||
@ -102,19 +106,28 @@ namespace MatFileHandler
|
|||||||
writer.Write((short)19785); // Magic number, 'IM'.
|
writer.Write((short)19785); // Magic number, 'IM'.
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteTag(BinaryWriter writer, Tag tag)
|
private void WriteTag(BinaryWriter writer, Tag tag)
|
||||||
{
|
{
|
||||||
writer.Write((int)tag.Type);
|
writer.Write((int)tag.Type);
|
||||||
writer.Write(tag.Length);
|
writer.Write(tag.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteShortTag(BinaryWriter writer, Tag tag)
|
private void WriteShortTag(BinaryWriter writer, Tag tag)
|
||||||
{
|
{
|
||||||
writer.Write((short)tag.Type);
|
writer.Write((short)tag.Type);
|
||||||
writer.Write((short)tag.Length);
|
writer.Write((short)tag.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteDataElement(BinaryWriter writer, DataType type, byte[] data)
|
private void WritePadding(BinaryWriter writer)
|
||||||
|
{
|
||||||
|
var positionMod8 = writer.BaseStream.Position % 8;
|
||||||
|
if (positionMod8 != 0)
|
||||||
|
{
|
||||||
|
writer.Write(new byte[8 - positionMod8]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteDataElement(BinaryWriter writer, DataType type, byte[] data)
|
||||||
{
|
{
|
||||||
if (data.Length > 4)
|
if (data.Length > 4)
|
||||||
{
|
{
|
||||||
@ -139,13 +152,13 @@ namespace MatFileHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteDimensions(BinaryWriter writer, int[] dimensions)
|
private void WriteDimensions(BinaryWriter writer, int[] dimensions)
|
||||||
{
|
{
|
||||||
var buffer = ConvertToByteArray(dimensions);
|
var buffer = ConvertToByteArray(dimensions);
|
||||||
WriteDataElement(writer, DataType.MiInt32, buffer);
|
WriteDataElement(writer, DataType.MiInt32, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] ConvertToByteArray<T>(T[] data)
|
private byte[] ConvertToByteArray<T>(T[] data)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
@ -189,10 +202,6 @@ namespace MatFileHandler
|
|||||||
{
|
{
|
||||||
size = sizeof(double);
|
size = sizeof(double);
|
||||||
}
|
}
|
||||||
else if (typeof(T) == typeof(bool))
|
|
||||||
{
|
|
||||||
size = sizeof(bool);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
@ -202,51 +211,51 @@ namespace MatFileHandler
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (byte[] real, byte[] imaginary) ConvertToPairOfByteArrays<T>(ComplexOf<T>[] data)
|
private (byte[] real, byte[] imaginary) ConvertToPairOfByteArrays<T>(ComplexOf<T>[] data)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
return (ConvertToByteArray(data.Select(x => x.Real).ToArray()),
|
return (ConvertToByteArray(data.Select(x => x.Real).ToArray()),
|
||||||
ConvertToByteArray(data.Select(x => x.Imaginary).ToArray()));
|
ConvertToByteArray(data.Select(x => x.Imaginary).ToArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (byte[] real, byte[] imaginary) ConvertToPairOfByteArrays(Complex[] data)
|
private (byte[] real, byte[] imaginary) ConvertToPairOfByteArrays(Complex[] data)
|
||||||
{
|
{
|
||||||
return (ConvertToByteArray(data.Select(x => x.Real).ToArray()),
|
return (ConvertToByteArray(data.Select(x => x.Real).ToArray()),
|
||||||
ConvertToByteArray(data.Select(x => x.Imaginary).ToArray()));
|
ConvertToByteArray(data.Select(x => x.Imaginary).ToArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteComplexValues(BinaryWriter writer, DataType type, (byte[] real, byte[] complex) data)
|
private void WriteComplexValues(BinaryWriter writer, DataType type, (byte[] real, byte[] complex) data)
|
||||||
{
|
{
|
||||||
WriteDataElement(writer, type, data.real);
|
WriteDataElement(writer, type, data.real);
|
||||||
WriteDataElement(writer, type, data.complex);
|
WriteDataElement(writer, type, data.complex);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteArrayFlags(BinaryWriter writer, ArrayFlags flags)
|
private void WriteArrayFlags(BinaryWriter writer, ArrayFlags flags)
|
||||||
{
|
{
|
||||||
var flag = (byte)flags.Variable;
|
var flag = (byte)flags.Variable;
|
||||||
WriteTag(writer, new Tag(DataType.MiUInt32, 8));
|
WriteTag(writer, new Tag(DataType.MiUInt32, 8));
|
||||||
writer.Write((byte)flags.Class);
|
writer.Write((byte)flags.Class);
|
||||||
writer.Write(flag);
|
writer.Write(flag);
|
||||||
writer.Write([0, 0, 0, 0, 0, 0]);
|
writer.Write(new byte[] { 0, 0, 0, 0, 0, 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteSparseArrayFlags(BinaryWriter writer, SparseArrayFlags flags)
|
private void WriteSparseArrayFlags(BinaryWriter writer, SparseArrayFlags flags)
|
||||||
{
|
{
|
||||||
var flag = (byte)flags.ArrayFlags.Variable;
|
var flag = (byte)flags.ArrayFlags.Variable;
|
||||||
WriteTag(writer, new Tag(DataType.MiUInt32, 8));
|
WriteTag(writer, new Tag(DataType.MiUInt32, 8));
|
||||||
writer.Write((byte)flags.ArrayFlags.Class);
|
writer.Write((byte)flags.ArrayFlags.Class);
|
||||||
writer.Write(flag);
|
writer.Write(flag);
|
||||||
writer.Write([0, 0]);
|
writer.Write(new byte[] { 0, 0 });
|
||||||
writer.Write(flags.NzMax);
|
writer.Write(flags.NzMax);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteName(BinaryWriter writer, string name)
|
private void WriteName(BinaryWriter writer, string name)
|
||||||
{
|
{
|
||||||
var nameBytes = Encoding.ASCII.GetBytes(name);
|
var nameBytes = Encoding.ASCII.GetBytes(name);
|
||||||
WriteDataElement(writer, DataType.MiInt8, nameBytes);
|
WriteDataElement(writer, DataType.MiInt8, nameBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteNumericalArrayValues(BinaryWriter writer, IArray value)
|
private void WriteNumericalArrayValues(BinaryWriter writer, IArray value)
|
||||||
{
|
{
|
||||||
switch (value)
|
switch (value)
|
||||||
{
|
{
|
||||||
@ -281,7 +290,10 @@ namespace MatFileHandler
|
|||||||
WriteDataElement(writer, DataType.MiDouble, ConvertToByteArray(doubleArray.Data));
|
WriteDataElement(writer, DataType.MiDouble, ConvertToByteArray(doubleArray.Data));
|
||||||
break;
|
break;
|
||||||
case IArrayOf<bool> boolArray:
|
case IArrayOf<bool> boolArray:
|
||||||
WriteDataElement(writer, DataType.MiUInt8, ConvertToByteArray(boolArray.Data));
|
WriteDataElement(
|
||||||
|
writer,
|
||||||
|
DataType.MiUInt8,
|
||||||
|
boolArray.Data.Select(element => element ? (byte)1 : (byte)0).ToArray());
|
||||||
break;
|
break;
|
||||||
case IArrayOf<ComplexOf<sbyte>> complexSbyteArray:
|
case IArrayOf<ComplexOf<sbyte>> complexSbyteArray:
|
||||||
WriteComplexValues(writer, DataType.MiInt8, ConvertToPairOfByteArrays(complexSbyteArray.Data));
|
WriteComplexValues(writer, DataType.MiInt8, ConvertToPairOfByteArrays(complexSbyteArray.Data));
|
||||||
@ -318,39 +330,63 @@ namespace MatFileHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ArrayFlags GetArrayFlags(IArray array, bool isGlobal)
|
private ArrayFlags GetArrayFlags(IArray array, bool isGlobal)
|
||||||
{
|
{
|
||||||
var variableFlags = isGlobal ? Variable.IsGlobal : 0;
|
var variableFlags = isGlobal ? Variable.IsGlobal : 0;
|
||||||
return array switch
|
switch (array)
|
||||||
{
|
{
|
||||||
IArrayOf<sbyte> => new ArrayFlags(ArrayType.MxInt8, variableFlags),
|
case IArrayOf<sbyte> _:
|
||||||
IArrayOf<byte> => new ArrayFlags(ArrayType.MxUInt8, variableFlags),
|
return new ArrayFlags(ArrayType.MxInt8, variableFlags);
|
||||||
IArrayOf<short> => new ArrayFlags(ArrayType.MxInt16, variableFlags),
|
case IArrayOf<byte> _:
|
||||||
IArrayOf<ushort> => new ArrayFlags(ArrayType.MxUInt16, variableFlags),
|
return new ArrayFlags(ArrayType.MxUInt8, variableFlags);
|
||||||
IArrayOf<int> => new ArrayFlags(ArrayType.MxInt32, variableFlags),
|
case IArrayOf<short> _:
|
||||||
IArrayOf<uint> => new ArrayFlags(ArrayType.MxUInt32, variableFlags),
|
return new ArrayFlags(ArrayType.MxInt16, variableFlags);
|
||||||
IArrayOf<long> => new ArrayFlags(ArrayType.MxInt64, variableFlags),
|
case IArrayOf<ushort> _:
|
||||||
IArrayOf<ulong> => new ArrayFlags(ArrayType.MxUInt64, variableFlags),
|
return new ArrayFlags(ArrayType.MxUInt16, variableFlags);
|
||||||
IArrayOf<float> => new ArrayFlags(ArrayType.MxSingle, variableFlags),
|
case IArrayOf<int> _:
|
||||||
IArrayOf<double> => new ArrayFlags(ArrayType.MxDouble, variableFlags),
|
return new ArrayFlags(ArrayType.MxInt32, variableFlags);
|
||||||
IArrayOf<bool> => new ArrayFlags(ArrayType.MxUInt8, variableFlags | Variable.IsLogical),
|
case IArrayOf<uint> _:
|
||||||
IArrayOf<ComplexOf<sbyte>> => new ArrayFlags(ArrayType.MxInt8, variableFlags | Variable.IsComplex),
|
return new ArrayFlags(ArrayType.MxUInt32, variableFlags);
|
||||||
IArrayOf<ComplexOf<byte>> => new ArrayFlags(ArrayType.MxUInt8, variableFlags | Variable.IsComplex),
|
case IArrayOf<long> _:
|
||||||
IArrayOf<ComplexOf<short>> => new ArrayFlags(ArrayType.MxInt16, variableFlags | Variable.IsComplex),
|
return new ArrayFlags(ArrayType.MxInt64, variableFlags);
|
||||||
IArrayOf<ComplexOf<ushort>> => new ArrayFlags(ArrayType.MxUInt16, variableFlags | Variable.IsComplex),
|
case IArrayOf<ulong> _:
|
||||||
IArrayOf<ComplexOf<int>> => new ArrayFlags(ArrayType.MxInt32, variableFlags | Variable.IsComplex),
|
return new ArrayFlags(ArrayType.MxUInt64, variableFlags);
|
||||||
IArrayOf<ComplexOf<uint>> => new ArrayFlags(ArrayType.MxUInt32, variableFlags | Variable.IsComplex),
|
case IArrayOf<float> _:
|
||||||
IArrayOf<ComplexOf<long>> => new ArrayFlags(ArrayType.MxInt64, variableFlags | Variable.IsComplex),
|
return new ArrayFlags(ArrayType.MxSingle, variableFlags);
|
||||||
IArrayOf<ComplexOf<ulong>> => new ArrayFlags(ArrayType.MxUInt64, variableFlags | Variable.IsComplex),
|
case IArrayOf<double> _:
|
||||||
IArrayOf<ComplexOf<float>> => new ArrayFlags(ArrayType.MxSingle, variableFlags | Variable.IsComplex),
|
return new ArrayFlags(ArrayType.MxDouble, variableFlags);
|
||||||
IArrayOf<Complex> => new ArrayFlags(ArrayType.MxDouble, variableFlags | Variable.IsComplex),
|
case IArrayOf<bool> _:
|
||||||
IStructureArray => new ArrayFlags(ArrayType.MxStruct, variableFlags),
|
return new ArrayFlags(ArrayType.MxUInt8, variableFlags | Variable.IsLogical);
|
||||||
ICellArray => new ArrayFlags(ArrayType.MxCell, variableFlags),
|
case IArrayOf<ComplexOf<sbyte>> _:
|
||||||
_ => throw new NotSupportedException(),
|
return new ArrayFlags(ArrayType.MxInt8, variableFlags | Variable.IsComplex);
|
||||||
};
|
case IArrayOf<ComplexOf<byte>> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxUInt8, variableFlags | Variable.IsComplex);
|
||||||
|
case IArrayOf<ComplexOf<short>> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxInt16, variableFlags | Variable.IsComplex);
|
||||||
|
case IArrayOf<ComplexOf<ushort>> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxUInt16, variableFlags | Variable.IsComplex);
|
||||||
|
case IArrayOf<ComplexOf<int>> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxInt32, variableFlags | Variable.IsComplex);
|
||||||
|
case IArrayOf<ComplexOf<uint>> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxUInt32, variableFlags | Variable.IsComplex);
|
||||||
|
case IArrayOf<ComplexOf<long>> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxInt64, variableFlags | Variable.IsComplex);
|
||||||
|
case IArrayOf<ComplexOf<ulong>> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxUInt64, variableFlags | Variable.IsComplex);
|
||||||
|
case IArrayOf<ComplexOf<float>> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxSingle, variableFlags | Variable.IsComplex);
|
||||||
|
case IArrayOf<Complex> _:
|
||||||
|
return new ArrayFlags(ArrayType.MxDouble, variableFlags | Variable.IsComplex);
|
||||||
|
case IStructureArray _:
|
||||||
|
return new ArrayFlags(ArrayType.MxStruct, variableFlags);
|
||||||
|
case ICellArray _:
|
||||||
|
return new ArrayFlags(ArrayType.MxCell, variableFlags);
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SparseArrayFlags GetSparseArrayFlags<T>(ISparseArrayOf<T> array, bool isGlobal, uint nonZero)
|
private SparseArrayFlags GetSparseArrayFlags<T>(ISparseArrayOf<T> array, bool isGlobal, uint nonZero)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
var flags = GetArrayFlags(array, isGlobal);
|
var flags = GetArrayFlags(array, isGlobal);
|
||||||
@ -365,12 +401,12 @@ namespace MatFileHandler
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ArrayFlags GetCharArrayFlags(bool isGlobal)
|
private ArrayFlags GetCharArrayFlags(bool isGlobal)
|
||||||
{
|
{
|
||||||
return new ArrayFlags(ArrayType.MxChar, isGlobal ? Variable.IsGlobal : 0);
|
return new ArrayFlags(ArrayType.MxChar, isGlobal ? Variable.IsGlobal : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteWrappingContents<T>(
|
private void WriteWrappingContents<T>(
|
||||||
BinaryWriter writer,
|
BinaryWriter writer,
|
||||||
T array,
|
T array,
|
||||||
Action<FakeWriter> lengthCalculator,
|
Action<FakeWriter> lengthCalculator,
|
||||||
@ -390,7 +426,7 @@ namespace MatFileHandler
|
|||||||
writeContents(writer);
|
writeContents(writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteNumericalArrayContents(BinaryWriter writer, IArray array, string name, bool isGlobal)
|
private void WriteNumericalArrayContents(BinaryWriter writer, IArray array, string name, bool isGlobal)
|
||||||
{
|
{
|
||||||
WriteArrayFlags(writer, GetArrayFlags(array, isGlobal));
|
WriteArrayFlags(writer, GetArrayFlags(array, isGlobal));
|
||||||
WriteDimensions(writer, array.Dimensions);
|
WriteDimensions(writer, array.Dimensions);
|
||||||
@ -398,7 +434,7 @@ namespace MatFileHandler
|
|||||||
WriteNumericalArrayValues(writer, array);
|
WriteNumericalArrayValues(writer, array);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteNumericalArray(
|
private void WriteNumericalArray(
|
||||||
BinaryWriter writer,
|
BinaryWriter writer,
|
||||||
IArray numericalArray,
|
IArray numericalArray,
|
||||||
string name = "",
|
string name = "",
|
||||||
@ -411,7 +447,7 @@ namespace MatFileHandler
|
|||||||
contentsWriter => { WriteNumericalArrayContents(contentsWriter, numericalArray, name, isGlobal); });
|
contentsWriter => { WriteNumericalArrayContents(contentsWriter, numericalArray, name, isGlobal); });
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteCharArrayContents(BinaryWriter writer, ICharArray charArray, string name, bool isGlobal)
|
private void WriteCharArrayContents(BinaryWriter writer, ICharArray charArray, string name, bool isGlobal)
|
||||||
{
|
{
|
||||||
WriteArrayFlags(writer, GetCharArrayFlags(isGlobal));
|
WriteArrayFlags(writer, GetCharArrayFlags(isGlobal));
|
||||||
WriteDimensions(writer, charArray.Dimensions);
|
WriteDimensions(writer, charArray.Dimensions);
|
||||||
@ -420,7 +456,7 @@ namespace MatFileHandler
|
|||||||
WriteDataElement(writer, DataType.MiUtf16, ConvertToByteArray(array));
|
WriteDataElement(writer, DataType.MiUtf16, ConvertToByteArray(array));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteCharArray(BinaryWriter writer, ICharArray charArray, string name, bool isGlobal)
|
private void WriteCharArray(BinaryWriter writer, ICharArray charArray, string name, bool isGlobal)
|
||||||
{
|
{
|
||||||
WriteWrappingContents(
|
WriteWrappingContents(
|
||||||
writer,
|
writer,
|
||||||
@ -429,7 +465,7 @@ namespace MatFileHandler
|
|||||||
contentsWriter => { WriteCharArrayContents(contentsWriter, charArray, name, isGlobal); });
|
contentsWriter => { WriteCharArrayContents(contentsWriter, charArray, name, isGlobal); });
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteSparseArrayValues<T>(
|
private void WriteSparseArrayValues<T>(
|
||||||
BinaryWriter writer, int[] rows, int[] columns, T[] data)
|
BinaryWriter writer, int[] rows, int[] columns, T[] data)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
@ -439,8 +475,9 @@ namespace MatFileHandler
|
|||||||
{
|
{
|
||||||
WriteDataElement(writer, DataType.MiDouble, ConvertToByteArray(data));
|
WriteDataElement(writer, DataType.MiDouble, ConvertToByteArray(data));
|
||||||
}
|
}
|
||||||
else if (data is Complex[] complexData)
|
else if (data is Complex[])
|
||||||
{
|
{
|
||||||
|
var complexData = data as Complex[];
|
||||||
WriteDataElement(
|
WriteDataElement(
|
||||||
writer,
|
writer,
|
||||||
DataType.MiDouble,
|
DataType.MiDouble,
|
||||||
@ -450,16 +487,17 @@ namespace MatFileHandler
|
|||||||
DataType.MiDouble,
|
DataType.MiDouble,
|
||||||
ConvertToByteArray(complexData.Select(c => c.Imaginary).ToArray()));
|
ConvertToByteArray(complexData.Select(c => c.Imaginary).ToArray()));
|
||||||
}
|
}
|
||||||
else if (data is bool[] boolData)
|
else if (data is bool[])
|
||||||
{
|
{
|
||||||
|
var boolData = data as bool[];
|
||||||
WriteDataElement(
|
WriteDataElement(
|
||||||
writer,
|
writer,
|
||||||
DataType.MiUInt8,
|
DataType.MiUInt8,
|
||||||
ConvertToByteArray(boolData));
|
boolData.Select(element => element ? (byte)1 : (byte)0).ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (int[] rowIndex, int[] columnIndex, T[] data, uint nonZero) PrepareSparseArrayData<T>(
|
private (int[] rowIndex, int[] columnIndex, T[] data, uint nonZero) PrepareSparseArrayData<T>(
|
||||||
ISparseArrayOf<T> array)
|
ISparseArrayOf<T> array)
|
||||||
where T : struct, IEquatable<T>
|
where T : struct, IEquatable<T>
|
||||||
{
|
{
|
||||||
@ -482,7 +520,7 @@ namespace MatFileHandler
|
|||||||
return (rowIndexList.ToArray(), columnIndex, valuesList.ToArray(), (uint)rowIndexList.Count);
|
return (rowIndexList.ToArray(), columnIndex, valuesList.ToArray(), (uint)rowIndexList.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteSparseArrayContents<T>(
|
private void WriteSparseArrayContents<T>(
|
||||||
BinaryWriter writer,
|
BinaryWriter writer,
|
||||||
ISparseArrayOf<T> array,
|
ISparseArrayOf<T> array,
|
||||||
string name,
|
string name,
|
||||||
@ -496,7 +534,7 @@ namespace MatFileHandler
|
|||||||
WriteSparseArrayValues(writer, rows, columns, data);
|
WriteSparseArrayValues(writer, rows, columns, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteSparseArray<T>(BinaryWriter writer, ISparseArrayOf<T> sparseArray, string name, bool isGlobal)
|
private void WriteSparseArray<T>(BinaryWriter writer, ISparseArrayOf<T> sparseArray, string name, bool isGlobal)
|
||||||
where T : unmanaged, IEquatable<T>
|
where T : unmanaged, IEquatable<T>
|
||||||
{
|
{
|
||||||
WriteWrappingContents(
|
WriteWrappingContents(
|
||||||
@ -506,11 +544,11 @@ namespace MatFileHandler
|
|||||||
contentsWriter => { WriteSparseArrayContents(contentsWriter, sparseArray, name, isGlobal); });
|
contentsWriter => { WriteSparseArrayContents(contentsWriter, sparseArray, name, isGlobal); });
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteFieldNames(BinaryWriter writer, IEnumerable<string> fieldNames)
|
private void WriteFieldNames(BinaryWriter writer, IEnumerable<string> fieldNames)
|
||||||
{
|
{
|
||||||
var fieldNamesArray = fieldNames.Select(name => Encoding.ASCII.GetBytes(name)).ToArray();
|
var fieldNamesArray = fieldNames.Select(name => Encoding.ASCII.GetBytes(name)).ToArray();
|
||||||
var maxFieldName = fieldNamesArray.Max(name => name.Length) + 1;
|
var maxFieldName = fieldNamesArray.Select(name => name.Length).Max() + 1;
|
||||||
WriteDataElement(writer, DataType.MiInt32, ConvertToByteArray([maxFieldName]));
|
WriteDataElement(writer, DataType.MiInt32, ConvertToByteArray(new[] { maxFieldName }));
|
||||||
var buffer = new byte[fieldNamesArray.Length * maxFieldName];
|
var buffer = new byte[fieldNamesArray.Length * maxFieldName];
|
||||||
var startPosition = 0;
|
var startPosition = 0;
|
||||||
foreach (var name in fieldNamesArray)
|
foreach (var name in fieldNamesArray)
|
||||||
@ -643,21 +681,24 @@ namespace MatFileHandler
|
|||||||
|
|
||||||
private void WriteCompressedVariableToUnseekableStream(BinaryWriter writer, IVariable variable)
|
private void WriteCompressedVariableToUnseekableStream(BinaryWriter writer, IVariable variable)
|
||||||
{
|
{
|
||||||
using var compressedStream = new MemoryStream();
|
using (var compressedStream = new MemoryStream())
|
||||||
|
{
|
||||||
uint crc;
|
uint crc;
|
||||||
using (var originalStream = new MemoryStream())
|
using (var originalStream = new MemoryStream())
|
||||||
{
|
{
|
||||||
using var internalWriter = new BinaryWriter(originalStream);
|
using (var internalWriter = new BinaryWriter(originalStream))
|
||||||
|
{
|
||||||
WriteVariable(internalWriter, variable);
|
WriteVariable(internalWriter, variable);
|
||||||
originalStream.Position = 0;
|
originalStream.Position = 0;
|
||||||
crc = CalculateAdler32Checksum(originalStream);
|
crc = CalculateAdler32Checksum(originalStream);
|
||||||
originalStream.Position = 0;
|
originalStream.Position = 0;
|
||||||
using var compressionStream = new DeflateStream(
|
using (var compressionStream =
|
||||||
compressedStream,
|
new DeflateStream(compressedStream, CompressionMode.Compress, leaveOpen: true))
|
||||||
CompressionMode.Compress,
|
{
|
||||||
leaveOpen: true);
|
|
||||||
originalStream.CopyTo(compressionStream);
|
originalStream.CopyTo(compressionStream);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
compressedStream.Position = 0;
|
compressedStream.Position = 0;
|
||||||
WriteTag(writer, new Tag(DataType.MiCompressed, (int)(compressedStream.Length + 6)));
|
WriteTag(writer, new Tag(DataType.MiCompressed, (int)(compressedStream.Length + 6)));
|
||||||
writer.Write((byte)0x78);
|
writer.Write((byte)0x78);
|
||||||
@ -666,4 +707,5 @@ namespace MatFileHandler
|
|||||||
writer.Write(BitConverter.GetBytes(crc).Reverse().ToArray());
|
writer.Write(BitConverter.GetBytes(crc).Reverse().ToArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Options for writing .mat files.
|
/// Options for writing .mat files.
|
||||||
@ -8,7 +10,7 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets default options.
|
/// Gets default options.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static MatFileWriterOptions Default => new()
|
public static MatFileWriterOptions Default => new MatFileWriterOptions
|
||||||
{
|
{
|
||||||
UseCompression = CompressionUsage.Always,
|
UseCompression = CompressionUsage.Always,
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
@ -47,7 +49,7 @@ namespace MatFileHandler
|
|||||||
get
|
get
|
||||||
{
|
{
|
||||||
var rowAndColumn = GetRowAndColumn(list);
|
var rowAndColumn = GetRowAndColumn(list);
|
||||||
return DataDictionary.TryGetValue(rowAndColumn, out var result) ? result : default;
|
return DataDictionary.ContainsKey(rowAndColumn) ? DataDictionary[rowAndColumn] : default(T);
|
||||||
}
|
}
|
||||||
set => DataDictionary[GetRowAndColumn(list)] = value;
|
set => DataDictionary[GetRowAndColumn(list)] = value;
|
||||||
}
|
}
|
||||||
@ -59,7 +61,7 @@ namespace MatFileHandler
|
|||||||
public override double[] ConvertToDoubleArray()
|
public override double[] ConvertToDoubleArray()
|
||||||
{
|
{
|
||||||
var data = ((IArrayOf<T>)this).Data;
|
var data = ((IArrayOf<T>)this).Data;
|
||||||
return data as double[] ?? data.Select(x => Convert.ToDouble(x, System.Globalization.CultureInfo.InvariantCulture)).ToArray();
|
return data as double[] ?? data.Select(x => Convert.ToDouble(x)).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@ -73,7 +75,7 @@ namespace MatFileHandler
|
|||||||
var result = new double[Dimensions[0], Dimensions[1]];
|
var result = new double[Dimensions[0], Dimensions[1]];
|
||||||
foreach (var pair in Data)
|
foreach (var pair in Data)
|
||||||
{
|
{
|
||||||
result[pair.Key.row, pair.Key.column] = Convert.ToDouble(pair.Value, System.Globalization.CultureInfo.InvariantCulture);
|
result[pair.Key.row, pair.Key.column] = Convert.ToDouble(pair.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -91,12 +93,15 @@ namespace MatFileHandler
|
|||||||
|
|
||||||
private (int row, int column) GetRowAndColumn(int[] indices)
|
private (int row, int column) GetRowAndColumn(int[] indices)
|
||||||
{
|
{
|
||||||
return indices.Length switch
|
switch (indices.Length)
|
||||||
{
|
{
|
||||||
1 => (indices[0] % Dimensions[0], indices[0] / Dimensions[0]),
|
case 1:
|
||||||
2 => (indices[0], indices[1]),
|
return (indices[0] % Dimensions[0], indices[0] / Dimensions[0]);
|
||||||
_ => throw new NotSupportedException("Invalid index for sparse array."),
|
case 2:
|
||||||
};
|
return (indices[0], indices[1]);
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException("Invalid index for sparse array.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -67,7 +69,7 @@ namespace MatFileHandler
|
|||||||
"Cannot set structure elements via this[params int[]] indexer. Use this[string, int[]] instead.");
|
"Cannot set structure elements via this[params int[]] indexer. Use this[string, int[]] instead.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private MatStructureArrayElement ExtractStructure(int i)
|
private IReadOnlyDictionary<string, IArray> ExtractStructure(int i)
|
||||||
{
|
{
|
||||||
return new MatStructureArrayElement(this, i);
|
return new MatStructureArrayElement(this, i);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
internal class MatVariable : IVariable
|
internal class MatVariable : IVariable
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A matrix of type T.
|
/// A matrix of type T.
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
namespace MatFileHandler;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Parser for object data.
|
|
||||||
/// </summary>
|
|
||||||
internal static class ObjectParser
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Parse object data.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="uintArray">Opaque link array.</param>
|
|
||||||
/// <param name="subsystemData">Current subsystem data.</param>
|
|
||||||
/// <returns>Parsed object.</returns>
|
|
||||||
public static IArray ParseObject(MatNumericalArrayOf<uint> uintArray, SubsystemData subsystemData)
|
|
||||||
{
|
|
||||||
var (dimensions, indexToObjectId, classIndex) = DataElementReader.ParseOpaqueData(uintArray.Data);
|
|
||||||
return new OpaqueLink(
|
|
||||||
uintArray.Name,
|
|
||||||
string.Empty,
|
|
||||||
string.Empty,
|
|
||||||
dimensions,
|
|
||||||
uintArray,
|
|
||||||
indexToObjectId,
|
|
||||||
classIndex,
|
|
||||||
subsystemData);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -76,7 +78,15 @@ namespace MatFileHandler
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IArray this[string field, params int[] list]
|
public IArray this[string field, params int[] list]
|
||||||
{
|
{
|
||||||
get => TryGetValue(field, out var result, list) ? result! : throw new ArgumentOutOfRangeException(nameof(list));
|
get
|
||||||
|
{
|
||||||
|
if (TryGetValue(field, out var result, list))
|
||||||
|
{
|
||||||
|
return result!;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IndexOutOfRangeException();
|
||||||
|
}
|
||||||
set => throw new NotImplementedException();
|
set => throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,7 +97,7 @@ namespace MatFileHandler
|
|||||||
set => throw new NotImplementedException();
|
set => throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private OpaqueObjectArrayElement ExtractObject(int i)
|
private IReadOnlyDictionary<string, IArray> ExtractObject(int i)
|
||||||
{
|
{
|
||||||
return new OpaqueObjectArrayElement(this, i);
|
return new OpaqueObjectArrayElement(this, i);
|
||||||
}
|
}
|
||||||
@ -96,7 +106,7 @@ namespace MatFileHandler
|
|||||||
{
|
{
|
||||||
var index = Dimensions.DimFlatten(list);
|
var index = Dimensions.DimFlatten(list);
|
||||||
var maybeFieldIndex = SubsystemData.ClassInformation[ClassIndex].FindField(field);
|
var maybeFieldIndex = SubsystemData.ClassInformation[ClassIndex].FindField(field);
|
||||||
if (maybeFieldIndex is not int fieldIndex)
|
if (!(maybeFieldIndex is int fieldIndex))
|
||||||
{
|
{
|
||||||
output = default;
|
output = default;
|
||||||
return false;
|
return false;
|
||||||
@ -161,8 +171,8 @@ namespace MatFileHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning disable CS8767
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
#pragma warning disable CS8767
|
||||||
public bool TryGetValue(string key, out IArray? value)
|
public bool TryGetValue(string key, out IArray? value)
|
||||||
#pragma warning restore CS8767
|
#pragma warning restore CS8767
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace MatFileHandler;
|
namespace MatFileHandler;
|
||||||
@ -46,7 +48,7 @@ internal sealed class PositionTrackingStream : Stream
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override int Read(byte[] buffer, int offset, int count)
|
public override int Read(byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
var bytesRead = _baseStream.Read(buffer, offset, count);
|
int bytesRead = _baseStream.Read(buffer, offset, count);
|
||||||
|
|
||||||
_position += bytesRead;
|
_position += bytesRead;
|
||||||
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
@ -8,6 +10,7 @@ namespace MatFileHandler
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class StringAdapter
|
public class StringAdapter
|
||||||
{
|
{
|
||||||
|
private readonly int[] dimensions;
|
||||||
private readonly string[] strings;
|
private readonly string[] strings;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -25,13 +28,13 @@ namespace MatFileHandler
|
|||||||
var binaryData = matObject["any", 0] as IArrayOf<ulong>
|
var binaryData = matObject["any", 0] as IArrayOf<ulong>
|
||||||
?? throw new HandlerException("Cannot extract string data.");
|
?? throw new HandlerException("Cannot extract string data.");
|
||||||
|
|
||||||
(Dimensions, strings) = ParseBinaryData(binaryData.Data);
|
(dimensions, strings) = ParseBinaryData(binaryData.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets string array dimensions.
|
/// Gets string array dimensions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int[] Dimensions { get; }
|
public int[] Dimensions => dimensions;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets string object at given position.
|
/// Gets string object at given position.
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
@ -47,7 +49,7 @@ namespace MatFileHandler
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override int Read(byte[] buffer, int offset, int count)
|
public override int Read(byte[] buffer, int offset, int count)
|
||||||
{
|
{
|
||||||
var bytesRead = _baseStream.Read(buffer, offset, (int)Math.Min(count, Length - _bytesRead));
|
int bytesRead = _baseStream.Read(buffer, offset, (int)Math.Min(count, Length - _bytesRead));
|
||||||
|
|
||||||
_bytesRead += bytesRead;
|
_bytesRead += bytesRead;
|
||||||
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
@ -117,7 +119,12 @@ namespace MatFileHandler
|
|||||||
/// <returns>Field index.</returns>
|
/// <returns>Field index.</returns>
|
||||||
public int? FindField(string fieldName)
|
public int? FindField(string fieldName)
|
||||||
{
|
{
|
||||||
return fieldToIndex.TryGetValue(fieldName, out var index) ? index : null;
|
if (fieldToIndex.TryGetValue(fieldName, out var index))
|
||||||
|
{
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -314,11 +317,21 @@ namespace MatFileHandler
|
|||||||
|
|
||||||
private static IArray TransformOpaqueData(IArray array, SubsystemData subsystemData)
|
private static IArray TransformOpaqueData(IArray array, SubsystemData subsystemData)
|
||||||
{
|
{
|
||||||
if (array is MatNumericalArrayOf<uint> uintArray &&
|
if (array is MatNumericalArrayOf<uint> uintArray)
|
||||||
uintArray.Data.Length == 6 &&
|
|
||||||
uintArray.Data[0] == 3707764736u)
|
|
||||||
{
|
{
|
||||||
return ObjectParser.ParseObject(uintArray, subsystemData);
|
if (uintArray.Data[0] == 3707764736u)
|
||||||
|
{
|
||||||
|
var (dimensions, indexToObjectId, classIndex) = DataElementReader.ParseOpaqueData(uintArray.Data);
|
||||||
|
return new OpaqueLink(
|
||||||
|
uintArray.Name,
|
||||||
|
string.Empty,
|
||||||
|
string.Empty,
|
||||||
|
dimensions,
|
||||||
|
uintArray,
|
||||||
|
indexToObjectId,
|
||||||
|
classIndex,
|
||||||
|
subsystemData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array is MatCellArray cellArray)
|
if (array is MatCellArray cellArray)
|
||||||
@ -331,30 +344,10 @@ namespace MatFileHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array is MatStructureArray structureArray)
|
|
||||||
{
|
|
||||||
var newFields = new Dictionary<string, List<IArray>>();
|
|
||||||
foreach (var pair in structureArray.Fields)
|
|
||||||
{
|
|
||||||
var values = pair.Value;
|
|
||||||
var transformedValues = new List<IArray>(values.Count);
|
|
||||||
foreach (var value in values)
|
|
||||||
{
|
|
||||||
var transformedValue = TransformOpaqueData(value, subsystemData);
|
|
||||||
transformedValues.Add(transformedValue);
|
|
||||||
}
|
|
||||||
newFields[pair.Key] = transformedValues;
|
|
||||||
}
|
|
||||||
foreach (var pair in newFields)
|
|
||||||
{
|
|
||||||
structureArray.Fields[pair.Key] = pair.Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly struct ObjectClassInformation
|
private struct ObjectClassInformation
|
||||||
{
|
{
|
||||||
public ObjectClassInformation(int embeddedObjectPosition, int objectPosition, int loadingOrder, int classId)
|
public ObjectClassInformation(int embeddedObjectPosition, int objectPosition, int loadingOrder, int classId)
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace MatFileHandler
|
namespace MatFileHandler
|
||||||
@ -85,9 +87,9 @@ namespace MatFileHandler
|
|||||||
.Where(i => VariableNames[i] == variableName)
|
.Where(i => VariableNames[i] == variableName)
|
||||||
.Select(i => (int?)i)
|
.Select(i => (int?)i)
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
if (maybeIndex is not int index)
|
if (!(maybeIndex is int index))
|
||||||
{
|
{
|
||||||
throw new ArgumentOutOfRangeException(nameof(variableName), $"Variable '{variableName}' not found.");
|
throw new IndexOutOfRangeException($"Variable '{variableName}' not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = matObject["data"] as ICellArray
|
var data = matObject["data"] as ICellArray
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace MatFileHandler
|
// Copyright 2017-2018 Alexander Luzgarev
|
||||||
|
|
||||||
|
namespace MatFileHandler
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Type of the data attached to the tag.
|
/// Type of the data attached to the tag.
|
||||||
|
@ -15,7 +15,7 @@ You can find (partial) technical description of MATLAB object data format
|
|||||||
This document briefly describes how to perform simple operations with .mat files
|
This document briefly describes how to perform simple operations with .mat files
|
||||||
using MatFileHandler.
|
using MatFileHandler.
|
||||||
|
|
||||||
If you have questions and/or ideas, you can [file a new issue](https://git.mahalex.net/mahalex/MatFileHandler/issues/new)
|
If you have questions and/or ideas, you can [file a new issue](https://github.com/mahalex/MatFileHandler/issues/new)
|
||||||
or contact me directly at <mahalex@gmail.com>.
|
or contact me directly at <mahalex@gmail.com>.
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
@ -11,9 +11,9 @@ variables:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- task: UseDotNet@2
|
- task: UseDotNet@2
|
||||||
displayName: 'Use .NET Core SDK 9.x'
|
displayName: 'Use .NET Core SDK 8.x'
|
||||||
inputs:
|
inputs:
|
||||||
version: 9.x
|
version: 8.x
|
||||||
|
|
||||||
- script: dotnet build --configuration $(buildConfiguration)
|
- script: dotnet build --configuration $(buildConfiguration)
|
||||||
displayName: 'dotnet build $(buildConfiguration)'
|
displayName: 'dotnet build $(buildConfiguration)'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user