2013-04-27 22:15:59 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
|
|
|
|
*
|
|
|
|
* 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 "history.h"
|
2013-05-03 22:59:59 +00:00
|
|
|
#include "../../msvc/msvc_compat.h"
|
2013-04-27 22:15:59 +00:00
|
|
|
#include "../../compat/posix_string.h"
|
|
|
|
#include "../../boolean.h"
|
|
|
|
#include "../../general.h"
|
|
|
|
#include "../../file.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct rom_history_entry
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
char *core_path;
|
|
|
|
char *core_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct rom_history
|
|
|
|
{
|
|
|
|
struct rom_history_entry *entries;
|
|
|
|
size_t size;
|
|
|
|
size_t cap;
|
|
|
|
|
|
|
|
char *conf_path;
|
|
|
|
};
|
|
|
|
|
|
|
|
void rom_history_get_index(rom_history_t *hist,
|
|
|
|
size_t index,
|
|
|
|
const char **path, const char **core_path,
|
|
|
|
const char **core_name)
|
|
|
|
{
|
|
|
|
*path = hist->entries[index].path;
|
|
|
|
*core_path = hist->entries[index].core_path;
|
|
|
|
*core_name = hist->entries[index].core_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rom_history_free_entry(struct rom_history_entry *entry)
|
|
|
|
{
|
|
|
|
free(entry->path);
|
|
|
|
free(entry->core_path);
|
|
|
|
free(entry->core_name);
|
|
|
|
memset(entry, 0, sizeof(*entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
void rom_history_push(rom_history_t *hist,
|
|
|
|
const char *path, const char *core_path,
|
|
|
|
const char *core_name)
|
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < hist->size; i++)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2013-05-02 12:42:58 +00:00
|
|
|
bool equal_path = (!path && !hist->entries[i].path) ||
|
|
|
|
(path && hist->entries[i].path && !strcmp(path, hist->entries[i].path));
|
|
|
|
|
|
|
|
if (equal_path &&
|
2013-04-27 22:15:59 +00:00
|
|
|
!strcmp(hist->entries[i].core_path, core_path) &&
|
|
|
|
!strcmp(hist->entries[i].core_name, core_name))
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Seen it before, bump to top.
|
|
|
|
struct rom_history_entry tmp = hist->entries[i];
|
|
|
|
memmove(hist->entries + 1, hist->entries,
|
2013-04-27 23:43:37 +00:00
|
|
|
i * sizeof(struct rom_history_entry));
|
2013-04-27 22:15:59 +00:00
|
|
|
hist->entries[0] = tmp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hist->size == hist->cap)
|
2013-04-27 23:35:31 +00:00
|
|
|
{
|
2013-04-27 22:15:59 +00:00
|
|
|
rom_history_free_entry(&hist->entries[hist->cap - 1]);
|
2013-04-27 23:35:31 +00:00
|
|
|
hist->size--;
|
|
|
|
}
|
2013-04-27 22:15:59 +00:00
|
|
|
|
|
|
|
memmove(hist->entries + 1, hist->entries,
|
|
|
|
(hist->cap - 1) * sizeof(struct rom_history_entry));
|
|
|
|
|
2013-05-02 12:42:58 +00:00
|
|
|
hist->entries[0].path = path ? strdup(path) : NULL;
|
2013-04-27 22:15:59 +00:00
|
|
|
hist->entries[0].core_path = strdup(core_path);
|
|
|
|
hist->entries[0].core_name = strdup(core_name);
|
2013-04-27 23:35:31 +00:00
|
|
|
hist->size++;
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rom_history_write_file(rom_history_t *hist)
|
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2013-04-27 22:15:59 +00:00
|
|
|
FILE *file = fopen(hist->conf_path, "w");
|
|
|
|
if (!file)
|
|
|
|
return;
|
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < hist->size; i++)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2013-06-09 19:59:48 +00:00
|
|
|
fprintf(file, "%s\n%s\n%s\n",
|
|
|
|
hist->entries[i].path ? hist->entries[i].path : "",
|
|
|
|
hist->entries[i].core_path,
|
|
|
|
hist->entries[i].core_name);
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rom_history_free(rom_history_t *hist)
|
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
size_t i;
|
2013-04-27 22:15:59 +00:00
|
|
|
if (!hist)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (hist->conf_path)
|
|
|
|
rom_history_write_file(hist);
|
|
|
|
free(hist->conf_path);
|
|
|
|
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < hist->cap; i++)
|
2013-04-27 23:35:31 +00:00
|
|
|
rom_history_free_entry(&hist->entries[i]);
|
|
|
|
free(hist->entries);
|
|
|
|
|
2013-04-27 22:15:59 +00:00
|
|
|
free(hist);
|
|
|
|
}
|
|
|
|
|
2013-12-16 02:27:17 +00:00
|
|
|
void rom_history_clear(rom_history_t *hist)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < hist->cap; i++)
|
|
|
|
rom_history_free_entry(&hist->entries[i]);
|
|
|
|
hist->size = 0;
|
|
|
|
}
|
|
|
|
|
2013-04-27 22:15:59 +00:00
|
|
|
size_t rom_history_size(rom_history_t *hist)
|
|
|
|
{
|
|
|
|
return hist->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool rom_history_read_file(rom_history_t *hist, const char *path)
|
|
|
|
{
|
|
|
|
FILE *file = fopen(path, "r");
|
|
|
|
if (!file)
|
2013-04-27 23:35:31 +00:00
|
|
|
return true;
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2013-06-09 19:59:48 +00:00
|
|
|
char buf[3][PATH_MAX];
|
|
|
|
struct rom_history_entry *entry = NULL;
|
|
|
|
char *last = NULL;
|
2013-10-22 13:08:17 +00:00
|
|
|
unsigned i;
|
2013-04-27 22:15:59 +00:00
|
|
|
|
2013-06-09 19:59:48 +00:00
|
|
|
for (hist->size = 0; hist->size < hist->cap; hist->size++)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
2013-04-27 22:15:59 +00:00
|
|
|
{
|
2013-06-09 19:59:48 +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
|
|
|
}
|
|
|
|
|
2013-06-09 19:59:48 +00:00
|
|
|
entry = &hist->entries[hist->size];
|
|
|
|
|
|
|
|
if (!*buf[1] || !*buf[2])
|
|
|
|
goto end;
|
2013-05-02 12:42:58 +00:00
|
|
|
|
|
|
|
|
2013-06-09 19:59:48 +00:00
|
|
|
if (*buf[0])
|
|
|
|
entry->path = strdup(buf[0]);
|
|
|
|
entry->core_path = strdup(buf[1]);
|
|
|
|
entry->core_name = strdup(buf[2]);
|
2013-04-27 22:15:59 +00:00
|
|
|
}
|
|
|
|
|
2013-06-09 19:59:48 +00:00
|
|
|
end:
|
2013-04-27 22:15:59 +00:00
|
|
|
fclose(file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
rom_history_t *rom_history_init(const char *path, size_t size)
|
|
|
|
{
|
|
|
|
rom_history_t *hist = (rom_history_t*)calloc(1, sizeof(*hist));
|
|
|
|
if (!hist)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hist->entries = (struct rom_history_entry*)calloc(size, sizeof(*hist->entries));
|
|
|
|
if (!hist->entries)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
hist->cap = size;
|
|
|
|
|
|
|
|
if (!rom_history_read_file(hist, path))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
hist->conf_path = strdup(path);
|
|
|
|
return hist;
|
|
|
|
|
|
|
|
error:
|
|
|
|
rom_history_free(hist);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|