protected override T ReadPropertyInternal <T>(PlyProperty expected) { var token = _textReader.ReadToken(); var parsedValue = PlyTypeConverter.ParseStringToNativeType <T>(token, expected.ValueType); return(parsedValue); }
private byte[] ReadBytesFor(PlyType dataType) { var typeSize = PlyTypeConverter.GetTypeSize(dataType); var readBytes = _binaryReader.ReadBytes(typeSize); return(readBytes.FlipIfTrue(_reverseByteOrder)); }
private byte[] GetBytesForArrayPrefix(int arrayLength) { var arrayProperty = (PlyArrayProperty)_iterator.CurrentProperty; var type = arrayProperty.ArraySizeType; EnsureValueFitsType(arrayLength, type); byte[] countAsBytes = PlyTypeConverter.ToBytes(arrayLength, type); return(countAsBytes.FlipIfTrue(_reverseByteOrder)); }
protected override IEnumerable <T> ReadArrayInternal <T>(PlyArrayProperty expected) { var countAsBytes = ReadBytesFor(expected.ArraySizeType); var count = PlyTypeConverter.ArraySizeInBytesToInt(countAsBytes); for (int i = 0; i < count; ++i) { yield return(ReadPropertyInternal <T>(expected)); } }
private void EnsureWriteCallIsValid <T>() { if (_iterator.CurrentProperty is PlyArrayProperty) { throw ExceptionFactory.GetGotArrayWhenValueExpectedException(_iterator.CurrentProperty); } if (PlyTypeConverter.ToNative(_iterator.CurrentProperty.ValueType) != typeof(T)) { throw ExceptionFactory.GetUnexpectedDataTypeException(_iterator.CurrentProperty, typeof(T)); } }
protected override IEnumerable <T> ReadArrayInternal <T>(PlyArrayProperty expected) { var countToken = _textReader.ReadToken(); int count = ParseInt(countToken); for (int i = 0; i < count; ++i) { var valueToken = _textReader.ReadToken(); var parsedValue = PlyTypeConverter.ParseStringToNativeType <T>(valueToken, expected.ValueType); yield return(parsedValue); } }
private void EnsureWriteArrayCallIsValid <T>() { var expected = _iterator.CurrentProperty as PlyArrayProperty; if (expected == null) { throw ExceptionFactory.GetGotValueWhenArrayExpectedException(_iterator.CurrentProperty); } if (PlyTypeConverter.ToNative(expected.ValueType) != typeof(T)) { throw ExceptionFactory.GetUnexpectedDataTypeException(expected, typeof(T)); } }
private PlyProperty ParseValueProperty() { string[] tokens = CurrentLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length != 3) { throw new PlyHeaderReaderException("Unexpected tokens when parsing property.", CurrentLine); } try { var name = tokens[2]; var type = PlyTypeConverter.GetNativeType(tokens[1]); return(new PlyProperty(name, type)); } catch (ArgumentException) { throw new PlyHeaderReaderException("Malformed data type.", CurrentLine); } }
private byte[] GetBytes <T>(T value) where T : IConvertible { var valueAsBytes = PlyTypeConverter.ToBytes(value, _iterator.CurrentProperty.ValueType); return(valueAsBytes.FlipIfTrue(_reverseByteOrder)); }
protected override T ReadPropertyInternal <T>(PlyProperty expected) { var value = ReadBytesFor(expected.ValueType); return(PlyTypeConverter.ParseBytesToNative <T>(value, expected.ValueType)); }
public static string GetHeader(PlyHeader header) { var sb = new StringBuilder(); sb.AppendLine(PlyKeywords.MagicNumber); var formatKeyword = default(string); if (header.Format == PlyFormat.Ascii) { formatKeyword = PlyKeywords.AsciiFormat; } else if (header.Format == PlyFormat.BinaryBigEndian) { formatKeyword = PlyKeywords.BinaryBigEndianFormat; } else if (header.Format == PlyFormat.BinaryLittleEndian) { formatKeyword = PlyKeywords.BinaryLittleEndianFormat; } else { throw new NotSupportedException("The declared header file format is not supported."); } sb.AppendLine($"{formatKeyword} {FormatVersion}"); if (header.Comment != null) { var escapedComment = EscapeNewLines(header.Comment); foreach (string comment in escapedComment) { var commentDeclaration = $"{PlyKeywords.Comment} {comment}"; sb.AppendLine(commentDeclaration); } } if (header.ObjectInfo != null) { var escapedObjectInfo = EscapeNewLines(header.ObjectInfo); foreach (string objectInfo in escapedObjectInfo) { var objectInfoDeclaration = $"{PlyKeywords.ObjectInfo} {objectInfo}"; sb.AppendLine(objectInfoDeclaration); } } foreach (PlyElement element in header.Elements) { var elementDeclaration = $"{PlyKeywords.Element} {element.Name} {element.InstanceCount}"; sb.AppendLine(elementDeclaration); foreach (PlyProperty property in element.Properties) { var valueType = PlyTypeConverter.ToStringRepresentation(property.ValueType); var propertyDeclaration = $"{PlyKeywords.Property} {valueType} {property.Name}"; if (property is PlyArrayProperty arrayProperty) { var arraySizeType = PlyTypeConverter.ToStringRepresentation(arrayProperty.ArraySizeType); propertyDeclaration = $"{PlyKeywords.PropertyList} {arraySizeType} {valueType} {property.Name}"; } sb.AppendLine(propertyDeclaration); } } sb.AppendLine(PlyKeywords.HeaderEnd); return(sb.ToString()); }