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/encode/main.c

93 lines
2.8 KiB
C

/* *****************************************************************************
* encode/main.c -- encode a file.
* 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>
#define FUNCS(NAME) g1m_announce_##NAME, g1m_encode_##NAME
/* ************************************************************************** */
/* Correspondances */
/* ************************************************************************** */
/* Types */
typedef int (*announce_t)(g1m_handle_t*, size_t*);
typedef int (*encode_t)(g1m_handle_t*, g1m_buffer_t*);
struct corresp {
/* 'identification' */
g1m_type_t types;
g1m_platform_t platforms;
/* functions */
announce_t announce;
encode_t encode;
};
/* All correspondances */
static const struct corresp encodings[] = {
/* add-in types */
{g1m_type_addin, g1m_platform_fx, FUNCS(addin)},
/* sentinel */
{0, 0, NULL, NULL}
};
/* ************************************************************************** */
/* Main function */
/* ************************************************************************** */
/**
* g1m_encode:
* Encode a file.
*
* @arg handle the handle.
* @arg buffer the buffer to write to.
* @return the error code (0 if ok).
*/
int g1m_encode(g1m_handle_t *handle, g1m_buffer_t *buffer)
{
int err;
/* find the announcement and encoding function */
const struct corresp *c = encodings - 1;
while ((++c)->encode) {
if (c->types && !(c->types & handle->type))
continue;
if (c->platforms && !(c->platforms & handle->platform))
continue;
break;
}
if (!c->encode) return (g1m_error_op);
/* announce, if necessary */
if (buffer->announce) {
size_t size = 0;
if ((err = (*c->announce)(handle, &size))
|| (err = (*buffer->announce)(buffer->cookie, size)))
return (err);
}
/* encode */
if (buffer->write) {
err = (*c->encode)(handle, buffer);
if (err) {
(*buffer->unannounce)(buffer->cookie);
return (err);
}
}
/* everything went well! */
return (0);
}