Esempio n. 1
0
        /// <summary>
        /// Loads a value from the specified buffer.
        /// </summary>
        /// <param name="buffer">The byte array from which a value should be loaded. The buffer length is always at least equal to the column size.</param>
        /// <param name="encoding">The encoding that should be used when loading a value. The encoding is never <c>null</c>.</param>
        /// <returns>A column value.</returns>
        protected override bool ValueFromRowBuffer(byte[] rowBuffer, ref byte[] cachedColumnData)
        { // This didn't use cachedColumnData, it for MemoColumn only
            byte code = rowBuffer[offset_ + 1];

            switch (code)
            {
            case 0x54:                                                  // 'T'
            case 0x74:                                                  // 't'
            case 0x59:                                                  // 'Y'
            case 0x79:                                                  // 'y'
                return(true);

            case 0x46:                                                  // 'F'
            case 0x66:                                                  // 'f'
            case 0x4E:                                                  // 'N'
            case 0x6E:                                                  // 'n'
                return(false);

            case 0x20:                                                  // ' '
            case 0x3F:                                                  // '?'
                return(false);

            default:
                throw ExceptionFactory.CreateArgumentOutOfRangeException(this.name, "Invalid boolean character: '{0}'", code);
            }
        }
Esempio n. 2
0
        private NdxKeyItem[] KeyPageRead(int pageNo)
        {
            if ((pageNo < 1) || (pageNo > 0x7FFFFF))
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("pageNo", "NDX index key page position invalid! [{0}]'!", pageNo);
            }

            int newPosition = pageNo * pageSize;

            if ((newPosition + pageSize) > stream.Length)
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("pageNo", "NDX index key page position invalid! (filesize) [{0}]'!", pageNo);
            }

            bool isRoot = (pageNo == header.rootPage);                          // if root we must correct this data structure with a 'close' item

            stream.Position = newPosition;

            BinaryReader reader = new BinaryReader(stream);                     // don't use 'using (BinaryReader reader...' because 'using' dispose 'stream' too!

            int itemsCount = reader.ReadInt32();

            NdxKeyItem[] keyItems = new NdxKeyItem[itemsCount + (isRoot ? 1 : 0)];

            for (int i = 0; i < itemsCount; i++)
            {
                NdxKeyItem keyItem = new NdxKeyItem();

                keyItem.leftPage = reader.ReadInt32();
                keyItem.recNo    = reader.ReadInt32();
                keyItem.key      = reader.ReadBytes(header.keyLen);

                if ((header.keyLen % 4) != 0)
                {
                    reader.ReadBytes(4 - (header.keyLen % 4));
                }

                keyItems[i] = keyItem;
            }


            if (isRoot)
            {
                NdxKeyItem keyItem = new NdxKeyItem();

                keyItem.leftPage = reader.ReadInt32();
                keyItem.recNo    = 0;
                keyItem.key      = new byte[header.keyLen];

                for (int i = 0; i < keyItem.key.Length; i++)
                { // fill all bytes of array
                    keyItem.key[i] = 0xFF;
                }

                keyItems[itemsCount] = keyItem;
            }

            return(keyItems);
        }
Esempio n. 3
0
        public static NdxHeader GetHeader(Stream stream)
        {
            if (stream.Length < (pageSize * 2))
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "NDX index stream length '{0}' < '{1}'!", stream.Length, (pageSize * 2));
            }

            if ((stream.Length % pageSize) != 0)
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "NDX index stream length ({0}) isn't a multiple of '{1}'!", stream.Length, pageSize);
            }

            //

            NdxHeader header = new NdxHeader();

            stream.Position = 0;

            BinaryReader reader = new BinaryReader(stream);                     // don't use 'using (BinaryReader reader...' because 'using' dispose 'stream' too!

            header.rootPage   = reader.ReadInt32();
            header.totalPages = reader.ReadInt32();

            reader.ReadInt32();                                                 // reserved space

            header.keyLen     = reader.ReadUInt16();
            header.keyPages   = reader.ReadUInt16();
            header.keyType    = reader.ReadUInt16();
            header.keyRecSize = reader.ReadInt32();                             // Size of key record is a multiplum of 4. Record size is 4 (Pointer to next page) + 4 (record number i dbf) + key size ( as a multiplum of 4 ). i.e. if the key size is 10, the record size is 20 (4+4+12)

            {
                int extraSpace = header.keyLen % 4;

                if (extraSpace != 0)
                {
                    extraSpace += (4 - extraSpace);
                }

                header.keyRecSize = 4 + 4 + header.keyLen + extraSpace;         // rewrite readed value because it was a not valid value
            }

            reader.ReadByte();                                                  // reserved space

            header.unique = reader.ReadBoolean();

            header.keyExpr = reader.ReadBytes(maxKeyLen);

            if (ProcessKeyExpressionBuffer(header.keyExpr) == null)
            {
                throw ExceptionFactory.CreateNotSupportedException("Content of NDX key expression bytes is envalid!");
            }

            return(header);
        }
Esempio n. 4
0
        public IColumn FindColumnByName(string columnName)
        {
            if (String.IsNullOrWhiteSpace(columnName))
            {
                throw new ArgumentNullException("columnName");
            }

            var column = _columns.FirstOrDefault(c => (String.Compare(c.name, columnName, true) == 0)); // case insensitive

            if (column == null)
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("columnName", "Column {0} not found.", columnName);
            }

            return(column);
        }
