Subversion Repositories mdb

Rev

Rev 14 | Details | Compare with Previous | 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
{
18 andreas 37
	char query[1024], field[128], hv0[128], buffer[8192];
10 andreas 38
	char fname[256];
39
	sqlite3 *db;
18 andreas 40
	int rc, id, total, pos, line;
10 andreas 41
	sqlite3_stmt *res;
42
	char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
14 andreas 43
	char *title, *artist, *album;
10 andreas 44
 
45
	strcpy(fname, configs.home);
46
	strcat(fname, MUSICDB);
47
 
48
	rc = sqlite3_open(fname, &db);
49
 
50
	if (rc)
51
	{
52
		syslog(LOG_WARNING, "Error opening database %s: %s", fname, sqlite3_errmsg(db));
53
		strcpy(query, "ERROR:USER:Error opening database;");
54
		write (s1, query, strlen(query));
55
		return FALSE;
56
	}
57
 
18 andreas 58
	memset(field, 0, sizeof(field));
10 andreas 59
 
60
	if (!strcasecmp(type, "TITLE"))
18 andreas 61
		sprintf(field, "title like \"%%%s%%\"", term);
10 andreas 62
	else if (!strcasecmp(type, "ARTIST"))
18 andreas 63
		sprintf(field, "interpret like \"%%%s%%\"", term);
10 andreas 64
	else if (!strcasecmp(type, "ALBUM"))
18 andreas 65
		sprintf(field, "album like \"%%%s%%\"", term);
10 andreas 66
	else if (!strcasecmp(type, "GENRE"))
18 andreas 67
		sprintf(field, "genre like \"%%%s%%\"", term);
10 andreas 68
 
18 andreas 69
	strcpy (query, "select count(*) from musicdb where ");
70
	strcat (query, field);
10 andreas 71
 
72
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
73
	{
74
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
75
		strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
76
		write(s1, query, strlen(query));
77
		return FALSE;
78
	}
18 andreas 79
 
80
	if ((rc = sqlite3_step(res)) == SQLITE_ROW)
81
	{
82
		total = sqlite3_column_int(res, 0);
83
		sprintf(hv0, "TOTAL:%d;", total);
84
		write (s1, hv0, strlen(hv0));
85
	}
86
	else
87
		total = 0;
10 andreas 88
 
18 andreas 89
	strcpy (query, "select id, title, interpret, album, genre from musicdb where ");
90
	strcat (query, field);
91
 
92
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
93
	{
94
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
95
		strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
96
		write(s1, query, strlen(query));
97
		return FALSE;
98
	}
99
 
10 andreas 100
	pos = 0;
18 andreas 101
	line = 1;
10 andreas 102
 
103
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
104
	{
105
		if (pos < (start - 1))
106
		{
107
			pos++;
108
			continue;
109
		}
110
 
111
		if (pos >= ((start - 1) + length))
112
			break;
113
 
114
		memset(id3_title, 0, sizeof(id3_title));
115
		memset(id3_artist, 0, sizeof(id3_artist));
116
		memset(id3_album, 0, sizeof(id3_album));
117
		memset(id3_genre, 0, sizeof(id3_genre));
118
		id = sqlite3_column_int(res, 0);
119
		strncpy(id3_title, (const char *)sqlite3_column_text(res, 1), sizeof(id3_title));
120
		strncpy(id3_artist, (const char *)sqlite3_column_text(res, 2), sizeof(id3_artist));
121
		strncpy(id3_album, (const char *)sqlite3_column_text(res, 3), sizeof(id3_album));
122
		strncpy(id3_genre, (const char *)sqlite3_column_text(res, 4), sizeof(id3_genre));
123
		title = urlencode(id3_title);
124
		artist = urlencode(id3_artist);
125
		album = urlencode(id3_album);
126
 
127
		if (title != NULL)
128
		{
129
			strncpy(id3_title, title, sizeof(id3_title));
130
			free(title);
131
		}
132
 
133
		if (artist != NULL)
134
		{
135
			strncpy(id3_artist, artist, sizeof(id3_artist));
136
			free(artist);
137
		}
138
 
139
		if (album != NULL)
140
		{
141
			strncpy(id3_album, album, sizeof(id3_album));
142
			free(album);
143
		}
144
 
18 andreas 145
		sprintf (buffer, "SEARCH:%s:%d:%d:%s:%s:%s:%s;", type, id, line, id3_title, id3_artist, id3_album, id3_genre);
10 andreas 146
		write (s1, buffer, strlen(buffer));
147
		pos++;
18 andreas 148
		line++;
10 andreas 149
	}
150
 
151
	sqlite3_finalize(res);
152
	sqlite3_close(db);
153
	return TRUE;
154
}