File: utils.c

package info (click to toggle)
xmms2 0.7DrNo%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,324 kB
  • ctags: 10,232
  • sloc: ansic: 65,934; python: 11,737; cpp: 5,382; perl: 202; ruby: 138; makefile: 47; asm: 7; sh: 4
file content (153 lines) | stat: -rw-r--r-- 3,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*  XMMS2 - X Music Multiplexer System
 *  Copyright (C) 2003-2009 XMMS2 Team
 *
 *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 */

/** @file
 * Miscellaneous internal utility functions specific to the daemon.
 */

#include <stdlib.h>
#include <glib.h>
#include <stdarg.h>
#include <string.h>

#include "xmmsc/xmmsc_util.h"
#include "xmms/xmms_util.h"
#include "xmmspriv/xmms_utils.h"
#include "xmmsc/xmmsc_strlist.h"

/**
 * Build path to file in xmms2 configuration directory.
 * @param first The first file or directory name in the path.
 * @param ... Additional file/directory names.
 * @return Absolute path to a file or directory.
 */
char *
xmms_build_path (const char *first, ...)
{
	va_list ap;
	gchar confdir[XMMS_PATH_MAX];
	gchar *ret, **vargv, **argv;

	g_return_val_if_fail (first, NULL);

	xmms_userconfdir_get (confdir, XMMS_PATH_MAX);

	va_start (ap, first);
	vargv = xmms_valist_to_strlist (first, ap);
	va_end (ap);

	argv = xmms_strlist_prepend_copy (vargv, confdir);

	ret = g_build_pathv (G_DIR_SEPARATOR_S, argv);
	xmms_strlist_destroy (vargv);
	xmms_strlist_destroy (argv);
	return ret;
}

static gchar *
path_get_body (const gchar *path)
{
	gchar *beg, *end;

	g_return_val_if_fail (path, NULL);

	beg = strstr (path, "://");

	if (!beg) {
		return g_strndup (path, strcspn (path, "/"));
	}

	beg += 3;
	end = strchr (beg, '/');

	if (!end) {
		return g_strdup (path);
	}

	return g_strndup (path, end - path);
}

/* g_path_get_dirname returns "file:" with "file:///foo.pls", while "file://"
   is wanted. */
static gchar *
path_get_dirname (const gchar *path)
{
	guint i, n = 0;

	g_return_val_if_fail (path, NULL);

	for (i = 0; path[i] ; i++) {
		if (path[i] == '/') {
			n = i;
		}
	}

	return g_strndup (path, n);
}

gchar *
xmms_build_playlist_url (const gchar *plspath, const gchar *file)
{
	gchar *url;
	gchar *path;

	g_return_val_if_fail (plspath, NULL);
	g_return_val_if_fail (file, NULL);

	if (strstr (file, "://") != NULL) {
		return g_strdup (file);
	}

	if (file[0] == '/') {
		path = path_get_body (plspath);
		url = g_strconcat (path, file, NULL);
	} else {
		path = path_get_dirname (plspath);
		url = g_strconcat (path, "/", file, NULL);
	}

	g_free (path);
	return url;
}

gint
xmms_natcmp_len (const gchar *str1, gint len1, const gchar *str2, gint len2)
{
	gchar *tmp1, *tmp2, *tmp3, *tmp4;
	gint res;

	/* FIXME: Implement a less allocation-happy variant */
	tmp1 = g_utf8_casefold (str1, len1);
	tmp2 = g_utf8_casefold (str2, len2);

	tmp3 = g_utf8_collate_key_for_filename (tmp1, -1);
	tmp4 = g_utf8_collate_key_for_filename (tmp2, -1);

	res = strcmp (tmp3, tmp4);

	g_free (tmp1);
	g_free (tmp2);
	g_free (tmp3);
	g_free (tmp4);

	return res;
}

gint
xmms_natcmp (const gchar *str1, const gchar *str2)
{
	return xmms_natcmp_len (str1, -1, str2, -1);
}