private void ParseText(string[] lines) { INIBlock block = new INIBlock(); block.Name = ""; foreach (string lineCR in lines) { string line = lineCR.TrimEnd('\r'); if (line.StartsWith("[")) { block = new INIBlock(); block.Name = line; if (!Blocks.ContainsKey(block.Name)) { Blocks.Add(block.Name, block); } else { block = Blocks[block.Name]; } } else if (!line.StartsWith(";") && !line.StartsWith("#") && line.Contains("=")) { block.SetValue(line); } } }
public void SetValue(string BlockName, string Key, string Value) { if (!Blocks.ContainsKey(BlockName)) { INIBlock block = new INIBlock(); block.Name = BlockName; Blocks.Add(block.Name, block); } Blocks[BlockName].SetValue(Key, Value); }
static void AddComment(INIBlock block, string value) { if (value == null) { return; } if (block.Comments != null) { block.Comments += Environment.NewLine; } block.Comments += value; }
public List <INIBlock> Read() { List <INIBlock> data = new List <INIBlock>(); INIBlock currentBlock = new INIBlock(); int currentOptionIndex = 0; bool currentBlockCommentedOut = false; using (FileStream stream = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read)) using (StreamReader streamReader = new StreamReader(stream, Encoding.Default)) { while (!streamReader.EndOfStream) { string line = streamReader.ReadLine(); if (line == null) { break; } int commentIndex; string newComment = null; // check for commented out block if (ReadWriteComments && line.Length > 2 && line[0] == TOKEN_BLOCK_START && line[1] == TOKEN_COMMENT) { line = line.Substring(2); // add additional comments commentIndex = line.IndexOf(TOKEN_COMMENT); if (commentIndex != -1) { // add comment newComment = line.Substring(commentIndex + 1); // remove comment from data line = line.Substring(0, commentIndex).TrimEnd(); } if (line[line.Length - 1] == TOKEN_BLOCK_END) { // add current block if (currentBlock.Name != null) { data.Add(currentBlock); } // new block string blockName = line.Substring(0, line.Length - 1).Trim(); currentBlock = CreateBlock(blockName, new INIOptions(), currentBlock.Name == null ? currentBlock.Comments : null); currentBlockCommentedOut = true; AddComment(currentBlock, newComment); continue; } } // handle comments commentIndex = line.IndexOf(TOKEN_COMMENT); if (commentIndex != -1) { // add comment if (ReadWriteComments) { newComment = line.Substring(commentIndex + 1); } // remove comment from data line = line.Substring(0, commentIndex).TrimEnd(); } else { line = line.Trim(); } if (line.Length == 0) { AddComment(currentBlock, newComment); continue; } if (line[0] == TOKEN_BLOCK_START) { // add current block if (currentBlock.Name != null) { data.Add(currentBlock); } if (line[line.Length - 1] == TOKEN_BLOCK_END) { // new block string blockName = line.Substring(1, line.Length - 2).Trim(); currentBlock = CreateBlock(blockName, new INIOptions(), currentBlock.Name == null ? currentBlock.Comments : null); } else { // reset block currentBlock = CreateBlock(null, null, null); } currentOptionIndex = 0; currentBlockCommentedOut = false; } else if (currentBlockCommentedOut) { // add comment AddComment(currentBlock, line); } else if (currentBlock.Name != null) { // new value for block int valueIndex = line.IndexOf(TOKEN_MAPPING_VALUE); if (valueIndex != -1) { // retrieve name and value from data string optionName = line.Substring(0, valueIndex).Trim(); string optionValue = line.Substring(valueIndex + 1, line.Length - valueIndex - 1).Trim(); currentBlock.Options.Add(optionName, new INIOption { Value = optionValue, Index = currentOptionIndex }); ++currentOptionIndex; } else { // entry without value currentBlock.Options.Add(line, new INIOption { Value = string.Empty, Index = currentOptionIndex }); ++currentOptionIndex; } } // add comment to current block AddComment(currentBlock, newComment); } } // add final block if (currentBlock.Name != null) { data.Add(currentBlock); } return(data); }
//private string OpenFileDialog() //{ // return _openFileDialog.ShowDialog() ?? false ? _openFileDialog.FileName : ""; //} private bool LoadINI(string path, out INI file) { if (!File.Exists(path)) { file = null; return(false); } Regex iniHeader = new Regex(@"^\[[^\]\r\n]+]"); Regex iniKeyValue = new Regex(@"^([^=;\r\n]+)=([^;\r\n]*)"); var lines = File.ReadAllLines(path); file = new INI(); var duplicateKeys = new List <Tuple <string, string, string, string> >(); //block, key, old value, new value Dictionary <string, string> currentBlock = null; foreach (var line in lines) { try { if (iniHeader.IsMatch(line)) { file.Add(line.Trim(new char[] { ' ', ']', '[', '\t' }), currentBlock = new INIBlock()); } if (iniKeyValue.IsMatch(line)) { if (currentBlock == null) { file.Add("", currentBlock = new INIBlock()); } var match = iniKeyValue.Match(line); var key = match.Groups[1].Value.Trim(); var value = match.Groups[2].Value.Trim(); if (currentBlock.ContainsKey(key)) { var currentBlockName = file.FirstOrDefault(kv => kv.Value == currentBlock).Key; duplicateKeys.Add(Tuple.Create(currentBlockName, key, currentBlock[key], value)); currentBlock[key] = value; } else { currentBlock.Add(key, value); } } } catch (Exception ex) { MessageBox.Show("This INI could not be parsed. The line being parsed was:\n" + line + "\n\nDetailed exception information below:\n" + ex.InnerException , "Exception Alert", MessageBoxButton.OK, MessageBoxImage.Exclamation); file = null; return(false); } } if (duplicateKeys.Count > 0) { MessageBox.Show(string.Format("{0} values were overwritten by same keys in the same section. Only the last set value for the key will be used.\n[Section] Key = Early Value -> Later Value\n{1}" , duplicateKeys.Count, String.Join("\n", duplicateKeys.Select(dup => string.Format("[{0}] {1} = {2} -> {3}", dup.Item1, dup.Item2, dup.Item3, dup.Item4)))) , "Duplicate Key-Values Overwritten", MessageBoxButton.OK, MessageBoxImage.Information); } return(true); }
//private string OpenFileDialog() //{ // return _openFileDialog.ShowDialog() ?? false ? _openFileDialog.FileName : ""; //} private bool LoadINI(string path, out INI file) { if (!File.Exists(path)) { file = null; return false; } Regex iniHeader = new Regex(@"^\[[^\]\r\n]+]"); Regex iniKeyValue = new Regex(@"^([^=;\r\n]+)=([^;\r\n]*)"); var lines = File.ReadAllLines(path); file = new INI(); var duplicateKeys = new List<Tuple<string, string, string, string>>(); //block, key, old value, new value Dictionary<string, string> currentBlock = null; foreach (var line in lines) { try { if (iniHeader.IsMatch(line)) file.Add(line.Trim(new char[] { ' ', ']', '[', '\t' }), currentBlock = new INIBlock()); if (iniKeyValue.IsMatch(line)) { if (currentBlock == null) file.Add("", currentBlock = new INIBlock()); var match = iniKeyValue.Match(line); var key = match.Groups[1].Value.Trim(); var value = match.Groups[2].Value.Trim(); if (currentBlock.ContainsKey(key)) { var currentBlockName = file.FirstOrDefault(kv => kv.Value == currentBlock).Key; duplicateKeys.Add(Tuple.Create(currentBlockName, key, currentBlock[key], value)); currentBlock[key] = value; } else { currentBlock.Add(key, value); } } } catch (Exception ex) { MessageBox.Show("This INI could not be parsed. The line being parsed was:\n" + line + "\n\nDetailed exception information below:\n" + ex.InnerException , "Exception Alert", MessageBoxButton.OK, MessageBoxImage.Exclamation); file = null; return false; } } if (duplicateKeys.Count > 0) MessageBox.Show(string.Format("{0} values were overwritten by same keys in the same section. Only the last set value for the key will be used.\n[Section] Key = Early Value -> Later Value\n{1}" , duplicateKeys.Count, String.Join("\n", duplicateKeys.Select(dup => string.Format("[{0}] {1} = {2} -> {3}", dup.Item1, dup.Item2, dup.Item3, dup.Item4)))) , "Duplicate Key-Values Overwritten", MessageBoxButton.OK, MessageBoxImage.Information); return true; }