public void LoadINI(StreamReader fromstream) { String currentline = null; Sections = new List <INISection>(); INISection globalsection = new INISection("cINIFilecsGlobals", "", new List <INIItem>(), this); INISection currentSection = globalsection; //while there is still text to read. while ((currentline = fromstream.ReadLine()) != null) { //trim the read in line... currentline = currentline.Trim(); //if it starts with a square bracket, it's a section. if (currentline.StartsWith("[")) { //parse out the section name... String newsectionname = currentline.Substring(1, currentline.IndexOf(']') - 1); String eolComment = ""; if (currentline.IndexOf(';') > -1) { eolComment = currentline.Substring(currentline.IndexOf(';')); } currentSection = new INISection(newsectionname, eolComment, new List <INIItem>(), this); Sections.Add(currentSection); } else if (currentline.StartsWith(";")) { //add a new Comment INIItem to the current section. INIItem newitem = new INIComment(currentline); currentSection.INIItems.Add(newitem); } else { INIDataItem createitem = ParseINIValue(currentline, currentSection); if (createitem != null) { currentSection.INIItems.Add(createitem); //createitem.OwnerSection=currentSection; } } } if (globalsection.INIItems.Count() > 0) { Sections.Add(globalsection); } }
public void LoadINI(StreamReader fromstream) { String currentline = null; Sections = new List <INISection>(); INISection globalsection = new INISection("cINIFilecsGlobals", "", new List <INIItem>(), this); INISection currentSection = globalsection; //while there is still text to read. while ((currentline = fromstream.ReadLine()) != null) { //trim the read in line... currentline = currentline.Trim(); if (currentline.StartsWith("$")) { //Special metacommand. String Metacommand = currentline.Substring(1); String CommandName = Metacommand.Substring(0, Metacommand.IndexOf(' ')); String parameter = Metacommand.Substring(CommandName.Length + 1); if (parameter.StartsWith("\"") && parameter.EndsWith("\"")) { parameter = parameter.Substring(1, parameter.Length - 2); } ProcessMetacommand(CommandName, parameter); } else if (currentline.StartsWith("[")) { //if it starts with a square bracket, it's a section. //parse out the section name... String newsectionname = currentline.Substring(1, currentline.IndexOf(']') - 1); String eolComment = ""; if (currentline.IndexOf(';') > -1) { eolComment = currentline.Substring(currentline.IndexOf(';')); } //Added May 2012: What if we have duplicates of the same section? coalesce them :P. //we first see if there is a section with this name. var foundsection = Sections.Find((w) => w.Name.ToUpper() == newsectionname.ToUpper()); if (foundsection != null) { currentSection = foundsection; //set to the existing section. //add to it's comment. why not. currentSection.eolComment += eolComment.Substring(1); } else { currentSection = new INISection(newsectionname, eolComment, new List <INIItem>(), this); } Sections.Add(currentSection); } else if (currentline.StartsWith(";")) { //add a new Comment INIItem to the current section. INIItem newitem = new INIComment(currentline); currentSection.INIItems.Add(newitem); } else { INIDataItem createitem = ParseINIValue(currentline, currentSection); if (createitem != null) { INIDataItem found = null; //look for an existing item in the current section. if (null != (found = (INIDataItem)(currentSection.INIItems.Find((t) => (t is INIDataItem && ((INIDataItem)t).Name.ToUpper() == createitem.Name.ToUpper()))))) { //set the value of found. found.Value = createitem.Value; } else { currentSection.INIItems.Add(createitem); //createitem.OwnerSection=currentSection; } } } } if (globalsection.INIItems.Count() > 0) { Sections.Add(globalsection); } }