Пример #1
0
        /// <summary>
        /// Add new word in DDX file.
        /// The word inputed must be sorted.
        /// </summary>
        /// <param name="word">word</param>
        /// <param name="position">position in .idx file</param>
        /// <param name="length">length in .idx file</param>
        public bool Add(string word, long position, long length)
        {
            if (_Mode != Mode.Write)
            {
                throw new System.IO.IOException(string.Format("DDX file: {0} is read only mode. Can't be written!",
                                                              _FilePath));
            }

            if (word.Length > MaxWordLength)
            {
                return(false);
            }

            try
            {
                _CurrentPositionInUnitBlock = UnicodeString.Encode(_TempMem, _UnitBlock,
                                                                   _CurrentPositionInUnitBlock, word, _PreWord, position, length);
            }
            catch (Exception e)
            {
                Global.Report.WriteErrorLog("Unicode string encode error", e);
                return(false);
            }

            if (_CurrentPositionInUnitBlock < 0)
            {
                //UnitBlock overflow

                Array.Copy(_UnitBlock, 0, _Segment, _CurrentUnitIndex * UnitBlockSize, _UnitBlock.Length);

                _CurrentUnitIndex++;
                _CurrentPositionInUnitBlock = 0;
                Array.Clear(_UnitBlock, 0, _UnitBlock.Length);
                _PreWord = "";

                if (_CurrentUnitIndex * UnitBlockSize >= _Segment.Length)
                {
                    //_Segment overflow
                    _File.Write(_Segment, 0, _Segment.Length);
                    _CurrentUnitIndex = 0;
                    Array.Clear(_Segment, 0, _Segment.Length);
                }

                _CurrentPositionInUnitBlock = UnicodeString.Encode(_TempMem, _UnitBlock,
                                                                   _CurrentPositionInUnitBlock, word, _PreWord, position, length);
                return(true);
            }

            _PreWord = word;

            return(true);
        }