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 ResourceWrit


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Resources ResourceWriter.cs, 1.3, 1.4
Date: Mon, 01 Sep 2003 22:21:58 -0400

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

Modified Files:
        ResourceWriter.cs 
Log Message:


Finish the implementation of "ResourceWriter".


Index: ResourceWriter.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Resources/ResourceWriter.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** ResourceWriter.cs   16 Apr 2003 03:17:55 -0000      1.3
--- ResourceWriter.cs   2 Sep 2003 02:21:55 -0000       1.4
***************
*** 3,7 ****
   *            "System.Resources.ResourceWriter" class.
   *
!  * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
   *
   * This program is free software; you can redistribute it and/or modify
--- 3,7 ----
   *            "System.Resources.ResourceWriter" class.
   *
!  * Copyright (C) 2002, 2003  Southern Storm Software, Pty Ltd.
   *
   * This program is free software; you can redistribute it and/or modify
***************
*** 29,32 ****
--- 29,35 ----
  using System.Collections;
  using System.Globalization;
+ using System.Runtime.Serialization;
+ using System.Runtime.Serialization.Formatters;
+ using System.Runtime.Serialization.Formatters.Binary;
  
  #if ECMA_COMPAT
***************
*** 42,45 ****
--- 45,49 ----
        private Hashtable table;
        private Hashtable ignoreCaseNames;
+       private ArrayList types;
        private TextInfo info;
  
***************
*** 60,63 ****
--- 64,68 ----
                                table = new Hashtable();
                                ignoreCaseNames = new Hashtable();
+                               types = new ArrayList();
                                info = CultureInfo.InvariantCulture.TextInfo;
                        }
***************
*** 69,74 ****
                        }
  
!       // Add a string to this writer.
!       private void AddString(String name, String value)
                        {
                                // See if we already have the name.
--- 74,79 ----
                        }
  
!       // Add a value to this writer.
!       private void AddValue(String name, Object value)
                        {
                                // See if we already have the name.
***************
*** 85,88 ****
--- 90,99 ----
                                // Add the lower-case name to "ignoreCaseNames".
                                ignoreCaseNames.Add(lowerName, String.Empty);
+ 
+                               // Add the value's type to the type list.
+                               if(value != null && 
!types.Contains(value.GetType()))
+                               {
+                                       types.Add(value.GetType());
+                               }
                        }
  
***************
*** 100,133 ****
                                        throw new ArgumentNullException("name");
                                }
!                               else if(value == null)
!                               {
!                                       throw new 
ArgumentNullException("value");
!                               }
!                               else if(stream == null)
                                {
                                        throw new InvalidOperationException
                                                
(_("Invalid_ResourceWriterClosed"));
                                }
!                               // TODO: we don't support binary resources yet.
                        }
        public void AddResource(String name, Object value)
                        {
-                               // TODO: we don't support non-string resources 
yet.
                                if(name == null)
                                {
                                        throw new ArgumentNullException("name");
                                }
!                               else if(value == null)
!                               {
!                                       throw new 
ArgumentNullException("value");
!                               }
!                               else if(stream == null)
                                {
                                        throw new InvalidOperationException
                                                
(_("Invalid_ResourceWriterClosed"));
                                }
!                               else if(value is String)
                                {
!                                       AddString(name, (String)value);
                                }
                        }
--- 111,138 ----
                                        throw new ArgumentNullException("name");
                                }
!                               else if(generateDone)
                                {
                                        throw new InvalidOperationException
                                                
(_("Invalid_ResourceWriterClosed"));
                                }
!                               else
!                               {
!                                       AddValue(name, value);
!                               }
                        }
        public void AddResource(String name, Object value)
                        {
                                if(name == null)
                                {
                                        throw new ArgumentNullException("name");
                                }
!                               else if(generateDone)
                                {
                                        throw new InvalidOperationException
                                                
(_("Invalid_ResourceWriterClosed"));
                                }
!                               else
                                {
!                                       AddValue(name, value);
                                }
                        }
***************
*** 138,146 ****
                                        throw new ArgumentNullException("name");
                                }
!                               else if(value == null)
!                               {
!                                       throw new 
ArgumentNullException("value");
!                               }
!                               else if(stream == null)
                                {
                                        throw new InvalidOperationException
--- 143,147 ----
                                        throw new ArgumentNullException("name");
                                }
!                               else if(generateDone)
                                {
                                        throw new InvalidOperationException
***************
*** 149,153 ****
                                else
                                {
!                                       AddString(name, value);
                                }
                        }
--- 150,154 ----
                                else
                                {
!                                       AddValue(name, value);
                                }
                        }
***************
*** 166,170 ****
        public void Generate()
                        {
!                               // TODO
                        }
  
