/// <summary> /// Searches for the given pattern in the file starting at the /// start line. /// /// Will stop searching after max successful lines are emitted. /// /// If max lines are never emitted then will search until the /// end of the file. This may unfortunately be slow when the /// file is very large. /// </summary> /// <param name="server">The server to convert to real file paths</param> /// <param name="tildeFilePath">The tilde file path</param> /// <param name="pattern">The search pattern</param> /// <param name="isRegex">If true, the pattern is a regex</param> /// <param name="ignoreCase">If true, do case-insensitive search</param> /// <param name="StartLine">The start line for the list</param> /// <param name="MaxLines">The maximum number of lines to list</param> /// <param name="showLineNumbers">If true, show line numbers</param> /// <returns></returns> public static string ListLinesFromSearch (HttpServerUtility server, string tildeFilePath, string pattern, bool isRegex, bool ignoreCase, long StartLine, long MaxLines, bool showLineNumbers, bool showAllLines) { if (StringTools.IsTrivial(pattern)) { return(""); } StringBuilder builder = new StringBuilder(); string filePath = null; FileInfo info = null; if (TildeFilePathExistsAndIsText(server, tildeFilePath, ref filePath, ref info)) { if (StartLine < 1) { StartLine = 1; } if (MaxLines < 1) { MaxLines = 1; } long LineNumber = 1; long FoundMatch = 0; using (StreamReader reader = new StreamReader(filePath)) { // Skip lines up to StartLine while ((LineNumber < StartLine) && !reader.EndOfStream) { string foobar = reader.ReadLine(); LineNumber++; } if (reader.EndOfStream) { NoLinesErrorMessage(builder); } else { builder.Append("<pre>"); while ((FoundMatch < MaxLines) && !reader.EndOfStream) { string line = reader.ReadLine(); List <Range> range = SearchTools.Search (line, pattern, 0, isRegex, ignoreCase); if (showAllLines || (range.Count > 0)) { string result = StringTools.HighlightMarkupForOneLine (line, range, 4, showLineNumbers, LineNumber); builder.Append(result); builder.Append("\n"); FoundMatch++; } LineNumber++; } if (FoundMatch > 0) { builder.Append("</pre>\n"); } else { builder = new StringBuilder(); NoLinesErrorMessage(builder); } } } } else { TildeFilePathErrorMessage(builder); } return(builder.ToString()); }
/// <summary> /// Return the HTML string to list consecutive lines in the file. /// /// The returned string has all required HTML to plug into a page. /// </summary> /// <param name="server">The server to convert to real file paths</param> /// <param name="tildeFilePath">The tilde file path</param> /// <param name="StartLine">The start line for the list</param> /// <param name="MaxLines">The maximum number of lines to list</param> /// <param name="showLineNumbers">If true, show line numbers</param> public static string ListLines (HttpServerUtility server, string tildeFilePath, long StartLine, long MaxLines, bool showLineNumbers) { StringBuilder builder = new StringBuilder(); string filePath = null; FileInfo info = null; if (TildeFilePathExistsAndIsText(server, tildeFilePath, ref filePath, ref info)) { if (StartLine < 1) { StartLine = 1; } if (MaxLines < 1) { MaxLines = 1; } long LineNumber = 1; using (StreamReader reader = new StreamReader(filePath)) { // Skip lines up to StartLine while ((LineNumber < StartLine) && !reader.EndOfStream) { string foobar = reader.ReadLine(); LineNumber++; } if (reader.EndOfStream) { NoLinesErrorMessage(builder); } else { builder.Append("<pre>"); long FinishLine = StartLine + MaxLines - 1; while ((LineNumber <= FinishLine) && !reader.EndOfStream) { string line = reader.ReadLine(); string result = StringTools.HighlightMarkupForOneLine (line, null, 4, showLineNumbers, LineNumber); builder.Append(result); builder.Append("\n"); LineNumber++; } builder.Append("</pre>\n"); } } } else { TildeFilePathErrorMessage(builder); } return(builder.ToString()); }