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

145 lines
4.4 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_t *handle, g1m_mcsfile_t **tofile,
const g1m_mcshead_t *head)
{
g1m_mcsfile_t **pfile = NULL; *tofile = NULL;
/* look if this file isn't already in the tab */
int istyp = g1m_mcshead_uses_id(head);
for (int i = 0; i < handle->count; i++) {
g1m_mcsfile_t *file = handle->files[i];
if (!file) {
pfile = &handle->files[i];
continue;
}
/* loop assertions */
if (file->head.type != head->type)
continue;
if (head->type) {
if (istyp) {
if (head->id != file->head.id)
continue;
} else if (strcmp(file->head.name, head->name))
continue;
} else switch (g1m_mcsinfo(head)) {
case g1m_mcsinfo_mcs:
if (strncmp((char*)head->_group, (char*)file->head._group, 16))
continue;
if (strncmp((char*)head->_dirname, (char*)file->head._dirname, 8))
continue;
if (strncmp((char*)head->name, (char*)file->head.name, 8))
continue;
break;
case g1m_mcsinfo_cas:
case g1m_mcsinfo_caspro:
if (strncmp((char*)head->_datatype,
(char*)file->head._datatype, 2))
continue;
break;
default: continue;
}
/* slot was found! */
g1m_free_mcsfile(file);
pfile = &handle->files[i];
goto found;
}
/* if no empty space was found, take one at the end (allocate if needed) */
if (!pfile) {
if (handle->count >= handle->_size) {
/* allocate new index */
int newsize = 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->files,
handle->_size * sizeof(g1m_mcsfile_t*));
memset(&newindex[handle->_size], 0,
(newsize - handle->_size) * sizeof(g1m_mcsfile_t*));
/* free old, assign new */
free(handle->files);
handle->files = newindex;
handle->_size = newsize;
}
pfile = &handle->files[handle->count++];
}
found:;
/* make the file */
int 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_t *handle)
{
if (handle->type != g1m_type_mcs) return (g1m_error_op);
if (handle->platform != g1m_platform_fx) return (g1m_error_op);
/* correct each head */
for (int i = 0; i < handle->count; i++) {
int err = g1m_correct_mcsfile_head(&handle->files[i]->head);
if (err) return (err);
}
/* sort */
qsort(handle->files, handle->count, sizeof(g1m_mcsfile_t*),
(g1m_compare_mcsfiles_t)g1m_compare_mcsfiles);
/* TODO: check for duplicates */
return (0);
}