Exemple #1
1
        internal void Write(DBFWriter aBase)
        {
            lock (_lockName)
            {
                if (!_new)
                {
                    return;
                }
                if (string.IsNullOrEmpty(aBase.DataMemoLoc))
                {
                    throw new Exception("No Memo Location Set");
                }

                var raf =
                    File.Open(aBase.DataMemoLoc,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite);

                /* before proceeding check whether the passed in File object
                 *  is an empty/non-existent file or not.
                 */


                using (var tWriter = new BinaryWriter(raf, aBase.CharEncoding))
                {
                    if (raf.Length == 0)
                    {
                        var tHeader = new DBTHeader();
                        tHeader.Write(tWriter);
                    }

                    var tValue = _value;
                    if ((tValue.Length + sizeof(int)) % aBase.BlockSize != 0)
                    {
                        tValue = tValue + MemoTerminator;
                    }

                    var tPosition  = raf.Seek(0, SeekOrigin.End); //Got To End Of File
                    var tBlockDiff = tPosition % aBase.BlockSize;
                    if (tBlockDiff != 0)
                    {
                        tPosition = raf.Seek(aBase.BlockSize - tBlockDiff, SeekOrigin.Current);
                    }
                    _block = tPosition / aBase.BlockSize;
                    var tData       = aBase.CharEncoding.GetBytes(tValue);
                    var tDataLength = tData.Length;
                    var tNewDiff    = (tDataLength % aBase.BlockSize);
                    tWriter.Write(tData);
                    if (tNewDiff != 0)
                    {
                        tWriter.Seek(aBase.BlockSize - (tDataLength % aBase.BlockSize), SeekOrigin.Current);
                    }
                }
            }
        }
