internal static void LoadAllConfigs() { //NOTE(adam): try multiple locations for config files string[] configFiles = null; if (Directory.Exists(FilePaths.adjacentConfigFolderPath)) { configFiles = Directory.GetFiles(FilePaths.adjacentConfigFolderPath, "*.cfg"); } else if (Directory.Exists(FilePaths.networkConfigFolderPath)) { configFiles = Directory.GetFiles(FilePaths.networkConfigFolderPath, "*.cfg"); } if (configFiles == null || configFiles.Length == 0) { ErrorHandler.HandleError(ErrorType.Critical, "Could not find config files."); return; } ConfigLoadingWindow configLoadingWindow = new ConfigLoadingWindow(); configLoadingWindow.Show(); configLoadingWindow.Location = new System.Drawing.Point(mainWindow.Location.X + (mainWindow.Width - configLoadingWindow.Width) / 2, mainWindow.Location.Y + (mainWindow.Height - configLoadingWindow.Height) / 2); for (int i = 0; i != configFiles.Length; ++i) { string fileName = Path.GetFileName(configFiles[i]); if (fileName.Split('.')[0] == "global") { ConfigReader.ReadGlobalFile(configFiles[i], globalConfig, configLoadingWindow); } else { ReportType type; if (Enum.TryParse(fileName.Split('.')[0], true, out type)) { StyleConfigData config = new StyleConfigData(type, globalConfig); configLoadingWindow.SetFilesProgress(fileName, i + 1, configFiles.Length); ConfigReader.ReadStyleFile(configFiles[i], config, configLoadingWindow); configs.Add(type, config); } else { ErrorHandler.HandleError(ErrorType.Alert, $"Could not find type for config file: {fileName}"); } } } configLoadingWindow.Hide(); //TODO(adam): investigate program freeze after reading config file }
internal static GlobalConfigData ReadGlobalFile(string configFilePath, GlobalConfigData config, ConfigLoadingWindow configLoadingWindow) { Sections curSection = Sections.Void; int totalLines = File.ReadLines(configFilePath).Count(); using (StreamReader sr = new StreamReader(configFilePath)) { int lineNumber = 0; string line; while (sr.Peek() > -1) { line = sr.ReadLine().Trim(); ++lineNumber; configLoadingWindow.SetLinesProgress(lineNumber, totalLines); //NOTE(adam): if is blank line or comment, skip if (!(line.Length > 0 && line[0] != '#')) { continue; } //NOTE(adam): set section or parse line based on current section if (line[0] == '>') { if (line.Contains("ROOT")) { curSection = Sections.Root; ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Should not have {curSection} in gloabl file."); } else if (line.Contains("EXTENSION")) { curSection = Sections.Extension; ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Should not have {curSection} in gloabl file."); } else if (line.Contains("TYPES")) { curSection = Sections.Types; ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Should not have {curSection} in gloabl file."); } else if (line.Contains("PREFIXES")) { curSection = Sections.Prefixes; } else if (line.Contains("PATHS")) { curSection = Sections.Paths; ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Should not have {curSection} in gloabl file."); } else if (line.Contains("EXPORTS")) { curSection = Sections.Exports; ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Should not have {curSection} in gloabl file."); } else if (line.Contains("EXCEPTIONS")) { curSection = Sections.Exceptions; ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Should not have {curSection} in gloabl file."); } else if (line.Contains("TRIMS")) { curSection = Sections.Trims; } else { ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Invalid section header."); } } else { switch (curSection) { case Sections.Root: //NOTE(adam): invalid section for global, logged when section set break; case Sections.Extension: //NOTE(adam): invalid section for global, logged when section set break; case Sections.Types: //NOTE(adam): invalid section for global, logged when section set break; case Sections.Prefixes: config.InsertStylePrefix(line); break; case Sections.Paths: //NOTE(adam): invalid section for global, logged when section set break; case Sections.Exports: //NOTE(adam): invalid section for global, logged when section set break; case Sections.Exceptions: //NOTE(adam): invalid section for global, logged when section set break; case Sections.Trims: config.InsertTrim(line); break; default: ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Unspecified section."); break; } } } } return(config); }
internal static StyleConfigData ReadStyleFile(string configFilePath, StyleConfigData config, ConfigLoadingWindow configLoadingWindow) { Sections curSection = Sections.Void; int totalLines = File.ReadLines(configFilePath).Count(); using (StreamReader sr = new StreamReader(configFilePath)) { int lineNumber = 0; string line; while (sr.Peek() > -1) { line = sr.ReadLine().Trim(); ++lineNumber; configLoadingWindow.SetLinesProgress(lineNumber, totalLines); //NOTE(adam): if is blank line or comment, skip if (!(line.Length > 0 && line[0] != '#')) { continue; } //NOTE(adam): set section or parse line based on current section if (line[0] == '>') { if (line.Contains("ROOT")) { curSection = Sections.Root; } else if (line.Contains("EXTENSION")) { curSection = Sections.Extension; } else if (line.Contains("TYPES")) { curSection = Sections.Types; } else if (line.Contains("PREFIXES")) { curSection = Sections.Prefixes; ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Should not have {curSection} in style file."); } else if (line.Contains("PATHS")) { curSection = Sections.Paths; } else if (line.Contains("EXPORTS")) { curSection = Sections.Exports; } else if (line.Contains("EXCEPTIONS")) { curSection = Sections.Exceptions; } else if (line.Contains("TRIMS")) { curSection = Sections.Trims; ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Should not have {curSection} in style file."); } else { ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Invalid section header."); } } else { switch (curSection) { case Sections.Root: config.SetRootPath(line); break; case Sections.Extension: config.SetFileExtension(line); break; case Sections.Types: { string[] tokens = line.Split(':'); //NOTE(adam): expecting line as #:desc if (tokens.Length < 2) { ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Type parse error."); } else { config.InsertPathType(int.Parse(tokens[0]), tokens[1]); } } break; case Sections.Prefixes: //NOTE(adam): invalid section for style, logged when section set break; case Sections.Paths: { StylePathData path = ParsePath(line); if (path == null) { ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Path parse error."); } else { config.InsertPath(path); } } break; case Sections.Exports: { ExportData export = ParseExport(line); if (export == null) { ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Export parse error."); } else { config.InsertExport(export); } } break; case Sections.Exceptions: { ExceptionData exception = ParseException(line); if (exception == null) { ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Exception parse error."); } else { config.InsertException(exception); } } break; case Sections.Trims: //NOTE(adam): invalid section for style, logged when section set break; default: ErrorHandler.HandleError(ErrorType.Log, $"config {lineNumber}: Unspecified section."); break; } } } } return(config); }