cake
/
libg1m
Archived
1
0
Fork 0

More special conditions and things

This commit is contained in:
Thomas Touhey 2016-12-10 14:57:28 +01:00
parent 6a0a54c71c
commit 1db76edb8f
4 changed files with 101 additions and 124 deletions

View File

@ -94,13 +94,10 @@ struct mcs_programheader {
};
/* And, well, spreadsheets are more complicated than that.
* There are different types of spreadsheets: */
enum mcs_spreadsheet_type {
mcs_spstype_normal = 0x01
};
/* And for all of them, there is a header: */
* There are different types of spreadsheets, and I don't yet know how to
* parse them all or differenciate them.
*
* For normal ones, there is a header: */
struct mcs_spreadsheetheader {
/* spreadsheet type */

View File

@ -26,12 +26,12 @@
/* ************************************************************************** */
/* read with EOF check */
#define READ(TO, SZ) { \
size_t READ_size = fread(TO, 1, SZ, stream); \
if (!READ_size) { \
log_error("READING failed: read %zu/%zu bytes, %zu missing.", \
READ_size, SZ, (SZ) - READ_size); \
return (g1m_error_eof); \
}}
size_t READ_size = fread(TO, 1, SZ, stream); \
if (!READ_size) { \
log_error("READING failed: read %zu/%zu bytes, %zu missing.", \
READ_size, SZ, (SZ) - READ_size); \
return (g1m_error_eof); \
}}
#define GREAD(TO, SZ) { \
size_t READ_size = fread(TO, 1, SZ, stream); \
if (!READ_size) { \

View File

@ -123,28 +123,3 @@ void g1m_log_mem(const char *prefx, void *m, size_t n)
n -= min(16, n);
}
}
/* ************************************************************************** */
/* Enumerations strings */
/* ************************************************************************** */
/**
* g1m_get_eact_ltype_string:
* Get string description of a type of an E-Act line.
*
* @arg code the code.
* @return the string.
*/
const char *g1m_get_eact_ltype_string(int code)
{
switch (code) {
case eact_ltype_calc: return ("calculation");
case eact_ltype_calc_result: return ("calculation result");
case eact_ltype_content: return ("content");
case eact_ltype_stdheading: return ("standard heading");
case eact_ltype_picture: return ("picture");
case eact_ltype_text: return ("pure text");
case eact_ltype_code: return ("text mixed with functions");
default: return ("unknown");
}
}

View File

