예제 #1
0
        /// <summary>
        /// Read a sequence of raw variables from .mat file.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <param name="subsystemDataOffset">Offset of subsystem data in the file;
        /// we need it because we may encounter it during reading, and
        /// the subsystem data should be parsed in a special way.</param>
        /// <param name="subsystemData">
        /// Link to the current file's subsystem data structure; initially it has dummy value
        /// which will be replaced after we parse the whole subsystem data.</param>
        /// <returns>List of "raw" variables; the actual variables are constructed from them later.</returns>
        internal static List<RawVariable> ReadRawVariables(BinaryReader reader, long subsystemDataOffset, SubsystemData subsystemData)
        {
            var variables = new List<RawVariable>();
            var dataElementReader = new DataElementReader(subsystemData);
            while (true)
            {
                try
                {
                    var position = reader.BaseStream.Position;
                    var dataElement = dataElementReader.Read(reader);
                    if (position == subsystemDataOffset)
                    {
                        var subsystemDataElement = dataElement as IArrayOf<byte>
                            ?? throw new HandlerException("Cannot parse subsystem data element.");
                        var newSubsystemData = ReadSubsystemData(subsystemDataElement.Data, subsystemData);
                        subsystemData.Set(newSubsystemData);
                    }
                    else
                    {
                        variables.Add(new RawVariable(position, dataElement));
                    }
                }
                catch (EndOfStreamException)
                {
                    break;
                }
            }

            return variables;
        }
        private static IArray TransformOpaqueData(IArray array, SubsystemData subsystemData)
        {
            if (array is MatNumericalArrayOf <uint> uintArray)
            {
                if (uintArray.Data[0] == 3707764736u)
                {
                    var(dimensions, indexToObjectId, classIndex) = DataElementReader.ParseOpaqueData(uintArray.Data);
                    return(new OpaqueLink(
                               uintArray.Name,
                               string.Empty,
                               string.Empty,
                               dimensions,
                               array as DataElement,
                               indexToObjectId,
                               classIndex,
                               subsystemData));
                }
            }

            return(array);
        }