File: utilities.c

package info (click to toggle)
alsaplayer 0.99.76-9%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 4,752 kB
  • ctags: 2,970
  • sloc: ansic: 16,775; sh: 10,709; cpp: 9,442; makefile: 694
file content (152 lines) | stat: -rw-r--r-- 3,031 bytes parent folder | download | duplicates (3)
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
/*  utilities.c
 *  Copyright (C) 1999 Richard Boulton <richard@tartarus.org>
 *  Copyright (C) 2003 Andy Lo A Foe <andy@alsaplayer.org>
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 *  $Id: utilities.c,v 1.8 2003/03/09 02:48:34 adnans Exp $
 *
*/ 

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.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;
	static int prefs_path_init = 0;
	char *homedir;

	if (prefs_path_init) {
		return prefs_path;
	}
	homedir = get_homedir();
	prefs_path = (char *)malloc(strlen(homedir) + 14);
	if (!prefs_path) 
		return NULL;
	sprintf(prefs_path, "%s/.alsaplayer", homedir);
	prefs_path_init = 1;
	return prefs_path;
}


char *parse_file_uri(const char *furi)
{
	char *res;
	char escape[4];
	int r,w, t, percent, e, val;
	if (!furi)
		return NULL;
	if ((strncmp(furi, "file:", 5) != 0)) {
		return NULL;
	}
	t=strlen(furi);
	res = (char *)malloc(t+1);
	r=5;
	w=0;
	e=0;
	percent = 0;
	while (r < t) {
		switch(furi[r]) {
			case '%':
				/*
				alsaplayer_error("found percent: (%s) (%s)",
						furi, res);
				*/				
				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;
	}
	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;
}