XMMS2

src/xmms/utils.c

Go to the documentation of this file.
00001 /*  XMMS2 - X Music Multiplexer System
00002  *  Copyright (C) 2003-2009 XMMS2 Team
00003  *
00004  *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  */
00016 
00017 /** @file
00018  * Miscellaneous internal utility functions specific to the daemon.
00019  */
00020 
00021 #include <stdlib.h>
00022 #include <glib.h>
00023 #include <stdarg.h>
00024 #include <string.h>
00025 
00026 #include "xmmsc/xmmsc_util.h"
00027 #include "xmms/xmms_util.h"
00028 #include "xmmspriv/xmms_utils.h"
00029 #include "xmmsc/xmmsc_strlist.h"
00030 
00031 /**
00032  * Build path to file in xmms2 configuration directory.
00033  * @param first The first file or directory name in the path.
00034  * @param ... Additional file/directory names.
00035  * @return Absolute path to a file or directory.
00036  */
00037 char *
00038 xmms_build_path (const char *first, ...)
00039 {
00040     va_list ap;
00041     gchar confdir[PATH_MAX];
00042     gchar *ret, **vargv, **argv;
00043 
00044     g_return_val_if_fail (first, NULL);
00045 
00046     xmms_userconfdir_get (confdir, PATH_MAX);
00047 
00048     va_start (ap, first);
00049     vargv = xmms_valist_to_strlist (first, ap);
00050     va_end (ap);
00051 
00052     argv = xmms_strlist_prepend_copy (vargv, confdir);
00053 
00054     ret = g_build_pathv (G_DIR_SEPARATOR_S, argv);
00055     xmms_strlist_destroy (vargv);
00056     xmms_strlist_destroy (argv);
00057     return ret;
00058 }
00059 
00060 static gchar *
00061 path_get_body (const gchar *path)
00062 {
00063     gchar *beg, *end;
00064 
00065     g_return_val_if_fail (path, NULL);
00066 
00067     beg = strstr (path, "://");
00068 
00069     if (!beg) {
00070         return g_strndup (path, strcspn (path, "/"));
00071     }
00072 
00073     beg += 3;
00074     end = strchr (beg, '/');
00075 
00076     if (!end) {
00077         return g_strdup (path);
00078     }
00079 
00080     return g_strndup (path, end - path);
00081 }
00082 
00083 /* g_path_get_dirname returns "file:" with "file:///foo.pls", while "file://"
00084    is wanted. */
00085 static gchar *
00086 path_get_dirname (const gchar *path)
00087 {
00088     guint i, n = 0;
00089 
00090     g_return_val_if_fail (path, NULL);
00091 
00092     for (i = 0; path[i] ; i++) {
00093         if (path[i] == '/') {
00094             n = i;
00095         }
00096     }
00097 
00098     return g_strndup (path, n);
00099 }
00100 
00101 gchar *
00102 xmms_build_playlist_url (const gchar *plspath, const gchar *file)
00103 {
00104     gchar *url;
00105     gchar *path;
00106 
00107     g_return_val_if_fail (plspath, NULL);
00108     g_return_val_if_fail (file, NULL);
00109 
00110     if (strstr (file, "://") != NULL) {
00111         return g_strdup (file);
00112     }
00113 
00114     if (file[0] == '/') {
00115         path = path_get_body (plspath);
00116         url = g_strconcat (path, file, NULL);
00117     } else {
00118         path = path_get_dirname (plspath);
00119         url = g_strconcat (path, "/", file, NULL);
00120     }
00121 
00122     g_free (path);
00123     return url;
00124 }
00125 
00126 gint
00127 xmms_natcmp_len (const gchar *str1, gint len1, const gchar *str2, gint len2)
00128 {
00129     gchar *tmp1, *tmp2, *tmp3, *tmp4;
00130     gint res;
00131 
00132     /* FIXME: Implement a less allocation-happy variant */
00133     tmp1 = g_utf8_casefold (str1, len1);
00134     tmp2 = g_utf8_casefold (str2, len2);
00135 
00136     tmp3 = g_utf8_collate_key_for_filename (tmp1, -1);
00137     tmp4 = g_utf8_collate_key_for_filename (tmp2, -1);
00138 
00139     res = strcmp (tmp3, tmp4);
00140 
00141     g_free (tmp1);
00142     g_free (tmp2);
00143     g_free (tmp3);
00144     g_free (tmp4);
00145 
00146     return res;
00147 }
00148 
00149 gint
00150 xmms_natcmp (const gchar *str1, const gchar *str2)
00151 {
00152     return xmms_natcmp_len (str1, -1, str2, -1);
00153 }