private static void ReadCharactersAndCountLines(FileItem fileItem) { int linesCount = 1; int currentLevel = 0; int functionLevel = 2; using (StreamReader sr = new StreamReader(fileItem.TheFile.FullName)) { while (!sr.EndOfStream) { int s = sr.Read(); char sC = (char)s; if (sC.Equals('\r')) { var nextChar = (char)sr.Peek(); if (nextChar == '\n') { sr.Read(); } linesCount++; } else if (sC.Equals('\n')) { linesCount++; } else { Console.Error.WriteLine("'{0}'", sC); } } } Console.WriteLine("Lines read: {0}", linesCount); }
private static List<FunctionContents> ParseFunctionLines(FileItem fileItem) { int linesCount = 1; int currentLevel = 0; int namespaceLevel = 1; int classLevel = 2; const int functionLevel = 3; FunctionContents currentFunction = null; List<FunctionContents> functionContentCol = new List<FunctionContents>(); List<string> previousLines = new List<string>(); using (TextReader reader = new StreamReader(fileItem.TheFile.FullName)) { var line = reader.ReadLine(); while (line != null) { if (line.Trim().StartsWith("}")) currentLevel--; else if (line.Trim().StartsWith("{")) currentLevel++; if (currentLevel >= functionLevel) { if (currentFunction == null) { // enter function currentFunction = new FunctionContents(GetFunctionLine(previousLines)); functionContentCol.Add(currentFunction); } else currentFunction.Lines.Add(line); } else { currentFunction = null; } linesCount++; previousLines.Add(line); line = reader.ReadLine(); } } Console.WriteLine("Lines read: {0}", linesCount); return functionContentCol; }
private static string FileItemToFolderName(FileItem fileItem) { var sb = new StringBuilder(); sb.Append(fileItem.TheFile.FullName.Replace(":", "").Replace(@"\", "_")); return sb.ToString(); }
static void Main(string[] args) { PrintVersionInfo(); if (args.Length == 0) { Console.WriteLine(Usage()); return; } var fileItem = new FileItem(args[0]); int fileLineLimit = 0; if (args.Length > 1) { int parsedInt; if (!int.TryParse(args[1], out parsedInt)) { Console.WriteLine(Usage()); } fileLineLimit = parsedInt; } Console.WriteLine("Reading {0}", fileItem.TheFile.FullName); DirectoryInfo outputDirInfo; try { outputDirInfo = CreateOutputDir(fileItem); } catch (Exception) { Console.Error.WriteLine("Could not create or delete output path"); return; } Console.WriteLine(); var functionContentCol = ParseFunctionLines(fileItem); foreach (var functionContents in functionContentCol) { SaveToFile(outputDirInfo, functionContents); } foreach (var functionContents in functionContentCol) { var function = functionContents; if (function.Lines.Count >= fileLineLimit) { Console.WriteLine("{0}: {1}", function.Name, function.Lines.Count); } } }
private static DirectoryInfo CreateOutputDir(FileItem fileItem) { var outputPath = Path.Combine(Directory.GetCurrentDirectory(), FileItemToFolderName(fileItem)); Console.WriteLine("output path:{0}{1}", Environment.NewLine, outputPath); var outputDirInfo = new DirectoryInfo(outputPath); if (Directory.Exists(outputPath)) { Console.Write("Directory exists - Delete? (Y/N) "); var readInput = Console.ReadLine(); if (readInput.ToUpper() == "Y") { Directory.Delete(outputPath, true); Thread.Sleep(TimeSpan.FromSeconds(2)); Directory.CreateDirectory(outputPath); } } else { Directory.CreateDirectory(outputPath); } return outputDirInfo; }