RetroArch/playlist.c

257 lines
6.2 KiB
C
Raw Normal View History

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
* Copyright (C) 2011-2014 - Daniel De Matteis
* Copyright (C) 2013-2014 - Jason Fetters
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/>.
*/
2014-08-15 15:24:28 +00:00
#include "playlist.h"
#include "msvc/msvc_compat.h"
#include "compat/posix_string.h"
#include "boolean.h"
#include "general.h"
#include "file.h"
2013-04-27 22:15:59 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-08-15 15:24:28 +00:00
struct content_playlist_entry
2013-04-27 22:15:59 +00:00
{
char *path;
char *core_path;
char *core_name;
};
2014-08-15 15:24:28 +00:00
struct content_playlist
2013-04-27 22:15:59 +00:00
{
2014-08-15 15:24:28 +00:00
struct content_playlist_entry *entries;
2013-04-27 22:15:59 +00:00
size_t size;
size_t cap;
char *conf_path;
};
2014-08-15 15:32:38 +00:00
void content_playlist_get_index(content_playlist_t *playlist,
2013-04-27 22:15:59 +00:00
size_t index,
const char **path, const char **core_path,
const char **core_name)
{
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
*path = playlist->entries[index].path;
2014-08-31 19:56:36 +00:00
if (core_path)
*core_path = playlist->entries[index].core_path;
2014-08-15 15:32:38 +00:00
*core_name = playlist->entries[index].core_name;
2013-04-27 22:15:59 +00:00
}
2014-08-20 15:23:21 +00:00
static void content_playlist_free_entry(
struct content_playlist_entry *entry)
2013-04-27 22:15:59 +00:00
{
2014-07-25 21:33:49 +00:00
if (entry->path)
free(entry->path);
entry->path = NULL;
if (entry->core_path)
free(entry->core_path);
entry->core_path = NULL;
if (entry->core_name)
free(entry->core_name);
entry->core_name = NULL;
2013-04-27 22:15:59 +00:00
memset(entry, 0, sizeof(*entry));
}
2014-08-15 15:32:38 +00:00
void content_playlist_push(content_playlist_t *playlist,
2013-04-27 22:15:59 +00:00
const char *path, const char *core_path,
const char *core_name)
{
size_t i;
2014-06-01 00:16:48 +00:00
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->size; i++)
2013-04-27 22:15:59 +00:00
{
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 &&
!strcmp(path,playlist->entries[i].path));
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. */
2014-08-15 15:32:38 +00:00
if (equal_path && !strcmp(playlist->entries[i].core_path, core_path))
2013-04-27 22:15:59 +00:00
{
if (i == 0)
return;
2014-08-20 15:23:21 +00:00
/* Seen it before, bump to top. */
2014-08-15 15:32:38 +00:00
struct content_playlist_entry tmp = playlist->entries[i];
memmove(playlist->entries + 1, playlist->entries,
2014-08-15 15:24:28 +00:00
i * sizeof(struct content_playlist_entry));
2014-08-15 15:32:38 +00:00
playlist->entries[0] = tmp;
2013-04-27 22:15:59 +00:00
return;
}
}
2014-08-15 15:32:38 +00:00
if (playlist->size == playlist->cap)
2013-04-27 23:35:31 +00:00
{
2014-08-15 15:32:38 +00:00
content_playlist_free_entry(&playlist->entries[playlist->cap - 1]);
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,
(playlist->cap - 1) * sizeof(struct content_playlist_entry));
2013-04-27 22:15:59 +00:00
2014-08-15 15:32:38 +00:00
playlist->entries[0].path = path ? strdup(path) : NULL;
playlist->entries[0].core_path = strdup(core_path);
playlist->entries[0].core_name = strdup(core_name);
playlist->size++;
2013-04-27 22:15:59 +00:00
}
2014-08-15 15:32:38 +00:00
static void content_playlist_write_file(content_playlist_t *playlist)
2013-04-27 22:15:59 +00:00
{
size_t i;
FILE *file = NULL;
2014-08-15 15:32:38 +00:00
if (!playlist)
return;
2014-08-15 15:32:38 +00:00
file = fopen(playlist->conf_path, "w");
if (!file)
2014-07-21 03:29:15 +00:00
{
2014-08-20 15:23:21 +00:00
RARCH_ERR("Couldn't write to content playlist file: %s.\n",
playlist->conf_path);
2013-04-27 22:15:59 +00:00
return;
2014-07-21 03:29:15 +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++)
2013-04-27 22:15:59 +00:00
{
fprintf(file, "%s\n%s\n%s\n",
2014-08-15 15:32:38 +00:00
playlist->entries[i].path ? playlist->entries[i].path : "",
playlist->entries[i].core_path,
playlist->entries[i].core_name);
2013-04-27 22:15:59 +00:00
}
fclose(file);
}
2014-08-15 15:32:38 +00:00
void content_playlist_free(content_playlist_t *playlist)
2013-04-27 22:15:59 +00:00
{
size_t i;
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)
content_playlist_write_file(playlist);
free(playlist->conf_path);
2013-04-27 22:15:59 +00:00
2014-08-15 15:32:38 +00:00
for (i = 0; i < playlist->cap; i++)
content_playlist_free_entry(&playlist->entries[i]);
free(playlist->entries);
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
}
2014-08-15 15:32:38 +00:00
void content_playlist_clear(content_playlist_t *playlist)
{
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++)
content_playlist_free_entry(&playlist->entries[i]);
playlist->size = 0;
}
2014-08-15 15:32:38 +00:00
size_t content_playlist_size(content_playlist_t *playlist)
2013-04-27 22:15:59 +00:00
{
2014-08-15 15:32:38 +00:00
if (playlist)
return playlist->size;
2014-06-01 00:16:48 +00:00
return 0;
2013-04-27 22:15:59 +00:00
}
2014-08-20 15:23:21 +00:00
static bool content_playlist_read_file(
content_playlist_t *playlist, const char *path)
2013-04-27 22:15:59 +00:00
{
2014-08-02 23:43:10 +00:00
char buf[3][PATH_MAX];
unsigned i;
2014-08-15 15:24:28 +00:00
struct content_playlist_entry *entry = NULL;
2014-08-02 23:43:10 +00:00
char *last = NULL;
2013-04-27 22:15:59 +00:00
FILE *file = fopen(path, "r");
2014-08-02 23:43:10 +00:00
2014-08-15 15:32:38 +00:00
if (!file || !playlist)
2014-07-21 03:29:15 +00:00
{
2014-08-15 15:24:28 +00:00
RARCH_ERR("Couldn't read content playlist file: %s.\n", path);
2013-04-27 23:35:31 +00:00
return true;
2014-07-21 03:29:15 +00:00
}
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
{
for (i = 0; i < 3; i++)
2013-04-27 22:15:59 +00:00
{
*buf[i] = '\0';
if (!fgets(buf[i], sizeof(buf[i]), file))
goto end;
last = strrchr(buf[i], '\n');
if (last)
*last = '\0';
2013-04-27 22:15:59 +00:00
}
2014-08-15 15:32:38 +00:00
entry = &playlist->entries[playlist->size];
if (!*buf[1] || !*buf[2])
continue;
if (*buf[0])
entry->path = strdup(buf[0]);
entry->core_path = strdup(buf[1]);
entry->core_name = strdup(buf[2]);
2014-08-15 15:32:38 +00:00
playlist->size++;
2013-04-27 22:15:59 +00:00
}
end:
2013-04-27 22:15:59 +00:00
fclose(file);
return true;
}
2014-08-15 15:24:28 +00:00
content_playlist_t *content_playlist_init(const char *path, size_t size)
2013-04-27 22:15:59 +00:00
{
2014-08-15 15:24:28 +00:00
RARCH_LOG("Opening playlist: %s.\n", path);
2014-08-20 15:23:21 +00:00
content_playlist_t *playlist = (content_playlist_t*)
calloc(1, sizeof(*playlist));
2014-08-15 15:32:38 +00:00
if (!playlist)
{
2014-08-15 15:24:28 +00:00
RARCH_ERR("Cannot initialize content playlist.\n");
2013-04-27 22:15:59 +00:00
return NULL;
}
2013-04-27 22:15:59 +00:00
2014-08-20 15:23:21 +00:00
playlist->entries = (struct content_playlist_entry*)calloc(size,
sizeof(*playlist->entries));
2014-08-15 15:32:38 +00:00
if (!playlist->entries)
2013-04-27 22:15:59 +00:00
goto error;
2014-08-15 15:32:38 +00:00
playlist->cap = size;
2013-04-27 22:15:59 +00:00
2014-08-15 15:32:38 +00:00
if (!content_playlist_read_file(playlist, path))
2013-04-27 22:15:59 +00:00
goto error;
2014-08-15 15:32:38 +00:00
playlist->conf_path = strdup(path);
return playlist;
2013-04-27 22:15:59 +00:00
error:
2014-08-15 15:32:38 +00:00
content_playlist_free(playlist);
2013-04-27 22:15:59 +00:00
return NULL;
}