File: utilities.c

package info (click to toggle)
alsaplayer 0.99.82-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,892 kB
  • sloc: ansic: 40,741; cpp: 14,400; makefile: 983; sh: 796; lex: 751; asm: 45; python: 29; sed: 16
file content (192 lines) | stat: -rw-r--r-- 3,450 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*  utilities.c
 *  Copyright (C) 1999 Richard Boulton <richard@tartarus.org>
 *  Copyright (C) 2003 Andy Lo A Foe <andy@loafoe.com>
 *
 *  This file is part of AlsaPlayer.
 *
 *  AlsaPlayer is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AlsaPlayer 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
*/

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#include "utilities.h"
#include "alsaplayer_error.h"

/* Threads and usleep does not work, select is very portable */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

void dosleep(unsigned int msec)
{
	struct timeval time;
	time.tv_sec=0;
	time.tv_usec=msec;

	select(0, NULL, NULL, NULL, &time);
}

void parse_file_uri_free(char *p)
{
	if (p)
		free(p);
}

char *get_homedir(void)
{
	char *homedir = NULL;

	if ((homedir = getenv("HOME")) == NULL) {
		homedir = strdup("/tmp");
	}
	return homedir;
}

char *get_prefsdir(void)
{
	static char prefs_path [PATH_MAX];
	static int prefs_path_init = 0;
	char *homedir;

	if (prefs_path_init) {
		return prefs_path;
	}
	homedir = get_homedir();
	snprintf(prefs_path, sizeof (prefs_path), "%s/.alsaplayer", homedir);
	prefs_path_init = 1;
	return prefs_path;
}

#if 0
static voidencode_percent_free(char *p)
{
	if (p)
		free(p);
}

char *encode_percent(const char *furi)
{
	char *res;
	int c, i, o, t;

	if (!furi)
		return NULL;
	/* count the percent signs */
	c = 0;
	i = 0;
	t = strlen(furi);
	while (i < t) {
		if (furi[i++] == '%') {
			c++;
		}
	}
	res = malloc(t + c + 1);
	if (res) {
		i = 0;
		o = 0;
		while (i  < t) {
			if (furi[i] == '%') {
				res[o++] = '%';
			}
			res[o++] = furi[i++];
		}
	}
	return res;
}
#endif

char *parse_file_uri(const char *furi)
{
	char *res;
	char escape[4];
	int r,w, t, percent, e, val;
	alsaplayer_error("parsing: %s", furi);

	if (!furi)
		return NULL;
	if ((strncmp(furi, "file:", 5) != 0) || !is_uri(furi)) {
		return NULL;
	}
	t=strlen(furi);
	res = malloc(t+1);
	r=5;
	w=0;
	e=0;
	percent = 0;
	while (r < t) {
		switch(furi[r]) {
			case '%':
				if (percent) {
					res[w++] = '%';
					percent = 0;
				} else {
					percent = 1;
					e = 0;
				}
				break;
			default:
				if (percent) {
					escape[e++] = furi[r];
					escape[e]=0;
					if (e == 2) {
						if ((sscanf(escape, "%x", &val) == 1)) {
							res[w++] = val;
						} else {
							alsaplayer_error("unandled percent escape (%s)", escape);
						}
						percent = 0;
					}
				} else {
					res[w++] = furi[r];
				}
				break;
		}
		r++;
		res[w] = 0;
	}
	alsaplayer_error("parsed to: %s", res);
	return res;

}


int is_playlist(const char *path)
{
	char *ext;

	if (!path)
		return 0;
	ext = strrchr(path, '.');
	if (!ext)
		return 0;
	ext++;
	if (strncasecmp(ext, "pls", 3) == 0 ||
		strncasecmp(ext, "m3u", 3) == 0) {
		return 1;
	}
	return 0;
}

int is_uri(const char *path)
{
	if (strstr(path, "://"))
		return 1;
	return 0;
}