Add g_output_stream_write_all_async()
[platform/upstream/glib.git] / gio / xdgmime / xdgmimeglob.c
index c82bf22..73ef7a5 100644 (file)
@@ -20,9 +20,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifdef HAVE_CONFIG_H
 #include <string.h>
 #include <fnmatch.h>
 
+#ifndef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
 #ifndef        FALSE
 #define        FALSE   (0)
 #endif
@@ -158,6 +160,8 @@ _xdg_glob_hash_node_new (void)
   return glob_hash_node;
 }
 
+#ifdef NOT_USED_IN_GIO
+
 static void
 _xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node,
                          int depth)
@@ -177,6 +181,8 @@ _xdg_glob_hash_node_dump (XdgGlobHashNode *glob_hash_node,
     _xdg_glob_hash_node_dump (glob_hash_node->next, depth);
 }
 
+#endif
+
 static XdgGlobHashNode *
 _xdg_glob_hash_insert_ucs4 (XdgGlobHashNode *glob_hash_node,
                            xdg_unichar_t   *text,
@@ -400,6 +406,34 @@ ascii_tolower (const char *str)
   return lower;
 }
 
+static int
+filter_out_dupes (MimeWeight mimes[], int n_mimes)
+{
+  int last;
+  int i, j;
+
+  last = n_mimes;
+
+  for (i = 0; i < last; i++)
+    {
+      j = i + 1;
+      while (j < last)
+        {
+          if (strcmp (mimes[i].mime, mimes[j].mime) == 0)
+            {
+              mimes[i].weight = MAX (mimes[i].weight, mimes[j].weight);
+              last--;
+              mimes[j].mime = mimes[last].mime;
+              mimes[j].weight = mimes[last].weight;
+            }
+          else
+            j++;
+        }
+    }
+
+  return last;
+}
+
 int
 _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
                                 const char  *file_name,
@@ -412,7 +446,6 @@ _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
   int n_mimes = 10;
   int len;
   char *lower_case;
-  int try_lower_case;
 
   /* First, check the literals */
 
@@ -447,11 +480,11 @@ _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
   len = strlen (file_name);
   n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, lower_case, len, FALSE,
                                            mimes, n_mimes);
-  if (n == 0)
-    n = _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, file_name, len, TRUE,
-                                             mimes, n_mimes);
+  if (n < 2)
+    n += _xdg_glob_hash_node_lookup_file_name (glob_hash->simple_node, file_name, len, TRUE,
+                                              mimes + n, n_mimes - n);
 
-  if (n == 0)
+  if (n < 2)
     {
       for (list = glob_hash->full_list; list && n < n_mime_types; list = list->next)
         {
@@ -465,6 +498,8 @@ _xdg_glob_hash_lookup_file_name (XdgGlobHash *glob_hash,
     }
   free (lower_case);
 
+  n = filter_out_dupes (mimes, n);
+
   qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
 
   if (n_mime_types < n)
@@ -570,6 +605,8 @@ _xdg_glob_hash_append_glob (XdgGlobHash *glob_hash,
     }
 }
 
+#ifdef NOT_USED_IN_GIO
+
 void
 _xdg_glob_hash_dump (XdgGlobHash *glob_hash)
 {
@@ -606,6 +643,7 @@ _xdg_glob_hash_dump (XdgGlobHash *glob_hash)
     }
 }
 
+#endif
 
 void
 _xdg_mime_glob_read_from_file (XdgGlobHash *glob_hash,