public ProcessListParser(IList<string> lines) { // We at least should have a line for the header, a system process, and 'ps' if (lines.Count < 3) { throw GetException(); } _lines = lines; TextColumn[] columns = TextColumn.TryParseHeader(lines[0]); if (columns == null || columns.Length < 2) { throw GetException(); } _pidColumn = columns.FirstOrDefault((column) => column.Name.Equals("PID", StringComparison.OrdinalIgnoreCase)); if (_pidColumn == null) throw GetException(); _nameColumn = columns.FirstOrDefault((column) => column.Name.Equals("NAME", StringComparison.OrdinalIgnoreCase)); if (_nameColumn == null) { // use the last column if one wasn't called 'name' _nameColumn = columns[columns.Length - 1]; } }
public ProcessListParser(IList <string> lines) { // We at least should have a line for the header, a system process, and 'ps' if (lines.Count < 3) { throw GetException(); } _lines = lines; TextColumn[] columns = TextColumn.TryParseHeader(lines[0]); if (columns == null || columns.Length < 2) { throw GetException(); } _pidColumn = columns.FirstOrDefault((column) => column.Name.Equals("PID", StringComparison.OrdinalIgnoreCase)); if (_pidColumn == null) { throw GetException(); } _nameColumn = columns.FirstOrDefault((column) => column.Name.Equals("NAME", StringComparison.OrdinalIgnoreCase)); if (_nameColumn == null) { // use the last column if one wasn't called 'name' _nameColumn = columns[columns.Length - 1]; } }
/// <summary> /// Parsers the header line from a table, returning an array of columns /// </summary> /// <param name="headerLine">The line of text with the header</param> /// <returns>[Optional] Array of columns</returns> public static TextColumn[] TryParseHeader(string headerLine) { List <TextColumn> columns = new List <TextColumn>(); int startIndex = 0; int position = 0; // Move past any initial whitespace if (!SkipWhitespace(ref position, headerLine)) { return(null); // line is nothing but whitespace } int nameStartIndex = position; while (true) { // Skip until the start of the next column int nextWhitespace = headerLine.IndexOfAny(s_spaceChars, position); position = nextWhitespace; if (position >= 0 && SkipWhitespace(ref position, headerLine)) { // Position is currently at the start of the next column. Create an object for the column we just went past TextColumn newColumn = new TextColumn(headerLine.Substring(nameStartIndex, nextWhitespace - nameStartIndex), startIndex, position - startIndex); columns.Add(newColumn); nameStartIndex = startIndex = position; } else { // Create the last column if (nextWhitespace < 0) { nextWhitespace = headerLine.Length; } TextColumn newColumn = new TextColumn(headerLine.Substring(nameStartIndex, nextWhitespace - nameStartIndex), startIndex, -1); columns.Add(newColumn); return(columns.ToArray()); } } }
/// <summary> /// Parsers the header line from a table, returning an array of columns /// </summary> /// <param name="headerLine">The line of text with the header</param> /// <returns>[Optional] Array of columns</returns> public static TextColumn[] TryParseHeader(string headerLine) { List<TextColumn> columns = new List<TextColumn>(); int startIndex = 0; int position = 0; // Move past any initial whitespace if (!SkipWhitespace(ref position, headerLine)) { return null; // line is nothing but whitespace } int nameStartIndex = position; while (true) { // Skip until the start of the next column int nextWhitespace = headerLine.IndexOfAny(s_spaceChars, position); position = nextWhitespace; if (position >= 0 && SkipWhitespace(ref position, headerLine)) { // Position is currently at the start of the next column. Create an object for the column we just went past TextColumn newColumn = new TextColumn(headerLine.Substring(nameStartIndex, nextWhitespace - nameStartIndex), startIndex, position - startIndex); columns.Add(newColumn); nameStartIndex = startIndex = position; } else { // Create the last column if (nextWhitespace < 0) nextWhitespace = headerLine.Length; TextColumn newColumn = new TextColumn(headerLine.Substring(nameStartIndex, nextWhitespace - nameStartIndex), startIndex, -1); columns.Add(newColumn); return columns.ToArray(); } } }