Esempio n. 1
0
        internal Object[] NextRecord(IEnumerable<int> selectIndexes, IList<int> sortedIndexes)
        {
            if (isClosed)
            {
                throw new DBFException("Source is not open");
            }
            IList<int> tOrderdSelectIndexes = sortedIndexes;

            var recordObjects = new Object[_header.FieldArray.Length];

            try
            {
                bool isDeleted = false;
                do
                {
                    if (isDeleted)
                    {
                        _dataInputStream.ReadBytes(_header.RecordLength - 1);
                    }

                    int t_byte = _dataInputStream.ReadByte();
                    if (t_byte == DBFFieldType.EndOfData)
                    {
                        return null;
                    }

                    isDeleted = (t_byte == '*');
                } while (isDeleted);

                int j = 0;
                int k = -1;
                for (int i = 0; i < _header.FieldArray.Length; i++)
                {

                    if (tOrderdSelectIndexes.Count == j && j != 0
                        || (tOrderdSelectIndexes.Count > j && tOrderdSelectIndexes[j] > i && tOrderdSelectIndexes[j] != k))
                    {
                        _dataInputStream.BaseStream.Seek(_header.FieldArray[i].FieldLength, SeekOrigin.Current);
                        continue;
                    }
                    if (tOrderdSelectIndexes.Count > j)
                        k = tOrderdSelectIndexes[j];
                    j++;

                  
                    switch (_header.FieldArray[i].DataType)
                    {
                        case NativeDbType.Char:

                            var b_array = new byte[
                                _header.FieldArray[i].FieldLength
                                ];
                            _dataInputStream.Read(b_array, 0, b_array.Length);
                            //StringBuilder sb = new StringBuilder();
                            //for (int c = 0; c < b_array.Length && b_array[c] != 0; c++)
                            //{
                            //    sb.Append((char)b_array[c]);
                            //}
                            //recordObjects[i] = sb.ToString().TrimEnd();
                            //recordObjects[i] = CharEncoding.GetString(b_array).TrimEnd();
                            recordObjects[i] = Encoding.GetEncoding("ISO-8859-1").GetString(b_array).TrimEnd();
                            break;

                        case NativeDbType.Date:

                            byte[] t_byte_year = new byte[4];
                            _dataInputStream.Read(t_byte_year,
                                                 0,
                                                 t_byte_year.Length);

                            byte[] t_byte_month = new byte[2];
                            _dataInputStream.Read(t_byte_month,
                                                 0,
                                                 t_byte_month.Length);

                            byte[] t_byte_day = new byte[2];
                            _dataInputStream.Read(t_byte_day,
                                                 0,
                                                 t_byte_day.Length);

                            try
                            {
                                var tYear = CharEncoding.GetString(t_byte_year);
                                var tMonth = CharEncoding.GetString(t_byte_month);
                                var tDay = CharEncoding.GetString(t_byte_day);

                                int tIntYear, tIntMonth, tIntDay;
                                if (Int32.TryParse(tYear, out tIntYear) &&
                                    Int32.TryParse(tMonth, out tIntMonth) &&
                                    Int32.TryParse(tDay, out tIntDay))
                                {
                                    recordObjects[i] = new DateTime(
                                        tIntYear,
                                        tIntMonth,
                                        tIntDay);
                                }
                                else
                                {
                                    recordObjects[i] = null;
                                }
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                /* this field may be empty or may have improper value set */
                                recordObjects[i] = null;
                            }

                            break;

                        case NativeDbType.Float:

                            try
                            {
                                byte[] t_float = new byte[
                                    _header.FieldArray[i].FieldLength
                                    ];
                                _dataInputStream.Read(t_float, 0, t_float.Length);
                                String tParsed = CharEncoding.GetString(t_float);
                                var tLast = tParsed.Substring(tParsed.Length - 1);
                                if (tParsed.Length > 0
                                    && tLast != " "
                                    && tLast != DBFFieldType.Unknown)
                                {
                                    //recordObjects[i] = Double.Parse(tParsed, NumberStyles.Float | NumberStyles.AllowLeadingWhite);
                                    recordObjects[i] = GlobalcachingApplication.Utils.Conversion.StringToDouble(tParsed);
                                }
                                else
                                {
                                    recordObjects[i] = null;
                                }
                            }
                            catch (FormatException e)
                            {
                                throw new DBFException("Failed to parse Float",
                                                       e);
                            }

                            break;

                        case NativeDbType.Numeric:

                            try
                            {
                                byte[] t_numeric = new byte[
                                    _header.FieldArray[i].FieldLength
                                    ];
                                _dataInputStream.Read(t_numeric,
                                                     0,
                                                     t_numeric.Length);
                                string tParsed =
                                    CharEncoding.GetString(t_numeric);
                                var tLast = tParsed.Substring(tParsed.Length - 1);
                                if (tParsed.Length > 0
                                    && tLast != " "
                                    && tLast != DBFFieldType.Unknown)
                                {
                                    //recordObjects[i] = Decimal.Parse(tParsed, NumberStyles.Float | NumberStyles.AllowLeadingWhite);
                                    recordObjects[i] = GlobalcachingApplication.Utils.Conversion.StringToDouble(tParsed);
                                }
                                else
                                {
                                    recordObjects[i] = null;
                                }
                            }
                            catch (FormatException e)
                            {
                                throw new DBFException(
                                    "Failed to parse Number", e);
                            }

                            break;

                        case NativeDbType.Logical:

                            byte t_logical = _dataInputStream.ReadByte();
                            //todo find out whats really valid
                            if (t_logical == 'Y' || t_logical == 't'
                                || t_logical == 'T'
                                || t_logical == 't')
                            {
                                recordObjects[i] = true;
                            }
                            else if (t_logical == DBFFieldType.UnknownByte)
                            {
                                recordObjects[i] = DBNull.Value;
                            }else
                            {
                                recordObjects[i] = false;
                            }
                            break;

                        case NativeDbType.Memo:
                            if(string.IsNullOrEmpty(_dataMemoLoc))
                                throw new Exception("Memo Location Not Set");


                            var tRawMemoPointer = _dataInputStream.ReadBytes(_header.FieldArray[i].FieldLength);
                            var tMemoPoiner = CharEncoding.GetString(tRawMemoPointer);
                            if(string.IsNullOrEmpty(tMemoPoiner))
                            {
                                recordObjects[i] = DBNull.Value;
                                break;
                            }
                            long tBlock;
                            if(!long.TryParse(tMemoPoiner, out tBlock))
                            {  //Because Memo files can vary and are often the least importat data, 
                                //we will return null when it doesn't match our format.
                                recordObjects[i] = DBNull.Value;
                                break;
                            }


                            recordObjects[i] = new MemoValue(tBlock, this, _dataMemoLoc);
                            break;
                        default:
                            _dataInputStream.ReadBytes(_header.FieldArray[i].FieldLength);
                            recordObjects[i] = DBNull.Value;
                            break;
                    }

                 
                }
            }
            catch (EndOfStreamException)
            {
                return null;
            }
            catch (IOException e)
            {
                throw new DBFException("Problem Reading File", e);
            }

            return selectIndexes.Any() ? selectIndexes.Select(it => recordObjects[it]).ToArray() : recordObjects;
        }
