2007-01-07 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Mime.cs
index 328a56b3c00f0979dfb62976f39a51711eb86ba2..d4e1a3e067c40ce5ed16d437e8a5949d527c6f42 100644 (file)
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2006 Alexander Olk
 //
 // Authors:
 //
-//  Alexander Olk      xenomorph2@onlinehome.de
+//  Alexander Olk      alex.olk@googlemail.com
 //
 
 using System;
@@ -88,6 +88,8 @@ namespace System.Windows.Forms
                
                private int bytes_read = 0;
                
+               private bool mime_available = false;
+               
                public static NameValueCollection Aliases;
                public static NameValueCollection SubClasses;
                
@@ -101,12 +103,22 @@ namespace System.Windows.Forms
                
                private Mime( )
                {
+#if NET_2_0
+                       Aliases = new NameValueCollection (StringComparer.CurrentCultureIgnoreCase);
+                       SubClasses = new NameValueCollection (StringComparer.CurrentCultureIgnoreCase);
+                       GlobalPatternsShort = new NameValueCollection (StringComparer.CurrentCultureIgnoreCase);
+                       GlobalPatternsLong = new NameValueCollection (StringComparer.CurrentCultureIgnoreCase);
+                       GlobalLiterals = new NameValueCollection (StringComparer.CurrentCultureIgnoreCase);
+                       GlobalSufPref = new NameValueCollection (StringComparer.CurrentCultureIgnoreCase);
+#else
                        Aliases = new NameValueCollection (new CaseInsensitiveHashCodeProvider (), new Comparer (System.Globalization.CultureInfo.CurrentUICulture));
                        SubClasses = new NameValueCollection (new CaseInsensitiveHashCodeProvider (), new Comparer (System.Globalization.CultureInfo.CurrentUICulture));
                        GlobalPatternsShort = new NameValueCollection (new CaseInsensitiveHashCodeProvider (), new Comparer (System.Globalization.CultureInfo.CurrentUICulture));
                        GlobalPatternsLong = new NameValueCollection (new CaseInsensitiveHashCodeProvider (), new Comparer (System.Globalization.CultureInfo.CurrentUICulture));
                        GlobalLiterals = new NameValueCollection (new CaseInsensitiveHashCodeProvider (), new Comparer (System.Globalization.CultureInfo.CurrentUICulture));
                        GlobalSufPref = new NameValueCollection (new CaseInsensitiveHashCodeProvider (), new Comparer (System.Globalization.CultureInfo.CurrentUICulture));
+#endif
+                       
                        Matches80Plus = new ArrayList ();
                        MatchesBelow80 = new ArrayList ();
                        
@@ -115,6 +127,14 @@ namespace System.Windows.Forms
                        
                        if (buffer_length != -1) {
                                buffer = new byte[ buffer_length ];
+                               mime_available = true;
+                       }
+               }
+               
+               public static bool MimeAvailable
+               {
+                       get {
+                               return Instance.mime_available;
                        }
                }
                
@@ -177,17 +197,19 @@ namespace System.Windows.Forms
                        GoByFileName( );
 //                     }
                        
-//                     if ( !mime_file_cache.ContainsKey( current_file_name ) )
                        mime_file_cache.Add( current_file_name, global_result );
                        
-                       // not tested
-                       if ( mime_file_cache.Count > mime_file_cache_max_size )
-                       {
-                               IEnumerator enumerator = mime_file_cache.GetEnumerator( );
+                       if (mime_file_cache.Count > mime_file_cache_max_size) {
+                               IEnumerator enumerator = mime_file_cache.GetEnumerator ();
                                
-                               for ( int i = 0; i < mime_file_cache_max_size - 1000; i++ )
-                               {
-                                       mime_file_cache.Remove( enumerator.Current.ToString( ) );
+                               int counter = mime_file_cache_max_size - 1000;
+                               
+                               while (enumerator.MoveNext ()) {
+                                       mime_file_cache.Remove (enumerator.Current.ToString ());
+                                       counter--;
+                                       
+                                       if (counter == 0)
+                                               break;
                                }
                        }
                }
@@ -478,18 +500,20 @@ namespace System.Windows.Forms
                                
                                if ( extension.Length != 0 )
                                {
-                                       global_result = GlobalPatternsShort[ extension ];
+                                       string global_result_tmp = GlobalPatternsShort[ extension ];
                                        
-                                       if ( global_result != null )
+                                       if ( global_result_tmp != null )
                                        {
+                                               global_result = global_result_tmp;
                                                CheckGlobalResult( );
                                                return true;
                                        }
                                        
-                                       global_result = GlobalPatternsShort[ extension.ToLower( ) ];
+                                       global_result_tmp = GlobalPatternsShort[ extension.ToLower( ) ];
                                        
-                                       if ( global_result != null )
+                                       if ( global_result_tmp != null )
                                        {
+                                               global_result = global_result_tmp;
                                                CheckGlobalResult( );
                                                return true;
                                        }
@@ -503,7 +527,7 @@ namespace System.Windows.Forms
                                
                                if ( key[0] == '*' )
                                {
-                                       if (FastEndsWidth(filename, key.Replace( "*", "" )))
+                                       if (FastEndsWidth(filename, key.Replace( "*", String.Empty )))
                                        {
                                                global_result = GlobalSufPref[ i ];
                                                CheckGlobalResult( );
@@ -512,7 +536,7 @@ namespace System.Windows.Forms
                                }
                                else
                                {
-                                       if ( FastStartsWith(filename, key.Replace( "*", "" ) ) )
+                                       if ( FastStartsWith(filename, key.Replace( "*", String.Empty ) ) )
                                        {
                                                global_result = GlobalSufPref[ i ];
                                                CheckGlobalResult( );
@@ -718,7 +742,7 @@ namespace System.Windows.Forms
                        {
                                index += 13; // Length of string "Content-type:"
                                
-                               global_result = "";
+                               global_result = String.Empty;
                                
                                while ( search_string[ index ] != ';' )
                                {
@@ -838,7 +862,7 @@ namespace System.Windows.Forms
                                                // indent
                                                char c;
                                                if (br.PeekChar () != '>') {
-                                                       string indent_string = "";
+                                                       string indent_string = String.Empty;
                                                        while (true) {
                                                                if (br.PeekChar () == '>')
                                                                        break;
@@ -1074,7 +1098,7 @@ namespace System.Windows.Forms
                
                private int ReadValue ()
                {
-                       string result_string = "";
+                       string result_string = String.Empty;
                        int result = 0;
                        char c;
                        
@@ -1094,7 +1118,7 @@ namespace System.Windows.Forms
                private string ReadPriorityAndMimeType (ref int priority)
                {
                        if (br.ReadChar () == '[') {
-                               string priority_string = "";
+                               string priority_string = String.Empty;
                                while (true) {
                                        char c = br.ReadChar ();
                                        if (c == ':')
@@ -1104,7 +1128,7 @@ namespace System.Windows.Forms
                                
                                priority = System.Convert.ToInt32 (priority_string);
                                
-                               string mime_type_result = "";
+                               string mime_type_result = String.Empty;
                                while (true) {
                                        char c = br.ReadChar ();
                                        if (c == ']')