/// <summary> /// Calls the passed function for every non-empty and non-comment /// string in the array. A path is still required, to be used as /// root folder for include/require. If those aren't required, /// pass null, which will also disable this feature. /// /// TODO: Maybe add a DoString method, DRY. /// </summary> /// <param name="filePath"></param> /// <param name="fn"></param> public static void DoEach(string[] args, string filePath, LineDoer fn) { foreach (var arg in args) { var line = ParseLine(arg); if (line == null) continue; bool require = false; if (filePath != null && (line.StartsWith("include ") || (require = line.StartsWith("require ")))) { var includeFilePath = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(filePath)), line.Substring(8)); try { FileReader.DoEach(includeFilePath, fn); } catch (FileNotFoundException) { if (require) throw new Exception("Required file '" + includeFilePath + "' not found."); } continue; } fn(line); } }
/// <summary> /// Calls the passed function for every non-empty and non-comment /// line in the file. /// </summary> /// <param name="filePath"></param> /// <param name="fn"></param> public static void DoEach(string filePath, LineDoer fn) { if (!File.Exists(filePath)) throw new FileNotFoundException("File not found."); using (var sr = new StreamReader(filePath)) { string line; while ((line = sr.ReadLine()) != null) { line = ParseLine(line); if (line == null) continue; bool require = false; // Including (include foo.conf) if (line.StartsWith("include ") || (require = line.StartsWith("require "))) { var includeFilePath = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(filePath)), line.Substring(8)); try { FileReader.DoEach(includeFilePath, fn); } catch (FileNotFoundException) { // Ignore exceptions for includes if (require) throw new Exception("Required file '" + includeFilePath + "' not found."); } continue; } fn(line); } } }