• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 [The "BSD licence"]
3 Copyright (c) 2005-2007 Kunle Odutola
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code MUST RETAIN the above copyright
10    notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form MUST REPRODUCE the above copyright
12    notice, this list of conditions and the following disclaimer in
13    the documentation and/or other materials provided with the
14    distribution.
15 3. The name of the author may not be used to endorse or promote products
16    derived from this software without specific prior WRITTEN permission.
17 4. Unless explicitly state otherwise, any contribution intentionally
18    submitted for inclusion in this work to the copyright owner or licensor
19    shall be under the terms and conditions of this license, without any
20    additional terms or conditions.
21 
22 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 
35 namespace Antlr.Runtime
36 {
37 	using System;
38 	using StreamReader	= System.IO.StreamReader;
39 	using FileInfo		= System.IO.FileInfo;
40 	using Encoding		= System.Text.Encoding;
41 
42 	/// <summary>
43 	/// A character stream - an <see cref="ICharStream"/> - that loads
44 	/// and caches the contents of it's underlying file fully during
45 	/// object construction
46 	/// </summary>
47 	/// <remarks>
48 	/// This looks very much like an ANTLReaderStream or an ANTLRInputStream
49 	/// but, it is a special case. Since we know the exact size of the file to
50 	/// load, we can avoid lots of data copying and buffer resizing.
51 	/// </remarks>
52 	public class ANTLRFileStream : ANTLRStringStream
53 	{
54 		#region Constructors
55 
56 		/// <summary>
57 		/// Initializes a new instance of the ANTLRFileStream class
58 		/// </summary>
ANTLRFileStream()59 		protected ANTLRFileStream()
60 		{
61 		}
62 
63 		/// <summary>
64 		/// Initializes a new instance of the ANTLRFileStream class for the
65 		/// specified file name
66 		/// </summary>
ANTLRFileStream(string fileName)67 		public ANTLRFileStream(string fileName) :
68 			this(fileName, Encoding.Default)
69 		{
70 		}
71 
72 		/// <summary>
73 		/// Initializes a new instance of the ANTLRFileStream class for the
74 		/// specified file name and encoding
75 		/// </summary>
ANTLRFileStream(string fileName, Encoding encoding)76 		public ANTLRFileStream(string fileName, Encoding encoding)
77 		{
78 			this.fileName = fileName;
79 			Load(fileName, encoding);
80 		}
81 
82 		/// <summary>
83 		/// Gets the file name of this ANTLRFileStream underlying file
84 		/// </summary>
85 		override public string SourceName
86 		{
87 			get { return fileName; }
88 		}
89 
90 		#endregion
91 
92 
93 		#region Public API
94 
95 		/// <summary>
96 		/// Loads and buffers the specified file to be used as this
97 		/// ANTLRFileStream's source
98 		/// </summary>
99 		/// <param name="fileName">File to load</param>
100 		/// <param name="encoding">Encoding to apply to file</param>
Load(string fileName, Encoding encoding)101 		public virtual void Load(string fileName, Encoding encoding)
102 		{
103 			if (fileName == null)
104 				return;
105 
106 			StreamReader fr = null;
107 			try
108 			{
109 				FileInfo f = new FileInfo(fileName);
110 				int filelen = (int)GetFileLength(f);
111 				data = new char[filelen];
112 				if (encoding != null)
113 					fr = new StreamReader(fileName, encoding);
114 				else
115 					fr = new StreamReader(fileName, Encoding.Default);
116 				n = fr.Read((Char[])data, 0, data.Length);
117 			}
118 			finally
119 			{
120 				if (fr != null)
121 				{
122 					fr.Close();
123 				}
124 			}
125 		}
126 
127 		#endregion
128 
129 
130 		#region Data Members
131 
132 		/// <summary>Fully qualified name of the stream's underlying file</summary>
133 		protected string fileName;
134 
135 		#endregion
136 
137 
138 		#region Private Members
139 
GetFileLength(FileInfo file)140 		private long GetFileLength(FileInfo file)
141 		{
142 			if (file.Exists)
143 				return file.Length;
144 			else
145 				return 0;
146 		}
147 
148 		#endregion
149 	}
150 }