2 // System.Resources.ResourceReader.cs
5 // Duncan Mak <duncan@ximian.com>
6 // Nick Drochak <ndrochak@gol.com>
8 // 2001 (C) Ximian Inc, http://www.ximian.com
12 using System.Collections;
13 using System.Resources;
16 namespace System.Resources
18 class MonoTODO : Attribute {}
\r
19 public sealed class ResourceReader : IResourceReader, IDisposable
22 ArrayList resourceNames = null;
23 ArrayList resourceValues = null;
24 BinaryReader binaryReader;
25 int resourceCount = 0;
27 ArrayList typeArray = new ArrayList();
28 ArrayList hashes = new ArrayList();
29 ArrayList positions = new ArrayList();
30 int dataSectionOffset;
33 public ResourceReader (Stream stream)
36 throw new ArgumentNullException ("Value cannot be null.");
39 throw new ArgumentException ("Stream was not readable.");
43 if (!IsStreamValid()){
44 throw new ArgumentException("Stream is not a valid .resources file! It was possibly truncated.");
48 public ResourceReader (string fileName)
51 throw new ArgumentException ("Path cannot be null.");
53 if (String.Empty == fileName)
54 throw new ArgumentException("Empty path name is not legal.");
56 if (!System.IO.File.Exists (fileName))
57 throw new FileNotFoundException ("Could not find file " + Path.GetFullPath(fileName));
59 stream = new FileStream (fileName, FileMode.Open);
61 if (!IsStreamValid()){
62 throw new ArgumentException("Stream is not a valid .resources file! It was possibly truncated.");
67 private bool IsStreamValid() {
68 // not sure how much to check to determine if it's valid,
69 // but look at magic number, version numbers and class names
71 string resourceSetClass;
73 binaryReader = new BinaryReader(stream);
74 int magicNumber = binaryReader.ReadInt32();
75 if (-1091581234 != magicNumber) {
78 int versionNumber = binaryReader.ReadInt32();
79 if (1 != versionNumber){
82 // Ignore next 32bits. they contain the length of the class name strings
83 binaryReader.ReadInt32();
85 readerClass = binaryReader.ReadString();
86 if (!readerClass.StartsWith("System.Resources.ResourceReader")){
89 resourceSetClass = binaryReader.ReadString();
90 if (!resourceSetClass.StartsWith("System.Resources.RuntimeResourceSet")){
93 int versionNumber2 = binaryReader.ReadInt32();
94 if (1 != versionNumber2){
98 resourceCount = binaryReader.ReadInt32();
99 typeCount = binaryReader.ReadInt32();
101 for (int i = 0; i < typeCount; i++) {
102 typeArray.Add(binaryReader.ReadString());
104 for (int i = 0; i < resourceCount; i++) {
105 hashes.Add(binaryReader.ReadInt32());
107 for (int i = 0; i < resourceCount; i++) {
108 positions.Add(binaryReader.ReadInt32());
111 dataSectionOffset = binaryReader.ReadInt32();
119 private string ReadString(BinaryReader br) {
120 return br.ReadString();
129 public IDictionaryEnumerator GetEnumerator () {
131 throw new InvalidOperationException("ResourceReader is closed.");
134 // STRATEGY: if this is the first enumerator requested, fill the hash.
135 // delaying in this way seems ok since there's not much you can do with just
136 // a reader except close it. And if you close it, you cannot get the enumerator.
137 // So, create the hash for the first enumerator, and re-use it for all others.
138 if (null == resourceNames) {
141 return new ResourceEnumerator (this);
146 private void FillResources(){
147 resourceNames = new ArrayList();
148 BinaryReader unicodeReader =
149 new BinaryReader(binaryReader.BaseStream, System.Text.Encoding.Unicode);
150 // TODO: need to put these in an array and work out when to get the values.
151 // also need to figure out the hash and how/if to use it.
152 string test = unicodeReader.ReadString();
153 int offset = binaryReader.ReadInt32();
156 IEnumerator IEnumerable.GetEnumerator ()
158 return ((IResourceReader) this).GetEnumerator();
162 void IDisposable.Dispose ()
164 // FIXME: is this all we need to do?
170 internal class ResourceEnumerator : IDictionaryEnumerator
172 protected DictionaryEntry entry;
173 protected object key;
174 protected object value;
175 protected ResourceReader reader;
177 public ResourceEnumerator(ResourceReader readerToEnumerate){
178 reader = readerToEnumerate;
181 public DictionaryEntry Entry
183 get { return entry; }
193 get { return value; }
197 public object Current
203 public bool MoveNext ()
209 public void Reset () { }