Ejemplo n.º 1
0
        static int MainImpl()
        {
            // Validate arguments.
            if (Global.Options.FilePattern == null || Global.Options.RegexPattern == null)
            {
                throw new ApplicationException("Please specify at least a pathspec and regex pattern.");
            }

            // Echo command line options.
            ConsoleWriter.Out.Write(@"Searching directory ""{0}"", files ""{1}"", for pattern ""{2}"" (options: ",
                                    Global.Options.BaseDirectory, Global.Options.FilePattern, Global.Options.RegexPattern);

            System.Collections.ArrayList optionstrings = new System.Collections.ArrayList();

            if (Global.Options.RegexIgnoreCase)
            {
                optionstrings.Add("IgnoreCase");
            }
            if (Global.Options.RegexSingleline)
            {
                optionstrings.Add("Singleline");
            }
            if (Global.Options.RegexMimicVS)
            {
                optionstrings.Add("RegexMimicVS");
            }
            if (Global.Options.IncludeHidden)
            {
                optionstrings.Add("IncludeHidden");
            }
            if (Global.Options.DoRecursion)
            {
                optionstrings.Add("DoRecursion");
            }
            optionstrings.Add(String.Format("OutputWidth: {0}", Global.Options.OutputWidth));
            if (Global.Options.ReplacementText != null)
            {
                optionstrings.Add(String.Format("Replacement pattern: {0}", Global.Options.ReplacementText));
            }

            if (optionstrings.Count == 0)
            {
                optionstrings.Add("none");
            }

            ConsoleWriter.Out.Write(String.Join(", ", (string[])optionstrings.ToArray(typeof(string))));
            ConsoleWriter.Out.WriteLine(")");

            // Reimplement VS's most useful helper patterns: C identifiers, number literals, and quoted strings.
            if (Global.Options.RegexMimicVS)
            {
                Global.Options.RegexPattern = Global.Options.RegexPattern.Replace("::", "\x003A");
                Global.Options.RegexPattern = Global.Options.RegexPattern.Replace(":i", @"(\b[_a-zA-Z][_a-zA-Z0-9]*\b)");
                Global.Options.RegexPattern = Global.Options.RegexPattern.Replace(":n", @"(\b[0-9]+(\.[0-9]+)?([Ee](\+|-)?[0-9]+)?\b)");
                Global.Options.RegexPattern = Global.Options.RegexPattern.Replace(":q", @"((@""(""""|[^""])*"")|(""(\\.|[^""\\])*""))");
            }

            // Create the regex, and populate the visitor callback with it.
            RegexOptions ro = RegexOptions.Compiled | RegexOptions.Multiline;

            if (Global.Options.RegexIgnoreCase)
            {
                ro |= RegexOptions.IgnoreCase;
            }
            if (Global.Options.RegexSingleline)
            {
                ro |= RegexOptions.Singleline;
            }

            FileVisitor visitor = new FileVisitor();

            visitor.UserRegex       = new Regex(Global.Options.RegexPattern, ro);
            visitor.ReplacementText = Global.Options.ReplacementText;

            visitor.OutputWidth = Global.Options.OutputWidth;

            // Do the dew!
            Jitsu.IO.DirectoryTraverser.FileVisitor onFile =
                new Jitsu.IO.DirectoryTraverser.FileVisitor(visitor.VisitFile);
            Jitsu.IO.DirectoryTraverser spider =
                new Jitsu.IO.DirectoryTraverser(null, onFile);

            string directoryPattern = (Global.Options.DoRecursion) ? "*" : String.Empty;

            spider.TraverseBreadthFirst(Global.Options.BaseDirectory, Global.Options.FilePattern, directoryPattern);

            // Report the totals.
            ConsoleWriter.Out.WriteLine();
            ConsoleWriter.Out.WriteLine("Total found: {0}    Matching files: {1}    Total files searched: {2}",
                                        visitor.MatchesFound, visitor.FilesFound, visitor.FilesSearched);

            // Done.
            return(0);
        }
Ejemplo n.º 2
0
        static int MainImpl()
        {
            // Validate arguments.
            if ( Global.Options.FilePattern == null || Global.Options.RegexPattern == null )
                throw new ApplicationException( "Please specify at least a pathspec and regex pattern." );

            // Echo command line options.
            ConsoleWriter.Out.Write( @"Searching directory ""{0}"", files ""{1}"", for pattern ""{2}"" (options: ",
                Global.Options.BaseDirectory, Global.Options.FilePattern, Global.Options.RegexPattern );

            System.Collections.ArrayList optionstrings = new System.Collections.ArrayList();

            if ( Global.Options.RegexIgnoreCase )
                optionstrings.Add( "IgnoreCase" );
            if ( Global.Options.RegexSingleline )
                optionstrings.Add( "Singleline" );
            if ( Global.Options.RegexMimicVS )
                optionstrings.Add( "RegexMimicVS" );
            if ( Global.Options.IncludeHidden )
                optionstrings.Add( "IncludeHidden" );
            if ( Global.Options.DoRecursion )
                optionstrings.Add( "DoRecursion" );
            optionstrings.Add( String.Format( "OutputWidth: {0}", Global.Options.OutputWidth ) );
            if ( Global.Options.ReplacementText != null )
                optionstrings.Add( String.Format( "Replacement pattern: {0}", Global.Options.ReplacementText ) );

            if ( optionstrings.Count == 0 )
                optionstrings.Add( "none" );

            ConsoleWriter.Out.Write( String.Join( ", ", (string[])optionstrings.ToArray( typeof( string ) ) ) );
            ConsoleWriter.Out.WriteLine( ")" );

            // Reimplement VS's most useful helper patterns: C identifiers, number literals, and quoted strings.
            if ( Global.Options.RegexMimicVS )
            {
                Global.Options.RegexPattern = Global.Options.RegexPattern.Replace( "::", "\x003A" );
                Global.Options.RegexPattern = Global.Options.RegexPattern.Replace( ":i", @"(\b[_a-zA-Z][_a-zA-Z0-9]*\b)" );
                Global.Options.RegexPattern = Global.Options.RegexPattern.Replace( ":n", @"(\b[0-9]+(\.[0-9]+)?([Ee](\+|-)?[0-9]+)?\b)" );
                Global.Options.RegexPattern = Global.Options.RegexPattern.Replace( ":q", @"((@""(""""|[^""])*"")|(""(\\.|[^""\\])*""))" );
            }

            // Create the regex, and populate the visitor callback with it.
            RegexOptions ro = RegexOptions.Compiled | RegexOptions.Multiline;
            if ( Global.Options.RegexIgnoreCase )
                ro |= RegexOptions.IgnoreCase;
            if ( Global.Options.RegexSingleline )
                ro |= RegexOptions.Singleline;

            FileVisitor visitor = new FileVisitor();
            visitor.UserRegex = new Regex( Global.Options.RegexPattern, ro );
            visitor.ReplacementText = Global.Options.ReplacementText;

            visitor.OutputWidth = Global.Options.OutputWidth;

            // Do the dew!
            Jitsu.IO.DirectoryTraverser.FileVisitor onFile =
                new Jitsu.IO.DirectoryTraverser.FileVisitor( visitor.VisitFile );
            Jitsu.IO.DirectoryTraverser spider =
                new Jitsu.IO.DirectoryTraverser( null, onFile );

            string directoryPattern = ( Global.Options.DoRecursion ) ? "*" : String.Empty;
            spider.TraverseBreadthFirst( Global.Options.BaseDirectory, Global.Options.FilePattern, directoryPattern );

            // Report the totals.
            ConsoleWriter.Out.WriteLine();
            ConsoleWriter.Out.WriteLine( "Total found: {0}    Matching files: {1}    Total files searched: {2}",
                visitor.MatchesFound, visitor.FilesFound, visitor.FilesSearched );

            // Done.
            return 0;
        }