/// <summary> /// Attempts to get the file type by searching the file for a matching structure. /// </summary> /// <param name="FileName"></param> /// <param name="minRows"></param> /// <returns></returns> private static FileType GetFileTypeFromContents( string FileName, int minRows ) { FileType fileType = null; DelimitedFileReader fileReader; Dictionary<int, int> colCounts; int rowIndex; int colRecCount; if (3 > minRows) { minRows = 3; } foreach (GuidFileType guidFileType in DelimitedFileReader.FileTypes) { using (fileReader = new DelimitedFileReader()) { try { fileReader.OpenFile(FileName, guidFileType.Value); } catch (Exception) { continue; } colCounts = new Dictionary<int, int>(); rowIndex = 0; while (!fileReader.EndOfStream && minRows > rowIndex++) { string[] lines = fileReader.ReadLine(); int columns = lines.Length; if (colCounts.ContainsKey(columns)) { colCounts[columns]++; } else { if (1 < columns) { colCounts.Add(columns, 1); } } } fileReader.Close(); } if (rowIndex < minRows) { throw new Exception("There must be at least " + minRows + " rows in the file for auto-testing to work"); } if (colCounts.Keys.Count == 1) { colRecCount = 0; colCounts.TryGetValue(new List<int>(colCounts.Keys)[0], out colRecCount); if (colRecCount == rowIndex - 1) { return guidFileType.Value; } } } return fileType; }
public bool Cat( CatOptions catOptions, string fileName, int lineStart, long linesToWrite ) { string ext = Path.GetExtension(fileName); string[] lines; Dictionary<int, int> maxLen = new Dictionary<int, int>(); List<string[]> lineColumns = new List<string[]>(); int lineNumber; int padLen; int winWidth = Console.WindowWidth - 1; string colPad; lines = File.ReadAllLines(fileName); lineStart = Math.Max(lineStart, 0); lineNumber = 0; padLen = catOptions.showLineNumbers ? 3 : 0; if (linesToWrite < 0) { linesToWrite = long.MaxValue; } colPad = string.Empty; DelimitedFileReader f = new DelimitedFileReader(); f.OpenFile(fileName, new FileType(ext.Substring(1), ",", new string[] { ext }, ext.Substring(1))); // Get the maxlength of each column. for (int i = Math.Min(lines.Length, Math.Max(0, lineStart)); i < Math.Min(lineStart + linesToWrite, lines.Length); i++) { string[] ls = f.ParseLine(lines[i]); lineColumns.Add(ls); for (int c = 0; c < ls.Length; c++) { if (maxLen.ContainsKey(c)) { maxLen[c] = Math.Max(maxLen[c], ls[c].Length + 1); } else { maxLen.Add(c, ls[c].Length + 1); } } } StringBuilder res = new StringBuilder(); foreach (string[] ls in lineColumns) { lineNumber++; if (catOptions.showLineNumbers) { Console.BackgroundColor = catOptions.lineNumBackColor; Console.ForegroundColor = catOptions.lineNumForeColor; Console.Write("{0," + padLen + "}", lineNumber); Console.BackgroundColor = catOptions.defaultBackColor; Console.ForegroundColor = catOptions.defaultForeColor; } res.Clear(); for (int c = 0; c < ls.Length; c++) { //res.AppendFormat("{0,-" + maxLen[c] + "}, ", ls[c]); //res.AppendFormat("{0,-" + maxLen[c] + "} ", ls[c] + ","); res.AppendFormat("{0,-" + maxLen[c] + "}, ", ls[c].TrimStart()); } Console.WriteLine(res.ToString().TrimEnd(' ', ',')); //Console.WriteLine(res.ToString().TrimEnd().TrimEnd(',')); } return true; }