Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ResourceReader.cs « System.Resources « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c2efa3fcf24e714d522eab9517709e5525def50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// System.Resources.ResourceReader.cs
//
// Authors: 
// 	Duncan Mak <duncan@ximian.com>
//	Nick Drochak <ndrochak@gol.com>
//
// 2001 (C) Ximian Inc, http://www.ximian.com
//
// TODO: Finish this

using System.Collections;
using System.Resources;
using System.IO;

namespace System.Resources
{
	class MonoTODO : Attribute {}
	public sealed class ResourceReader : IResourceReader, IDisposable
	{
		Stream stream;
		internal ArrayList resourceNames = null;
		internal ArrayList resourceValues = null;
		BinaryReader binaryReader;
		internal int resourceCount = 0;
		int typeCount = 0;
		ArrayList typeArray = new ArrayList();
		ArrayList hashes = new ArrayList();
		ArrayList positions = new ArrayList();
		int dataSectionOffset;

		// Constructors
		public ResourceReader (Stream stream)
		{
			if (stream == null)
				throw new ArgumentNullException ("Value cannot be null.");
			
			if (!stream.CanRead)
				throw new ArgumentException ("Stream was not readable.");

			this.stream = stream;
			
			if (!IsStreamValid()){
				throw new ArgumentException("Stream is not a valid .resources file!  It was possibly truncated.");
			}
		}
		
		public ResourceReader (string fileName)
		{
			if (fileName == null)
				throw new ArgumentException ("Path cannot be null.");
			
			if (String.Empty == fileName)
				throw new ArgumentException("Empty path name is not legal.");

			if (!System.IO.File.Exists (fileName)) 
				throw new FileNotFoundException ("Could not find file " + Path.GetFullPath(fileName));

			stream = new FileStream (fileName, FileMode.Open);

			if (!IsStreamValid()){
				throw new ArgumentException("Stream is not a valid .resources file!  It was possibly truncated.");
			}
		}
		
		[MonoTODO]
		private bool IsStreamValid() {
			// not sure how much to check to determine if it's valid, 
			// but look at magic number, version numbers and class names
			string readerClass;
			string resourceSetClass;
			try {
				binaryReader = new BinaryReader(stream);
				int magicNumber = binaryReader.ReadInt32();
				if (-1091581234 != magicNumber) {
					return false;
				}
				int versionNumber = binaryReader.ReadInt32();
				if (1 != versionNumber){
					return false;
				}
				// Ignore next 32bits. they contain the length of the class name strings
				binaryReader.ReadInt32();

				readerClass = binaryReader.ReadString();
				if (!readerClass.StartsWith("System.Resources.ResourceReader")){
					return false;
				}
				resourceSetClass = binaryReader.ReadString();
				if (!resourceSetClass.StartsWith("System.Resources.RuntimeResourceSet")){
					return false;
				}
				int versionNumber2 = binaryReader.ReadInt32();
				if (1 != versionNumber2){
					return false;
				}

				resourceCount = binaryReader.ReadInt32();
				typeCount = binaryReader.ReadInt32();

				for (int i = 0; i < typeCount; i++) {
					typeArray.Add(binaryReader.ReadString());
				}
				for (int i = 0; i < resourceCount; i++) {
					hashes.Add(binaryReader.ReadInt32());
				}
				for (int i = 0; i < resourceCount; i++) {
					positions.Add(binaryReader.ReadInt32());
				}

				dataSectionOffset = binaryReader.ReadInt32();

				// LAMESPEC: what is the next Int32 here?
				binaryReader.ReadInt32();
			}
			catch{
				return false;
			}
			return true;
		}

		private string ReadString(BinaryReader br) {
			return br.ReadString();
		}

		public void Close ()
		{
			stream.Close ();
			stream = null;
		}
		
		public IDictionaryEnumerator GetEnumerator () {
			if (null == stream){
				throw new InvalidOperationException("ResourceReader is closed.");
			}
			else {
				// STRATEGY: if this is the first enumerator requested, fill the hash.
				// delaying in this way seems ok since there's not much you can do with just
				// a reader except close it.  And if you close it, you cannot get the enumerator.
				// So, create the hash for the first enumerator, and re-use it for all others.
				if (null == resourceNames) {
					FillResources();
				}
				return new ResourceEnumerator (this);
			}
		}
		
		internal struct NameOffsetPair {
			public string name;
			public int offset;
		}

		[MonoTODO]
		private void FillResources(){
			NameOffsetPair pair;
			resourceNames = new ArrayList();
			resourceValues = new ArrayList();
			BinaryReader unicodeReader = 
				new BinaryReader(binaryReader.BaseStream, System.Text.Encoding.Unicode);
			// TODO: need to put these in an array and work out when to get the values.
			// also need to figure out the hash and how/if to use it.
			for (int index=0; index < resourceCount; index++){
				pair = new NameOffsetPair();
				pair.name = unicodeReader.ReadString();
				pair.offset = binaryReader.ReadInt32();
				resourceNames.Add(pair);
			}
			for (int index=0; index < resourceCount; index++){
				// LAMESPEC: what the heck is this byte here?  always 0? just a separator?
				binaryReader.ReadByte();
				resourceValues.Add(binaryReader.ReadString());
			}
		}

		IEnumerator IEnumerable.GetEnumerator ()
		{
			return ((IResourceReader) this).GetEnumerator();
		}
		
		[MonoTODO]
		void IDisposable.Dispose ()
		{
			// FIXME: is this all we need to do?
			Close();
		}
		
		internal class ResourceEnumerator : IDictionaryEnumerator
		{
			protected ResourceReader reader;
			protected int index = -1;

			
			internal ResourceEnumerator(ResourceReader readerToEnumerate){
				reader = readerToEnumerate;
			}

			public virtual DictionaryEntry Entry
			{
				get {
					if (null == reader.stream)
						throw new InvalidOperationException("ResourceReader is closed.");
					if (index < 0)
						throw new InvalidOperationException("Enumeration has not started. Call MoveNext.");

					DictionaryEntry entry = new DictionaryEntry();
					entry.Key = Key;
					entry.Value = Value;
					return entry; 
				}
			}
			
			public virtual object Key
			{
				get { 
					if (null == reader.stream)
						throw new InvalidOperationException("ResourceReader is closed.");
					if (index < 0)
						throw new InvalidOperationException("Enumeration has not started. Call MoveNext.");
					return ((NameOffsetPair)(reader.resourceNames[index])).name; 
				}
			}
			
			public virtual object Value
			{
				get { 
					if (null == reader.stream)
						throw new InvalidOperationException("ResourceReader is closed.");
					if (index < 0)
						throw new InvalidOperationException("Enumeration has not started. Call MoveNext.");
					return reader.resourceValues[index];
				}
			}
			
			public virtual object Current
			{
				get {
					if (null == reader.stream)
						throw new InvalidOperationException("ResourceReader is closed.");
					if (index < 0)
						throw new InvalidOperationException("Enumeration has not started. Call MoveNext.");
					return Entry; 
				}
			}
			
			public virtual bool MoveNext ()
			{
				if (null == reader.stream)
					throw new InvalidOperationException("ResourceReader is closed.");
				if (++index < reader.resourceCount){
					return true;
				}
				else {
					--index;
					return false;
				}
			}
			
			public void Reset () {
				if (null == reader.stream)
					throw new InvalidOperationException("ResourceReader is closed.");
				index = -1;
			}
		} // internal class ResourceEnumerator
	}  // public sealed class ResourceReader
} // namespace System.Resources