Example #1
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
        /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        /// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform    = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using (outputStream_ = new ZipOutputStream(outputStream)) {
                outputStream_.SetLevel((int)CompressionLevel);


                outputStream_.UseZip64 = UseZip64;
                var scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += ProcessFile;
                if (this.CreateEmptyDirectories)
                {
                    scanner.ProcessDirectory += ProcessDirectory;
                }

                if (events_ != null)
                {
                    if (events_.FileFailure != null)
                    {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if (events_.DirectoryFailure != null)
                    {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Example #2
0
 public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
 {
     this.NameTransform    = new ZipNameTransform(sourceDirectory);
     this.sourceDirectory_ = sourceDirectory;
     using (this.outputStream_ = new ZipOutputStream(outputStream))
     {
         if (this.password_ != null)
         {
             this.outputStream_.Password = this.password_;
         }
         this.outputStream_.UseZip64 = this.UseZip64;
         FileSystemScanner fileSystemScanner  = new FileSystemScanner(fileFilter, directoryFilter);
         FileSystemScanner fileSystemScanner2 = fileSystemScanner;
         fileSystemScanner2.ProcessFile = (ProcessFileHandler)Delegate.Combine(fileSystemScanner2.ProcessFile, new ProcessFileHandler(this.ProcessFile));
         if (this.CreateEmptyDirectories)
         {
             FileSystemScanner fileSystemScanner3 = fileSystemScanner;
             fileSystemScanner3.ProcessDirectory = (ProcessDirectoryHandler)Delegate.Combine(fileSystemScanner3.ProcessDirectory, new ProcessDirectoryHandler(this.ProcessDirectory));
         }
         if (this.events_ != null)
         {
             if (this.events_.FileFailure != null)
             {
                 FileSystemScanner fileSystemScanner4 = fileSystemScanner;
                 fileSystemScanner4.FileFailure = (FileFailureHandler)Delegate.Combine(fileSystemScanner4.FileFailure, this.events_.FileFailure);
             }
             if (this.events_.DirectoryFailure != null)
             {
                 FileSystemScanner fileSystemScanner5 = fileSystemScanner;
                 fileSystemScanner5.DirectoryFailure = (DirectoryFailureHandler)Delegate.Combine(fileSystemScanner5.DirectoryFailure, this.events_.DirectoryFailure);
             }
         }
         fileSystemScanner.Scan(sourceDirectory, recurse);
     }
 }
Example #3
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
        /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform    = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using (outputStream_ = new ZipOutputStream(outputStream)) {
                FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += new ProcessFileDelegate(ProcessFile);
                if (this.CreateEmptyDirectories)
                {
                    scanner.ProcessDirectory += new ProcessDirectoryDelegate(ProcessDirectory);
                }

                if (events_ != null)
                {
                    if (events_.FileFailure != null)
                    {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if (events_.DirectoryFailure != null)
                    {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Example #4
0
 public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
 {
     NameTransform    = new ZipNameTransform(sourceDirectory);
     _sourceDirectory = sourceDirectory;
     using (_outputStream = new ZipOutputStream(outputStream))
     {
         if (_password != null)
         {
             _outputStream.Password = _password;
         }
         _outputStream.UseZip64 = UseZip64;
         FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
         scanner.ProcessFile =
             (ProcessFileHandler)Delegate.Combine(scanner.ProcessFile, new ProcessFileHandler(ProcessFile));
         if (CreateEmptyDirectories)
         {
             scanner.ProcessDirectory
                 = (ProcessDirectoryHandler)Delegate.Combine(scanner.ProcessDirectory,
                                                             new ProcessDirectoryHandler(ProcessDirectory));
         }
         if (_events != null)
         {
             if (_events.FileFailure != null)
             {
                 scanner.FileFailure = (FileFailureHandler)Delegate.Combine(scanner.FileFailure, _events.FileFailure);
             }
             if (_events.DirectoryFailure != null)
             {
                 scanner.DirectoryFailure = (DirectoryFailureHandler)Delegate.Combine(scanner.DirectoryFailure, _events.DirectoryFailure);
             }
         }
         scanner.Scan(sourceDirectory, recurse);
     }
 }
Example #5
0
 public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
 {
     NameTransform    = new ZipNameTransform(sourceDirectory);
     sourceDirectory_ = sourceDirectory;
     using (outputStream_ = new ZipOutputStream(outputStream))
     {
         if (password_ != null)
         {
             outputStream_.Password = password_;
         }
         outputStream_.UseZip64 = UseZip64;
         FileSystemScanner fileSystemScanner = new FileSystemScanner(fileFilter, directoryFilter);
         fileSystemScanner.ProcessFile = (ProcessFileHandler)Delegate.Combine(fileSystemScanner.ProcessFile, new ProcessFileHandler(ProcessFile));
         if (CreateEmptyDirectories)
         {
             fileSystemScanner.ProcessDirectory = (ProcessDirectoryHandler)Delegate.Combine(fileSystemScanner.ProcessDirectory, new ProcessDirectoryHandler(ProcessDirectory));
         }
         if (events_ != null)
         {
             if (events_.FileFailure != null)
             {
                 fileSystemScanner.FileFailure = (FileFailureHandler)Delegate.Combine(fileSystemScanner.FileFailure, events_.FileFailure);
             }
             if (events_.DirectoryFailure != null)
             {
                 fileSystemScanner.DirectoryFailure = (DirectoryFailureHandler)Delegate.Combine(fileSystemScanner.DirectoryFailure, events_.DirectoryFailure);
             }
         }
         fileSystemScanner.Scan(sourceDirectory, recurse);
     }
 }
Example #6
0
    void ListFileSystem(string directory, bool recurse, string fileFilter, string directoryFilter)
    {
        var scanner = new FileSystemScanner(fileFilter, directoryFilter);

        scanner.ProcessDirectory += ListDir;
        scanner.ProcessFile      += ListFile;
        scanner.Scan(directory, recurse);
    }
Example #7
0
        void ListFileSystem(string directory, bool recurse, string fileFilter, string directoryFilter)
        {
            FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);

            scanner.ProcessDirectory += new ProcessDirectoryHandler(ListDir);
            scanner.ProcessFile      += new ProcessFileHandler(ListFile);
            scanner.Scan(directory, recurse);
        }
Example #8
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
        /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            entryFactory_.NameTransform = new ZipNameTransform(sourceDirectory);
            using (outputStream_ = new ZipOutputStream(outputStream)) {
                FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += new ProcessFileHandler(ProcessFile);

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Example #9
0
        /// <summary>
        /// Add files to an archive
        /// </summary>
        /// <param name="fileSpecs">The specification for files to add.</param>
        void Add(ArrayList fileSpecs)
        {
            string zipFileName = fileSpecs[0] as string;

            if (Path.GetExtension(zipFileName).Length == 0)
            {
                zipFileName = Path.ChangeExtension(zipFileName, ".zip");
            }

            fileSpecs.RemoveAt(0);

            ZipFile zipFile;

            try
            {
                if (File.Exists(zipFileName))
                {
                    zipFile = new ZipFile(zipFileName);
                }
                else
                {
                    zipFile = ZipFile.Create(zipFileName);
                }

                using (zipFile)
                {
                    zipFile.Password = password_;
                    zipFile.UseZip64 = useZip64_;

                    zipFile.BeginUpdate();

                    activeZipFile_ = zipFile;

                    foreach (string spec in fileSpecs)
                    {
                        string path     = Path.GetDirectoryName(Path.GetFullPath(spec));
                        string fileSpec = Path.GetFileName(spec);

                        zipFile.NameTransform = new ZipNameTransform(path);

                        FileSystemScanner scanner = new FileSystemScanner(WildcardToRegex(fileSpec));
                        scanner.ProcessFile      = new ProcessFileHandler(ProcessFile);
                        scanner.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
                        scanner.Scan(path, recursive_);
                    }
                    zipFile.CommitUpdate();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Problem adding to archive - '{0}'", ex.Message);
            }
        }
Example #10
0
        /// <summary>
        /// Create archives based on specifications passed and internal state
        /// </summary>
        void Create(ArrayList fileSpecs)
        {
            string zipFileName = fileSpecs[0] as string;

            if (Path.GetExtension(zipFileName).Length == 0)
            {
                zipFileName = Path.ChangeExtension(zipFileName, ".zip");
            }

            fileSpecs.RemoveAt(0);

            if ((overwriteFiles == Overwrite.Never) && File.Exists(zipFileName))
            {
                Console.Error.WriteLine("File {0} already exists", zipFileName);
                return;
            }

            try
            {
                using (ZipFile zf = ZipFile.Create(zipFileName))
                {
                    zf.Password = password_;
                    zf.UseZip64 = useZip64_;

                    zf.BeginUpdate();

                    activeZipFile_ = zf;

                    foreach (string spec in fileSpecs)
                    {
                        // This can fail with wildcards in spec...
                        string path     = Path.GetDirectoryName(Path.GetFullPath(spec));
                        string fileSpec = Path.GetFileName(spec);

                        zf.NameTransform = new ZipNameTransform(path);

                        FileSystemScanner scanner = new FileSystemScanner(WildcardToRegex(fileSpec));
                        scanner.ProcessFile      = new ProcessFileHandler(ProcessFile);
                        scanner.ProcessDirectory = new ProcessDirectoryHandler(ProcessDirectory);
                        scanner.Scan(path, recursive_);
                    }

                    zf.CommitUpdate();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Problem creating archive - '{0}'", ex.Message);
            }
        }
Example #11
0
        /// <summary>
        /// Create a zip file.
        /// </summary>
        /// <param name="zipFileName">The name of the zip file to create.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The file filter to apply.</param>
        /// <param name="directoryFilter">The directory filter to apply.</param>
        public void CreateZip(string zipFileName, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform        = new ZipNameTransform(true, sourceDirectory);
            this.sourceDirectory = sourceDirectory;

            outputStream = new ZipOutputStream(File.Create(zipFileName));
            try {
                FileSystemScanner scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += new ProcessFileDelegate(ProcessFile);
                if (this.CreateEmptyDirectories)
                {
                    scanner.ProcessDirectory += new ProcessDirectoryDelegate(ProcessDirectory);
                }
                scanner.Scan(sourceDirectory, recurse);
            }
            finally {
                outputStream.Close();
            }
        }
Example #12
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="scanner">For performing the actual file system scan</param>
        /// <param name="leaveOpen">true to leave <paramref name="outputStream"/> open after the zip has been created, false to dispose it.</param>
        /// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
        private void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, FileSystemScanner scanner, bool leaveOpen)
        {
            NameTransform    = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using (outputStream_ = new ZipOutputStream(outputStream))
            {
                outputStream_.SetLevel((int)CompressionLevel);
                outputStream_.IsStreamOwner = !leaveOpen;
                outputStream_.NameTransform = null;                 // all required transforms handled by us

                if (false == string.IsNullOrEmpty(password_) && EntryEncryptionMethod != ZipEncryptionMethod.None)
                {
                    outputStream_.Password = password_;
                }

                outputStream_.UseZip64 = UseZip64;
                scanner.ProcessFile   += ProcessFile;
                if (this.CreateEmptyDirectories)
                {
                    scanner.ProcessDirectory += ProcessDirectory;
                }

                if (events_ != null)
                {
                    if (events_.FileFailure != null)
                    {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if (events_.DirectoryFailure != null)
                    {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Example #13
0
        /// <summary>
        /// Create a zip archive sending output to the <paramref name="outputStream"/> passed.
        /// </summary>
        /// <param name="outputStream">The stream to write archive data to.</param>
        /// <param name="sourceDirectory">The directory to source files from.</param>
        /// <param name="recurse">True to recurse directories, false for no recursion.</param>
        /// <param name="fileFilter">The <see cref="PathFilter">file filter</see> to apply.</param>
        /// <param name="directoryFilter">The <see cref="PathFilter">directory filter</see> to apply.</param>
        /// <remarks>The <paramref name="outputStream"/> is closed after creation.</remarks>
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter,
                              string directoryFilter)
        {
            NameTransform    = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;

            using (outputStream_ = new ZipOutputStream(outputStream))
            {
#if !NETCF_1_0
                if (password_ != null)
                {
                    outputStream_.Password = password_;
                }
#endif

                outputStream_.UseZip64 = UseZip64;
                var scanner = new FileSystemScanner(fileFilter, directoryFilter);
                scanner.ProcessFile += ProcessFile;
                if (CreateEmptyDirectories)
                {
                    scanner.ProcessDirectory += ProcessDirectory;
                }

                if (events_ != null)
                {
                    if (events_.FileFailure != null)
                    {
                        scanner.FileFailure += events_.FileFailure;
                    }

                    if (events_.DirectoryFailure != null)
                    {
                        scanner.DirectoryFailure += events_.DirectoryFailure;
                    }
                }

                scanner.Scan(sourceDirectory, recurse);
            }
        }
Example #14
0
        /// <summary>
        /// Create archives based on specifications passed and internal state
        /// </summary>
        void Create(string zipFileName, ArrayList fileSpecs, ref ToolStripProgressBar ProgressBar)
        {
            if (Path.GetExtension(zipFileName).Length == 0)
            {
                zipFileName = Path.ChangeExtension(zipFileName, ".zip");
            }

            try
            {
                using (ZipFile zf = ZipFile.Create(zipFileName))
                {
                    zf.BeginUpdate();

                    activeZipFile_ = zf;

                    foreach (string spec in fileSpecs)
                    {
                        // This can fail with wildcards in spec...
                        string path     = Path.GetDirectoryName(Path.GetFullPath(spec));
                        string fileSpec = Path.GetFileName(spec);

                        zf.NameTransform = new ZipNameTransform(Settings.Global.Properties["POLPath"]);

                        FileSystemScanner scanner = new FileSystemScanner(fileSpec);
                        scanner.ProcessFile = new ProcessFileHandler(ProcessFile);
                        scanner.Scan(path, false);
                        ProgressBar.PerformStep();
                    }
                    zf.CommitUpdate();
                    ProgressBar.PerformStep();
                }
            }
            catch (Exception ex)
            {
                ExceptionBox.ExceptionForm tmp = new ExceptionBox.ExceptionForm(ref ex);
                tmp.ShowDialog();
            }
        }
Example #15
0
        public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse, string fileFilter, string directoryFilter)
        {
            NameTransform    = new ZipNameTransform(sourceDirectory);
            sourceDirectory_ = sourceDirectory;
            ZipOutputStream zipOutputStream = (outputStream_ = new ZipOutputStream(outputStream));

            try
            {
                if (password_ != null)
                {
                    outputStream_.Password = password_;
                }
                outputStream_.UseZip64 = UseZip64;
                FileSystemScanner fileSystemScanner = new FileSystemScanner(fileFilter, directoryFilter);
                fileSystemScanner.ProcessFile = (ProcessFileHandler)global::System.Delegate.Combine((global::System.Delegate)fileSystemScanner.ProcessFile, (global::System.Delegate) new ProcessFileHandler(ProcessFile));
                if (CreateEmptyDirectories)
                {
                    fileSystemScanner.ProcessDirectory = (ProcessDirectoryHandler)global::System.Delegate.Combine((global::System.Delegate)fileSystemScanner.ProcessDirectory, (global::System.Delegate) new ProcessDirectoryHandler(ProcessDirectory));
                }
                if (events_ != null)
                {
                    if (events_.FileFailure != null)
                    {
                        fileSystemScanner.FileFailure = (FileFailureHandler)global::System.Delegate.Combine((global::System.Delegate)fileSystemScanner.FileFailure, (global::System.Delegate)events_.FileFailure);
                    }
                    if (events_.DirectoryFailure != null)
                    {
                        fileSystemScanner.DirectoryFailure = (DirectoryFailureHandler)global::System.Delegate.Combine((global::System.Delegate)fileSystemScanner.DirectoryFailure, (global::System.Delegate)events_.DirectoryFailure);
                    }
                }
                fileSystemScanner.Scan(sourceDirectory, recurse);
            }
            finally
            {
                ((global::System.IDisposable)zipOutputStream)?.Dispose();
            }
        }
    public static void EachAllFile(System.Action <string> fun)
    {
#if UNITY_EDITOR || UNITY_IPHONE || UNITY_STANDALONE
        string            fullfile = ResourcesPath.streamingAssetsPath;
        FileSystemScanner scanner  = new FileSystemScanner("", "");
        scanner.ProcessFile =
            (object sender, ScanEventArgs e) =>
        {
            fun(e.Name.Substring(fullfile.Length).Replace('\\', '/'));
        };

        scanner.Scan(fullfile, true);
#elif UNITY_ANDROID // 安卓平台的,数据存储在apk包当中
        Init();
        ApkFile.EachAllFile(
            (string file) =>
        {
            if (file.StartsWith("assets/"))
            {
                fun(file.Substring(7));
            }
        });
#endif
    }
Example #17
0
    private void Search(string directory, Regex regex, string pattern, bool ignoreCase, string filter, string ignoreDirs)
    {
        positions                    = new List <Position>();
        buffer                       = new Buffer(null, "Find in files results", SettingsMode.Normal);
        buffer.showEncoding          = false;
        buffer.Controller.isReadonly = true;
        lines = buffer.Controller.Lines;
        lines.ClearAllUnsafely();

        bool needCutCurrent = false;

        if (string.IsNullOrEmpty(directory))
        {
            directory      = Directory.GetCurrentDirectory();
            needCutCurrent = true;
        }

        FileSystemScanner scanner = new FileSystemScanner(directory, filter, ignoreDirs);

        scanner.Scan();
        if (scanner.error != null)
        {
            AddLine("Error: File list reading error: " + scanner.error, Ds.Error);
        }
        fsScanComplete = true;

        string currentDirectory       = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
        List <IndexAndLength> indices = new List <IndexAndLength>();
        CompareInfo           ci      = ignoreCase ? CultureInfo.InvariantCulture.CompareInfo : null;
        int    remainsMatchesCount    = 2000000;
        string stopReason             = null;

        foreach (string file in scanner.files)
        {
            if (isStopped)
            {
                if (stopReason == null)
                {
                    stopReason = "STOPPED";
                }
                break;
            }
            string text = null;
            try
            {
                text = File.ReadAllText(file);
            }
            catch (IOException e)
            {
                --remainsMatchesCount;
                if (remainsMatchesCount < 0)
                {
                    isStopped  = true;
                    stopReason = "TOO MANY LINES";
                    break;
                }
                AddLine(file + ": " + e.Message, Ds.Error);
                continue;
            }
            indices.Clear();
            if (regex != null)
            {
                MatchCollection matches = regex.Matches(text);
                if (matches.Count == 0)
                {
                    continue;
                }
                foreach (Match match in matches)
                {
                    indices.Add(new IndexAndLength(match.Index, match.Length));
                }
            }
            else
            {
                int index = ci != null?ci.IndexOf(text, pattern, CompareOptions.IgnoreCase) : text.IndexOf(pattern);

                if (index == -1)
                {
                    continue;
                }
                while (true)
                {
                    indices.Add(new IndexAndLength(index, pattern.Length));
                    index = ci != null?ci.IndexOf(text, pattern, index + 1, CompareOptions.IgnoreCase) : text.IndexOf(pattern, index + 1);

                    if (index == -1)
                    {
                        break;
                    }
                }
            }
            string path = file;
            if (needCutCurrent && path.StartsWith(currentDirectory))
            {
                path = file.Substring(currentDirectory.Length);
            }
            int offset           = 0;
            int currentLineIndex = 0;
            foreach (IndexAndLength indexAndLength in indices)
            {
                int index   = indexAndLength.index;
                int length  = indexAndLength.length;
                int lineEnd = -1;
                while (true)
                {
                    int nIndex = text.IndexOf('\n', offset);
                    int rIndex = text.IndexOf('\r', offset);
                    if (nIndex == -1 && rIndex == -1)
                    {
                        lineEnd = text.Length;
                        break;
                    }
                    int nrIndex = System.Math.Min(nIndex, rIndex);
                    if (nrIndex == -1)
                    {
                        nrIndex = nIndex != -1 ? nIndex : rIndex;
                    }
                    if (nrIndex > index)
                    {
                        lineEnd = nrIndex;
                        break;
                    }
                    currentLineIndex++;
                    if (nrIndex == nIndex)
                    {
                        offset = nIndex + 1;
                    }
                    else
                    {
                        if (rIndex + 1 < text.Length || text[rIndex + 1] == '\n')
                        {
                            offset = rIndex + 2;
                        }
                        else
                        {
                            offset = rIndex + 1;
                        }
                    }
                }

                --remainsMatchesCount;
                if (remainsMatchesCount < 0)
                {
                    isStopped  = true;
                    stopReason = "TOO MANY MATCHES";
                    break;
                }

                int trimOffset      = 0;
                int rightTrimOffset = 0;
                int lineLength      = lineEnd - offset;
                if (index - offset - MaxPartChars > 0)
                {
                    trimOffset = index - offset - MaxPartChars;
                }
                if (lineLength - index + offset - length - MaxPartChars > 0)
                {
                    rightTrimOffset = lineLength - index + offset - length - MaxPartChars;
                }
                if (trimOffset == 0)
                {
                    int whitespaceLength = CommonHelper.GetFirstSpaces(text, offset, lineLength - rightTrimOffset);
                    if (whitespaceLength > 0 && whitespaceLength <= index - offset)
                    {
                        trimOffset = whitespaceLength;
                    }
                }
                positions.Add(new Position(file, new Place(index - offset, currentLineIndex), length));

                int index0  = offset + trimOffset;
                int length0 = lineLength - trimOffset - rightTrimOffset;
                AddLine(path, (currentLineIndex + 1) + " " + (index - offset + 1), text, index0, length0, index - offset - trimOffset, length);
            }
        }
        if (isStopped)
        {
            AddLine("…", Ds.Normal);
            AddLine(stopReason, Ds.Error);
        }
        if (lines.LinesCount == 0)
        {
            lines.AddLineUnsafely(new Line(32));
        }
        else
        {
            lines.CutLastLineBreakUnsafely();
        }
        buffer.additionKeyMap = new KeyMap();
        {
            KeyAction action = new KeyAction("F&ind\\Navigate to found", ExecuteEnter, null, false);
            buffer.additionKeyMap.AddItem(new KeyItem(Keys.Enter, null, action));
            buffer.additionKeyMap.AddItem(new KeyItem(Keys.None, null, action).SetDoubleClick(true));
        }
        finishBuffer = buffer;
        mainForm.Invoke(new Setter(CloseAlert));
    }