2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file plm_dbtool.c Simple playlist manager for paraslash */
21 #include <sys/time.h> /* gettimeofday */
22 #include "server.cmdline.h"
29 #define MAX_PLAYLIST_LEN 10000
30 #define MAX_PLAYLIST_BYTES (1024 * 1024)
32 static unsigned playlist_len, playlist_size, current_playlist_entry;
33 static char **playlist;
35 static int com_ppl(int, int, char **);
36 static int com_lpl(int, int, char **);
37 extern struct misc_meta_data *mmd;
39 /* array of commands that are supported by this database tool */
40 static struct server_command cmds[] = {
45 .description = "print playlist",
48 "Print out the current playlist"
53 .description = "load playlist",
56 "Read a new playlist from stdin"
63 static void playlist_add(char *path)
65 if (playlist_len >= playlist_size) {
66 if (playlist_size >= MAX_PLAYLIST_LEN)
69 playlist = para_realloc(playlist, playlist_size * sizeof(char *));
71 PARA_DEBUG_LOG("adding #%d: %s\n", playlist_len, path);
72 playlist[playlist_len] = para_strdup(path);
76 static int com_lpl(int fd, __unused int argc, __unused char *argv[])
78 unsigned i, loaded = 0;
79 char buf[_POSIX_PATH_MAX];
82 PARA_DEBUG_LOG("freeing playlist (%d entries)\n", playlist_len);
83 for (i = 0; i < playlist_len; i++)
85 current_playlist_entry = 0;
87 ret = send_buffer(fd, AWAITING_DATA_MSG);
91 ret = recv_bin_buffer(fd, buf + loaded, sizeof(buf) - loaded);
95 PARA_DEBUG_LOG("loaded playlist (%d entries)\n", playlist_len);
99 loaded = for_each_line(buf, loaded, &playlist_add, 0);
100 if (loaded >= sizeof(buf))
104 return -E_LOAD_PLAYLIST;
107 static int com_ppl(int fd, __unused int argc, __unused char *argv[])
111 PARA_DEBUG_LOG("sending playlist (%d entries)\n", playlist_len);
112 for (i = 0; i < playlist_len; i++) {
113 int ret = send_buffer(fd, playlist[i]);
120 static char **plm_get_audio_file_list(unsigned int num)
126 num = MIN(num, playlist_len);
129 file_list = para_malloc((num + 1) * sizeof(char *));
130 for (i = 0; i < num; i++) {
131 unsigned j = (current_playlist_entry + i) % playlist_len;
132 file_list[i] = para_strdup(playlist[j]);
138 static void plm_shutdown(void)
140 /* free the playlist */
144 * the init function for the plm database tool
146 * Init all function pointers of \a db
148 * \sa struct dbtool, misc_meta_data::dbinfo, mysql.c random_dbtool.c
150 int plm_dbtool_init(struct dbtool *db)
152 playlist = para_calloc(100 * sizeof(char *)); /* guess 100 is enough */
154 sprintf(mmd->dbinfo, "plm initialized");
156 db->get_audio_file_list = plm_get_audio_file_list;
157 db->shutdown = plm_shutdown;