/* ***************************************************************************** * encode/mcs.c -- encode an MCS file. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * 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 . * ************************************************************************** */ #include #define FUNCS(name) g1m_announce_mcs_##name, g1m_encode_mcs_##name /* ************************************************************************** */ /* Correspondances */ /* ************************************************************************** */ /* Types */ typedef int (*announce_t)(const g1m_mcsfile_t*, size_t*); typedef int (*encode_t)(const g1m_mcsfile_t*, g1m_buffer_t *buffer); struct corresp { /* 'identification' */ g1m_mcstype_t types; /* functions */ announce_t announce; encode_t encode; }; /* All correspondances */ static const struct corresp encodings[] = { {g1m_mcstype_list | g1m_mcstype_matrix, FUNCS(cells)}, {g1m_mcstype_program, FUNCS(program)}, {g1m_mcstype_alpha, FUNCS(var)}, /* sentinel */ {0, NULL, NULL} }; /* ************************************************************************** */ /* Default functions */ /* ************************************************************************** */ /** * default_announce: * Announce a file with no type. * * @arg handle the file handle. * @arg sz the size to contribute to. * @return the error code (0 if ok). */ static int default_announce(const g1m_mcsfile_t *handle, size_t *sz) { *sz += handle->g1m_mcsfile_head.g1m_mcshead_size; return (0); } /** * default_encode: * 'Encode' a file with no type. * * @arg handle the file handle. * @arg buffer the buffer to write to. * @return the error code (0 if ok). */ static int default_encode(const g1m_mcsfile_t *handle, g1m_buffer_t *buffer) { WRITE(handle->g1m_mcsfile_content, handle->g1m_mcsfile_head.g1m_mcshead_size) return (0); } /* default correspondance */ static struct corresp default_corresp = { 0, default_announce, default_encode}; /* ************************************************************************** */ /* Main function */ /* ************************************************************************** */ /** * g1m_encode_mcsfile: * Encode an MCS file. * * @arg handle the file handle. * @arg buffer the buffer to which to encode it. * @return the error that occured (0 if ok). */ int g1m_encode_mcsfile(g1m_mcsfile_t *handle, g1m_buffer_t *buffer) { int err; /* find the announcement and encoding function */ const struct corresp *c = encodings - 1; if (!c->types) c = &default_corresp; else { while ((++c)->encode) { if (c->types && !(c->types & handle->g1m_mcsfile_head .g1m_mcshead_type)) continue; break; } if (!c->encode) return (g1m_error_op); } /* announce if necessary */ if (buffer->g1m_buffer_announce) { size_t size = 0; if ((err = (*c->announce)(handle, &size)) || (err = (*buffer->g1m_buffer_announce)(buffer->g1m_buffer_cookie, size))) return (err); } /* encode */ if (buffer->g1m_buffer_write) { err = (*c->encode)(handle, buffer); if (err && buffer->g1m_buffer_unannounce) { (*buffer->g1m_buffer_unannounce)(buffer->g1m_buffer_cookie); return (err); } } /* everything went well! */ return (0); }