Mercurial > repos > IBBoard.Ini
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 |
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 | 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 | 15 MemoryStream memoryStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(iniFileContent)); |
1 | 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 | 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 | 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 | 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 | 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 | 42 |
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 | 46 private static IniFile LoadIniFileFromStream(StreamReader stream) |
47 { | |
48 string firstHeader = ReadToFirstSectionHeader(stream); | |
49 return ReadIniFileSectionsToFile(stream, firstHeader); | |
50 } | |
51 | |
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 | 54 string firstSectionHeader = null; |
55 | |
56 while (!stream.EndOfStream && firstSectionHeader == null) | |
57 { | |
58 string line = stream.ReadLine().Trim(); | |
59 | |
60 if (IniSectionParser.IsLineStartOfNewSection(line)) | |
61 { | |
62 firstSectionHeader = line + "\n"; | |
63 } | |
64 } | |
65 | |
66 return firstSectionHeader; | |
67 } | |
68 | |
69 private static IniFile ReadIniFileSectionsToFile(StreamReader stream, string firstHeader) | |
70 { | |
71 IniFile iniFile = new IniFile(); | |
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 | 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 | 87 |
88 if (sectionStringBuilder.Length > 0) | |
89 { | |
90 AddSectionFromStringBuilderContents(sectionStringBuilder, iniFile); | |
91 } | |
92 | |
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 | 95 |
96 private static StringBuilder SetUpStringBuilder(string firstHeader) | |
97 { | |
98 StringBuilder sectionStringBuilder = new StringBuilder(); | |
99 | |
100 if (firstHeader!=null) | |
101 { | |
102 sectionStringBuilder.Append(firstHeader); | |
103 sectionStringBuilder.Append("\n"); | |
104 } | |
105 | |
106 return sectionStringBuilder; | |
107 } | |
108 | |
109 private static void AddSectionFromStringBuilderContents(StringBuilder sectionStringBuilder, IniFile iniFile) | |
110 { | |
111 IniSection section = IniSectionParser.CreateSection(sectionStringBuilder.ToString()); | |
112 iniFile.AddSection(section); | |
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 } |