Imported Upstream version 2.66.6
[platform/upstream/glib.git] / gio / giomodule-priv.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2017 Collabora Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Xavier Claessens <xavier.claessens@collabora.com>
19  */
20
21 #include "config.h"
22 #include "giomodule.h"
23 #include "giomodule-priv.h"
24
25 #include <string.h>
26
27 /**
28  * _g_io_module_extract_name:
29  * @filename: filename of a GIOModule
30  *
31  * Extract the plugin name from its filename. It removes optional "lib" or
32  * "libgio" prefix, and removes everything after the first dot. For example:
33  * "libgiognutls.so" -> "gnutls".
34  *
35  * Returns: (transfer full): the module's name
36  */
37 gchar *
38 _g_io_module_extract_name (const char *filename)
39 {
40   gchar *bname, *name;
41   const gchar *dot;
42   gsize prefix_len, len;
43   gsize i;
44
45   bname = g_path_get_basename (filename);
46   for (i = 0; bname[i]; ++i)
47     {
48       if (bname[i] == '-')
49         bname[i] = '_';
50     }
51
52   if (g_str_has_prefix (bname, "libgio"))
53     prefix_len = 6;
54   /* DLLs built with MSVC generally do not have the 'lib' prefix */
55   else if (g_str_has_prefix (bname, "lib") || g_str_has_prefix (bname, "gio"))
56     prefix_len = 3;
57   else
58     prefix_len = 0; /* use whole name (minus suffix) as plugin name */
59
60   dot = strchr (bname, '.');
61   if (dot != NULL)
62     len = dot - bname - prefix_len;
63   else
64     len = strlen (bname + prefix_len);
65
66   name = g_strndup (bname + prefix_len, len);
67   g_free (bname);
68
69   return name;
70 }