@ -67,7 +67,7 @@ static int get_image(FILE *stream, uint32_t ***img,
/* MCS file parsing */
/* ************************************************************************** */
/**
* g1m_parse_mcs_program:
* mcs_parse_program:
* Parse a program.
*
* @arg handle the handle.
@ -76,7 +76,7 @@ static int get_image(FILE *stream, uint32_t ***img,
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_program(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_program(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t length)
{
/* read header */
@ -102,7 +102,7 @@ static int g1m_parse_mcs_program(g1m_mcsfile_t *handle, FILE *stream,
}
/**
* g1m_parse_mcs_spreadsheet:
* mcs_parse_spreadsheet:
* Parse a spreadsheet.
*
* @arg handle the handle.
@ -111,7 +111,7 @@ static int g1m_parse_mcs_program(g1m_mcsfile_t *handle, FILE *stream,
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_spreadsheet(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_spreadsheet(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t length)
{
/* read header */
@ -119,7 +119,7 @@ static int g1m_parse_mcs_spreadsheet(g1m_mcsfile_t *handle, FILE *stream,
logm_info(&hd, sizeof(struct mcs_spreadsheetheader));
/* check if the spreadsheet type is known */
if (hd.type != mcs_spstype_normal) {
if (hd.type != 0x01) {
log_info("Spreadsheet type is unknown. (0x%02x)", hd.type);
size_t leftsize = length - sizeof(struct mcs_spreadsheetheader);
@ -142,18 +142,11 @@ static int g1m_parse_mcs_spreadsheet(g1m_mcsfile_t *handle, FILE *stream,
/* read subheader */
DREAD(shd, mcs_spreadsheet_subheader)
/* correct endianess */
/* correct endianness */
uint_fast32_t colcount = hd.column_count;
colcount = be32toh(colcount << 8);
shd.defs_size = be32toh(shd.defs_size);
/* log some info */
log_info("%ld columns to parse!", colcount);
/* get the column directory */
uint32_t column_directory[colcount];
READ(&column_directory, sizeof(uint32_t) * colcount)
/* prepare */
g1m_mcs_cell_t cells[1000 * colcount];
bzero(cells, sizeof(g1m_mcs_cell_t) * 1000 * colcount);
@ -161,48 +154,57 @@ static int g1m_parse_mcs_spreadsheet(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t rows = 0;
uint_fast32_t cols = 0;
/* browse columns */
for (uint_fast32_t c = 0; c < colcount; c++) {
/* check if column is empty */
if (!column_directory[c])
continue;
/* log some info */
log_info("%ld columns to parse!", colcount);
/* get the row directory */
uint8_t row_directory[0x80];
READ(&row_directory, (size_t)0x80)
if (colcount) {
/* get the column directory */
uint32_t column_directory[colcount];
READ(&column_directory, sizeof(uint32_t) * colcount)
/* initialize loop values */
uint8_t *rd = row_directory;
int bit = 1 << 7;
/* browse columns */
for (uint_fast32_t c = 0; c < colcount; c++) {
/* check if column is empty */
if (!column_directory[c])
continue;
/* explore each cell */
for (uint_fast32_t i = 0; i < 1000; i++) {
/* check if used */
if (*rd & bit) {
/* get cell */
DREAD(cell, bcd)
/* get the row directory */
uint8_t row_directory[0x80];
READ(&row_directory, (size_t)0x80)
/* store it */
cells[c * 1000 + i] = (g1m_mcs_cell_t){
.used = 1,
.real = cell,
.imgn = {}
};
/* initialize loop values */
uint8_t *rd = row_directory;
int bit = 1 << 7;
/* check things (max row, max col, cells count) */
rows = max(rows, i);
cols = c;
cells_count++;
/* explore each cell */
for (uint_fast32_t i = 0; i < 1000; i++) {
/* check if used */
if (*rd & bit) {
/* get cell */
DREAD(cell, bcd)
/* log it */
char *bcd_string = g1m_bcdtoa(&cell);
log_info("[%ld, %ld] contains '%s'.", c, i, bcd_string);
free(bcd_string);
/* store it */
cells[c * 1000 + i] = (g1m_mcs_cell_t){
.used = 1,
.real = cell,
.imgn = {}
};
/* check things (max row, max col, cells count) */
rows = max(rows, i);
cols = c;
cells_count++;
/* log it */
char *bcd_string = g1m_bcdtoa(&cell);
log_info("[%ld, %ld] contains '%s'.", c, i, bcd_string);
free(bcd_string);
}
/* iterate bit and rd */
rd += (bit & 1);
bit = (bit >> 1) | ((bit & 1) << 7);
}
/* iterate bit and rd */
rd += (bit & 1);
bit = (bit >> 1) | ((bit & 1) << 7);
}
}
@ -242,7 +244,7 @@ static int g1m_parse_mcs_spreadsheet(g1m_mcsfile_t *handle, FILE *stream,
}
/**
* g1m_parse_mcs_list:
* mcs_parse_list:
* Parse an List.
*
* @arg handle the handle.
@ -251,7 +253,7 @@ static int g1m_parse_mcs_spreadsheet(g1m_mcsfile_t *handle, FILE *stream,
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_list(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_list(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t size)
{
/* read header */
@ -272,21 +274,23 @@ static int g1m_parse_mcs_list(g1m_mcsfile_t *handle, FILE *stream,
struct bcd real[elcount];
struct bcd imgn[elcount];
/* browse real elements */
log_info("Browsing %d list elements", elcount);
READ(&real, elsize)
size -= elsize;
if (elcount) {
/* browse real elements */
log_info("Browsing %d list elements", elcount);
READ(&real, elsize)
size -= elsize;
/* check if there is one imaginary number */
int one_imgn = 0;
for (uint_fast16_t i = 0; i < elcount && !one_imgn; i++)
one_imgn |= g1m_bcd_has_complex(&real[i]);
/* check if there is one imaginary number */
int one_imgn = 0;
for (uint_fast16_t i = 0; i < elcount && !one_imgn; i++)
one_imgn |= g1m_bcd_has_complex(&real[i]);
/* browse imaginary elements */
if (one_imgn) {
log_info("They have complex parts!");
size_t toread = min(size, elsize);
READ(&imgn, toread)
/* browse imaginary elements */
if (one_imgn) {
log_info("They have complex parts!");
size_t toread = min(size, elsize);
READ(&imgn, toread)
}
}
/* fill final tab */
@ -335,8 +339,8 @@ static int g1m_parse_mcs_list(g1m_mcsfile_t *handle, FILE *stream,
}
/**
* g1m_parse_mcs_mat:
* Parse a Matrix.
* mcs_parse_matrix:
* Parse a matrix.
*
* @arg handle the handle.
* @arg stream the stream to parse from.
@ -344,7 +348,7 @@ static int g1m_parse_mcs_list(g1m_mcsfile_t *handle, FILE *stream,
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_mat(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_matrix(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t length)
{
g1m_mcs_cell_t **tab, *rws;
@ -407,7 +411,7 @@ fail:
}
/**
* g1m_parse_mcs_capture:
* mcs_parse_capture:
* Parse a capture.
*
* @arg handle the handle.
@ -416,7 +420,7 @@ fail:
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_capture(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_capture(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t length)
{
/* read header */
@ -438,7 +442,7 @@ static int g1m_parse_mcs_capture(g1m_mcsfile_t *handle, FILE *stream,
}
/**
* g1m_parse_mcs_picture:
* mcs_parse_picture:
* Parse a picture.
*
* @arg handle the handle.
@ -447,7 +451,7 @@ static int g1m_parse_mcs_capture(g1m_mcsfile_t *handle, FILE *stream,
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_picture(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_picture(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t length)
{
int err;
@ -473,7 +477,7 @@ static int g1m_parse_mcs_picture(g1m_mcsfile_t *handle, FILE *stream,
}
/**
* g1m_parse_mcs_string:
* mcs_parse_string:
* Parse string.
*
* @arg handle the handle.
@ -482,7 +486,7 @@ static int g1m_parse_mcs_picture(g1m_mcsfile_t *handle, FILE *stream,
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_string(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_string(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t length)
{
(void)handle;
@ -495,7 +499,7 @@ static int g1m_parse_mcs_string(g1m_mcsfile_t *handle, FILE *stream,
}
/**
* g1m_parse_mcs_setup:
* mcs_parse_setup:
* Parse settings.
*
* @arg handle the handle.
@ -504,7 +508,7 @@ static int g1m_parse_mcs_string(g1m_mcsfile_t *handle, FILE *stream,
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_setup(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_setup(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t length)
{
(void)handle;
@ -515,7 +519,7 @@ static int g1m_parse_mcs_setup(g1m_mcsfile_t *handle, FILE *stream,
}
/**
* g1m_parse_mcs_alphamem:
* mcs_parse_alphamem:
* Parse alphamem.
*
* @arg handle the handle.
@ -524,7 +528,7 @@ static int g1m_parse_mcs_setup(g1m_mcsfile_t *handle, FILE *stream,
* @return the error code (0 if ok).
*/
static int g1m_parse_mcs_alphamem(g1m_mcsfile_t *handle, FILE *stream,
static int mcs_parse_alphamem(g1m_mcsfile_t *handle, FILE *stream,
uint_fast32_t length)
{
(void)handle;
@ -549,31 +553,31 @@ struct mcs_type_corresp {
/* All correspondances */
static struct mcs_type_corresp mcs_types[] = {
{mcs_ftype_program, g1m_mcstype_program,
&g1m_parse_mcs_program,
mcs_parse_program,
"program"},
{mcs_ftype_list, g1m_mcstype_list,
&g1m_parse_mcs_list,
mcs_parse_list,
"list"},
{mcs_ftype_mat, g1m_mcstype_mat,
&g1m_parse_mcs_mat,
mcs_parse_matrix,
"matrix"},
{mcs_ftype_spreadsheet, g1m_mcstype_spreadsheet,
&g1m_parse_mcs_spreadsheet,
mcs_parse_spreadsheet,
"spreadsheet"},
{mcs_ftype_picture, g1m_mcstype_pict,
&g1m_parse_mcs_picture,
mcs_parse_picture,
"picture"},
{mcs_ftype_capture, g1m_mcstype_capt,
&g1m_parse_mcs_capture,
mcs_parse_capture,
"capture"},
{mcs_ftype_string, 0x00, /* TODO */
&g1m_parse_mcs_string,
mcs_parse_string,
"string"},
{mcs_ftype_setup, 0x00, /* TODO */
&g1m_parse_mcs_setup,
mcs_parse_setup,
"setup"},
{mcs_ftype_alphamem, 0x00, /* TODO */
&g1m_parse_mcs_alphamem,
mcs_parse_alphamem,
"alphamem"},
{}
};
@ -678,6 +682,7 @@ int g1m_parse_mcs(g1m_t *handle, FILE *stream,
/* log info about part */
log_info("[%d] Internal name is '%.16s'", i, hd.intname);
log_info("[%d] %d mcs files to browse", i, hd.subcount);
/* check mcs tab length - in case of 0-files mcs */
if (i >= handle->_mcs_size) {
@ -709,7 +714,7 @@ int g1m_parse_mcs(g1m_t *handle, FILE *stream,
bzero(mcs->files, sizeof(g1m_mcsfile_t*) * hd.subcount);
/* foreach subpart */
log_info("[%d] %d mcs files to browse", i, hd.subcount);
log_info("[%d] starting to browse them!", i);
for (int j = 0; j < (int)hd.subcount; j++) {
/* get the part header */
GDREAD(fhd, mcs_fileheader)