2013-04-27 22:15:59 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-03-22 02:09:18 +00:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
2013-04-27 22:15:59 +00:00
|
|
|
*
|
|
|
|
* RetroArch 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 Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch 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 RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-06-29 22:38:10 +00:00
|
|
|
#include <boolean.h>
|
|
|
|
#include <compat/posix_string.h>
|
2016-01-20 03:07:24 +00:00
|
|
|
#include <string/stdstring.h>
|
2016-06-02 22:12:58 +00:00
|
|
|
#include <streams/file_stream.h>
|
2016-07-23 00:53:48 +00:00
|
|
|
#include <file/file_path.h>
|
2015-06-29 22:38:10 +00:00
|
|
|
|
|
|
|
#include "playlist.h"
|
2015-11-23 11:03:38 +00:00
|
|
|
#include "verbosity.h"
|
2015-06-29 22:38:10 +00:00
|
|
|
|
2016-01-23 18:39:18 +00:00
|
|
|
#ifndef PLAYLIST_ENTRIES
|
|
|
|
#define PLAYLIST_ENTRIES 6
|
|
|
|
#endif
|
|
|
|
|
2016-08-28 16:47:34 +00:00
|
|
|
typedef int (playlist_sort_fun_t)(
|
|
|
|
const struct playlist_entry *a,
|
|
|
|
const struct playlist_entry *b);
|
|
|
|
|
2015-01-16 19:19:21 +00:00
|
|
|
/**
|
2016-05-16 07:07:44 +00:00
|
|
|
* playlist_get_index:
|
2016-05-06 18:20:16 +00:00
|
|
|
* @playlist : Playlist handle.
|
2015-01-16 19:19:21 +00:00
|
|
|
* @idx : Index of playlist entry.
|
|
|
|
* @path : Path of playlist entry.
|
|
|
|
* @core_path : Core path of playlist entry.
|
|
|
|
* @core_name : Core name of playlist entry.
|
|
|
|
*
|
|
|
|
* Gets values of playlist index:
|
|
|
|
**/
|
2016-05-16 07:07:44 +00:00
|
|
|
void playlist_get_index(playlist_t *playlist,
|
2014-10-20 17:22:33 +00:00
|
|
|
size_t idx,
|
2015-05-25 22:12:49 +00:00
|
|
|
const char **path, const char **label,
|
|
|
|
const char **core_path, const char **core_name,
|
2015-06-08 21:38:03 +00:00
|
|
|
const char **crc32,
|
|
|
|
const char **db_name)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2014-08-15 15:32:38 +00:00
|
|
|
if (!playlist)
|
2014-06-01 00:16:48 +00:00
|
|
|
return;
|
|
|
|
|
2014-09-02 22:14:02 +00:00
|
|
|
if (path)
|
2014-10-20 17:22:33 +00:00
|
|
|
*path = playlist->entries[idx].path;
|
2015-05-25 22:12:49 +00:00
|
|
|
if (label)
|
|
|
|
*label = playlist->entries[idx].label;
|
2014-08-31 19:56:36 +00:00
|
|
|
if (core_path)
|
2014-10-20 17:22:33 +00:00
|
|
|
*core_path = playlist->entries[idx].core_path;
|
2014-09-02 22:14:02 +00:00
|
|
|
if (core_name)
|
2014-10-20 17:22:33 +00:00
|
|
|
*core_name = playlist->entries[idx].core_name;
|
2015-06-08 21:38:03 +00:00
|
|
|
if (db_name)
|
2016-03-21 18:53:48 +00:00
|
|
|
*db_name = playlist->entries[idx].db_name;
|
2015-05-25 22:12:49 +00:00
|
|
|
if (crc32)
|
|
|
|
*crc32 = playlist->entries[idx].crc32;
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 23:17:02 +00:00
|
|
|
/**
|
|
|
|
* playlist_delete_index:
|
|
|
|
* @playlist : Playlist handle.
|
|
|
|
* @idx : Index of playlist entry.
|
|
|
|
*
|
|
|
|
* Delete the entry at the index:
|
|
|
|
**/
|
|
|
|
void playlist_delete_index(playlist_t *playlist,
|
|
|
|
size_t idx)
|
|
|
|
{
|
|
|
|
if (!playlist)
|
|
|
|
return;
|
|
|
|
|
2016-08-28 23:38:46 +00:00
|
|
|
memmove(playlist->entries + idx, playlist->entries + idx + 1,
|
|
|
|
(playlist->size - idx) * sizeof(struct playlist_entry));
|
2016-08-28 23:17:02 +00:00
|
|
|
|
2017-04-23 10:54:13 +00:00
|
|
|
playlist->size = playlist->size - 1;
|
|
|
|
playlist->modified = true;
|
2016-08-28 23:17:02 +00:00
|
|
|
|
2016-08-28 23:38:46 +00:00
|
|
|
playlist_write_file(playlist);
|
2016-08-28 23:17:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 07:07:44 +00:00
|
|
|
void playlist_get_index_by_path(playlist_t *playlist,
|
2015-02-03 03:12:32 +00:00
|
|
|
const char *search_path,
|
2015-05-25 22:12:49 +00:00
|
|
|
char **path, char **label,
|
|
|
|
char **core_path, char **core_name,
|
2015-06-08 21:38:03 +00:00
|
|
|
char **crc32,
|
|
|
|
char **db_name)
|
2015-02-03 03:12:32 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
if (!playlist)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < playlist->size; i++)
|
|
|
|
{
|
2016-01-20 03:07:24 +00:00
|
|
|
if (!string_is_equal(playlist->entries[i].path, search_path))
|
2015-02-03 03:12:32 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (path)
|
|
|
|
*path = playlist->entries[i].path;
|
2015-05-25 22:12:49 +00:00
|
|
|
if (label)
|
|
|
|
*label = playlist->entries[i].label;
|
2015-02-03 03:12:32 +00:00
|
|
|
if (core_path)
|
|
|
|
*core_path = playlist->entries[i].core_path;
|
|
|
|
if (core_name)
|
|
|
|
*core_name = playlist->entries[i].core_name;
|
2015-06-08 21:38:03 +00:00
|
|
|
if (db_name)
|
|
|
|
*db_name = playlist->entries[i].db_name;
|
2015-05-25 22:12:49 +00:00
|
|
|
if (crc32)
|
|
|
|
*crc32 = playlist->entries[i].crc32;
|
2015-02-03 03:12:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 07:07:44 +00:00
|
|
|
bool playlist_entry_exists(playlist_t *playlist,
|
2016-01-26 20:21:01 +00:00
|
|
|
const char *path,
|
|
|
|
const char *crc32)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
if (!playlist)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = 0; i < playlist->size; i++)
|
2016-01-27 03:10:24 +00:00
|
|
|
if (string_is_equal(playlist->entries[i].path, path))
|
2016-01-26 20:21:01 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-17 02:48:59 +00:00
|
|
|
/**
|
2016-05-16 07:07:44 +00:00
|
|
|
* playlist_free_entry:
|
2016-05-06 18:20:16 +00:00
|
|
|
* @entry : Playlist entry handle.
|
2015-01-17 02:48:59 +00:00
|
|
|
*
|
|
|
|
* Frees playlist entry.
|
|
|
|
**/
|
2016-08-28 16:47:34 +00:00
|
|
|
static void playlist_free_entry(struct playlist_entry *entry)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2014-09-15 01:24:03 +00:00
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->path != NULL)
|
2014-07-25 21:33:49 +00:00
|
|
|
free(entry->path);
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->label != NULL)
|
2015-05-25 22:12:49 +00:00
|
|
|
free(entry->label);
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->core_path != NULL)
|
2014-07-25 21:33:49 +00:00
|
|
|
free(entry->core_path);
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->core_name != NULL)
|
2014-07-25 21:33:49 +00:00
|
|
|
free(entry->core_name);
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->db_name != NULL)
|
2015-06-08 21:38:03 +00:00
|
|
|
free(entry->db_name);
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->crc32 != NULL)
|
2015-05-25 22:12:49 +00:00
|
|
|
free(entry->crc32);
|
2017-05-29 18:36:18 +00:00
|
|
|
|
|
|
|
entry->path = NULL;
|
|
|
|
entry->label = NULL;
|
|
|
|
entry->core_path = NULL;
|
|
|
|
entry->core_name = NULL;
|
|
|
|
entry->db_name = NULL;
|
2016-10-21 17:32:44 +00:00
|
|
|
entry->crc32 = NULL;
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 07:07:44 +00:00
|
|
|
void playlist_update(playlist_t *playlist, size_t idx,
|
2015-05-26 04:28:16 +00:00
|
|
|
const char *path, const char *label,
|
|
|
|
const char *core_path, const char *core_name,
|
2015-06-08 21:38:03 +00:00
|
|
|
const char *crc32,
|
|
|
|
const char *db_name)
|
2015-05-26 04:28:16 +00:00
|
|
|
{
|
2016-08-28 16:47:34 +00:00
|
|
|
struct playlist_entry *entry = NULL;
|
2016-09-29 08:16:48 +00:00
|
|
|
|
|
|
|
if (!playlist || idx > playlist->size)
|
2015-05-26 04:28:16 +00:00
|
|
|
return;
|
|
|
|
|
2016-05-24 02:26:24 +00:00
|
|
|
entry = &playlist->entries[idx];
|
2016-06-17 03:37:46 +00:00
|
|
|
|
2016-08-28 16:47:34 +00:00
|
|
|
if (path && (path != entry->path))
|
|
|
|
{
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->path != NULL)
|
|
|
|
free(entry->path);
|
|
|
|
entry->path = NULL;
|
2017-04-23 10:25:54 +00:00
|
|
|
entry->path = strdup(path);
|
|
|
|
playlist->modified = true;
|
2016-06-17 03:37:46 +00:00
|
|
|
}
|
2016-08-28 16:47:34 +00:00
|
|
|
|
|
|
|
if (label && (label != entry->label))
|
|
|
|
{
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->label != NULL)
|
|
|
|
free(entry->label);
|
|
|
|
entry->label = NULL;
|
2017-04-23 10:25:54 +00:00
|
|
|
entry->label = strdup(label);
|
|
|
|
playlist->modified = true;
|
2016-06-17 03:37:46 +00:00
|
|
|
}
|
2016-08-28 16:47:34 +00:00
|
|
|
|
|
|
|
if (core_path && (core_path != entry->core_path))
|
|
|
|
{
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->core_path != NULL)
|
|
|
|
free(entry->core_path);
|
|
|
|
entry->core_path = NULL;
|
2017-04-23 10:25:54 +00:00
|
|
|
entry->core_path = strdup(core_path);
|
|
|
|
playlist->modified = true;
|
2016-06-17 03:37:46 +00:00
|
|
|
}
|
2016-08-28 16:47:34 +00:00
|
|
|
|
|
|
|
if (core_name && (core_name != entry->core_name))
|
|
|
|
{
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->core_name != NULL)
|
|
|
|
free(entry->core_name);
|
|
|
|
entry->core_name = NULL;
|
2017-04-23 10:25:54 +00:00
|
|
|
entry->core_name = strdup(core_name);
|
|
|
|
playlist->modified = true;
|
2016-06-17 03:37:46 +00:00
|
|
|
}
|
2016-08-28 16:47:34 +00:00
|
|
|
|
|
|
|
if (db_name && (db_name != entry->db_name))
|
|
|
|
{
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->db_name != NULL)
|
|
|
|
free(entry->db_name);
|
|
|
|
entry->db_name = NULL;
|
2017-04-23 10:25:54 +00:00
|
|
|
entry->db_name = strdup(db_name);
|
|
|
|
playlist->modified = true;
|
2016-06-17 03:37:46 +00:00
|
|
|
}
|
2016-08-28 16:47:34 +00:00
|
|
|
|
|
|
|
if (crc32 && (crc32 != entry->crc32))
|
|
|
|
{
|
2017-05-29 18:36:18 +00:00
|
|
|
if (entry->crc32 != NULL)
|
|
|
|
free(entry->crc32);
|
|
|
|
entry->crc32 = NULL;
|
2017-04-23 10:25:54 +00:00
|
|
|
entry->crc32 = strdup(crc32);
|
|
|
|
playlist->modified = true;
|
2016-06-17 03:37:46 +00:00
|
|
|
}
|
2015-05-26 04:28:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 20:09:05 +00:00
|
|
|
/**
|
2016-05-16 07:07:44 +00:00
|
|
|
* playlist_push:
|
2015-01-17 02:48:59 +00:00
|
|
|
* @playlist : Playlist handle.
|
2015-01-16 20:09:05 +00:00
|
|
|
* @path : Path of new playlist entry.
|
|
|
|
* @core_path : Core path of new playlist entry.
|
|
|
|
* @core_name : Core name of new playlist entry.
|
|
|
|
*
|
|
|
|
* Push entry to top of playlist.
|
|
|
|
**/
|
2016-07-30 19:27:40 +00:00
|
|
|
bool playlist_push(playlist_t *playlist,
|
2015-05-25 22:12:49 +00:00
|
|
|
const char *path, const char *label,
|
|
|
|
const char *core_path, const char *core_name,
|
2015-06-08 21:38:03 +00:00
|
|
|
const char *crc32,
|
|
|
|
const char *db_name)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2014-06-01 00:16:48 +00:00
|
|
|
|
2016-07-22 08:15:33 +00:00
|
|
|
if (string_is_empty(core_path) || string_is_empty(core_name))
|
2015-03-22 22:17:29 +00:00
|
|
|
{
|
2016-07-22 22:15:37 +00:00
|
|
|
if (string_is_empty(core_name) && !string_is_empty(core_path))
|
|
|
|
{
|
2016-10-27 07:44:20 +00:00
|
|
|
static char base_path[255] = {0};
|
2016-07-22 22:15:37 +00:00
|
|
|
fill_pathname_base_noext(base_path, core_path, sizeof(base_path));
|
|
|
|
core_name = base_path;
|
|
|
|
RARCH_LOG("core_name is now: %s\n", core_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
RARCH_LOG("core_name: %s.\n", string_is_empty(core_name) ? "N/A" : core_name);
|
|
|
|
RARCH_LOG("core_path: %s.\n", string_is_empty(core_path) ? "N/A" : core_path);
|
|
|
|
|
|
|
|
if (string_is_empty(core_path) || string_is_empty(core_name))
|
|
|
|
{
|
|
|
|
RARCH_ERR("cannot push NULL or empty core name into the playlist.\n");
|
2016-07-30 19:27:40 +00:00
|
|
|
return false;
|
2016-07-22 22:15:37 +00:00
|
|
|
}
|
2015-03-22 22:17:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 08:15:33 +00:00
|
|
|
if (string_is_empty(path))
|
2015-03-22 22:17:29 +00:00
|
|
|
path = NULL;
|
|
|
|
|
2016-09-29 08:16:48 +00:00
|
|
|
if (!playlist)
|
|
|
|
return false;
|
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
for (i = 0; i < playlist->size; i++)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2016-08-28 16:47:34 +00:00
|
|
|
struct playlist_entry tmp;
|
2014-08-15 15:32:38 +00:00
|
|
|
bool equal_path = (!path && !playlist->entries[i].path) ||
|
2014-08-20 15:23:21 +00:00
|
|
|
(path && playlist->entries[i].path &&
|
2016-01-20 03:07:24 +00:00
|
|
|
string_is_equal(path,playlist->entries[i].path));
|
2013-05-02 12:42:58 +00:00
|
|
|
|
2014-08-20 15:23:21 +00:00
|
|
|
/* Core name can have changed while still being the same core.
|
|
|
|
* Differentiate based on the core path only. */
|
2015-01-16 18:42:11 +00:00
|
|
|
if (!equal_path)
|
|
|
|
continue;
|
|
|
|
|
2016-01-20 03:07:24 +00:00
|
|
|
if (!string_is_equal(playlist->entries[i].core_path, core_path))
|
2015-01-16 18:42:11 +00:00
|
|
|
continue;
|
|
|
|
|
2015-01-16 18:46:10 +00:00
|
|
|
/* If top entry, we don't want to push a new entry since
|
|
|
|
* the top and the entry to be pushed are the same. */
|
2015-01-16 18:42:11 +00:00
|
|
|
if (i == 0)
|
2016-07-30 19:27:40 +00:00
|
|
|
return false;
|
2015-01-16 18:42:11 +00:00
|
|
|
|
|
|
|
/* Seen it before, bump to top. */
|
|
|
|
tmp = playlist->entries[i];
|
|
|
|
memmove(playlist->entries + 1, playlist->entries,
|
2016-08-28 16:47:34 +00:00
|
|
|
i * sizeof(struct playlist_entry));
|
2015-01-16 18:42:11 +00:00
|
|
|
playlist->entries[0] = tmp;
|
|
|
|
|
2017-04-23 10:25:54 +00:00
|
|
|
goto success;
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
if (playlist->size == playlist->cap)
|
2013-04-27 23:35:31 +00:00
|
|
|
{
|
2016-09-29 07:36:24 +00:00
|
|
|
struct playlist_entry *entry = &playlist->entries[playlist->cap - 1];
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
playlist_free_entry(entry);
|
2014-08-15 15:32:38 +00:00
|
|
|
playlist->size--;
|
2013-04-27 23:35:31 +00:00
|
|
|
}
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
memmove(playlist->entries + 1, playlist->entries,
|
2016-08-28 16:47:34 +00:00
|
|
|
(playlist->cap - 1) * sizeof(struct playlist_entry));
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2016-12-25 00:46:32 +00:00
|
|
|
playlist->entries[0].path = NULL;
|
|
|
|
playlist->entries[0].label = NULL;
|
|
|
|
playlist->entries[0].core_path = NULL;
|
|
|
|
playlist->entries[0].core_name = NULL;
|
|
|
|
playlist->entries[0].db_name = NULL;
|
|
|
|
playlist->entries[0].crc32 = NULL;
|
2016-06-01 03:34:15 +00:00
|
|
|
if (!string_is_empty(path))
|
2016-12-25 00:46:32 +00:00
|
|
|
playlist->entries[0].path = strdup(path);
|
2016-06-01 03:34:15 +00:00
|
|
|
if (!string_is_empty(label))
|
2016-12-25 00:46:32 +00:00
|
|
|
playlist->entries[0].label = strdup(label);
|
2016-06-01 03:34:15 +00:00
|
|
|
if (!string_is_empty(core_path))
|
|
|
|
playlist->entries[0].core_path = strdup(core_path);
|
|
|
|
if (!string_is_empty(core_name))
|
|
|
|
playlist->entries[0].core_name = strdup(core_name);
|
|
|
|
if (!string_is_empty(db_name))
|
|
|
|
playlist->entries[0].db_name = strdup(db_name);
|
|
|
|
if (!string_is_empty(crc32))
|
|
|
|
playlist->entries[0].crc32 = strdup(crc32);
|
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
playlist->size++;
|
2016-07-30 19:27:40 +00:00
|
|
|
|
2017-04-23 10:25:54 +00:00
|
|
|
success:
|
|
|
|
playlist->modified = true;
|
|
|
|
|
2016-07-30 19:27:40 +00:00
|
|
|
return true;
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 07:07:44 +00:00
|
|
|
void playlist_write_file(playlist_t *playlist)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2014-07-11 23:57:01 +00:00
|
|
|
FILE *file = NULL;
|
|
|
|
|
2017-04-23 10:25:54 +00:00
|
|
|
if (!playlist || !playlist->modified)
|
2014-07-11 23:57:01 +00:00
|
|
|
return;
|
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
file = fopen(playlist->conf_path, "w");
|
2014-07-11 23:57:01 +00:00
|
|
|
|
2016-07-22 08:15:33 +00:00
|
|
|
RARCH_LOG("Trying to write to playlist file: %s\n", playlist->conf_path);
|
|
|
|
|
2014-07-11 23:57:01 +00:00
|
|
|
if (!file)
|
2016-07-22 08:15:33 +00:00
|
|
|
{
|
|
|
|
RARCH_ERR("Failed to write to playlist file: %s\n", playlist->conf_path);
|
2013-04-27 22:15:59 +00:00
|
|
|
return;
|
2016-07-22 08:15:33 +00:00
|
|
|
}
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
for (i = 0; i < playlist->size; i++)
|
2015-06-08 21:38:03 +00:00
|
|
|
fprintf(file, "%s\n%s\n%s\n%s\n%s\n%s\n",
|
2016-03-21 18:52:33 +00:00
|
|
|
playlist->entries[i].path ? playlist->entries[i].path : "",
|
|
|
|
playlist->entries[i].label ? playlist->entries[i].label : "",
|
2014-08-15 15:32:38 +00:00
|
|
|
playlist->entries[i].core_path,
|
2015-05-25 22:12:49 +00:00
|
|
|
playlist->entries[i].core_name,
|
2016-03-21 18:52:33 +00:00
|
|
|
playlist->entries[i].crc32 ? playlist->entries[i].crc32 : "",
|
2015-06-08 21:38:03 +00:00
|
|
|
playlist->entries[i].db_name ? playlist->entries[i].db_name : ""
|
|
|
|
);
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2017-04-23 10:54:13 +00:00
|
|
|
playlist->modified = false;
|
2013-04-27 22:15:59 +00:00
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
2015-01-16 18:42:11 +00:00
|
|
|
/**
|
2016-05-16 07:07:44 +00:00
|
|
|
* playlist_free:
|
2016-05-06 18:20:16 +00:00
|
|
|
* @playlist : Playlist handle.
|
2015-01-16 18:42:11 +00:00
|
|
|
*
|
|
|
|
* Frees playlist handle.
|
|
|
|
*/
|
2016-05-16 07:07:44 +00:00
|
|
|
void playlist_free(playlist_t *playlist)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2015-01-16 20:09:05 +00:00
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
if (!playlist)
|
2013-04-27 22:15:59 +00:00
|
|
|
return;
|
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
if (playlist->conf_path)
|
2015-06-09 20:05:21 +00:00
|
|
|
free(playlist->conf_path);
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2015-06-09 20:06:20 +00:00
|
|
|
playlist->conf_path = NULL;
|
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
for (i = 0; i < playlist->cap; i++)
|
2016-09-29 07:36:24 +00:00
|
|
|
{
|
|
|
|
struct playlist_entry *entry = &playlist->entries[i];
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
playlist_free_entry(entry);
|
|
|
|
}
|
2015-06-09 20:06:20 +00:00
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
free(playlist->entries);
|
2015-06-09 20:06:20 +00:00
|
|
|
playlist->entries = NULL;
|
2013-04-27 23:35:31 +00:00
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
free(playlist);
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 18:42:11 +00:00
|
|
|
/**
|
2016-05-16 07:07:44 +00:00
|
|
|
* playlist_clear:
|
2015-01-17 02:48:59 +00:00
|
|
|
* @playlist : Playlist handle.
|
2015-01-16 18:42:11 +00:00
|
|
|
*
|
|
|
|
* Clears all playlist entries in playlist.
|
|
|
|
**/
|
2016-05-16 07:07:44 +00:00
|
|
|
void playlist_clear(playlist_t *playlist)
|
2013-12-16 02:27:17 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
2014-08-15 15:32:38 +00:00
|
|
|
if (!playlist)
|
2014-06-01 00:16:48 +00:00
|
|
|
return;
|
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
for (i = 0; i < playlist->cap; i++)
|
2016-09-29 07:36:24 +00:00
|
|
|
{
|
|
|
|
struct playlist_entry *entry = &playlist->entries[i];
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
playlist_free_entry(entry);
|
|
|
|
}
|
2014-08-15 15:32:38 +00:00
|
|
|
playlist->size = 0;
|
2013-12-16 02:27:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 19:19:21 +00:00
|
|
|
/**
|
2016-05-16 07:07:44 +00:00
|
|
|
* playlist_size:
|
2015-01-17 02:48:59 +00:00
|
|
|
* @playlist : Playlist handle.
|
2015-01-16 19:19:21 +00:00
|
|
|
*
|
|
|
|
* Gets size of playlist.
|
|
|
|
* Returns: size of playlist.
|
|
|
|
**/
|
2016-05-16 07:07:44 +00:00
|
|
|
size_t playlist_size(playlist_t *playlist)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2015-01-16 19:19:21 +00:00
|
|
|
if (!playlist)
|
|
|
|
return 0;
|
|
|
|
return playlist->size;
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2015-11-28 23:46:47 +00:00
|
|
|
|
2016-05-16 07:07:44 +00:00
|
|
|
static bool playlist_read_file(
|
|
|
|
playlist_t *playlist, const char *path)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2016-10-09 07:07:53 +00:00
|
|
|
unsigned i;
|
|
|
|
char buf[PLAYLIST_ENTRIES][1024];
|
2016-09-29 08:16:48 +00:00
|
|
|
RFILE *file = filestream_open(
|
|
|
|
path, RFILE_MODE_READ_TEXT, -1);
|
2014-08-02 23:43:10 +00:00
|
|
|
|
2016-10-09 07:07:53 +00:00
|
|
|
for (i = 0; i < PLAYLIST_ENTRIES; i++)
|
|
|
|
buf[i][0] = '\0';
|
|
|
|
|
2015-01-16 18:42:11 +00:00
|
|
|
/* If playlist file does not exist,
|
|
|
|
* create an empty playlist instead.
|
|
|
|
*/
|
2014-11-29 15:05:52 +00:00
|
|
|
if (!file)
|
|
|
|
return true;
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
for (playlist->size = 0; playlist->size < playlist->cap; )
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2016-09-29 08:16:48 +00:00
|
|
|
unsigned i;
|
|
|
|
struct playlist_entry *entry = NULL;
|
2015-05-25 22:12:49 +00:00
|
|
|
for (i = 0; i < PLAYLIST_ENTRIES; i++)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2016-09-29 08:16:48 +00:00
|
|
|
char *last = NULL;
|
|
|
|
*buf[i] = '\0';
|
2015-01-16 18:42:11 +00:00
|
|
|
|
2016-06-02 22:12:58 +00:00
|
|
|
if (!filestream_gets(file, buf[i], sizeof(buf[i])))
|
2013-06-09 19:59:48 +00:00
|
|
|
goto end;
|
|
|
|
|
2016-12-14 20:32:47 +00:00
|
|
|
/* Read playlist entry and terminate string with NUL character
|
2016-12-07 20:52:14 +00:00
|
|
|
* regardless of Windows or Unix line endings
|
2016-12-07 21:07:45 +00:00
|
|
|
*/
|
2016-12-10 16:03:28 +00:00
|
|
|
if((last = strrchr(buf[i], '\r')))
|
2016-12-07 21:07:45 +00:00
|
|
|
*last = '\0';
|
2016-12-10 16:03:28 +00:00
|
|
|
else if((last = strrchr(buf[i], '\n')))
|
2016-12-07 21:07:45 +00:00
|
|
|
*last = '\0';
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
entry = &playlist->entries[playlist->size];
|
2013-06-09 19:59:48 +00:00
|
|
|
|
2015-05-25 22:12:49 +00:00
|
|
|
if (!*buf[2] || !*buf[3])
|
2014-01-04 15:38:23 +00:00
|
|
|
continue;
|
2013-05-02 12:42:58 +00:00
|
|
|
|
2013-06-09 19:59:48 +00:00
|
|
|
if (*buf[0])
|
2015-06-08 21:38:03 +00:00
|
|
|
entry->path = strdup(buf[0]);
|
2015-05-25 22:12:49 +00:00
|
|
|
if (*buf[1])
|
2015-06-08 21:38:03 +00:00
|
|
|
entry->label = strdup(buf[1]);
|
2016-12-15 11:02:01 +00:00
|
|
|
|
2015-06-08 21:38:03 +00:00
|
|
|
entry->core_path = strdup(buf[2]);
|
|
|
|
entry->core_name = strdup(buf[3]);
|
2015-05-25 22:12:49 +00:00
|
|
|
if (*buf[4])
|
|
|
|
entry->crc32 = strdup(buf[4]);
|
2015-06-08 21:38:03 +00:00
|
|
|
if (*buf[5])
|
|
|
|
entry->db_name = strdup(buf[5]);
|
2014-08-15 15:32:38 +00:00
|
|
|
playlist->size++;
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2013-06-09 19:59:48 +00:00
|
|
|
end:
|
2016-06-02 22:12:58 +00:00
|
|
|
filestream_close(file);
|
2013-04-27 22:15:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-16 18:42:11 +00:00
|
|
|
/**
|
2016-05-16 07:07:44 +00:00
|
|
|
* playlist_init:
|
2015-01-17 02:48:59 +00:00
|
|
|
* @path : Path to playlist contents file.
|
2015-01-16 18:42:11 +00:00
|
|
|
* @size : Maximum capacity of playlist size.
|
|
|
|
*
|
|
|
|
* Creates and initializes a playlist.
|
|
|
|
*
|
|
|
|
* Returns: handle to new playlist if successful, otherwise NULL
|
|
|
|
**/
|
2016-05-16 07:07:44 +00:00
|
|
|
playlist_t *playlist_init(const char *path, size_t size)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2016-12-25 00:46:32 +00:00
|
|
|
struct playlist_entry *entries = NULL;
|
|
|
|
playlist_t *playlist = (playlist_t*)calloc(1, sizeof(*playlist));
|
2014-08-15 15:32:38 +00:00
|
|
|
if (!playlist)
|
2013-04-27 22:15:59 +00:00
|
|
|
return NULL;
|
|
|
|
|
2016-12-25 00:46:32 +00:00
|
|
|
entries = (struct playlist_entry*)calloc(size, sizeof(*entries));
|
|
|
|
if (!entries)
|
|
|
|
{
|
|
|
|
free(playlist);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2016-12-25 00:46:32 +00:00
|
|
|
playlist->entries = entries;
|
|
|
|
playlist->cap = size;
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2016-05-16 07:07:44 +00:00
|
|
|
playlist_read_file(playlist, path);
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2014-08-15 15:32:38 +00:00
|
|
|
playlist->conf_path = strdup(path);
|
|
|
|
return playlist;
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
2015-06-11 13:04:35 +00:00
|
|
|
|
2016-08-28 16:47:34 +00:00
|
|
|
static int playlist_qsort_func(const struct playlist_entry *a,
|
|
|
|
const struct playlist_entry *b)
|
|
|
|
{
|
2016-12-15 11:02:01 +00:00
|
|
|
const char *a_label = a ? a->label : NULL;
|
|
|
|
const char *b_label = b ? b->label : NULL;
|
2016-08-28 16:47:34 +00:00
|
|
|
|
|
|
|
if (!a_label || !b_label)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return strcasecmp(a_label, b_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void playlist_qsort(playlist_t *playlist)
|
2015-06-11 13:04:35 +00:00
|
|
|
{
|
2016-03-21 18:52:33 +00:00
|
|
|
qsort(playlist->entries, playlist->size,
|
2016-08-28 16:47:34 +00:00
|
|
|
sizeof(struct playlist_entry),
|
|
|
|
(int (*)(const void *, const void *))playlist_qsort_func);
|
2015-06-11 13:04:35 +00:00
|
|
|
}
|