Exemple #2
0
        internal void Write(DBFWriter aBase)
        {
            lock (_lockName)
            {
                if (!_new)
                {
                    return;
                }

                var raf = aBase.DataMemo;

                /* before proceeding check whether the passed in File object
                 *  is an empty/non-existent file or not.
                 */
                if (raf == null)
                {
                    throw new InvalidDataException("Null Memo Field Stream from Writer");
                }

                var tWriter = new BinaryWriter(raf, aBase.CharEncoding);     //Don't close the stream could be used else where;

                if (raf.Length == 0)
                {
                    var tHeader = new DBTHeader();
                    tHeader.Write(tWriter);
                }

                var tValue = _value;
                if ((tValue.Length + sizeof(int)) % aBase.BlockSize != 0)
                {
                    tValue = tValue + MemoTerminator;
                }

                var tPosition  = raf.Seek(0, SeekOrigin.End);    //Got To End Of File
                var tBlockDiff = tPosition % aBase.BlockSize;
                if (tBlockDiff != 0)
                {
                    tPosition = raf.Seek(aBase.BlockSize - tBlockDiff, SeekOrigin.Current);
                }
                _block = tPosition / aBase.BlockSize;
                var tData       = aBase.CharEncoding.GetBytes(tValue);
                var tDataLength = tData.Length;
                var tNewDiff    = (tDataLength % aBase.BlockSize);
                tWriter.Write(tData);
                if (tNewDiff != 0)
                {
                    tWriter.Seek(aBase.BlockSize - (tDataLength % aBase.BlockSize), SeekOrigin.Current);
                }
            }
        }
        public void checkAnnoymous()
        {
            var fieldLength = 2400;
            string writtenValue;
            using (
                Stream fos =
                    File.Open(TestSelectPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var writer = new DBFWriter
                {
                    DataMemoLoc = Path.ChangeExtension(TestSelectPath, "DBT")
                };
                var field = new DBFField("F1", NativeDbType.Memo);
                var field2 = new DBFField("F2", NativeDbType.Numeric, 10);
                var field3 = new DBFField("F3", NativeDbType.Char, 10);
                writer.Fields = new[] { field, field2, field3 };

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

                var readValues = reader.AllRecords(new {F2 = default(decimal), F3 = default(string)});

                Assert.That(readValues.First().F3, StartsWith(writtenValue), "Written Value not equaling Read");
            }
        }
        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");
            }
        }
 public void WriteSample()
 {
     var field = new DBFField {Name = "F1", DataType = NativeDbType.Numeric};
     var writer = new DBFWriter {Fields = new[] {field}};
     writer.AddRecord(3);
     using (
         Stream fos =
             File.Open(TestPath,
                       FileMode.OpenOrCreate,
                       FileAccess.ReadWrite))
     {
         writer.Write(fos);
     }
 }
        public void checkSelectDynamic()
        {
            var fieldLength = 2400;
            string writtenValue;
            string writtenMemo;
            using (
                Stream fos =
                    File.Open(TestSelectPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var writer = new DBFWriter
                {
                    DataMemoLoc = Path.ChangeExtension(TestSelectPath, "DBT")
                };
                var field = new DBFField("F1", NativeDbType.Memo);
                var field2 = new DBFField("F2", NativeDbType.Numeric, 10);
                var field3 = new DBFField("F3", NativeDbType.Char, 10);
                writer.Fields = new[] { field, field2, field3 };

                writtenValue = "alpha";
                writtenMemo = GetCharacters(fieldLength);
                writer.AddRecord(new MemoValue(writtenMemo), 10, writtenValue);
                writer.Write(fos);
            }
            using (
                Stream fis =
                    File.Open(TestSelectPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var reader = new DBFReader(fis)
                {
                    DataMemoLoc = Path.ChangeExtension(TestSelectPath, "DBT")
                };
                reader.SetSelectFields("F1","F3");
                var readValues = reader.DynamicAllRecords().First();

                Assert.That(readValues.F1.ToString(), EqualTo(writtenMemo), "Written Value not equaling Read");
                Assert.That(readValues.F3, EqualTo(writtenValue), "Written Value not equaling Read");
            }
        }
        public void checkRAFwriting()
        {
            println("Writing in RAF mode ... ");

            if (File.Exists(TestRAFPath))
            {
                File.Delete(TestRAFPath);
            }
            using (var writer = new DBFWriter(TestRAFPath))
            {
                var fields = new DBFField[2];

                fields[0] = new DBFField("F1", NativeDbType.Char, 10);

                fields[1] = new DBFField("F2", NativeDbType.Numeric, 2);

                writer.Fields = fields;

                writer.WriteRecord("Red", 10);
                writer.WriteRecord("Blue", 20);
            }

            println("done.");

            println("Appending to this file");

            using (var writer = new DBFWriter(TestRAFPath))
            {
                writer.WriteRecord("Green", 33);

                writer.WriteRecord("Yellow", 44);
            }
            println("done.");
        }
        public void checkLongCharLengthWithClipper()
        {
            var fieldLength = 750;
            string writtenValue;
            using (
                Stream fos =
                    File.Open(TestClipLongPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var writer = new DBFWriter();
                var field = new DBFField("F1", NativeDbType.Char, fieldLength);
                writer.Fields = new[] { field };

                writtenValue = GetCharacters(fieldLength);
                writer.AddRecord(writtenValue);
                writer.Write(fos);
            }
            using (
                Stream fis =
                    File.Open(TestClipLongPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var reader = new DBFReader(fis);
                Assert.That(reader.Fields.First().FieldLength, EqualTo(fieldLength));
                var readValues = reader.NextRecord();

                Assert.That(readValues[0], EqualTo(writtenValue), "Written Value not equaling Read");
            }
        }
        public void checkDynamicProxyWhere()
        {
            var fieldLength = 2400;
            string writtenValue;
            using (
                Stream fos =
                    File.Open(TestSelectPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var writer = new DBFWriter
                {
                    DataMemoLoc = Path.ChangeExtension(TestSelectPath, "DBT")
                };
                var field = new DBFField("F1", NativeDbType.Memo);
                var field2 = new DBFField("F2", NativeDbType.Numeric, 10);
                var field3 = new DBFField("F3", NativeDbType.Char, 10);
                writer.Fields = new[] { field, field2, field3 };

                writtenValue = "alpha";
                writer.AddRecord(new MemoValue(GetCharacters(fieldLength)), 10, writtenValue);

                writer.AddRecord(new MemoValue(GetCharacters(fieldLength)), 12, "beta");

                writer.Write(fos);
            }

                using(var reader = new DBFReader(TestSelectPath){
                    DataMemoLoc = Path.ChangeExtension(TestSelectPath, "DBT")})
                {

                    var readValues = reader.DynamicAllRecords();

                    Assert.That(Equals(readValues.Count(), 2), "All Records not matching");

                }

                using (var reader = new DBFReader(TestSelectPath)
                {
                    DataMemoLoc = Path.ChangeExtension(TestSelectPath, "DBT")})
                {

                    var readValues = reader.DynamicAllRecords(whereColumn: "F2", whereColumnEquals: 10);

                    Assert.That(Equals(readValues.Count(), 1), "All Records not matching");

                }

                using (var reader = new DBFReader(TestSelectPath)
                {
                    DataMemoLoc = Path.ChangeExtension(TestSelectPath, "DBT")})
                {

                    var readValues = reader.DynamicAllRecords(whereColumn: "F2", whereColumnEquals: 12);

                    Assert.That(Equals(readValues.Count(), 1), "All Records not matching");

                }
                using (var reader = new DBFReader(TestSelectPath)
                {
                    DataMemoLoc = Path.ChangeExtension(TestSelectPath, "DBT")})
                {

                    var readValues = reader.DynamicAllRecords(whereColumn: "F2", whereColumnEquals: 13);

                    Assert.That(Equals(readValues.Count(), 0), "All Records not matching");

                }
        }
Exemple #10
0
        public void checkDataType_N()
        {
            Decimal writtenValue;
            using (
                Stream fos =
                    File.Open(TestPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var writer = new DBFWriter();
                var field = new DBFField("F1", NativeDbType.Numeric, 15, 0);
                writer.Fields = new[] { field };

                writtenValue = 123456789012345L;
                writer.AddRecord(writtenValue);
                writer.Write(fos);
            }
            using (
                Stream fis =
                    File.Open(TestPath,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite))
            {
                var reader = new DBFReader(fis);

                var readValues = reader.NextRecord();

                Assert.That(readValues[0], EqualTo(writtenValue),"Written Value Equals Read");
            }
        }
Exemple #11
0
        internal void Write(DBFWriter aBase)
        {
            lock (_lockName)
            {
                if (!_new)
                    return;
                if (string.IsNullOrEmpty(aBase.DataMemoLoc))
                    throw new Exception("No Memo Location Set");

                var raf =
                    File.Open(aBase.DataMemoLoc,
                              FileMode.OpenOrCreate,
                              FileAccess.ReadWrite);

                /* before proceeding check whether the passed in File object
                    is an empty/non-existent file or not.
                    */

                using (var tWriter = new BinaryWriter(raf, aBase.CharEncoding))
                {
                    if (raf.Length == 0)
                    {
                        var tHeader = new DBTHeader();
                        tHeader.Write(tWriter);
                    }

                    var tValue = _value;
                    if ((tValue.Length + sizeof(int)) % aBase.BlockSize != 0)
                    {
                        tValue = tValue + MemoTerminator;
                    }

                    var tPosition = raf.Seek(0, SeekOrigin.End); //Got To End Of File
                    var tBlockDiff = tPosition%aBase.BlockSize;
                    if (tBlockDiff != 0)
                    {
                        tPosition = raf.Seek(aBase.BlockSize - tBlockDiff, SeekOrigin.Current);
                    }
                    _block = tPosition/aBase.BlockSize;
                    var tData = aBase.CharEncoding.GetBytes(tValue);
                    var tDataLength = tData.Length;
                    var tNewDiff = (tDataLength%aBase.BlockSize);
                    tWriter.Write(tData);
                    if (tNewDiff != 0)
                        tWriter.Seek(aBase.BlockSize - (tDataLength % aBase.BlockSize), SeekOrigin.Current);
                }
            }
        }