Esempio n. 2
0
        internal Object[] NextRecord(IEnumerable <int> selectIndexes, IList <int> sortedIndexes)
        {
            if (isClosed)
            {
                throw new DBFException("Source is not open");
            }
            IList <int> tOrderdSelectIndexes = sortedIndexes;

            var recordObjects = new Object[_header.FieldArray.Length];

            try
            {
                bool isDeleted = false;
                do
                {
                    if (isDeleted)
                    {
                        _dataInputStream.ReadBytes(_header.RecordLength - 1);
                    }

                    int t_byte = _dataInputStream.ReadByte();
                    if (t_byte == DBFFieldType.EndOfData)
                    {
                        return(null);
                    }

                    isDeleted = (t_byte == '*');
                } while (isDeleted);

                int j = 0;
                int k = -1;
                for (int i = 0; i < _header.FieldArray.Length; i++)
                {
                    if (tOrderdSelectIndexes.Count == j && j != 0 ||
                        (tOrderdSelectIndexes.Count > j && tOrderdSelectIndexes[j] > i && tOrderdSelectIndexes[j] != k))
                    {
                        _dataInputStream.BaseStream.Seek(_header.FieldArray[i].FieldLength, SeekOrigin.Current);
                        continue;
                    }
                    if (tOrderdSelectIndexes.Count > j)
                    {
                        k = tOrderdSelectIndexes[j];
                    }
                    j++;


                    switch (_header.FieldArray[i].DataType)
                    {
                    case NativeDbType.Char:

                        var b_array = new byte[
                            _header.FieldArray[i].FieldLength
                                      ];
                        _dataInputStream.Read(b_array, 0, b_array.Length);
                        //StringBuilder sb = new StringBuilder();
                        //for (int c = 0; c < b_array.Length && b_array[c] != 0; c++)
                        //{
                        //    sb.Append((char)b_array[c]);
                        //}
                        //recordObjects[i] = sb.ToString().TrimEnd();
                        recordObjects[i] = CharEncoding.GetString(b_array).TrimEnd();
                        //recordObjects[i] = Encoding.GetEncoding("ISO-8859-1").GetString(b_array).TrimEnd();
                        break;

                    case NativeDbType.Date:

                        byte[] t_byte_year = new byte[4];
                        _dataInputStream.Read(t_byte_year,
                                              0,
                                              t_byte_year.Length);

                        byte[] t_byte_month = new byte[2];
                        _dataInputStream.Read(t_byte_month,
                                              0,
                                              t_byte_month.Length);

                        byte[] t_byte_day = new byte[2];
                        _dataInputStream.Read(t_byte_day,
                                              0,
                                              t_byte_day.Length);

                        try
                        {
                            var tYear  = CharEncoding.GetString(t_byte_year);
                            var tMonth = CharEncoding.GetString(t_byte_month);
                            var tDay   = CharEncoding.GetString(t_byte_day);

                            int tIntYear, tIntMonth, tIntDay;
                            if (Int32.TryParse(tYear, out tIntYear) &&
                                Int32.TryParse(tMonth, out tIntMonth) &&
                                Int32.TryParse(tDay, out tIntDay))
                            {
                                recordObjects[i] = new DateTime(
                                    tIntYear,
                                    tIntMonth,
                                    tIntDay);
                            }
                            else
                            {
                                recordObjects[i] = null;
                            }
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            /* this field may be empty or may have improper value set */
                            recordObjects[i] = null;
                        }

                        break;

                    case NativeDbType.Float:

                        try
                        {
                            byte[] t_float = new byte[
                                _header.FieldArray[i].FieldLength
                                             ];
                            _dataInputStream.Read(t_float, 0, t_float.Length);
                            String tParsed = CharEncoding.GetString(t_float);
                            var    tLast   = tParsed.Substring(tParsed.Length - 1);
                            if (tParsed.Length > 0 &&
                                tLast != " " &&
                                tLast != DBFFieldType.Unknown)
                            {
                                //recordObjects[i] = Double.Parse(tParsed, NumberStyles.Float | NumberStyles.AllowLeadingWhite);
                                recordObjects[i] = GAPPSF.Utils.Conversion.StringToDouble(tParsed);
                            }
                            else
                            {
                                recordObjects[i] = null;
                            }
                        }
                        catch (FormatException e)
                        {
                            throw new DBFException("Failed to parse Float",
                                                   e);
                        }

                        break;

                    case NativeDbType.Numeric:

                        try
                        {
                            byte[] t_numeric = new byte[
                                _header.FieldArray[i].FieldLength
                                               ];
                            _dataInputStream.Read(t_numeric,
                                                  0,
                                                  t_numeric.Length);
                            string tParsed =
                                CharEncoding.GetString(t_numeric);
                            var tLast = tParsed.Substring(tParsed.Length - 1);
                            if (tParsed.Length > 0 &&
                                tLast != " " &&
                                tLast != DBFFieldType.Unknown)
                            {
                                //recordObjects[i] = Decimal.Parse(tParsed, NumberStyles.Float | NumberStyles.AllowLeadingWhite);
                                recordObjects[i] = GAPPSF.Utils.Conversion.StringToDouble(tParsed);
                            }
                            else
                            {
                                recordObjects[i] = null;
                            }
                        }
                        catch (FormatException e)
                        {
                            throw new DBFException(
                                      "Failed to parse Number", e);
                        }

                        break;

                    case NativeDbType.Logical:

                        byte t_logical = _dataInputStream.ReadByte();
                        //todo find out whats really valid
                        if (t_logical == 'Y' || t_logical == 't' ||
                            t_logical == 'T' ||
                            t_logical == 't')
                        {
                            recordObjects[i] = true;
                        }
                        else if (t_logical == DBFFieldType.UnknownByte)
                        {
                            recordObjects[i] = DBNull.Value;
                        }
                        else
                        {
                            recordObjects[i] = false;
                        }
                        break;

                    case NativeDbType.Memo:
                        if (string.IsNullOrEmpty(_dataMemoLoc))
                        {
                            throw new Exception("Memo Location Not Set");
                        }


                        var tRawMemoPointer = _dataInputStream.ReadBytes(_header.FieldArray[i].FieldLength);
                        var tMemoPoiner     = CharEncoding.GetString(tRawMemoPointer);
                        if (string.IsNullOrEmpty(tMemoPoiner))
                        {
                            recordObjects[i] = DBNull.Value;
                            break;
                        }
                        long tBlock;
                        if (!long.TryParse(tMemoPoiner, out tBlock))
                        {      //Because Memo files can vary and are often the least importat data,
                            //we will return null when it doesn't match our format.
                            recordObjects[i] = DBNull.Value;
                            break;
                        }


                        recordObjects[i] = new MemoValue(tBlock, this, _dataMemoLoc);
                        break;

                    default:
                        _dataInputStream.ReadBytes(_header.FieldArray[i].FieldLength);
                        recordObjects[i] = DBNull.Value;
                        break;
                    }
                }
            }
            catch (EndOfStreamException)
            {
                return(null);
            }
            catch (IOException e)
            {
                throw new DBFException("Problem Reading File", e);
            }

            return(selectIndexes.Any() ? selectIndexes.Select(it => recordObjects[it]).ToArray() : recordObjects);
        }
Esempio n. 3
0
        public void checkDataType_M()
        {
            var fieldLength = 2400;
            MemoValue writtenValue;
            using (
                Stream fos =
                    File.Open(TestMemoPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var writer = new DBFWriter
                                 {
                                     DataMemoLoc = Path.ChangeExtension(TestMemoPath, "DBT")
                                 };
                var field = new DBFField("F1", NativeDbType.Memo);
                writer.Fields = new[] { field };

                writtenValue = new MemoValue(GetCharacters(fieldLength));
                writer.AddRecord(writtenValue);
                writer.Write(fos);
            }
            using (
                Stream fis =
                    File.Open(TestMemoPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var reader = new DBFReader(fis)
                {
                    DataMemoLoc = Path.ChangeExtension(TestMemoPath, "DBT")
                };
                var readValues = reader.NextRecord();

                Assert.That(readValues[0], EqualTo(writtenValue), "Written Value not equaling Read");
            }
        }