--- 167,360 ----
        public void Generate()
                        {
!                               if(generateDone)
!                               {
!                                       throw new InvalidOperationException
!                                               
(_("Invalid_ResourceWriterClosed"));
!                               }
!                               BinaryWriter bw = new BinaryWriter(stream);
!                               try
!                               {
!                                       // Write the resource file header.
!                                       bw.Write(ResourceManager.MagicNumber);
!                                       
bw.Write(ResourceManager.HeaderVersionNumber);
!                                       MemoryStream mem = new MemoryStream();
!                                       BinaryWriter membw = new 
BinaryWriter(mem);
!                                       
membw.Write("System.Resources.ResourceReader, mscorlib");
!                                       membw.Write
!                                               
("System.Resources.RuntimeResourceSet, mscorlib");
!                                       membw.Flush();
!                                       bw.Write((int)(mem.Length));
!                                       bw.Write(mem.GetBuffer(), 0, 
(int)(mem.Length));
! 
!                                       // Write the resource set header.
!                                       bw.Write(1);                    // 
Resource set version number.
!                                       bw.Write(table.Count);  // Number of 
resources.
! 
!                                       // Create streams for the name and 
value sections.
!                                       MemoryStream names = new MemoryStream();
!                                       BinaryWriter namesWriter = new 
BinaryWriter(names);
!                                       MemoryStream values = new 
MemoryStream();
!                                       BinaryWriter valuesWriter = new 
BinaryWriter(values);
!                                       int[] nameHash = new int [table.Count];
!                                       int[] namePosition = new int 
[table.Count];
!                                       int posn = 0;
! 
!                                       // Process all values in the resource 
set.
!                                       IDictionaryEnumerator e = 
table.GetEnumerator();
!                                       while(e.MoveNext())
!                                       {
!                                               // Hash the name and record the 
name position.
!                                               nameHash[posn] = 
ResourceReader.Hash((String)(e.Key));
!                                               namePosition[posn] = 
(int)(names.Position);
!                                               ++posn;
! 
!                                               // Write out the name and value 
index.
!                                               
namesWriter.Write((String)(e.Key));
!                                               
namesWriter.Write((int)(values.Position));
! 
!                                               // Write the type table index 
to the value section.
!                                               Object value = e.Value;
!                                               if(value == null)
!                                               {
!                                                       
valuesWriter.Write7BitEncoded(-1);
!                                               }
!                                               else
!                                               {
!                                                       
valuesWriter.Write7BitEncoded
!                                                               
(types.IndexOf(value.GetType()));
!                                               }
! 
!                                               // Write the value to the value 
section.
!                                               if(value != null)
!                                               {
!                                                       WriteValue(values, 
valuesWriter, value);
!                                               }
!                                       }
! 
!                                       // Write the type table.
!                                       bw.Write(types.Count);
!                                       foreach(Type t in types)
!                                       {
!                                               
bw.Write(t.AssemblyQualifiedName);
!                                       }
! 
!                                       // Align on an 8-byte boundary.
!                                       bw.Flush();
!                                       while((bw.BaseStream.Position & 7) != 0)
!                                       {
!                                               bw.Write((byte)0);
!                                       }
! 
!                                       // Sort the name hash and write it out.
!                                       Array.Sort(nameHash, namePosition);
!                                       foreach(int hash in nameHash)
!                                       {
!                                               bw.Write(hash);
!                                       }
!                                       foreach(int pos in nameHash)
!                                       {
!                                               bw.Write(pos);
!                                       }
! 
!                                       // Write the offset of the value 
section.
!                                       bw.Flush();
!                                       namesWriter.Flush();
!                                       valuesWriter.Flush();
!                                       bw.Write((int)(bw.BaseStream.Position + 
names.Length + 4));
! 
!                                       // Write the name and value sections.
!                                       bw.Write(names.GetBuffer(), 0, 
(int)(names.Length));
!                                       bw.Write(values.GetBuffer(), 0, 
(int)(values.Length));
!                                       names.Close();
!                                       values.Close();
!                               }
!                               finally
!                               {
!                                       generateDone = true;
!                                       bw.Flush();
!                                       ((IDisposable)bw).Dispose();
!                               }
!                       }
! 
!       // Write a resource value to a stream.
!       private void WriteValue(MemoryStream stream, BinaryWriter writer,
!                                                       Object value)
!                       {
!                               Type type = value.GetType();
!                               if(type == typeof(String))
!                               {
!                                       writer.Write((String)value);
!                               }
!                               else if(type == typeof(Byte))
!                               {
!                                       writer.Write((byte)value);
!                               }
!                               else if(type == typeof(SByte))
!                               {
!                                       writer.Write((sbyte)value);
!                               }
!                               else if(type == typeof(Int16))
!                               {
!                                       writer.Write((short)value);
!                               }
!                               else if(type == typeof(UInt16))
!                               {
!                                       writer.Write((ushort)value);
!                               }
!                               else if(type == typeof(Int32))
!                               {
!                                       writer.Write((int)value);
!                               }
!                               else if(type == typeof(UInt32))
!                               {
!                                       writer.Write((uint)value);
!                               }
!                               else if(type == typeof(Int64))
!                               {
!                                       writer.Write((long)value);
!                               }
!                               else if(type == typeof(UInt64))
!                               {
!                                       writer.Write((ulong)value);
!                               }
!                       #if CONFIG_EXTENDED_NUMERICS
!                               else if(type == typeof(Single))
!                               {
!                                       writer.Write((float)value);
!                               }
!                               else if(type == typeof(Double))
!                               {
!                                       writer.Write((double)value);
!                               }
!                               else if(type == typeof(Decimal))
!                               {
!                                       int[] bits = 
Decimal.GetBits((Decimal)value);
!                                       writer.Write(bits[0]);
!                                       writer.Write(bits[1]);
!                                       writer.Write(bits[2]);
!                                       writer.Write(bits[3]);
!                               }
!                       #endif
!                               else if(type == typeof(DateTime))
!                               {
!                                       writer.Write(((DateTime)value).Ticks);
!                               }
!                               else if(type == typeof(TimeSpan))
!                               {
!                                       writer.Write(((TimeSpan)value).Ticks);
!                               }
!                               else
!                               {
!                               #if CONFIG_SERIALIZATION
!                                       // Serialize the value with a binary 
formatter.
!                                       writer.Flush();
!                                       BinaryFormatter formatter;
!                                       formatter = new BinaryFormatter
!                                               (null, new StreamingContext
!                                                               
(StreamingContextStates.File |
!                                                                
StreamingContextStates.Persistence));
!                                       formatter.Serialize(stream, value);
!                               #endif
!                               }
                        }
  





reply via email to

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