Example #1
0
        public DxfCodePair GetCodePair()
        {
            if (_lineEnumerator.MoveNext())
            {
                var codeLine       = _lineEnumerator.Current;
                var codeLineNumber = _lineNumber;
                int code;
                if (string.IsNullOrEmpty(codeLine))
                {
                    // a blank line when expecting a code means we can't parse any further
                    return(null);
                }
                else if (int.TryParse(codeLine, NumberStyles.Integer, CultureInfo.InvariantCulture, out code))
                {
                    if (_lineEnumerator.MoveNext())
                    {
                        DxfCodePair pair         = null;
                        var         valueLine    = _lineEnumerator.Current;
                        var         expectedType = DxfCodePair.ExpectedType(code);
                        if (expectedType == typeof(short))
                        {
                            pair = GetCodePair <short>(code, valueLine, TryParseShortValue, (c, v) => new DxfCodePair(c, v), minValue: short.MinValue, maxValue: short.MaxValue);
                        }
                        else if (expectedType == typeof(double))
                        {
                            pair = GetCodePair <double>(code, valueLine, TryParseDoubleValue, (c, v) => new DxfCodePair(c, v));
                        }
                        else if (expectedType == typeof(int))
                        {
                            pair = GetCodePair <int>(code, valueLine, TryParseIntValue, (c, v) => new DxfCodePair(c, v), minValue: int.MinValue, maxValue: int.MaxValue);
                        }
                        else if (expectedType == typeof(long))
                        {
                            pair = GetCodePair <long>(code, valueLine, TryParseLongValue, (c, v) => new DxfCodePair(c, v), minValue: long.MinValue, maxValue: long.MaxValue);
                        }
                        else if (expectedType == typeof(string))
                        {
                            var value = valueLine.Trim();
                            if (_encoding == null)
                            {
                                // read as ASCII, transform UTF codes
                                value = DxfReader.TransformUnicodeCharacters(value);
                            }

                            pair = new DxfCodePair(code, DxfReader.TransformControlCharacters(value));
                        }
                        else if (expectedType == typeof(bool))
                        {
                            // this should really parse as a short, but it could be anything
                            double result;
                            if (double.TryParse(valueLine, NumberStyles.Float, CultureInfo.InvariantCulture, out result))
                            {
                                if (result < short.MinValue)
                                {
                                    result = short.MinValue;
                                }

                                if (result > short.MaxValue)
                                {
                                    result = short.MaxValue;
                                }

                                pair = DxfCodePair.IsPotentialShortAsBool(code)
                                    ? new DxfCodePair(code, (short)result)
                                    : new DxfCodePair(code, result != 0.0);
                            }
                            else
                            {
                                throw new DxfReadException($"Unsupported value '{valueLine}' for code '{code}'", _lineNumber);
                            }
                        }
                        else if (expectedType == typeof(byte[]))
                        {
                            var data = DxfCommonConverters.HexBytes(valueLine);
                            pair = new DxfCodePair(code, data);
                        }
                        else
                        {
                            throw new DxfReadException("Unsupported code " + code, codeLineNumber);
                        }

                        if (pair != null)
                        {
                            pair.Offset = codeLineNumber;
                        }

                        return(pair);
                    }
                    else
                    {
                        throw new DxfReadException("Expected value", _lineNumber);
                    }
                }
                else
                {
                    if (codeLineNumber == 1)
                    {
                        // parsing the very first code pair failed
                        throw new DxfReadException($"Not a valid DXF file header: `{codeLine}`.", _lineNumber);
                    }
                    else
                    {
                        throw new DxfReadException("Unexpected code value", _lineNumber);
                    }
                }
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        private DxfCodePair GetCodePair()
        {
            if (_lineEnumerator.MoveNext())
            {
                var codeLine       = _lineEnumerator.Current;
                var codeLineNumber = _lineNumber;
                int code;
                if (string.IsNullOrEmpty(codeLine))
                {
                    // a blank line when expecting a code means we can't parse any further
                    return(null);
                }
                else if (int.TryParse(codeLine, NumberStyles.Integer, CultureInfo.InvariantCulture, out code))
                {
                    if (_lineEnumerator.MoveNext())
                    {
                        DxfCodePair pair         = null;
                        var         valueLine    = _lineEnumerator.Current;
                        var         expectedType = DxfCodePair.ExpectedType(code);
                        if (expectedType == typeof(short))
                        {
                            pair = GetCodePair <short>(code, _lineEnumerator.Current, short.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v), minValue: short.MinValue, maxValue: short.MaxValue);
                        }
                        else if (expectedType == typeof(double))
                        {
                            pair = GetCodePair <double>(code, _lineEnumerator.Current, double.TryParse, NumberStyles.Float, (c, v) => new DxfCodePair(c, v));
                        }
                        else if (expectedType == typeof(int))
                        {
                            pair = GetCodePair <int>(code, _lineEnumerator.Current, int.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v), minValue: int.MinValue, maxValue: int.MaxValue);
                        }
                        else if (expectedType == typeof(long))
                        {
                            pair = GetCodePair <long>(code, _lineEnumerator.Current, long.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v), minValue: long.MinValue, maxValue: long.MaxValue);
                        }
                        else if (expectedType == typeof(string))
                        {
                            pair = new DxfCodePair(code, DxfReader.TransformControlCharacters(_lineEnumerator.Current.Trim()));
                        }
                        else if (expectedType == typeof(bool))
                        {
                            // this should really parse as a short, but it could be anything
                            double result;
                            if (double.TryParse(_lineEnumerator.Current, NumberStyles.Float, CultureInfo.InvariantCulture, out result))
                            {
                                if (result < short.MinValue)
                                {
                                    result = short.MinValue;
                                }

                                if (result > short.MaxValue)
                                {
                                    result = short.MaxValue;
                                }

                                pair = DxfCodePair.IsPotentialShortAsBool(code)
                                    ? new DxfCodePair(code, (short)result)
                                    : new DxfCodePair(code, result != 0.0);
                            }
                            else
                            {
                                throw new DxfReadException($"Unsupported value '{_lineEnumerator.Current}' for code '{code}'", _lineNumber);
                            }
                        }
                        else
                        {
                            throw new DxfReadException("Unsupported code " + code, codeLineNumber);
                        }

                        if (pair != null)
                        {
                            pair.Offset = codeLineNumber;
                        }

                        return(pair);
                    }
                    else
                    {
                        throw new DxfReadException("Expected value", _lineNumber);
                    }
                }
                else
                {
                    throw new DxfReadException("Unexpected code value", _lineNumber);
                }
            }
            else
            {
                return(null);
            }
        }
