aboutsummaryrefslogtreecommitdiff
path: root/lib/mcsfile/ref/cas_data.c
blob: 6f47677b837c1715011363ec1727ce1eebafef36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* ****************************************************************************
 * mcsfile/ref/cas_data.c -- get the CAS type out of raw data.
 * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of libcasio.
 * libcasio 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.
 *
 * libcasio 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 libcasio; if not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include "ref.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;

	/* libcasio MCS file type, flags */
	casio_mcstype_t type;
	casio_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, casio_mcstype_capture, PT}
#define UNIMPLEMENTED(S) {S,    noarg, 0,                     0}
#define TTERM            {NULL, noarg, 0,                     0}
CASIO_LOCAL const struct type_corresp cas_groups[] = {
	/* basic things */
	BASIC("LT",       casio_mcstype_list),
	BASIC("MT",       casio_mcstype_matrix),
	BASIC("PG",       casio_mcstype_program),
	BASIC("VM",       casio_mcstype_variable),
	BASIC("\xFF\xFF", casio_mcstype_end),

	/* programs */
	{"P", arg | arg_is_num, casio_mcstype_program, 0},
	{"PZ", mult | noarg, casio_mcstype_program, 0}, /* 38 progs: 0-9,A-Z,... */

	/* captures */
	CAPT("DC", casio_pictureformat_4bit_color),
	CAPT("DD", casio_pictureformat_4bit_mono),

	/* decoding is being implemented */
	UNIMPLEMENTED("GF"), /* graph zoom factor (graph function?) */
	UNIMPLEMENTED("AA"), /* dynamic graph functions */
	UNIMPLEMENTED("BU"), /* backup */

	/* not implemented yet */
	UNIMPLEMENTED("AD"), /* variable memory */
	UNIMPLEMENTED("AL"), /* all */
	UNIMPLEMENTED("AM"), /* alpha variable memory */
	UNIMPLEMENTED("DM"), /* defined memory */
	UNIMPLEMENTED("EN"), /* one editor file */
	UNIMPLEMENTED("FT"), /* ??? (cafix) */
	UNIMPLEMENTED("F1"), /* one function memory */
	UNIMPLEMENTED("GM"), /* gmem (cafix) */
	UNIMPLEMENTED("GR"), /* graph range */
	UNIMPLEMENTED("GT"), /* function table */
	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) */

	/* sets */
	UNIMPLEMENTED("FN"), /* set of editor files */
	UNIMPLEMENTED("F6"), /* set of function memories */
	UNIMPLEMENTED("GA"), /* set of graph functions */
	UNIMPLEMENTED("MA"), /* set of matrices */

	/* 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).
 */

CASIO_LOCAL int get_number(const char *s, int *num)
{
	if (!(*num = atoi(s))) return (1);
	return (0);
}

/**
 *	casio_correct_mcshead_from_casdata:
 *	Get the abstract type from the CAS data type.
 *
 *	@arg	head		the head.
 *	@return				the error (if any).
 */

int CASIO_EXPORT casio_correct_mcshead_from_casdata(casio_mcshead_t *head)
{
	const struct type_corresp *c;
	const char *datatype = head->casio_mcshead_datatype;
	int id = 0;

	/* copy information */
	memcpy(head->casio_mcshead_datatype, datatype, 2);
	head->casio_mcshead_datatype[2] = 0;

	/* look for correspondance */
	for (c = cas_groups; c->type; c++) {
		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->casio_mcshead_name[pl], &id))
			continue;
		break;
	}
	if (!c->type) goto notfound;

	/* fill in info and return */
	head->casio_mcshead_type = c->type;
	head->casio_mcshead_id = id;
	head->casio_mcshead_picformat = c->picformat;
	if (c->flags & mult)
		head->casio_mcshead_flags |= casio_mcsflag_multiple;
	return (0);

notfound:
	msg((ll_info, "Type with '%.2s' data string was not implemented or not "
		"recognized.", datatype));
	head->casio_mcshead_type = 0;
	return (casio_error_op);
}

/**
 *	casio_correct_mcshead_to_cas:
 *	Correct a CAS file head.
 *
 *	@arg	head		the head to correct.
 *	@return				the error (0 if ok).
 */

int CASIO_EXPORT casio_correct_mcshead_to_casdata(casio_mcshead_t *head)
{
	const struct type_corresp *c;

	/* look for correspondance */
	for (c = cas_groups; c->type; c++) {
		if (head->casio_mcshead_type == c->type) /* FIXME: picture format? */
			goto found;
		break;
	}

	/* not found :( */
	return (casio_error_unknown);

	/* found :) */
found:
	memcpy(head->casio_mcshead_datatype, c->datatype, 2);
	/* TODO: things with IDs, etc */
	/* TODO: add app? */
	return (0);
}