<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;

namespace ShapefileSharp.Spec
{
    internal sealed class ShapeField : Field<IShape>
    {
        public ShapeField(WordCount offset) : base(offset)
        {
            ShapeTypeField = new ShapeTypeField(offset);
        }

        private ShapeTypeField ShapeTypeField { get; }

        public override IShape Read(BinaryReader reader, WordCount origin)
        {
            ShapeType shapeType = ShapeTypeField.Read(reader, origin);

            switch (shapeType)
            {
                case ShapeType.NullShape:
                    return new NullShape();

                case ShapeType.Point:
                    {
                        var shapeField = new PointShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader, origin);
                    }

                case ShapeType.MultiPoint:
                    {
                        var shapeField = new MultiPointShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.PolyLine:
                    {
                        var shapeField = new PolyLineShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.Polygon:
                    {
                        var shapeField = new PolygonShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.PointM:
                    {
                        var shapeField = new PointMShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.MultiPointM:
                    {
                        var shapeField = new MultiPointMShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.PointZ:
                    {
                        var shapeField = new PointZShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                default:
                    Debug.Fail(string.Format("Unimplemented ShapeType: {0}", shapeType));
                    throw new NotImplementedException();
            }
        }

        public override void Write(BinaryWriter writer, IShape value, WordCount origin)
        {
            writer.BaseStream.Position = origin.Bytes;
            writer.Write((int)value.ShapeType);

            //TODO: This switch should drive off of the Type somehow...
            //      So then we can serialize a PointZ as a PointM, for example.
            switch (value.ShapeType)
            {
                case ShapeType.NullShape:
                    throw new NotImplementedException();

                case ShapeType.Point:
                    {
                        var shapeField = new PointShapeField(Offset + ShapeTypeField.Length);

                        shapeField.Write(writer, (IPointShape<IPoint>)value); //TODO: Presumptuous cast...
                    }
                    break;

                default:
                    Debug.Fail(string.Format("Unimplemented IShape: {0}", typeof(IShape)));
                    throw new NotImplementedException();
            }
        }
    }
}<commit_msg>* ShapeField -- Write the ShapeType correctly<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;

namespace ShapefileSharp.Spec
{
    internal sealed class ShapeField : Field<IShape>
    {
        public ShapeField(WordCount offset) : base(offset)
        {
            ShapeTypeField = new ShapeTypeField(offset);
        }

        private ShapeTypeField ShapeTypeField { get; }

        public override IShape Read(BinaryReader reader, WordCount origin)
        {
            ShapeType shapeType = ShapeTypeField.Read(reader, origin);

            switch (shapeType)
            {
                case ShapeType.NullShape:
                    return new NullShape();

                case ShapeType.Point:
                    {
                        var shapeField = new PointShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader, origin);
                    }

                case ShapeType.MultiPoint:
                    {
                        var shapeField = new MultiPointShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.PolyLine:
                    {
                        var shapeField = new PolyLineShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.Polygon:
                    {
                        var shapeField = new PolygonShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.PointM:
                    {
                        var shapeField = new PointMShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.MultiPointM:
                    {
                        var shapeField = new MultiPointMShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                case ShapeType.PointZ:
                    {
                        var shapeField = new PointZShapeField(Offset + ShapeTypeField.Length);

                        return shapeField.Read(reader);
                    }

                default:
                    Debug.Fail(string.Format("Unimplemented ShapeType: {0}", shapeType));
                    throw new NotImplementedException();
            }
        }

        public override void Write(BinaryWriter writer, IShape value, WordCount origin)
        {
            ShapeTypeField.Write(writer, value.ShapeType, origin);

            //TODO: This switch should drive off of the Type somehow...
            //      So then we can serialize a PointZ as a PointM, for example.
            switch (value.ShapeType)
            {
                case ShapeType.NullShape:
                    throw new NotImplementedException();

                case ShapeType.Point:
                    {
                        var shapeField = new PointShapeField(Offset + ShapeTypeField.Length);

                        shapeField.Write(writer, (IPointShape<IPoint>)value); //TODO: Presumptuous cast...
                    }
                    break;

                default:
                    Debug.Fail(string.Format("Unimplemented IShape: {0}", typeof(IShape)));
                    throw new NotImplementedException();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.0699831365935919, 'fraction_numerical': 0.0005621135469364812, 'mean_word_length': 1.2529281418170306, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4802275', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1496, 'n_words': 357}