diff IniFileReader.cs @ 1:f9444f1786cd

Re #6 - INI parsing library * Add ToString methods * Add types of string and parse to objects instead of parsing to key-value pair * Create method to load INI data from string * Add content of AddSection method
author IBBoard <dev@ibboard.co.uk>
date Sun, 11 Jan 2009 15:37:14 +0000
parents fbde5e1920ba
children 2dde4c1d19d9
line wrap: on
line diff
--- a/IniFileReader.cs	Thu Jan 08 20:33:56 2009 +0000
+++ b/IniFileReader.cs	Sun Jan 11 15:37:14 2009 +0000
@@ -10,9 +10,10 @@
 {
 	public class IniFileReader
 	{
-		public static IniFile ReadFile(string path)
+		public static IniFile ReadFile(string iniFileContent)
 		{
-			return ReadFile(new FileInfo(path));
+			MemoryStream memoryStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes("test"));
+			return LoadIniFileFromStream(new StreamReader(memoryStream));
 		}
 		
 		public static IniFile ReadFile(FileInfo file)
@@ -22,28 +23,53 @@
 				throw new FileNotFoundException(file.FullName+" did not exist and could not be read");
 			}
 			
-			IniFile iniFile = new IniFile();
-			LoadDataFromFile(file, iniFile);			
-			return iniFile;
+			return LoadIniFileFromFile(file);
 		}
 		
-		private static void LoadDataFromFile(FileInfo file, IniFile iniFile)
+		private static IniFile LoadIniFileFromFile(FileInfo file)
 		{
 			StreamReader stream = file.OpenText();
+			IniFile iniFile = null;
 			
 			try
 			{
-				LoadDataFromStream(iniFile, stream);
+				iniFile = LoadIniFileFromStream(stream);
 			}
 			finally
 			{
 				stream.Close();
 			}
+
+			return iniFile;
 		}
 		
-		private static void LoadDataFromStream(IniFile iniFile, StreamReader stream)
+		private static IniFile LoadIniFileFromStream(StreamReader stream)
+		{
+			string firstHeader = ReadToFirstSectionHeader(stream);
+			return ReadIniFileSectionsToFile(stream, firstHeader);
+		}
+
+		private static string ReadToFirstSectionHeader(StreamReader stream)
 		{
-			StringBuilder sectionStringBuilder = new StringBuilder();
+			string firstSectionHeader = null;
+			
+			while (!stream.EndOfStream && firstSectionHeader == null)
+			{
+				string line = stream.ReadLine().Trim();
+
+				if (IniSectionParser.IsLineStartOfNewSection(line))
+				{
+					firstSectionHeader = line + "\n";
+				}
+			}
+
+			return firstSectionHeader;
+		}
+
+		private static IniFile ReadIniFileSectionsToFile(StreamReader stream, string firstHeader)
+		{
+			IniFile iniFile = new IniFile();			
+			StringBuilder sectionStringBuilder = SetUpStringBuilder(firstHeader);
 			
 			while (!stream.EndOfStream)
 			{
@@ -51,22 +77,39 @@
 				
 				if (IniSectionParser.IsLineStartOfNewSection(line))
 				{
-					String currSection = sectionStringBuilder.ToString();
-					
-					if (IniSectionParser.IsStringAnIniSection(currSection))
-					{
-						IniSection section = IniSectionParser.CreateSection(currSection);
-						iniFile.AddSection(section);
-					}
-					
+					AddSectionFromStringBuilderContents(sectionStringBuilder, iniFile);
 					sectionStringBuilder.Length = 0;
 				}
 				
 				sectionStringBuilder.Append(line);
 				sectionStringBuilder.Append("\n");
 			}
+
+			if (sectionStringBuilder.Length > 0)
+			{
+				AddSectionFromStringBuilderContents(sectionStringBuilder, iniFile);
+			}
+
+			return iniFile;
 		}
-		
-		
+
+		private static StringBuilder SetUpStringBuilder(string firstHeader)
+		{
+			StringBuilder sectionStringBuilder = new StringBuilder();
+
+			if (firstHeader!=null)
+			{
+				sectionStringBuilder.Append(firstHeader);
+				sectionStringBuilder.Append("\n");
+			}
+
+			return sectionStringBuilder;
+		}
+
+		private static void AddSectionFromStringBuilderContents(StringBuilder sectionStringBuilder, IniFile iniFile)
+		{			
+			IniSection section = IniSectionParser.CreateSection(sectionStringBuilder.ToString());
+			iniFile.AddSection(section);
+		}
 	}
 }