dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Resources ResourceRead


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Resources ResourceReader.cs, 1.5, 1.6
Date: Fri, 22 Aug 2003 03:13:52 -0400

Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Resources
In directory subversions:/tmp/cvs-serv21968/runtime/System/Resources

Modified Files:
        ResourceReader.cs 
Log Message:


Add support for reading non-string resources.


Index: ResourceReader.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Resources/ResourceReader.cs,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** ResourceReader.cs   16 Apr 2003 03:17:55 -0000      1.5
--- ResourceReader.cs   22 Aug 2003 07:13:50 -0000      1.6
***************
*** 29,32 ****
--- 29,35 ----
  using System.IO;
  using System.Text;
+ using System.Runtime.Serialization;
+ using System.Runtime.Serialization.Formatters;
+ using System.Runtime.Serialization.Formatters.Binary;
  
  #if ECMA_COMPAT
***************
*** 44,47 ****
--- 47,51 ----
        private long nameStart;
        private long dataStart;
+       private Type[] types;
  
        // Constructors.
***************
*** 236,239 ****
--- 240,262 ----
                        }
  
+       // Convert a string name into a type.
+       private static Type StringToType(String name)
+                       {
+                               if(name == null)
+                               {
+                                       return null;
+                               }
+                               if(name == "System.String" || 
name.StartsWith("System.String,"))
+                               {
+                                       // This is the most common case that we 
will encounter.
+                                       return typeof(String);
+                               }
+                       #if CONFIG_REFLECTION
+                               return Type.GetType(name);
+                       #else
+                               return null;
+                       #endif
+                       }
+ 
        // Read the resource stream header.  Returns false if
        // the header was invalid in some way.
***************
*** 269,278 ****
                                        return false;
                                }
-                               numTypes = ReadUInt(header, 8);
  
!                               // Skip the type table.  TODO: handle 
non-string types.
                                while(numTypes > 0)
                                {
!                                       ReadString(stream);
                                        --numTypes;
                                }
--- 292,303 ----
                                        return false;
                                }
  
!                               // Read the type table.
!                               numTypes = ReadUInt(header, 8);
!                               types = new Type [(int)numTypes];
!                               posn = 0;
                                while(numTypes > 0)
                                {
!                                       types[posn++] = 
StringToType(ReadString(stream));
                                        --numTypes;
                                }
***************
*** 319,322 ****
--- 344,493 ----
                        }
  
+       // Read floating-point values.
+       private static float ReadSingle(Stream stream)
+                       {
+                               int value;
+                               byte[] buf = new byte [4];
+                               stream.Read(buf, 0, 4);
+                               if(BitConverter.IsLittleEndian)
+                               {
+                                       value = ((int)(buf[0])) |
+                                               (((int)(buf[1])) << 8) |
+                                               (((int)(buf[2])) << 16) |
+                                               (((int)(buf[3])) << 24);
+                               }
+                               else
+                               {
+                                       value = ((int)(buf[3])) |
+                                               (((int)(buf[2])) << 8) |
+                                               (((int)(buf[1])) << 16) |
+                                               (((int)(buf[0])) << 24);
+                               }
+                               return BitConverter.Int32BitsToFloat(value);
+                       }
+       private static double ReadDouble(Stream stream)
+                       {
+                               long value;
+                               byte[] buf = new byte [8];
+                               stream.Read(buf, 0, 8);
+                               if(BitConverter.IsLittleEndian)
+                               {
+                                       value = ((long)(buf[0])) |
+                                                   (((long)(buf[1])) << 8) |
+                                                   (((long)(buf[2])) << 16) |
+                                                   (((long)(buf[3])) << 24) |
+                                                   (((long)(buf[4])) << 32) |
+                                                   (((long)(buf[5])) << 40) |
+                                                   (((long)(buf[6])) << 48) |
+                                                   (((long)(buf[7])) << 56);
+                               }
+                               else
+                               {
+                                       value = ((long)(buf[7])) |
+                                                   (((long)(buf[6])) << 8) |
+                                                   (((long)(buf[5])) << 16) |
+                                                   (((long)(buf[4])) << 24) |
+                                                   (((long)(buf[3])) << 32) |
+                                                   (((long)(buf[2])) << 40) |
+                                                   (((long)(buf[1])) << 48) |
+                                                   (((long)(buf[0])) << 56);
+                               }
+                               return BitConverter.Int64BitsToDouble(value);
+                       }
+ 
+       // Read an object value of a particular type.
+       private static Object ReadObject(Stream stream, Type type)
+                       {
+                               int byteval;
+                               long temp;
+ 
+                               // Handle the simple type cases first.
+                               if(type == null)
+                               {
+                                       return null;
+                               }
+                               if(type == typeof(Byte))
+                               {
+                                       return (byte)(stream.ReadByte());
+                               }
+                               else if(type == typeof(SByte))
+                               {
+                                       return (sbyte)(stream.ReadByte());
+                               }
+                               else if(type == typeof(Int16))
+                               {
+                                       byteval = stream.ReadByte();
+                                       return (short)(byteval | 
(stream.ReadByte() << 8));
+                               }
+                               else if(type == typeof(UInt16))
+                               {
+                                       byteval = stream.ReadByte();
+                                       return (ushort)(byteval | 
(stream.ReadByte() << 8));
+                               }
+                               else if(type == typeof(Int32))
+                               {
+                                       return ReadInt(stream);
+                               }
+                               else if(type == typeof(UInt32))
+                               {
+                                       return (uint)(ReadInt(stream));
+                               }
+                               else if(type == typeof(Int64))
+                               {
+                                       temp = (long)(ReadInt(stream));
+                                       return (temp | (((long)ReadInt(stream)) 
<< 32));
+                               }
+                               else if(type == typeof(UInt64))
+                               {
+                                       temp = (long)(ReadInt(stream));
+                                       return (ulong)(temp | 
(((long)ReadInt(stream)) << 32));
+                               }
+                               else if(type == typeof(Single))
+                               {
+                                       return ReadSingle(stream);
+                               }
+                               else if(type == typeof(Double))
+                               {
+                                       return ReadDouble(stream);
+                               }
+                               else if(type == typeof(Decimal))
+                               {
+                                       int[] bits = new int [4];
+                                       bits[0] = ReadInt(stream);
+                                       bits[1] = ReadInt(stream);
+                                       bits[2] = ReadInt(stream);
+                                       bits[3] = ReadInt(stream);
+                                       return new Decimal(bits);
+                               }
+                               else if(type == typeof(DateTime))
+                               {
+                                       temp = (long)(ReadInt(stream));
+                                       return new DateTime(temp | 
(((long)ReadInt(stream)) << 32));
+                               }
+                               else if(type == typeof(TimeSpan))
+                               {
+                                       temp = (long)(ReadInt(stream));
+                                       return new TimeSpan(temp | 
(((long)ReadInt(stream)) << 32));
+                               }
+ 
+                       #if CONFIG_SERIALIZATION
+                               // De-serialize the value with a binary 
formatter.
+                               BinaryFormatter formatter;
+                               formatter = new BinaryFormatter
+                                       (null, new StreamingContext
+                                                       
(StreamingContextStates.File |
+                                                        
StreamingContextStates.Persistence));
+                               formatter.AssemblyFormat = 
FormatterAssemblyStyle.Simple;
+                               Object obj = formatter.Deserialize(stream);
+                               if(obj != null && obj.GetType() == type)
+                               {
+                                       return obj;
+                               }
+                       #endif
+ 
+                               // Don't know how to de-serialize, so return 
null.
+                               return null;
+                       }
+ 
        // Look up a resource object by name.  This is a short-cut
        // that "ResourceSet" can use to perform quicker lookups in
