cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/manage/mcs.c

128 lines
4.1 KiB
C

/* *****************************************************************************
* manage/mcs.c -- manage an MCS archive handle.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libg1m.
* libg1m is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3.0 of the License,
* or (at your option) any later version.
*
* libg1m 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libg1m/internals.h>
#include <stdlib.h>
#define MCS_CHUNK_SIZE 16
/* ************************************************************************** */
/* General function for inserting files */
/* ************************************************************************** */
/**
* g1m_mcs_insert:
* Find space for a file and allocate.
*
* @arg handle the handle.
* @arg tofile the user file pointer, to set.
* @arg head the head.
* @return the mcs file (NULL if allocation error).
*/
int g1m_mcs_insert(g1m_handle_t *handle, g1m_mcsfile_t **tofile,
const g1m_mcshead_t *head)
{
g1m_mcsfile_t **pfile = NULL; *tofile = NULL;
int i, err;
/* look if this file isn't already in the tab */
int istyp = g1m_mcshead_uses_id(head);
for (i = 0; i < handle->g1m_handle_count; i++) {
g1m_mcsfile_t *file = handle->g1m_handle_files[i];
if (!file) {
pfile = &handle->g1m_handle_files[i];
continue;
}
/* Assertions. */
if (!casio_match_mcsfiles(&file->casio_mcsfile_head, head))
continue;
/* slot was found! */
g1m_free_mcsfile(file);
pfile = &handle->g1m_handle_files[i];
goto found;
}
/* if no empty space was found, take one at the end (allocate if needed) */
if (!pfile) {
if (handle->g1m_handle_count >= handle->g1m_handle__size) {
/* allocate new index */
int newsize = handle->g1m_handle_count + MCS_CHUNK_SIZE;
g1m_mcsfile_t **newindex = malloc(newsize * sizeof(g1m_mcsfile_t*));
if (!newindex) return (g1m_error_alloc);
/* copy the data from the old index */
memcpy(newindex, handle->g1m_handle_files,
handle->g1m_handle__size * sizeof(g1m_mcsfile_t*));
memset(&newindex[handle->g1m_handle__size], 0,
(newsize - handle->g1m_handle__size) * sizeof(g1m_mcsfile_t*));
/* free old, assign new */
free(handle->g1m_handle_files);
handle->g1m_handle_files = newindex;
handle->g1m_handle__size = newsize;
}
pfile = &handle->g1m_handle_files[handle->g1m_handle_count++];
}
found:;
/* make the file */
err = g1m_make_mcsfile(pfile, head);
if (err) return (err);
/* return it */
*tofile = *pfile;
return (0);
}
/* ************************************************************************** */
/* Reorganize an MCS handle for encoding */
/* ************************************************************************** */
/**
* g1m_mcs_sort:
* Sort an MCS archive.
*
* @arg handle the MCS archive handle.
* @return the error code (0 if ok).
*/
typedef int (*g1m_compare_mcsfiles_t)(const void*, const void*);
int g1m_mcs_sort(g1m_handle_t *handle)
{
int i;
/* make checks */
if (handle->g1m_handle_type != g1m_type_mcs)
return (g1m_error_op);
if (handle->g1m_handle_platform != g1m_platform_fx)
return (g1m_error_op);
/* correct each head */
for (i = 0; i < handle->g1m_handle_count; i++) {
int err = g1m_correct_mcsfile_head(
&handle->g1m_handle_files[i]->g1m_mcsfile_head);
if (err) return (err);
}
/* sort */
qsort(handle->g1m_handle_files, handle->g1m_handle_count,
sizeof(g1m_mcsfile_t*), (g1m_compare_mcsfiles_t)g1m_compare_mcsfiles);
/* TODO: check for duplicates */
return (0);
}