Subversion Repositories mdb

Rev

Rev 14 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 */
16
#include <stdio.h>
17
#include <string.h>
18
#include <strings.h>
19
#include <unistd.h>
20
#include <stdlib.h>
21
#include <ctype.h>
22
#include <math.h>
23
#include <syslog.h>
24
#include <errno.h>
25
#include <sys/stat.h>
26
#include <sys/types.h>
27
#include <fcntl.h>
28
#include <sqlite3.h>
29
#include "config.h"
30
#include "helplib.h"
31
#include "user.h"
32
#include "play.h"
33
#include "search.h"
34
 
35
int searchTerm(int s1, const char *type, const char *term, int start, int length)
36
{
37
	char query[1024], hv0[128], buffer[8192];
38
	char fname[256];
39
	sqlite3 *db;
40
	char *zErrMsg = 0;
41
	int rc, id, total, pos;
42
	sqlite3_stmt *res;
43
	char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
44
	char *title, *artist, *album, *t;
45
 
46
	strcpy(fname, configs.home);
47
	strcat(fname, MUSICDB);
48
 
49
	rc = sqlite3_open(fname, &db);
50
 
51
	if (rc)
52
	{
53
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
54
		strcpy(query, "ERROR:USER:Error opening database;");
55
		write (s1, query, strlen(query));
56
		return FALSE;
57
	}
58
 
59
	strcpy (query, "select id, title, interpret, album, genre from musicdb where ");
60
 
61
	if (!strcasecmp(type, "TITLE"))
62
		strcat (query, "title like \"%");
63
	else if (!strcasecmp(type, "ARTIST"))
64
		strcat (query, "interpret like \"%");
65
	else if (!strcasecmp(type, "ALBUM"))
66
		strcat (query, "album like \"%");
67
	else if (!strcasecmp(type, "GENRE"))
68
		strcat (query, "genre like \"%");
69
 
70
	strcat (query, term);
71
	strcat (query, "%\"");
72
 
73
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
74
	{
75
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
76
		strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
77
		write(s1, query, strlen(query));
78
		return FALSE;
79
	}
80
 
81
	total = sqlite3_column_count(res);
82
	sprintf(hv0, "TOTAL:%d;", total);
83
	write (s1, hv0, strlen(hv0));
84
	pos = 0;
85
 
86
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
87
	{
88
		if (pos < (start - 1))
89
		{
90
			pos++;
91
			continue;
92
		}
93
 
94
		if (pos >= ((start - 1) + length))
95
			break;
96
 
97
		memset(id3_title, 0, sizeof(id3_title));
98
		memset(id3_artist, 0, sizeof(id3_artist));
99
		memset(id3_album, 0, sizeof(id3_album));
100
		memset(id3_genre, 0, sizeof(id3_genre));
101
		id = sqlite3_column_int(res, 0);
102
		strncpy(id3_title, (const char *)sqlite3_column_text(res, 1), sizeof(id3_title));
103
		strncpy(id3_artist, (const char *)sqlite3_column_text(res, 2), sizeof(id3_artist));
104
		strncpy(id3_album, (const char *)sqlite3_column_text(res, 3), sizeof(id3_album));
105
		strncpy(id3_genre, (const char *)sqlite3_column_text(res, 4), sizeof(id3_genre));
106
		title = urlencode(id3_title);
107
		artist = urlencode(id3_artist);
108
		album = urlencode(id3_album);
109
 
110
		if (title != NULL)
111
		{
112
			strncpy(id3_title, title, sizeof(id3_title));
113
			free(title);
114
		}
115
 
116
		if (artist != NULL)
117
		{
118
			strncpy(id3_artist, artist, sizeof(id3_artist));
119
			free(artist);
120
		}
121
 
122
		if (album != NULL)
123
		{
124
			strncpy(id3_album, album, sizeof(id3_album));
125
			free(album);
126
		}
127
 
128
		sprintf (buffer, "SEARCH:%s:%d:%s:%s:%s:%s;", type, id, id3_title, id3_artist, id3_album, id3_genre);
129
		write (s1, buffer, strlen(buffer));
130
		pos++;
131
	}
132
 
133
	sqlite3_finalize(res);
134
	sqlite3_close(db);
135
	return TRUE;
136
}