annotate IniFileReader.cs @ 3:3c26f463977a

Re #7 - Add INI parsing tests * Make IniSection throw a new DuplicateIniLineException to make testing of duplicates easier
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Jan 2009 14:18:07 +0000
parents 2dde4c1d19d9
children f6f726c92e56
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (IniFileReader.cs) is a part of the IBBoard.Ini library and is copyright 2009 IBBoard.
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.IO;
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Text;
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 namespace IBBoard.Ini
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 public class IniFileReader
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 {
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
13 public static IniFile ReadFile(string iniFileContent)
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
2
2dde4c1d19d9 Closes #6 - Create INI parser
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
15 MemoryStream memoryStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(iniFileContent));
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
16 return LoadIniFileFromStream(new StreamReader(memoryStream));
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 public static IniFile ReadFile(FileInfo file)
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 if (!file.Exists)
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 throw new FileNotFoundException(file.FullName+" did not exist and could not be read");
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
26 return LoadIniFileFromFile(file);
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
29 private static IniFile LoadIniFileFromFile(FileInfo file)
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 StreamReader stream = file.OpenText();
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
32 IniFile iniFile = null;
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 try
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 {
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
36 iniFile = LoadIniFileFromStream(stream);
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 finally
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 stream.Close();
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 }
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
42
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
43 return iniFile;
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
46 private static IniFile LoadIniFileFromStream(StreamReader stream)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
47 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
48 string firstHeader = ReadToFirstSectionHeader(stream);
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
49 return ReadIniFileSectionsToFile(stream, firstHeader);
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
50 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
51
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
52 private static string ReadToFirstSectionHeader(StreamReader stream)
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 {
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
54 string firstSectionHeader = null;
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
55
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
56 while (!stream.EndOfStream && firstSectionHeader == null)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
57 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
58 string line = stream.ReadLine().Trim();
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
59
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
60 if (IniSectionParser.IsLineStartOfNewSection(line))
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
61 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
62 firstSectionHeader = line + "\n";
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
63 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
64 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
65
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
66 return firstSectionHeader;
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
67 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
68
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
69 private static IniFile ReadIniFileSectionsToFile(StreamReader stream, string firstHeader)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
70 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
71 IniFile iniFile = new IniFile();
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
72 StringBuilder sectionStringBuilder = SetUpStringBuilder(firstHeader);
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 while (!stream.EndOfStream)
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 {
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 string line = stream.ReadLine().Trim();
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 if (IniSectionParser.IsLineStartOfNewSection(line))
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 {
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
80 AddSectionFromStringBuilderContents(sectionStringBuilder, iniFile);
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 sectionStringBuilder.Length = 0;
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 sectionStringBuilder.Append(line);
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 sectionStringBuilder.Append("\n");
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 }
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
87
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
88 if (sectionStringBuilder.Length > 0)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
89 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
90 AddSectionFromStringBuilderContents(sectionStringBuilder, iniFile);
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
91 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
92
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
93 return iniFile;
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 }
1
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
95
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
96 private static StringBuilder SetUpStringBuilder(string firstHeader)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
97 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
98 StringBuilder sectionStringBuilder = new StringBuilder();
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
99
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
100 if (firstHeader!=null)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
101 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
102 sectionStringBuilder.Append(firstHeader);
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
103 sectionStringBuilder.Append("\n");
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
104 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
105
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
106 return sectionStringBuilder;
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
107 }
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
108
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
109 private static void AddSectionFromStringBuilderContents(StringBuilder sectionStringBuilder, IniFile iniFile)
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
110 {
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
111 IniSection section = IniSectionParser.CreateSection(sectionStringBuilder.ToString());
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
112 iniFile.AddSection(section);
f9444f1786cd Re #6 - INI parsing library
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
113 }
0
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
114 }
fbde5e1920ba Re #6 (Ini parsing library) - Initial commit of IBBoard Ini parsing
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
115 }