90 lines
2.7 KiB
C
90 lines
2.7 KiB
C
/* *****************************************************************************
|
|
* decode/mcs/list.c -- decode an MCS list.
|
|
* 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>
|
|
|
|
/**
|
|
* g1m_decode_mcs_list:
|
|
* Decode an List.
|
|
*
|
|
* @arg handle the handle to make.
|
|
* @arg buffer the buffer to read from.
|
|
* @arg head the pre-filled head to complete and use.
|
|
* @return the error code (0 if ok).
|
|
*/
|
|
|
|
int g1m_decode_mcs_list(g1m_mcsfile_t **handle, g1m_buffer_t *buffer,
|
|
g1m_mcshead_t *head)
|
|
{
|
|
/* read header */
|
|
DREAD(hd, mcs_listheader)
|
|
uint_fast16_t elcount = be16toh(hd.element_count);
|
|
log_info("Title is '%.8s'", hd.title);
|
|
|
|
/* make final head */
|
|
head->width = 1;
|
|
head->height = elcount;
|
|
int err = g1m_make_mcsfile(handle, head);
|
|
if (err) return (err);
|
|
|
|
/* main copying loop */
|
|
g1m_mcscell_t **tab = (*handle)->cells; int one_imgn = 0;
|
|
for (uint_fast32_t y = 0; y < elcount; y++) {
|
|
g1m_bcd_t bcd; GDREAD(rawbcd, bcd)
|
|
one_imgn |= g1m_bcd_frommcs(&rawbcd, &bcd);
|
|
|
|
/* prepare index and store */
|
|
tab[y][0] = (g1m_mcscell_t){
|
|
.real = bcd,
|
|
.imgn = {},
|
|
.flags = g1m_mcscellflag_used
|
|
};
|
|
}
|
|
|
|
/* main copying loop for imaginary parts */
|
|
if (one_imgn) for (uint_fast32_t y = 0; y < elcount; y++) {
|
|
g1m_bcd_t bcd; GDREAD(rawbcd, bcd)
|
|
if (g1m_bcd_has_special(&tab[y][0].real)) {
|
|
g1m_bcd_frommcs(&rawbcd, &bcd);
|
|
tab[y][0].imgn = bcd;
|
|
}
|
|
}
|
|
|
|
#if LOGLEVEL <= ll_info
|
|
/* logging loop */
|
|
char rbuf[G1M_BCD_GOODBUFSIZE], ibuf[G1M_BCD_GOODBUFSIZE];
|
|
for (uint_fast32_t y = 0; y < elcount; y++) {
|
|
g1m_bcdtoa(&tab[y][0].real, rbuf, G1M_BCD_GOODBUFSIZE);
|
|
if (g1m_bcd_has_special(&tab[y][0].real)) {
|
|
g1m_bcdtoa(&tab[y][0].imgn, ibuf, G1M_BCD_GOODBUFSIZE);
|
|
log_info("[%" PRIuFAST32 "] %si + %s", y, ibuf, rbuf);
|
|
} else
|
|
log_info("[%" PRIuFAST32 "] %s", y, rbuf);
|
|
}
|
|
#endif
|
|
|
|
/* no error */
|
|
return (0);
|
|
|
|
/* in case */
|
|
fail:
|
|
g1m_free_mcsfile(*handle);
|
|
*handle = NULL;
|
|
return (err);
|
|
}
|