1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3 
4 /*  libtifiles - file format library, a part of the TiLP project
5  *  Copyright (C) 1999-2005  Romain Lievin
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software Foundation,
19  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include "tifiles.h"
25 #include "gettext.h"
26 #include "logging.h"
27 #include "internal.h"
28 #include "typesxx.h"
29 #include "error.h"
30 #include "rwfile.h"
31 
32 
33 /********************************/
34 /* Calculator independent types */
35 /********************************/
36 
37 /**
38  * tifiles_vartype2string:
39  * @model: a calculator model.
40  * @data: a type ID.
41  *
42  * Returns the type of variable (REAL, EQU, PRGM, ...).
43  *
44  * Return value: a string like "REAL".
45  **/
tifiles_vartype2string(CalcModel model,uint8_t data)46 TIEXPORT2 const char *TICALL tifiles_vartype2string(CalcModel model, uint8_t data)
47 {
48   switch (model)
49   {
50 #ifndef DISABLE_TI8X
51   case CALC_TI73:
52     return tixx_byte2type(TI73_CONST, TI73_MAXTYPES, data);
53   case CALC_TI82:
54     return tixx_byte2type(TI82_CONST, TI82_MAXTYPES, data);
55   case CALC_TI83:
56     return tixx_byte2type(TI83_CONST, TI83_MAXTYPES, data);
57   case CALC_TI83P:
58     return ti83p_byte2type(data);
59   case CALC_TI84P:
60   case CALC_TI84P_USB:
61     return ti84p_byte2type(data);
62   case CALC_TI82A_USB:
63     return ti82a_byte2type(data);
64   case CALC_TI84PT_USB:
65     return ti84pt_byte2type(data);
66   case CALC_TI84PC:
67   case CALC_TI84PC_USB:
68     return ti84pc_byte2type(data);
69   case CALC_TI83PCE_USB:
70     return ti83pce_byte2type(data);
71   case CALC_TI84PCE_USB:
72     return ti84pce_byte2type(data);
73   case CALC_TI85:
74     return tixx_byte2type(TI85_CONST, TI85_MAXTYPES, data);
75   case CALC_TI86:
76     return tixx_byte2type(TI86_CONST, TI86_MAXTYPES, data);
77 #endif
78 #ifndef DISABLE_TI9X
79   case CALC_TI89:
80     return ti89_byte2type(data);
81   case CALC_TI89T:
82   case CALC_TI89T_USB:
83     return ti89t_byte2type(data);
84   case CALC_TI92:
85     return ti92_byte2type(data);
86   case CALC_TI92P:
87     return ti92p_byte2type(data);
88   case CALC_V200:
89     return v200_byte2type(data);
90   case CALC_NSPIRE:
91     return tixx_byte2type(NSP_CONST, NSP_MAXTYPES, data);
92 #endif
93   default:
94     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
95     return "";
96   }
97 }
98 
99 /**
100  * tifiles_string2vartype:
101  * @model: a calculator model.
102  * @s: a type as string (like "REAL").
103  *
104  * Returns the type of variable.
105  *
106  * Return value: a type ID.
107  **/
tifiles_string2vartype(CalcModel model,const char * s)108 TIEXPORT2 uint8_t TICALL tifiles_string2vartype(CalcModel model, const char *s)
109 {
110   if (s == NULL)
111   {
112     tifiles_critical("%s: invalid string !", __FUNCTION__);
113     return 0;
114   }
115 
116   switch (model)
117   {
118 #ifndef DISABLE_TI8X
119   case CALC_TI73:
120     return tixx_type2byte(TI73_CONST, TI73_MAXTYPES, s);
121   case CALC_TI82:
122     return tixx_type2byte(TI82_CONST, TI82_MAXTYPES, s);
123   case CALC_TI83:
124     return tixx_type2byte(TI83_CONST, TI83_MAXTYPES, s);
125   case CALC_TI83P:
126     return ti83p_type2byte(s);
127   case CALC_TI84P:
128   case CALC_TI84P_USB:
129     return ti84p_type2byte(s);
130   case CALC_TI82A_USB:
131     return ti82a_type2byte(s);
132   case CALC_TI84PT_USB:
133     return ti84pt_type2byte(s);
134   case CALC_TI84PC:
135   case CALC_TI84PC_USB:
136     return ti84pc_type2byte(s);
137   case CALC_TI83PCE_USB:
138     return ti83pce_type2byte(s);
139   case CALC_TI84PCE_USB:
140     return ti84pce_type2byte(s);
141   case CALC_TI85:
142     return tixx_type2byte(TI85_CONST, TI85_MAXTYPES, s);
143   case CALC_TI86:
144     return tixx_type2byte(TI86_CONST, TI86_MAXTYPES, s);
145 #endif
146 #ifndef DISABLE_TI9X
147   case CALC_TI89:
148     return ti89_type2byte(s);
149   case CALC_TI89T:
150   case CALC_TI89T_USB:
151     return ti89t_type2byte(s);
152   case CALC_TI92:
153     return ti92_type2byte(s);
154   case CALC_TI92P:
155     return ti92p_type2byte(s);
156   case CALC_V200:
157     return v200_type2byte(s);
158   case CALC_NSPIRE:
159     return tixx_type2byte(NSP_CONST, NSP_MAXTYPES, s);
160 #endif
161   default:
162     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
163     return 0;
164   }
165 }
166 
167 /**
168  * tifiles_vartype2fext:
169  * @model: a calculator model.
170  * @data: a type ID.
171  *
172  * Returns the file extension tipcially used to store this kind of variable(REAL, EQU, PRGM, ...).
173  *
174  * Return value: a string like "REAL".
175  **/
tifiles_vartype2fext(CalcModel model,uint8_t data)176 TIEXPORT2 const char *TICALL tifiles_vartype2fext(CalcModel model, uint8_t data)
177 {
178   switch (model)
179   {
180 #ifndef DISABLE_TI8X
181   case CALC_TI73:
182     return tixx_byte2fext(TI73_CONST, TI73_MAXTYPES, data, "73?");
183   case CALC_TI82:
184     return tixx_byte2fext(TI82_CONST, TI82_MAXTYPES, data, "82?");
185   case CALC_TI83:
186     return tixx_byte2fext(TI83_CONST, TI83_MAXTYPES, data, "83?");
187   case CALC_TI83P:
188     return ti83p_byte2fext(data);
189   case CALC_TI84P:
190   case CALC_TI84P_USB:
191     return ti84p_byte2fext(data);
192   case CALC_TI82A_USB:
193     return ti82a_byte2fext(data);
194   case CALC_TI84PT_USB:
195     return ti84pt_byte2fext(data);
196   case CALC_TI84PC:
197   case CALC_TI84PC_USB:
198     return ti84pc_byte2fext(data);
199   case CALC_TI83PCE_USB:
200     return ti83pce_byte2fext(data);
201   case CALC_TI84PCE_USB:
202     return ti84pce_byte2fext(data);
203   case CALC_TI85:
204     return tixx_byte2fext(TI85_CONST, TI85_MAXTYPES, data, "85?");
205   case CALC_TI86:
206     return tixx_byte2fext(TI86_CONST, TI86_MAXTYPES, data, "86?");
207 #endif
208 #ifndef DISABLE_TI9X
209   case CALC_TI89:
210     return ti89_byte2fext(data);
211   case CALC_TI89T:
212   case CALC_TI89T_USB:
213     return ti89t_byte2fext(data);
214   case CALC_TI92:
215     return ti92_byte2fext(data);
216   case CALC_TI92P:
217     return ti92p_byte2fext(data);
218   case CALC_V200:
219     return v200_byte2fext(data);
220   case CALC_NSPIRE:
221     return tixx_byte2fext(NSP_CONST, NSP_MAXTYPES, data, "tn?");
222 #endif
223   default:
224     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
225     return "";
226   }
227 }
228 
229 /**
230  * tifiles_fext2vartype:
231  * @model: a calculator model.
232  * @s: a file extension as string (like 89p).
233  *
234  * Returns the type ID of variable (REAL, EQU, PRGM, ...).
235  *
236  * Return value: a string like "PRGM".
237  **/
tifiles_fext2vartype(CalcModel model,const char * s)238 TIEXPORT2 uint8_t TICALL tifiles_fext2vartype(CalcModel model, const char *s)
239 {
240   if (s == NULL)
241   {
242     tifiles_critical("%s: invalid string !", __FUNCTION__);
243     return 0;
244   }
245 
246   switch (model)
247   {
248 #ifndef DISABLE_TI8X
249   case CALC_TI73:
250     return tixx_fext2byte(TI73_CONST, TI73_MAXTYPES, s);
251   case CALC_TI82:
252     return tixx_fext2byte(TI82_CONST, TI82_MAXTYPES, s);
253   case CALC_TI83:
254     return tixx_fext2byte(TI83_CONST, TI83_MAXTYPES, s);
255   case CALC_TI83P:
256     return ti83p_fext2byte(s);
257   case CALC_TI84P:
258   case CALC_TI84P_USB:
259     return ti84p_fext2byte(s);
260   case CALC_TI82A_USB:
261     return ti82a_fext2byte(s);
262   case CALC_TI84PT_USB:
263     return ti84pt_fext2byte(s);
264   case CALC_TI84PC:
265   case CALC_TI84PC_USB:
266     return ti84pc_fext2byte(s);
267   case CALC_TI83PCE_USB:
268     return ti83pce_fext2byte(s);
269   case CALC_TI84PCE_USB:
270     return ti84pce_fext2byte(s);
271   case CALC_TI85:
272     return tixx_fext2byte(TI85_CONST, TI85_MAXTYPES, s);
273   case CALC_TI86:
274     return tixx_fext2byte(TI86_CONST, TI86_MAXTYPES, s);
275 #endif
276 #ifndef DISABLE_TI9X
277   case CALC_TI89:
278     return ti89_fext2byte(s);
279   case CALC_TI89T:
280   case CALC_TI89T_USB:
281     return ti89t_fext2byte(s);
282   case CALC_TI92:
283     return ti92_fext2byte(s);
284   case CALC_TI92P:
285     return ti92p_fext2byte(s);
286   case CALC_V200:
287     return v200_fext2byte(s);
288   case CALC_NSPIRE:
289     return tixx_fext2byte(NSP_CONST, NSP_MAXTYPES, s);
290 #endif
291   default:
292     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
293     return 0;
294   }
295 }
296 
297 /**
298  * tifiles_vartype2type:
299  * @model: a calculator model.
300  * @id: a vartype ID.
301  *
302  * Returns the type ID of variable as string ("Real", "Program", ...).
303  * The function is localized.
304  *
305  * Return value: a string like "Assembly Program".
306  **/
tifiles_vartype2type(CalcModel model,uint8_t vartype)307 TIEXPORT2 const char *TICALL tifiles_vartype2type(CalcModel model, uint8_t vartype)
308 {
309   switch (model)
310   {
311 #ifndef DISABLE_TI8X
312   case CALC_TI73:
313     return tixx_byte2desc(TI73_CONST, TI73_MAXTYPES, vartype);
314   case CALC_TI82:
315     return tixx_byte2desc(TI82_CONST, TI82_MAXTYPES, vartype);
316   case CALC_TI83:
317     return tixx_byte2desc(TI83_CONST, TI83_MAXTYPES, vartype);
318   case CALC_TI83P:
319     return ti83p_byte2desc(vartype);
320   case CALC_TI84P:
321   case CALC_TI84P_USB:
322     return ti84p_byte2desc(vartype);
323   case CALC_TI82A_USB:
324     return ti82a_byte2desc(vartype);
325   case CALC_TI84PT_USB:
326     return ti84pt_byte2desc(vartype);
327   case CALC_TI84PC:
328   case CALC_TI84PC_USB:
329     return ti84pc_byte2desc(vartype);
330   case CALC_TI83PCE_USB:
331     return ti83pce_byte2desc(vartype);
332   case CALC_TI84PCE_USB:
333     return ti84pce_byte2desc(vartype);
334   case CALC_TI85:
335     return tixx_byte2desc(TI85_CONST, TI85_MAXTYPES, vartype);
336   case CALC_TI86:
337     return tixx_byte2desc(TI86_CONST, TI86_MAXTYPES, vartype);
338 #endif
339 #ifndef DISABLE_TI9X
340   case CALC_TI89:
341     return ti89_byte2desc(vartype);
342   case CALC_TI89T:
343   case CALC_TI89T_USB:
344     return ti89t_byte2desc(vartype);
345   case CALC_TI92:
346     return ti92_byte2desc(vartype);
347   case CALC_TI92P:
348     return ti92p_byte2desc(vartype);
349   case CALC_V200:
350     return v200_byte2desc(vartype);
351   case CALC_NSPIRE:
352     return tixx_byte2desc(NSP_CONST, NSP_MAXTYPES, vartype);
353 #endif
354   default:
355     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
356     return "";
357   }
358 }
359 
360 /**
361  * tifiles_vartype2icon:
362  * @model: a calculator model.
363  * @id: a vartype ID.
364  *
365  * Returns the type ID of variable as string ("Real", "Program", ...).
366  * Same as #tifiles_vartype2type but un-localized.
367  *
368  * Return value: a string like "Assembly Program".
369  **/
tifiles_vartype2icon(CalcModel model,uint8_t vartype)370 TIEXPORT2 const char *TICALL tifiles_vartype2icon(CalcModel model, uint8_t vartype)
371 {
372   switch (model)
373   {
374 #ifndef DISABLE_TI8X
375   case CALC_TI73:
376     return tixx_byte2icon(TI73_CONST, TI73_MAXTYPES, vartype);
377   case CALC_TI82:
378     return tixx_byte2icon(TI82_CONST, TI82_MAXTYPES, vartype);
379   case CALC_TI83:
380     return tixx_byte2icon(TI83_CONST, TI83_MAXTYPES, vartype);
381   case CALC_TI83P:
382     return ti83p_byte2icon(vartype);
383   case CALC_TI84P:
384   case CALC_TI84P_USB:
385     return ti84p_byte2icon(vartype);
386   case CALC_TI82A_USB:
387     return ti82a_byte2icon(vartype);
388   case CALC_TI84PT_USB:
389     return ti84pt_byte2icon(vartype);
390   case CALC_TI84PC:
391   case CALC_TI84PC_USB:
392     return ti84pc_byte2icon(vartype);
393   case CALC_TI83PCE_USB:
394     return ti83pce_byte2icon(vartype);
395   case CALC_TI84PCE_USB:
396     return ti84pce_byte2icon(vartype);
397   case CALC_TI85:
398     return tixx_byte2icon(TI85_CONST, TI85_MAXTYPES, vartype);
399   case CALC_TI86:
400     return tixx_byte2icon(TI86_CONST, TI86_MAXTYPES, vartype);
401 #endif
402 #ifndef DISABLE_TI9X
403   case CALC_TI89:
404     return ti89_byte2icon(vartype);
405   case CALC_TI89T:
406   case CALC_TI89T_USB:
407     return ti89t_byte2icon(vartype);
408   case CALC_TI92:
409     return ti92_byte2icon(vartype);
410   case CALC_TI92P:
411     return ti92p_byte2icon(vartype);
412   case CALC_V200:
413     return v200_byte2icon(vartype);
414   case CALC_NSPIRE:
415     return tixx_byte2icon(NSP_CONST, NSP_MAXTYPES, vartype);
416 #endif
417   default:
418     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
419     return "";
420   }
421 }
422 
423 /****************/
424 /* Global types */
425 /****************/
426 
427 /**
428  * tifiles_folder_type
429  * @model: a calculator model in #CalcModel enumeration.
430  *
431  * Returns the variable type ID used for encoding folders.
432  *
433  * Return value: a type ID.
434  **/
tifiles_folder_type(CalcModel model)435 TIEXPORT2 uint8_t TICALL tifiles_folder_type(CalcModel model)
436 {
437   switch (model)
438   {
439   case CALC_NONE:
440     return -1;
441   case CALC_TI73:
442     return TI73_DIR;
443   case CALC_TI80:
444     return -1;
445   case CALC_TI82:
446     return -1;
447   case CALC_TI83:
448     return TI83_DIR;
449   case CALC_TI83P:
450   case CALC_TI84P:
451   case CALC_TI84PC:
452   case CALC_TI84P_USB:
453   case CALC_TI84PC_USB:
454   case CALC_TI83PCE_USB:
455   case CALC_TI84PCE_USB:
456   case CALC_TI82A_USB:
457   case CALC_TI84PT_USB:
458     return TI83p_DIR;
459   case CALC_TI85:
460     return -1;
461   case CALC_TI86:
462     return TI86_DIR;
463   case CALC_TI89:
464   case CALC_TI89T:
465   case CALC_TI89T_USB:
466     return TI89_DIR;
467   case CALC_TI92:
468     return TI92_DIR;
469   case CALC_TI92P:
470     return TI92p_DIR;
471   case CALC_V200:
472     return V200_DIR;
473   case CALC_NSPIRE:
474     return NSP_DIR;
475   default:
476     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
477     break;
478   }
479 
480   return -1;
481 }
482 
483 /**
484  * tifiles_flash_type
485  * @model: a calculator model in #CalcModel enumeration.
486  *
487  * Returns the variable type ID used for encoding FLASH apps.
488  *
489  * Return value: a type ID.
490  **/
tifiles_flash_type(CalcModel model)491 TIEXPORT2 uint8_t TICALL tifiles_flash_type(CalcModel model)
492 {
493   switch (model)
494   {
495   case CALC_NONE:
496     return -1;
497   case CALC_TI73:
498     return TI73_APPL;
499   case CALC_TI80:
500     return -1;
501   case CALC_TI82:
502     return -1;
503   case CALC_TI83:
504     return -1;
505   case CALC_TI83P:
506   case CALC_TI84P:
507   case CALC_TI84PC:
508   case CALC_TI84P_USB:
509   case CALC_TI84PC_USB:
510   case CALC_TI83PCE_USB:
511   case CALC_TI84PCE_USB:
512     return TI83p_APPL;
513   case CALC_TI82A_USB:
514   case CALC_TI84PT_USB:
515     return -1;
516   case CALC_TI85:
517     return -1;
518   case CALC_TI86:
519     return -1;
520   case CALC_TI89:
521   case CALC_TI89T:
522   case CALC_TI89T_USB:
523     return TI89_APPL;
524   case CALC_TI92:
525     return -1;
526   case CALC_TI92P:
527     return TI92p_APPL;
528   case CALC_V200:
529     return V200_APPL;
530   case CALC_NSPIRE:
531     return -1;
532   default:
533     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
534     break;
535   }
536 
537   return -1;
538 }
539 
540 /**
541  * tifiles_idlist_type
542  * @model: a calculator model in #CalcModel enumeration.
543  *
544  * Returns the variable type ID used for encoding IDLIST variable.
545  *
546  * Return value: a type ID.
547  **/
tifiles_idlist_type(CalcModel model)548 TIEXPORT2 uint8_t TICALL tifiles_idlist_type(CalcModel model)
549 {
550   switch (model)
551   {
552   case CALC_NONE:
553     return -1;
554   case CALC_TI73:
555     return TI73_IDLIST;
556   case CALC_TI80:
557     return -1;
558   case CALC_TI82:
559     return -1;
560   case CALC_TI83:
561     return -1;
562   case CALC_TI83P:
563   case CALC_TI84P:
564   case CALC_TI84PC:
565   case CALC_TI84P_USB:
566   case CALC_TI84PC_USB:
567   case CALC_TI83PCE_USB:
568   case CALC_TI84PCE_USB:
569   case CALC_TI82A_USB:
570   case CALC_TI84PT_USB:
571     return TI83p_IDLIST;
572   case CALC_TI85:
573     return -1;
574   case CALC_TI86:
575     return -1;
576   case CALC_TI89:
577   case CALC_TI89T:
578   case CALC_TI89T_USB:
579     return TI89_IDLIST;
580   case CALC_TI92:
581     return -1;
582   case CALC_TI92P:
583     return TI92p_IDLIST;
584   case CALC_V200:
585     return V200_IDLIST;
586   case CALC_NSPIRE:
587     return -1;
588   default:
589     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
590     break;
591   }
592 
593   return -1;
594 }
595 
596 /*****************/
597 /* Miscellaneous */
598 /*****************/
599 
600 /**
601  * tifiles_calctype2signature:
602  * @model: a calculator model.
603  *
604  * Returns the signature used at the top of a TI file depending on the
605  * calculator model.
606  *
607  * Return value: a string like "**TI89**".
608  **/
tifiles_calctype2signature(CalcModel model)609 TIEXPORT2 const char *TICALL tifiles_calctype2signature(CalcModel model)
610 {
611   switch (model)
612   {
613   case CALC_NONE:
614     return "**TI??**";
615   case CALC_TI73:
616     return "**TI73**";
617   case CALC_TI82:
618     return "**TI82**";
619   case CALC_TI83:
620     return "**TI83**";
621   case CALC_TI83P:
622   case CALC_TI84P:
623   case CALC_TI84PC:
624   case CALC_TI84P_USB:
625   case CALC_TI84PC_USB:
626   case CALC_TI83PCE_USB:
627   case CALC_TI84PCE_USB:
628   case CALC_TI82A_USB:
629   case CALC_TI84PT_USB:
630     return "**TI83F*";
631   case CALC_TI85:
632     return "**TI85**";
633   case CALC_TI86:
634     return "**TI86**";
635   case CALC_TI89:
636     return "**TI89**";
637   case CALC_TI89T:
638   case CALC_TI89T_USB:
639     return "**TI89**";
640   case CALC_TI92:
641     return "**TI92**";
642   case CALC_TI92P:
643   case CALC_V200:
644     return "**TI92P*";
645   case CALC_NSPIRE:
646     return "";
647   default:
648     tifiles_critical("%s: invalid model argument.", __FUNCTION__);
649     break;
650   }
651 
652   return NULL;
653 }
654 
655 /**
656  * tifiles_signature2calctype:
657  * @s: a TI file signature like "**TI89**".
658  *
659  * Returns the calculator model contained in the signature.
660  *
661  * Return value: a calculator model.
662  **/
tifiles_signature2calctype(const char * s)663 TIEXPORT2 CalcModel TICALL tifiles_signature2calctype(const char *s)
664 {
665   if (s != NULL)
666   {
667     if (!g_ascii_strcasecmp(s, "**TI73**"))
668       return CALC_TI73;
669     else if (!g_ascii_strcasecmp(s, "**TI82**"))
670       return CALC_TI82;
671     else if (!g_ascii_strcasecmp(s, "**TI83**"))
672       return CALC_TI83;
673     else if (!g_ascii_strcasecmp(s, "**TI83F*"))
674       return CALC_TI83P;
675     else if (!g_ascii_strcasecmp(s, "**TI85**"))
676       return CALC_TI85;
677     else if (!g_ascii_strcasecmp(s, "**TI86**"))
678       return CALC_TI86;
679     else if (!g_ascii_strcasecmp(s, "**TI89**"))
680       return CALC_TI89;
681     else if (!g_ascii_strcasecmp(s, "**TI92**"))
682       return CALC_TI92;
683     else if (!g_ascii_strcasecmp(s, "**TI92P*"))
684       return CALC_TI92P;
685     else if (!g_ascii_strcasecmp(s, "**V200**"))
686       return CALC_V200;
687   }
688   tifiles_critical("%s: invalid signature.", __FUNCTION__);
689   return CALC_NONE;
690 }
691