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); }
public IndexFileNDX(Stream stream, DbfTable dbfTable, bool?skipDeleted = null, int indexPageCacheSize = 0) : base(stream, dbfTable, skipDeleted, indexPageCacheSize) // 'stream', 'dbfTable' and 'skipDeleted' already stored by base class constructor { this.header = GetHeader(stream); // fill 'header' & IsStreamValid/Exception if error // #if DEBUG List <string> list = new List <string>(); for (int i = 1; (i < this.header.totalPages); i++) { list.Add(String.Empty); list.Add(String.Format("*** Page {0} *** {1}", i, (i == this.header.rootPage) ? "[ROOT]" : "")); var keyPages = KeyPageRead(i); foreach (var kp in keyPages) { string line = String.Empty; if (kp.leftPage > 0) { line = String.Format(">>page {0}: '", kp.leftPage); } else if (kp.recNo > 0) { line = String.Format("{0,5} rec: '", kp.recNo); } line += _dbfTable.parametersReadOnly.encoding.GetString(kp.key) + "'"; list.Add(line); } } File.WriteAllLines(@".\IndexPages.txt", list, _dbfTable.parametersReadOnly.encoding); #endif }