***************
*** 327,330 ****
--- 498,502 ----
                                int left, right, middle;
                                String test;
+                               int typeCode;
  
                                // Search for the hash value using a binary 
search.
***************
*** 379,384 ****
                                                }
                                                stream.Seek(dataStart + 
valuePosn, SeekOrigin.Begin);
!                                               ReadLength(stream);     // Skip 
type value (TODO)
!                                               return ReadString(stream);
                                        }
                                        ++left;
--- 551,568 ----
                                                }
                                                stream.Seek(dataStart + 
valuePosn, SeekOrigin.Begin);
!                                               typeCode = ReadLength(stream);
!                                               if(typeCode < 0 || typeCode >= 
types.Length)
!                                               {
!                                                       return null;
!                                               }
!                                               if(types[typeCode] == 
typeof(String))
!                                               {
!                                                       // This is the most 
common case.
!                                                       return 
ReadString(stream);
!                                               }
!                                               else
!                                               {
!                                                       return 
ReadObject(stream, types[typeCode]);
!                                               }
                                        }
                                        ++left;
***************
*** 397,401 ****
                private int posn;
                private String key;
!               private String value;
  
                // Constructor.
--- 581,585 ----
                private int posn;
                private String key;
!               private Object value;
  
                // Constructor.
***************
*** 409,412 ****
--- 593,597 ----
                public bool MoveNext()
                                {
+                                       int typeCode;
                                        Stream stream = reader.stream;
                                        if(stream == null)
***************
*** 434,439 ****
                                                stream.Seek(reader.dataStart + 
(long)valuePosn,
                                                                        
SeekOrigin.Begin);
!                                               ReadLength(stream);     // Skip 
type value (TODO)
!                                               value = ReadString(stream);
                                                if(value == null)
                                                {
--- 619,636 ----
                                                stream.Seek(reader.dataStart + 
(long)valuePosn,
                                                                        
SeekOrigin.Begin);
!                                               typeCode = ReadLength(stream);
!                                               if(typeCode < 0 || typeCode >= 
types.Length)
!                                               {
!                                                       value = null;
!                                               }
!                                               else if(types[typeCode] == 
typeof(String))
!                                               {
!                                                       // This is the most 
common case.
!                                                       value = 
ReadString(stream);
!                                               }
!                                               else
!                                               {
!                                                       value = 
ReadObject(stream, types[typeCode]);
!                                               }
                                                if(value == null)
                                                {





reply via email to

[Prev in Thread] Current Thread [Next in Thread]