/// <summary> /// Writes the specified data to the file /// </summary> /// <param name="data">The data to write</param> public void WriteFile(DosBoxAutoConfigData data) { StringBuilder stringBuilder = new StringBuilder(); // Add the first lines stringBuilder.AppendLine(FirstLines); // Add the custom commands separator stringBuilder.AppendLine(CustomCommandsSeparator); // Add the custom commands foreach (string customLine in data.CustomLines) { stringBuilder.AppendLine(customLine); } // Add the sections foreach (var sections in data.SectionNames) { // Add the section name stringBuilder.AppendLine($"[{sections.Key}]"); // Add each command foreach (var key in sections.Value) { // Make sure the key has been added if (data.Configuration.ContainsKey(key)) { // Add the command stringBuilder.AppendLine($"{key}={data.Configuration[key]}"); } } // Add empty line stringBuilder.AppendLine(); } // Write the text to the file File.WriteAllText(FilePath, stringBuilder.ToString()); }
/// <summary> /// Reads the file and returns the data /// </summary> /// <returns>The file data</returns> public DosBoxAutoConfigData ReadFile() { // Read the file content var content = File.ReadAllText(FilePath); // Remove the first lines content = content.Substring(FirstLines.Length); // Split into lines var lines = content.Split(new[] { Environment.NewLine }, StringSplitOptions.None); // Create the output var output = new DosBoxAutoConfigData(); // Keep track of if we've reached the custom commands bool inCustomCommands = false; // Keep track if we're in a section bool inSection = false; // Check each line foreach (var line in lines) { // Flag when the custom commands are reached if (!inCustomCommands && line == CustomCommandsSeparator) { inCustomCommands = true; continue; } // If we've reached the custom commands, check if the line has a section name if (!inSection && inCustomCommands && line.StartsWith("[")) { inSection = true; continue; } // Ignore section names if (line.StartsWith("[")) { continue; } // Ignore if it's empty if (line.IsNullOrWhiteSpace()) { continue; } if (inSection) { // Get the values var values = line.Split('='); // Make sure we have two values if (values.Length != 2) { continue; } // Add the values output.Configuration.Add(values[0], values[1]); } else if (inCustomCommands) { output.CustomLines.Add(line); } else { // Ignore if it's not a valid config line if (!line.StartsWith(ConfigLineStart)) { continue; } // Get the values var values = line.Substring(ConfigLineStart.Length).Trim('\"').Split('='); // Make sure we have two values if (values.Length != 2) { continue; } // Add the values output.Configuration.Add(values[0], values[1]); } } return(output); }