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/type/cas.c

164 lines
5.5 KiB
C

/* *****************************************************************************
* type/cas.c -- get the CAS type and data types out of raw identification data.
* 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>
/* ************************************************************************** */
/* Local types */
/* ************************************************************************** */
/* Flags */
#define mult 0x0001
#define noarg 0x0000
#define arg 0x0002
#define arg_is_num 0x0000
/* Correspondance type */
struct type_corresp {
/* identification */
const char *datatype;
unsigned int flags;
/* libg1m MCS file type, flags */
g1m_mcstype_t type;
g1m_pictureformat_t picformat;
};
/* ************************************************************************** */
/* Correspondances */
/* ************************************************************************** */
/* All correspondances found by Tom Wheeley, Tom Lynn (creators of CaS),
* and Göran Weinholt (creator of Cafix). */
#define BASIC(S, T) {S, noarg, T, 0}
#define CAPT(S, PT) {S, noarg, g1m_mcstype_capture, PT}
#define UNIMPLEMENTED(S) {S, noarg, 0, 0}
#define TTERM {NULL, noarg, 0, 0}
static struct type_corresp cas_groups[] = {
/* basic things */
BASIC("LT", g1m_mcstype_list),
BASIC("MT", g1m_mcstype_matrix),
BASIC("PG", g1m_mcstype_program),
BASIC("VM", g1m_mcstype_variable),
/* programs */
{"P", arg | arg_is_num, g1m_mcstype_program, 0},
{"PZ", mult | noarg, g1m_mcstype_program, 0},
/* captures */
CAPT("DC", g1m_pictureformat_4bit_color),
CAPT("DD", g1m_pictureformat_4bit_mono),
/* not implemented yet */
UNIMPLEMENTED("AA"), // dynamic graph functions
UNIMPLEMENTED("AD"), // variable memory
UNIMPLEMENTED("AL"), // all
UNIMPLEMENTED("AM"), // alpha variable memory
UNIMPLEMENTED("BU"), // backup
UNIMPLEMENTED("DM"), // defined memory
UNIMPLEMENTED("EN"), // one editor file
UNIMPLEMENTED("FN"), // set of editor files
UNIMPLEMENTED("FT"), // ??? (cafix)
UNIMPLEMENTED("F1"), // one function memory
UNIMPLEMENTED("F6"), // set of function memories
UNIMPLEMENTED("GA"), // set of graph functions
UNIMPLEMENTED("GF"), // graph zoom factor (graph function?)
UNIMPLEMENTED("GM"), // gmem (cafix)
UNIMPLEMENTED("GR"), // graph range
UNIMPLEMENTED("GT"), // function table
UNIMPLEMENTED("MA"), // set of matrices
UNIMPLEMENTED("PC"), // picture from 9xxx (cafix)
UNIMPLEMENTED("PD"), // polynomial equations
UNIMPLEMENTED("RF"), // ??? (cafix)
UNIMPLEMENTED("RR"), // ??? (cafix)
UNIMPLEMENTED("RT"), // recursion table
UNIMPLEMENTED("SD"), // simultaneous equations
UNIMPLEMENTED("SE"), // equation (cafix)
UNIMPLEMENTED("SR"), // linear regression data
UNIMPLEMENTED("SS"), // standard deviation data
UNIMPLEMENTED("TR"), // ??? (cafix)
UNIMPLEMENTED("WD"), // window data (cafix)
/* terminating entry */
TTERM
};
/* ************************************************************************** */
/* Main functions */
/* ************************************************************************** */
/**
* get_number:
* Get number from string.
*
* Adapted from `type/mcs.c`. If you need letter ID decoding or other
* stuff, go and see there.
*
* @arg s the string.
* @arg num pointer to the num to fill.
* @return if there was an error (0 if ok).
*/
static int get_number(const char *s, int *num)
{
if (!(*num = atoi(s))) return (1);
return (0);
}
/**
* g1m_maketype_cas:
* Get the libg1m MCS type from raw CAS identification data.
*
* @arg head the head to fill.
* @arg datatype the data type string (2 bytes long).
* @return the error (if any).
*/
int g1m_maketype_cas(g1m_mcshead_t *head, const char *datatype)
{
/* copy information */
memset(head, 0, sizeof(g1m_mcshead_t));
head->flags |= g1m_mcsinfo_cas;
memcpy(head->_datatype, datatype, 2);
/* look for correspondance */
struct type_corresp *c = cas_groups - 1; int id = 0;
while ((++c)->type) {
size_t pl = strlen(c->datatype);
/* check if pattern is there */
if (strncmp(c->datatype, datatype, pl))
continue;
if ((c->flags & arg) && get_number(&head->name[pl], &id))
continue;
break;
}
if (!c->type) goto notfound;
/* fill in info and return */
head->type = c->type;
head->id = id;
head->_picformat = c->picformat;
if (c->flags & mult) head->flags |= g1m_mcsflag_multiple;
return (0);
notfound:
log_info("Type with '%.2s' data string was not implemented or not "
"recognized.", datatype);
head->type = 0;
return (1);
}