Example #3
0
        private DxfCodePair GetCodePair()
        {
            if (_lineEnumerator.MoveNext())
            {
                var codeLine       = _lineEnumerator.Current;
                var codeLineNumber = _lineNumber;
                int code;
                if (int.TryParse(codeLine, NumberStyles.Integer, CultureInfo.InvariantCulture, out code))
                {
                    if (_lineEnumerator.MoveNext())
                    {
                        DxfCodePair pair         = null;
                        var         valueLine    = _lineEnumerator.Current;
                        var         expectedType = DxfCodePair.ExpectedType(code);
                        if (expectedType == typeof(short))
                        {
                            pair = GetCodePair <short>(code, _lineEnumerator.Current, short.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v));
                        }
                        else if (expectedType == typeof(double))
                        {
                            pair = GetCodePair <double>(code, _lineEnumerator.Current, double.TryParse, NumberStyles.Float, (c, v) => new DxfCodePair(c, v));
                        }
                        else if (expectedType == typeof(int))
                        {
                            pair = GetCodePair <int>(code, _lineEnumerator.Current, int.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v));
                        }
                        else if (expectedType == typeof(long))
                        {
                            pair = GetCodePair <long>(code, _lineEnumerator.Current, long.TryParse, NumberStyles.Integer, (c, v) => new DxfCodePair(c, v));
                        }
                        else if (expectedType == typeof(string))
                        {
                            pair = new DxfCodePair(code, DxfReader.TransformControlCharacters(_lineEnumerator.Current.Trim()));
                        }
                        else if (expectedType == typeof(bool))
                        {
                            short result;
                            if (short.TryParse(_lineEnumerator.Current, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
                            {
                                pair = DxfCodePair.IsPotentialShortAsBool(code)
                                    ? new DxfCodePair(code, result)
                                    : new DxfCodePair(code, result != 0);
                            }
                            else
                            {
                                throw new DxfReadException("Unsupported value for code", _lineNumber);
                            }
                        }
                        else
                        {
                            throw new DxfReadException("Unsupported code " + code, codeLineNumber);
                        }

                        if (pair != null)
                        {
                            pair.Offset = codeLineNumber;
                        }

                        return(pair);
                    }
                    else
                    {
                        throw new DxfReadException("Expected value", _lineNumber);
                    }
                }
                else
                {
                    throw new DxfReadException("Unexpected code value", _lineNumber);
                }
            }
            else
            {
                return(null);
            }
        }
        private DxfCodePair GetCodePair()
        {
            var codeOffset = _totalBytesRead + _miniBufferStart;
            int code;

            if (!TryReadCode(out code))
            {
                return(null);
            }

            var         expectedType = DxfCodePair.ExpectedType(code);
            DxfCodePair pair;

            if (expectedType == typeof(short))
            {
                short s;
                if (!TryReadInt16(out s))
                {
                    return(null);
                }

                pair = new DxfCodePair(code, s);
            }
            else if (expectedType == typeof(double))
            {
                double d;
                if (!TryReadDouble(out d))
                {
                    return(null);
                }

                pair = new DxfCodePair(code, d);
            }
            else if (expectedType == typeof(int))
            {
                int i;
                if (!TryReadInt32(out i))
                {
                    return(null);
                }

                pair = new DxfCodePair(code, i);
            }
            else if (expectedType == typeof(long))
            {
                long l;
                if (!TryReadInt64(out l))
                {
                    return(null);
                }

                pair = new DxfCodePair(code, l);
            }
            else if (expectedType == typeof(string))
            {
                var  sb = new StringBuilder();
                byte b;
                for (; TryReadByte(out b) && b != 0;)
                {
                    sb.Append((char)b);
                }

                pair = new DxfCodePair(code, DxfReader.TransformControlCharacters(sb.ToString()));
            }
            else if (expectedType == typeof(bool))
            {
                var value = _reader.ReadInt16();
                pair = DxfCodePair.IsPotentialShortAsBool(code)
                    ? new DxfCodePair(code, value)
                    : new DxfCodePair(code, value != 0);
            }
            else
            {
                throw new DxfReadException("Unsupported code " + code, codeOffset);
            }

            pair.Offset = codeOffset;
            return(pair);
        }