XMMS2
utils.c
Go to the documentation of this file.
1 /* XMMS2 - X Music Multiplexer System
2  * Copyright (C) 2003-2011 XMMS2 Team
3  *
4  * PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  */
16 
17 /** @file
18  * Miscellaneous internal utility functions specific to the daemon.
19  */
20 
21 #include <stdlib.h>
22 #include <glib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 
26 #include "xmmsc/xmmsc_util.h"
27 #include "xmms/xmms_util.h"
28 #include "xmmspriv/xmms_utils.h"
29 #include "xmmsc/xmmsc_strlist.h"
30 
31 /**
32  * Build path to file in xmms2 configuration directory.
33  * @param first The first file or directory name in the path.
34  * @param ... Additional file/directory names.
35  * @return Absolute path to a file or directory.
36  */
37 char *
38 xmms_build_path (const char *first, ...)
39 {
40  va_list ap;
41  gchar confdir[XMMS_PATH_MAX];
42  gchar *ret, **vargv, **argv;
43 
44  g_return_val_if_fail (first, NULL);
45 
47 
48  va_start (ap, first);
49  vargv = xmms_valist_to_strlist (first, ap);
50  va_end (ap);
51 
52  argv = xmms_strlist_prepend_copy (vargv, confdir);
53 
54  ret = g_build_pathv (G_DIR_SEPARATOR_S, argv);
55  xmms_strlist_destroy (vargv);
56  xmms_strlist_destroy (argv);
57  return ret;
58 }
59 
60 static gchar *
61 path_get_body (const gchar *path)
62 {
63  gchar *beg, *end;
64 
65  g_return_val_if_fail (path, NULL);
66 
67  beg = strstr (path, "://");
68 
69  if (!beg) {
70  return g_strndup (path, strcspn (path, "/"));
71  }
72 
73  beg += 3;
74  end = strchr (beg, '/');
75 
76  if (!end) {
77  return g_strdup (path);
78  }
79 
80  return g_strndup (path, end - path);
81 }
82 
83 /* g_path_get_dirname returns "file:" with "file:///foo.pls", while "file://"
84  is wanted. */
85 static gchar *
86 path_get_dirname (const gchar *path)
87 {
88  guint i, n = 0;
89 
90  g_return_val_if_fail (path, NULL);
91 
92  for (i = 0; path[i] ; i++) {
93  if (path[i] == '/') {
94  n = i;
95  }
96  }
97 
98  return g_strndup (path, n);
99 }
100 
101 gchar *
102 xmms_build_playlist_url (const gchar *plspath, const gchar *file)
103 {
104  gchar *url;
105  gchar *path;
106 
107  g_return_val_if_fail (plspath, NULL);
108  g_return_val_if_fail (file, NULL);
109 
110  if (strstr (file, "://") != NULL) {
111  return g_strdup (file);
112  }
113 
114  if (file[0] == '/') {
115  path = path_get_body (plspath);
116  url = g_strconcat (path, file, NULL);
117  } else {
118  path = path_get_dirname (plspath);
119  url = g_strconcat (path, "/", file, NULL);
120  }
121 
122  g_free (path);
123  return url;
124 }
125 
126 gint
127 xmms_natcmp_len (const gchar *str1, gint len1, const gchar *str2, gint len2)
128 {
129  gchar *tmp1, *tmp2, *tmp3, *tmp4;
130  gint res;
131 
132  /* FIXME: Implement a less allocation-happy variant */
133  tmp1 = g_utf8_casefold (str1, len1);
134  tmp2 = g_utf8_casefold (str2, len2);
135 
136  tmp3 = g_utf8_collate_key_for_filename (tmp1, -1);
137  tmp4 = g_utf8_collate_key_for_filename (tmp2, -1);
138 
139  res = strcmp (tmp3, tmp4);
140 
141  g_free (tmp1);
142  g_free (tmp2);
143  g_free (tmp3);
144  g_free (tmp4);
145 
146  return res;
147 }
148 
149 gint
150 xmms_natcmp (const gchar *str1, const gchar *str2)
151 {
152  return xmms_natcmp_len (str1, -1, str2, -1);
153 }
gchar * xmms_build_playlist_url(const gchar *plspath, const gchar *file)
Definition: utils.c:102
char ** xmms_strlist_prepend_copy(char **data, char *newstr)
Return a copy of a list with newstr prepended.
Definition: strlist.c:119
gint xmms_natcmp_len(const gchar *str1, gint len1, const gchar *str2, gint len2)
Definition: utils.c:127
char * xmms_build_path(const char *first,...)
Build path to file in xmms2 configuration directory.
Definition: utils.c:38
gint xmms_natcmp(const gchar *str1, const gchar *str2)
Definition: utils.c:150
void xmms_strlist_destroy(char **data)
Destroy a list of strings.
Definition: strlist.c:101
#define XMMS_PATH_MAX
Definition: xmmsc_util.h:43
char ** xmms_valist_to_strlist(const char *first, va_list ap)
Convert a list of variable arguments into a list of strings.
Definition: strlist.c:37
const char * xmms_userconfdir_get(char *buf, int len)
Get the absolute path to the user config dir.
Definition: utils_unix.c:80