コード例 #1
0
ファイル: main.cs プロジェクト: surak8/unix
        private static int grepText(string szFile, string szText, string szRegex, GrepOpts go, bool bMultiFile)
        {
            int ret = 0;

            if (validString(szText) && validString(szRegex))
            {
                string[] aszLines = szText.Split(new char[] { '\n' });
                int      nLines = (aszLines == null?0:aszLines.Length);
                bool     bWroteFilename = false, bNeedNL = false;

                if (nLines > 0)
                {
                    Match        m;
                    RegexOptions ro = go.ignoreCase?RegexOptions.IgnoreCase:RegexOptions.None;

                    for (int i = 0; i < nLines; i++)
                    {
                        if ((m = Regex.Match(aszLines[i], szRegex, ro)) != Match.Empty)
                        {
                            ret++;
                            if (bMultiFile || go.fileName)
                            {
                                if (!bWroteFilename)
                                {
                                    bWroteFilename = true;
                                    Console.WriteLine("file {0}:{1}", szFile, go.extraLine?"\n":"");
                                }
                            }
                            if (go.lineNumbers && !go.fileName)
                            {
                                Console.Write("{0}:", i);
                            }
                            if (!(go.fileName && go.lineNumbers))
                            {
                                Console.WriteLine(aszLines[i]);
                                bNeedNL = true;
                            }
                        }
                    }
                    if (/*go.extraLine && */ ret > 0 && bNeedNL)
                    {
                        Console.WriteLine();
                    }
                }
            }
            else
            {
                if (!validString(szText))
                {
                    Console.Error.WriteLine("file {0} is empty?", szFile);
                }
                if (!validString(szRegex))
                {
                    Console.Error.WriteLine("invalid regex here");
                }

//                Console.Error.WriteLine("invalid string or regexp here!");
            }
            return(ret);
        }
コード例 #2
0
ファイル: main.cs プロジェクト: surak8/unix
        private static int grepStuff(string szRegex, string szFileDesc, GrepOpts go)
        {
            int    ret = 0;
            string szPath, szWildcard;

            Trace.WriteLine(string.Format("regex='{0}', filedes='{1}'", szRegex, szFileDesc));
            if (splitFileDescription(szFileDesc, out szPath, out szWildcard))
            {
                if (Directory.Exists(szPath))
                {
                    string[] files = Directory.GetFiles(szPath, szWildcard);

                    if (files == null)
                    {
                        Console.Error.WriteLine("{0}: no files matching '{1}'.", szPath, szWildcard);
                    }
                    else
                    {
                        foreach (string szFile in files)
                        {
                            ret += doGrep(szFile, szRegex, go, files.Length > 1);
                        }
                    }
                }
                else
                {
                    Console.Error.WriteLine("Directory {0} does not exist!", szPath);
                }
            }
            return(ret);
        }
コード例 #3
0
ファイル: main.cs プロジェクト: surak8/unix
        private static int doGrep(string szFile, string szRegex, GrepOpts go, bool bMultiFile)
        {
            int          ret = 0;
            StreamReader sr  = null;

            try {
                sr = new StreamReader(szFile);
            } catch (Exception ex) {
                Console.Error.WriteLine("StreamReader({0}) failed [{1}]!",
                                        szFile, ex.Message);
            }
            if (sr != null)
            {
                string szContent = sr.ReadToEnd();

                //				go.fileName=szFile;
                sr.Close();
                ret = grepText(szFile, szContent, szRegex, go, bMultiFile);
            }
            return(ret);
        }
コード例 #4
0
ファイル: main.cs プロジェクト: surak8/unix
        // entry-point comment
        /// entry-point comment
        public static void Main(string[] args)
        {
            int      nArgs, nExitCode;
            bool     bShowUsage, bShowVersion, bTrace;
            GrepOpts go;

            go           = new GrepOpts();
            nArgs        = args == null?0:args.Length;
            nExitCode    = 0;
            bShowUsage   = false;
            bShowVersion = false;
            bTrace       = false;

            if (nArgs > 0)
            {
                int c;
                while ((c = GetOpt.getopt(args, "hilntv?")) >= 0)
                {
                    switch (c)
                    {
                    case 'i': go.ignoreCase = true; break;

                    case 'l': go.lineNumbers = true; break;

                    case 'n': go.fileName = true; break;

                    case 'v': bShowVersion = !bShowVersion; break;

                    case 't': bTrace = true; break;

                    case 'x': go.extraLine = true; break;

                    case 'h':
                    case '?': nExitCode = 0; bShowUsage = true; break;

                    default:
                        nExitCode  = 1;
                        bShowUsage = true;
                        break;
                    }
                }
            }
            if (bShowUsage)
            {
                usage(nExitCode);
            }
            else
            {
                if (GetOpt.optind >= nArgs)
                {
                    usage(1);
                }
                else
                {
                    string szRegex;
                    int    i, nFound = 0;
#if TRACE
                    TextWriterTraceListener twtl;

                    twtl            = null;
                    Trace.AutoFlush = true;
                    if (bTrace)
                    {
                        try {
                            twtl = new TextWriterTraceListener(Console.Error);
                            Trace.Listeners.Add(twtl);
                        } catch (Exception ex) {
                            Console.Error.WriteLine("{0}\n{1}{2}\n{3}",
                                                    ex.GetType().FullName,
                                                    ex.Message,
                                                    ex.InnerException == null?string.Empty:" [" + ex.InnerException.Message + "]",
                                                    ex.StackTrace);
                        }
                    }
#endif

                    if (bShowVersion)
                    {
                        Console.WriteLine(buildDisplayString(Assembly.GetExecutingAssembly()));
                    }
                    i       = GetOpt.optind;
                    szRegex = args[i++];
                    if (i >= nArgs)
                    {
                        Console.Error.WriteLine("reading 'stdin' for regex['{0}'].", szRegex);
                        nFound    = grepText("stdin", Console.In.ReadToEnd(), szRegex, go, false);
                        nExitCode = nFound > 0?0:2;
                    }
                    else
                    {
                        for (; i < nArgs; i++)
                        {
                            nFound += grepStuff(szRegex, args[i], go);
                        }
                        nExitCode = nFound > 0?0:2;
                    }
#if TRACE
                    if (bTrace)
                    {
                        if (twtl != null)
                        {
                            twtl.Flush();
                            Trace.Listeners.Remove(twtl);
                        }
                    }
#endif
                }
            }
            Environment.Exit(nExitCode);
        }