Update an example from 1.3 to 2.x
[platform/upstream/glib.git] / gmodule / gmodule.c
index 507f527..1582c96 100644 (file)
@@ -31,6 +31,7 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+#include       "gstdio.h"
 #include       "gmodule.h"
 #include       "gmoduleconf.h"
 #include       <errno.h>
@@ -58,6 +59,9 @@
 struct _GModule
 {
   gchar        *file_name;
+#ifdef G_OS_WIN32
+  gchar *cp_file_name;
+#endif
   gpointer handle;
   guint ref_count : 31;
   guint is_resident : 1;
@@ -207,10 +211,12 @@ parse_libtool_archive (const gchar* libtool_name)
   GTokenType token;
   GScanner *scanner;
   
-  int fd = open (libtool_name, O_RDONLY, 0);
+  int fd = g_open (libtool_name, O_RDONLY, 0);
   if (fd < 0)
     {
-      g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive \"%s\"", libtool_name));   
+      gchar *display_libtool_name = g_filename_display_name (libtool_name);
+      g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive \"%s\"", display_libtool_name));
+      g_free (display_libtool_name);
       return NULL;
     }
   /* search libtool's dlname specification  */
@@ -234,7 +240,9 @@ parse_libtool_archive (const gchar* libtool_name)
              (token == TOKEN_INSTALLED ? 
               G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
            {
-             g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive \"%s\"", libtool_name));
+             gchar *display_libtool_name = g_filename_display_name (libtool_name);
+             g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive \"%s\"", display_libtool_name));
+             g_free (display_libtool_name);
 
              g_free (lt_dlname);
              g_free (lt_libdir);
@@ -313,6 +321,9 @@ g_module_open (const gchar    *file_name,
            {
              main_module = g_new (GModule, 1);
              main_module->file_name = NULL;
+#ifdef G_OS_WIN32
+             main_module->cp_file_name = NULL;
+#endif
              main_module->handle = handle;
              main_module->ref_count = 1;
              main_module->is_resident = TRUE;
@@ -338,13 +349,13 @@ g_module_open (const gchar    *file_name,
     }
 
   /* check whether we have a readable file right away */
-  if (g_file_test (file_name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
+  if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
     name = g_strdup (file_name);
   /* try completing file name with standard library suffix */
   if (!name)
     {
       name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
-      if (!g_file_test (name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
+      if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
        {
          g_free (name);
          name = NULL;
@@ -354,7 +365,7 @@ g_module_open (const gchar    *file_name,
   if (!name)
     {
       name = g_strconcat (file_name, ".la", NULL);
-      if (!g_file_test (name, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
+      if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
        {
          g_free (name);
          name = NULL;
@@ -392,7 +403,11 @@ g_module_open (const gchar    *file_name,
                        (flags & G_MODULE_BIND_LOCAL) != 0);
     }
   else
-    g_module_set_error_unduped (g_strdup_printf ("unable to access file \"%s\"", file_name));
+    {
+      gchar *display_file_name = g_filename_display_name (file_name);
+      g_module_set_error_unduped (g_strdup_printf ("unable to access file \"%s\"", display_file_name));
+      g_free (display_file_name);
+    }
   g_free (name);
 
   if (handle)
@@ -418,6 +433,10 @@ g_module_open (const gchar    *file_name,
       
       module = g_new (GModule, 1);
       module->file_name = g_strdup (file_name);
+#ifdef G_OS_WIN32
+      module->cp_file_name = g_locale_from_utf8 (file_name, -1,
+                                                NULL, NULL, NULL);
+#endif
       module->handle = handle;
       module->ref_count = 1;
       module->is_resident = FALSE;
@@ -453,6 +472,24 @@ g_module_open (const gchar    *file_name,
   return module;
 }
 
+#ifdef G_OS_WIN32
+
+#undef g_module_open
+
+GModule*
+g_module_open (const gchar    *file_name,
+              GModuleFlags    flags)
+{
+  gchar *utf8_file_name = g_locale_to_utf8 (file_name, -1, NULL, NULL, NULL);
+  GModule *retval = g_module_open_utf8 (utf8_file_name, flags);
+
+  g_free (utf8_file_name);
+
+  return retval;
+}
+
+#endif
+
 gboolean
 g_module_close (GModule               *module)
 {
@@ -499,7 +536,9 @@ g_module_close (GModule            *module)
       
       _g_module_close (module->handle, FALSE);
       g_free (module->file_name);
-      
+#ifdef G_OS_WIN32
+      g_free (module->cp_file_name);
+#endif
       g_free (module);
     }
   
@@ -576,6 +615,23 @@ g_module_name (GModule *module)
   return module->file_name;
 }
 
+#ifdef G_OS_WIN32
+
+#undef g_module_name
+
+G_CONST_RETURN gchar*
+g_module_name (GModule *module)
+{
+  g_return_val_if_fail (module != NULL, NULL);
+  
+  if (module == main_module)
+    return "main";
+  
+  return module->cp_file_name;
+}
+
+#endif
+
 gchar*
 g_module_build_path (const gchar *directory,
                     const gchar *module_name)