示例#1
0
        public int Replace(IEnumerable <string> files, SearchType searchType, string baseFolder, string searchPattern, string replacePattern, GrepSearchOption searchOptions, int codePage)
        {
            string tempFolder = Utils.GetTempFolder();

            if (Directory.Exists(tempFolder))
            {
                Utils.DeleteFolder(tempFolder);
            }
            Directory.CreateDirectory(tempFolder);

            if (files == null || !Directory.Exists(tempFolder) || !Directory.Exists(baseFolder))
            {
                return(0);
            }

            baseFolder     = Utils.FixFolderName(baseFolder);
            tempFolder     = Utils.FixFolderName(tempFolder);
            replacePattern = Utils.ReplaceSpecialCharacters(replacePattern);

            int processedFiles = 0;

            Utils.CancelSearch = false;

            try
            {
                foreach (string file in files)
                {
                    if (!file.Contains(baseFolder))
                    {
                        continue;
                    }

                    string      tempFileName = file.Replace(baseFolder, tempFolder);
                    IGrepEngine engine       = GrepEngineFactory.GetReplaceEngine(file, searchParams);

                    try
                    {
                        processedFiles++;
                        // Copy file
                        Utils.CopyFile(file, tempFileName, true);
                        Utils.DeleteFile(file);

                        Encoding encoding = null;
                        if (codePage == -1)
                        {
                            encoding = Utils.GetFileEncoding(tempFileName);
                        }
                        else
                        {
                            encoding = Encoding.GetEncoding(codePage);
                        }


                        if (Utils.CancelSearch)
                        {
                            break;
                        }

                        if (!engine.Replace(tempFileName, file, searchPattern, replacePattern, searchType, searchOptions, encoding))
                        {
                            throw new ApplicationException("Replace failed for file: " + file);
                        }

                        if (!Utils.CancelSearch && ProcessedFile != null)
                        {
                            ProcessedFile(this, new ProgressStatus(processedFiles, null));
                        }


                        File.SetAttributes(file, File.GetAttributes(tempFileName));

                        if (Utils.CancelSearch)
                        {
                            // Replace the file
                            Utils.DeleteFile(file);
                            Utils.CopyFile(tempFileName, file, true);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.LogException(LogLevel.Error, ex.Message, ex);
                        try
                        {
                            // Replace the file
                            if (File.Exists(tempFileName) && File.Exists(file))
                            {
                                Utils.DeleteFile(file);
                                Utils.CopyFile(tempFileName, file, true);
                            }
                        }
                        catch
                        {
                            // DO NOTHING
                        }
                        return(-1);
                    }
                }
            }
            finally
            {
                GrepEngineFactory.UnloadEngines();
            }

            return(processedFiles);
        }
示例#2
0
        public int Replace(IEnumerable <ReplaceDef> files, SearchType searchType, string searchPattern, string replacePattern, GrepSearchOption searchOptions, int codePage)
        {
            string tempFolder = Utils.GetTempFolder();

            if (files == null || !files.Any() || !Directory.Exists(tempFolder))
            {
                return(0);
            }

            GrepEngineBase.ResetGuidxCache();

            replacePattern = Utils.ReplaceSpecialCharacters(replacePattern);

            int processedFiles = 0;

            Utils.CancelSearch = false;
            string tempFileName = null;

            try
            {
                foreach (var item in files)
                {
                    ProcessedFile(this, new ProgressStatus(true, processedFiles, processedFiles, null, item.OrginalFile));

                    // the value in the files dictionary is the temp file name assigned by
                    // the caller for any possible Undo operation
                    tempFileName = Path.Combine(tempFolder, item.BackupName);
                    IGrepEngine engine = GrepEngineFactory.GetReplaceEngine(item.OrginalFile, SearchParams, FileFilter);

                    try
                    {
                        processedFiles++;
                        // Copy file
                        Utils.CopyFile(item.OrginalFile, tempFileName, true);
                        Utils.DeleteFile(item.OrginalFile);

                        Encoding encoding = Encoding.Default;
                        if (codePage > -1)
                        {
                            encoding = Encoding.GetEncoding(codePage);
                        }
                        else if (!Utils.IsBinary(tempFileName))
                        {
                            encoding = Utils.GetFileEncoding(tempFileName);
                        }

                        // The UTF-8 encoding returned from Encoding.GetEncoding("utf-8") includes the BOM - see Encoding.GetPreamble()
                        // If this file does not have the BOM, then change to an encoder without the BOM so the BOM is not added in
                        // the replace operation
                        if (encoding is UTF8Encoding && !Utils.HasUtf8ByteOrderMark(tempFileName))
                        {
                            encoding = new UTF8Encoding(false);
                        }

                        if (Utils.CancelSearch)
                        {
                            break;
                        }

                        if (!engine.Replace(tempFileName, item.OrginalFile, searchPattern, replacePattern, searchType, searchOptions, encoding, item.ReplaceItems))
                        {
                            throw new ApplicationException("Replace failed for file: " + item.OrginalFile);
                        }

                        if (!Utils.CancelSearch)
                        {
                            ProcessedFile(this, new ProgressStatus(false, processedFiles, processedFiles, null, item.OrginalFile));
                        }


                        File.SetAttributes(item.OrginalFile, File.GetAttributes(tempFileName));

                        GrepEngineFactory.ReturnToPool(item.OrginalFile, engine);

                        if (Utils.CancelSearch)
                        {
                            // Replace the file
                            Utils.DeleteFile(item.OrginalFile);
                            Utils.CopyFile(tempFileName, item.OrginalFile, true);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Log <Exception>(LogLevel.Error, ex.Message, ex);
                        try
                        {
                            // Replace the file
                            if (File.Exists(tempFileName) && File.Exists(item.OrginalFile))
                            {
                                Utils.DeleteFile(item.OrginalFile);
                                Utils.CopyFile(tempFileName, item.OrginalFile, true);
                            }
                        }
                        catch
                        {
                            // DO NOTHING
                        }
                        return(-1);
                    }
                }
            }
            finally
            {
                GrepEngineFactory.UnloadEngines();
            }

            return(processedFiles);
        }
示例#3
0
        public int Replace(Dictionary <string, string> files, SearchType searchType, string searchPattern, string replacePattern, GrepSearchOption searchOptions, int codePage)
        {
            string tempFolder = Utils.GetTempFolder();

            if (files == null || files.Count == 0 || !Directory.Exists(tempFolder))
            {
                return(0);
            }

            replacePattern = Utils.ReplaceSpecialCharacters(replacePattern);

            int processedFiles = 0;

            Utils.CancelSearch = false;
            string tempFileName = null;

            try
            {
                foreach (string file in files.Keys)
                {
                    ProcessedFile(this, new ProgressStatus(true, processedFiles, processedFiles, null, file));

                    // the value in the files dictionary is the temp file name assigned by
                    // the caller for any possible Undo operation
                    tempFileName = Path.Combine(tempFolder, files[file]);
                    IGrepEngine engine = GrepEngineFactory.GetReplaceEngine(file, SearchParams, FileFilter);

                    try
                    {
                        processedFiles++;
                        // Copy file
                        Utils.CopyFile(file, tempFileName, true);
                        Utils.DeleteFile(file);

                        Encoding encoding = Encoding.Default;
                        if (codePage > -1)
                        {
                            encoding = Encoding.GetEncoding(codePage);
                        }
                        else if (!Utils.IsBinary(tempFileName))
                        {
                            encoding = Utils.GetFileEncoding(tempFileName);
                        }

                        if (Utils.CancelSearch)
                        {
                            break;
                        }

                        if (!engine.Replace(tempFileName, file, searchPattern, replacePattern, searchType, searchOptions, encoding))
                        {
                            throw new ApplicationException("Replace failed for file: " + file);
                        }

                        if (!Utils.CancelSearch)
                        {
                            ProcessedFile(this, new ProgressStatus(false, processedFiles, processedFiles, null, file));
                        }


                        File.SetAttributes(file, File.GetAttributes(tempFileName));

                        GrepEngineFactory.ReturnToPool(file, engine);

                        if (Utils.CancelSearch)
                        {
                            // Replace the file
                            Utils.DeleteFile(file);
                            Utils.CopyFile(tempFileName, file, true);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Log <Exception>(LogLevel.Error, ex.Message, ex);
                        try
                        {
                            // Replace the file
                            if (File.Exists(tempFileName) && File.Exists(file))
                            {
                                Utils.DeleteFile(file);
                                Utils.CopyFile(tempFileName, file, true);
                            }
                        }
                        catch
                        {
                            // DO NOTHING
                        }
                        return(-1);
                    }
                }
            }
            finally
            {
                GrepEngineFactory.UnloadEngines();
            }

            return(processedFiles);
        }
示例#4
0
        public int Replace(IEnumerable <string> files, SearchType searchType, string searchPattern, string replacePattern, GrepSearchOption searchOptions, int codePage)
        {
            string tempFolder = Utils.GetTempFolder();

            if (files == null || !Directory.Exists(tempFolder))
            {
                return(0);
            }

            replacePattern = Utils.ReplaceSpecialCharacters(replacePattern);

            int processedFiles = 0;

            Utils.CancelSearch = false;
            string tempFileName = null;

            try
            {
                foreach (string file in files)
                {
                    ProcessedFile(this, new ProgressStatus(processedFiles, null, file));

                    tempFileName = Path.Combine(tempFolder, Path.GetFileName(file));
                    IGrepEngine engine = GrepEngineFactory.GetReplaceEngine(file, SearchParams, FileFilter);

                    try
                    {
                        processedFiles++;
                        // Copy file
                        Utils.CopyFile(file, tempFileName, true);
                        Utils.DeleteFile(file);

                        Encoding encoding = Encoding.Default;
                        if (codePage > -1)
                        {
                            encoding = Encoding.GetEncoding(codePage);
                        }
                        else if (!Utils.IsBinary(file))
                        {
                            encoding = Utils.GetFileEncoding(tempFileName);
                        }

                        if (Utils.CancelSearch)
                        {
                            break;
                        }

                        if (!engine.Replace(tempFileName, file, searchPattern, replacePattern, searchType, searchOptions, encoding))
                        {
                            throw new ApplicationException("Replace failed for file: " + file);
                        }

                        if (!Utils.CancelSearch)
                        {
                            ProcessedFile(this, new ProgressStatus(processedFiles, null, file));
                        }


                        File.SetAttributes(file, File.GetAttributes(tempFileName));

                        if (Utils.CancelSearch)
                        {
                            // Replace the file
                            Utils.DeleteFile(file);
                            Utils.CopyFile(tempFileName, file, true);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Log <Exception>(LogLevel.Error, ex.Message, ex);
                        try
                        {
                            // Replace the file
                            if (File.Exists(tempFileName) && File.Exists(file))
                            {
                                Utils.DeleteFile(file);
                                Utils.CopyFile(tempFileName, file, true);
                            }
                        }
                        catch
                        {
                            // DO NOTHING
                        }
                        return(-1);
                    }
                    finally
                    {
                        try
                        {
                            if (!string.IsNullOrWhiteSpace(tempFileName))
                            {
                                Utils.DeleteFile(tempFileName);
                            }
                        }
                        catch
                        {
                            // DO NOTHING
                        }
                    }
                }
            }
            finally
            {
                GrepEngineFactory.UnloadEngines();
            }

            return(processedFiles);
        }