Esempio n. 5
0
        protected DbfTable(Stream stream, DbfTableParameters parameters)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (!stream.CanRead)
            {
                throw ExceptionFactory.CreateArgumentException("stream", "The stream does not allow reading (CanRead property returns false).");
            }

            if (!stream.CanSeek)
            {
                throw ExceptionFactory.CreateArgumentException("stream", "The stream does not allow reading (CanSeek property returns false).");
            }

            this._stream    = stream;
            this.parameters = parameters;

            RefreshHeaderInfo();

            //

            if (this.parameters.encoding == null)
            {
                this.parameters.encoding = ReadDbfHeader_Encoding(_header.codepageCode);

                if (this.parameters.encoding == null)
                {
                    throw new Exception("DbfTable: the DBF file don't contains codepage information!");
                }
            }

            this._columns = ReadDbfColumns(_stream, this.parameters.encoding, _header.newHeaderStructure, this.parameters.openMemo);

            //

            int calcLen = _header.firstRecordPosition + (_header.recCount * _header.rowLength) + 1;

            if ((stream.Length < calcLen - 1) || (stream.Length > calcLen + 1))
            { // dBase & Clipper different (There is or there isn't a 0x1F character at end of DBF data file .
                throw ExceptionFactory.CreateArgumentOutOfRangeException("DBF table", "Datafile length error! [got: {0} expected: {1}]", stream.Length, calcLen);
            }
        }
Esempio n. 6
0
        public IColumn FindColumnByName(string columnName, bool nullReturnEnable)
        { // There isn't default parameter value, so this function is wrote same as previous function, because this way is quicker then call it only with name parameter.
            if (String.IsNullOrWhiteSpace(columnName))
            {
                throw new ArgumentNullException("columnName");
            }

            var column = _columns.FirstOrDefault(c => (String.Compare(c.name, columnName, true) == 0)); // case insensitive

            if (column == null)
            {
                if (!nullReturnEnable)
                {
                    throw ExceptionFactory.CreateArgumentOutOfRangeException("columnName", "Column {0} not found.", columnName);
                }
            }

            return(column);
        }
Esempio n. 7
0
        private void Resize(int newPageCount)
        {
            if (newPageCount > MAXCACHESIZE)
            {
                newPageCount = MAXCACHESIZE;
            }
            else if (newPageCount < 0)
            {
                newPageCount = 0;

        #if DEBUG
                throw ExceptionFactory.CreateArgumentOutOfRangeException("newPageCount", "Count of pages must greater or equal then 0!");
        #endif
            }

            if (items.Count > newPageCount)
            { // Shrink size, remove oldest/not-used items
                RemovePages(items.Count - newPageCount);
            }

            _pageCount = newPageCount;
        }
Esempio n. 8
0
        public static NtxHeader GetHeader(Stream stream)
        {
            if (stream.Length < (pageSize * 2))
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "NTX index stream length '{0}' < '{1}'!", stream.Length, (pageSize * 2));
            }

            if ((stream.Length % pageSize) != 0)
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "NTX index stream length ({0}) isn't a multiple of '{1}'!", stream.Length, pageSize);
            }

            //

            NtxHeader header = new NtxHeader();

            stream.Position = 0;

            BinaryReader reader = new BinaryReader(stream);                     // don't use 'using (BinaryReader reader...' because 'using' dispose 'stream' too!

            header.signature = reader.ReadUInt16();
            header.version   = reader.ReadUInt16();
            header.root      = reader.ReadInt32();
            header.unused    = reader.ReadInt32();
            header.itemSize  = reader.ReadUInt16();
            header.keySize   = reader.ReadUInt16();
            header.keyDec    = reader.ReadUInt16();
            header.maxItem   = reader.ReadUInt16();
            header.halfPage  = reader.ReadUInt16();
            header.keyExpr   = reader.ReadBytes(maxKeyLen);

            byte uniqueFlag = reader.ReadByte();

            header.unique = (uniqueFlag != 0);

            Debug.Assert(reader.BaseStream.Position == 279);

            //

            byte sign1Byte = (byte)(header.signature >> 8);                       // get first byte

            if (!Array.Exists(validSignatures, s => (s == sign1Byte)))
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "Signature byte of NTX index stream is invalid! '{0}'", sign1Byte);
            }

            byte sign2Byte = (byte)(header.signature);                            // cut off first byte

            if (sign2Byte != 0)
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "Second signature byte in NTX index stream header is invalid!'{0}'", sign2Byte);
            }

            if ((header.keySize < 1) || (header.keySize > 250))
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "Key size in NTX index stream header is invalid!");
            }

            if ((header.keySize + 8) != header.itemSize)
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "Key size (+8) in NTX index stream header is invalid!");
            }

            if (ProcessKeyExpressionBuffer(header.keyExpr) == null)
            {
                throw ExceptionFactory.CreateNotSupportedException("Content of key expression bytes is envalid!");
            }

            if (!((uniqueFlag == 0) || (uniqueFlag == 1)))
            {
                throw ExceptionFactory.CreateArgumentOutOfRangeException("stream", "Unique flag in NTX index stream header is invalid!");
            }

            //

            return(header);
        }