comparison IniFileReader.cs @ 0:fbde5e1920ba

Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
author IBBoard <dev@ibboard.co.uk>
date Thu, 08 Jan 2009 20:33:56 +0000
parents
children f9444f1786cd
comparison
equal deleted inserted replaced
-1:000000000000 0:fbde5e1920ba
1 // This file (IniFileReader.cs) is a part of the IBBoard.Ini library and is copyright 2009 IBBoard.
2 //
3 // The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license.
4
5 using System;
6 using System.IO;
7 using System.Text;
8
9 namespace IBBoard.Ini
10 {
11 public class IniFileReader
12 {
13 public static IniFile ReadFile(string path)
14 {
15 return ReadFile(new FileInfo(path));
16 }
17
18 public static IniFile ReadFile(FileInfo file)
19 {
20 if (!file.Exists)
21 {
22 throw new FileNotFoundException(file.FullName+" did not exist and could not be read");
23 }
24
25 IniFile iniFile = new IniFile();
26 LoadDataFromFile(file, iniFile);
27 return iniFile;
28 }
29
30 private static void LoadDataFromFile(FileInfo file, IniFile iniFile)
31 {
32 StreamReader stream = file.OpenText();
33
34 try
35 {
36 LoadDataFromStream(iniFile, stream);
37 }
38 finally
39 {
40 stream.Close();
41 }
42 }
43
44 private static void LoadDataFromStream(IniFile iniFile, StreamReader stream)
45 {
46 StringBuilder sectionStringBuilder = new StringBuilder();
47
48 while (!stream.EndOfStream)
49 {
50 string line = stream.ReadLine().Trim();
51
52 if (IniSectionParser.IsLineStartOfNewSection(line))
53 {
54 String currSection = sectionStringBuilder.ToString();
55
56 if (IniSectionParser.IsStringAnIniSection(currSection))
57 {
58 IniSection section = IniSectionParser.CreateSection(currSection);
59 iniFile.AddSection(section);
60 }
61
62 sectionStringBuilder.Length = 0;
63 }
64
65 sectionStringBuilder.Append(line);
66 sectionStringBuilder.Append("\n");
67 }
68 }
69
70
71 }
72 }