File: mp3db.c

package info (click to toggle)
teknap 1.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,428 kB
  • ctags: 6,297
  • sloc: ansic: 55,591; makefile: 545; sh: 112
file content (196 lines) | stat: -rw-r--r-- 4,026 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
193
194
195
196
/* $Id: mp3db.c,v 1.1.1.1 2000/10/16 01:09:49 edwards Exp $ */
 
#include "teknap.h"
#include "struct.h"
#include "ircaux.h"
#include "output.h"
#include "mp3db.h"

typedef struct _mp3db_search_ {
	struct _mp3db_search_ *next;
	char *pattern;
	int bitrate;
	int type;
	FILE *fp;
} Mp3Search;

static char	FORMAT_MP3[512] = " %N %g4.1L %b %t %f";
static char	FORMAT_DIR[512] = " %f - %N";
#define OTHER_FORMAT_MP3 " %S %B %T %L %F"

Mp3Search *search_param = NULL;

#define SEARCH_FILENAME	0x0001
#define SEARCH_BITRATE	0x0002
#define SEARCH_LABEL	0x0004

#define PRINT_LABEL	0x0001
#define PRINT_ONE	0x0002
#define PRINT_ALL	0x0004

FILE *open_database(char *filename)
{
FILE *fp = NULL;
char *p;
	if ((p = expand_twiddle(filename)))
		fp = uzfopen(&p, ".", 0, NULL);
	new_free(&p);
	return fp;
}

int mp3printf(char *, int, char *, FileStruct *);

void search_database(Mp3Search *find)
{
FILE *fp;
int print_it;
FileStruct fs = { 0 };
Tmp3_Entry tmp;
char buffer[BIG_BUFFER_SIZE+1];
int count = 0;
int number = 0;
Mp3Search *new, *last;

	fp = open_database("/etc/mp3.db");
	if (!fp)
	{
		say("No Such file as /etc/mp3.db");
		return;
	}
	for (new = find; new; new = last)
	{
		last = new->next;
		fread(&tmp, sizeof(tmp), 1, fp);
		while(!feof(fp))
		{
			count++;
			memset(&fs, 0, sizeof(FileStruct));
			fs.filename = tmp.pathname;
			fs.nick = tmp.label;
			fs.bitrate = tmp.bitrate;
			fs.seconds = tmp.time;
			fs.stereo = 1;
			fs.filesize = tmp.size;
			print_it = 0;
			switch(new->type)
			{
				case SEARCH_FILENAME:
					if (wild_match(new->pattern, fs.filename))
						if (new->bitrate == -1 || new->bitrate == fs.bitrate)
							print_it++;
					break;
				case SEARCH_BITRATE:
					if (new->bitrate == fs.bitrate)
						print_it++;
					break;
				case SEARCH_LABEL:
					if (wild_match(new->pattern, tmp.label))
						print_it++;
					break;
				default:
					break;
			}
			if (print_it)
			{
				if (tmp.mode == M_MP3)
					mp3printf(buffer, BIG_BUFFER_SIZE, FORMAT_MP3, &fs); 
				else
					mp3printf(buffer, BIG_BUFFER_SIZE, FORMAT_DIR, &fs);
				if (new->fp)
					fprintf(new->fp, "%s\n", buffer);
				else
					put_it(buffer);
				number++;
			}
			fread(&tmp, sizeof(tmp), 1, fp);
		}
		say("Searched for %s/%d and returned %d entries", new->pattern, count, number);
		count = 0; number = 0;
		new_free(&new->pattern);
		if (new->fp)
		{
			if (!last || (last && last->fp != new->fp))
				fclose(new->fp);
		}
		new_free(&new);
	}
	fclose(fp);
}

#if 0
void search_by_filename(char *pattern, int type)
{
	search_database(pattern, SEARCH_FILENAME, type);
}

void search_by_label(char *pattern, int type)
{
	search_database(pattern, SEARCH_LABEL, type);
}

void search_by_bitrate(char *pattern, int type)
{
	if (my_atol(pattern))
		search_database(pattern, SEARCH_BITRATE, type);
}
#endif

BUILT_IN_COMMAND(dbsearch)
{
char *arg;
Mp3Search *new = NULL;
int bitrate = -1;

	while ((arg = new_next_arg(args, &args)))
	{
		if (!arg || !*arg)
			break;
		new = (Mp3Search *)new_malloc(sizeof(Mp3Search));
		new->type = SEARCH_FILENAME;
		new->bitrate = bitrate;

		if (*arg == '-')
			arg++;
		else
		{
			new->pattern = m_sprintf("*%s*", arg);
			continue;
		}
		switch(*arg)
		{
			case 'F':
				if ((arg = next_arg(args, &args)))
					if (search_param)
						search_param->fp = fopen(arg, "w");
				new_free(&new);
				break;
			case 'S':
				new->type = SEARCH_FILENAME;
				new->pattern = m_sprintf("*%s*", new_next_arg(args, &args));
				break;
			case 'B':
				bitrate = my_atol(next_arg(args, &args));
				new_free(&new);
				break;
			case 'l':
				new->type = SEARCH_LABEL;
				new->pattern = m_sprintf("*%s*", new_next_arg(args, &args));
				break;
			case 'b':
				new->type = SEARCH_BITRATE;
				new->bitrate = my_atol(next_arg(args, &args));
				break;
			default:
				say("Unknown option. -S, -l, -b");
				new_free(&new);
				break;
		}
		if (!new)
			continue;
		new->next = search_param;
		search_param = new;
	}
	if (search_param)
		search_database(search_param);
	search_param = NULL;
}