ttbin2oth.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /******************************************************************************
  2. *
  3. * project name: TI-68k Developer Utilities (formerly TIGCC Tools Suite)
  4. * file name: ttbin2oth.c
  5. * initial date: 14/08/2000
  6. * author: thomas.nussbaumer@gmx.net
  7. * description: enwraps inputfile into a custom TI89 and TI92 variable
  8. *
  9. ******************************************************************************/
  10. /*
  11. This file is part of TI-68k Developer Utilities.
  12. This file is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU Lesser General Public
  14. License as published by the Free Software Foundation; either
  15. version 2.1 of the License, or (at your option) any later version.
  16. As a special exception, UNMODIFIED copies of ttbin2oth may also be
  17. redistributed or sold without source code, for any purpose. (The Lesser
  18. General Public License restrictions do apply in other respects; for example,
  19. they cover modification of the program.) This exception notice must be
  20. removed on modified copies of this file.
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. Lesser General Public License for more details.
  25. You should have received a copy of the GNU Lesser General Public
  26. License along with this library; if not, write to the Free Software
  27. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #ifndef __TTBIN2OTH__
  30. #define __TTBIN2OTH__
  31. // if EMBEDDED_USE is defined, than we use this sourcefile from within another
  32. // sourcefile
  33. #ifndef EMBEDDED_USE
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include "ttversion.h"
  38. #include "revtools.h"
  39. #include "tt.h"
  40. #include "strhead.h"
  41. #ifdef FILE_REVISION
  42. #undef FILE_REVISION
  43. #endif
  44. #define FILE_REVISION "1.11"
  45. //=============================================================================
  46. // outputs usage information of this tool
  47. //=============================================================================
  48. void PrintUsage() {
  49. #ifndef EMBEDDED_USE
  50. PRINT_ID("TTBin2OTH");
  51. #endif
  52. fprintf(USAGE_OUT, "Usage: ttbin2oth [flags] <-89 | -92> <extension> <infile> <varname> [folder]\n\n" \
  53. " -quiet ... don't output standard messages\n" \
  54. " -strip ... MUST be set if the input is already a TI file\n" \
  55. " (generates a temporary file called bin2oth.tmp for\n" \
  56. " the stripped inputfile)\n" \
  57. " -89 ... generate a TI89 file\n" \
  58. " -92 ... generate a TI92p file\n\n" \
  59. " extension ... on-calc extension (up to 4 characters)\n" \
  60. " varname ... on-calc variable name (up to 8 characters)\n" \
  61. " folder ... on-calc destination folder (OPTIONAL)\n\n" \
  62. " enwraps file into a custom TI89 or TI92p variable with given <extension>\n"\
  63. " NOTE1: the extension length may vary between 1 and 4 characters\n" \
  64. " NOTE2: the output filename is generated by taking the given varname and\n" \
  65. " appending \".89y\" or \".9xy\" to it.\n" \
  66. " NOTE3: if a generated file CAN'T BE UPLOADED, you are probably using a\n" \
  67. " reserved AMS name for your variable. Just try another name in\n" \
  68. " this case.\n\n");
  69. }
  70. #endif
  71. #include "bin2oth.c"
  72. #ifndef EMBEDDED_USE
  73. #define EMBEDDED_USE
  74. #include "ttstrip.c"
  75. #undef EMBEDDED_USE
  76. #else
  77. #include "ttstrip.c"
  78. #endif
  79. //=============================================================================
  80. // ... just a stupid main ...
  81. //=============================================================================
  82. #ifndef EMBEDDED_USE
  83. int main(int argc,char *argv[]) {
  84. #else
  85. int TTBin2OTH(int argc,char *argv[]) {
  86. #endif
  87. char* infile = NULL;
  88. char* folder = NULL;
  89. char* name = NULL;
  90. char* extension = NULL;
  91. int calctype = -1;
  92. int strip = 0;
  93. FILE* ifp;
  94. FILE* ofp;
  95. char outfile[1024];
  96. unsigned char* buffer;
  97. int length;
  98. unsigned char* outbuffer;
  99. unsigned int outlength;
  100. int n;
  101. char* tmpfilename = "bin2oth.tmp";
  102. int quiet = 0;
  103. // check for too less arguments
  104. if (argc < 4) {
  105. #ifndef EMBEDDED_USE
  106. PrintUsage();
  107. #endif
  108. return 1;
  109. }
  110. // parse arguments
  111. for (n=1; n<argc; n++) {
  112. if (!strcmp(argv[n], "-89")) calctype = CALC_TI89;
  113. else if (!strcmp(argv[n], "-92")) calctype = CALC_TI92P;
  114. else if (!strcmp(argv[n], "-strip")) strip = 1;
  115. else if (!strcmp(argv[n], "-quiet")) quiet = 1;
  116. else if (argv[n][0] == '-') {
  117. fprintf(stderr,"ERROR: invalid option %s",argv[n]);
  118. return 1;
  119. }
  120. else if (!extension) extension = argv[n];
  121. else if (!infile) infile = argv[n];
  122. else if (!name) name = argv[n];
  123. else if (!folder) folder = argv[n];
  124. else {
  125. #ifndef EMBEDDED_USE
  126. PrintUsage();
  127. #endif
  128. return 1;
  129. }
  130. }
  131. // check if all necessary arguments are supplied
  132. if (!infile || !name || calctype == -1 || !extension) {
  133. #ifndef EMBEDDED_USE
  134. PrintUsage();
  135. #endif
  136. return 1;
  137. }
  138. n = strlen(extension);
  139. if (n<1 || n>4) {
  140. fprintf(stderr,"ERROR: extension length must be between 1 and 4 characters\n");
  141. return 1;
  142. }
  143. if (strip) {
  144. char* params[4];
  145. params[0] = "";
  146. params[1] = "-quiet";
  147. params[2] = infile;
  148. params[3] = tmpfilename;
  149. if (TTStrip(4,params)) {
  150. fprintf(stderr,"ERROR: stripping inputfile %s failed\n",infile);
  151. return 1;
  152. }
  153. infile = tmpfilename;
  154. }
  155. #ifndef EMBEDDED_USE
  156. if (!quiet) PRINT_ID("TTBin2OTH");
  157. #endif
  158. if (!(ifp = fopen(infile,"rb"))) {
  159. fprintf(stderr,"ERROR: cannot open inputfile %s\n",infile);
  160. if (strip) remove(tmpfilename);
  161. return 1;
  162. }
  163. if (calctype == CALC_TI89) sprintf(outfile,"%s.89y",name);
  164. else sprintf(outfile,"%s.9xy",name);
  165. if (!(ofp = fopen(outfile,"wb"))) {
  166. fprintf(stderr,"ERROR: cannot open outputfile %s\n",outfile);
  167. fclose(ifp);
  168. if (strip) remove(tmpfilename);
  169. return 1;
  170. }
  171. // read infile into buffer
  172. fseek(ifp,0,SEEK_END);
  173. length = ftell(ifp);
  174. buffer = (unsigned char*)malloc(length);
  175. if (!buffer) {
  176. fprintf(stderr,"ERROR: cannot allocate memory (%d bytes)\n",length);
  177. fclose(ifp);
  178. fclose(ofp);
  179. if (strip) remove(tmpfilename);
  180. return 1;
  181. }
  182. rewind(ifp);
  183. fread(buffer,1,length,ifp);
  184. fclose(ifp);
  185. outbuffer = DataBuffer2OTHBuffer(calctype,folder,name,extension,length,buffer,&outlength);
  186. n = 0;
  187. if (!outbuffer) {
  188. n = 1;
  189. }
  190. else {
  191. if (fwrite(outbuffer,outlength,1,ofp) != 1) {
  192. fprintf(stderr,"ERROR: cannot write %u bytes to outputfile to %s\n",outlength,outfile);
  193. n = 1;
  194. }
  195. else {
  196. if (!quiet) fprintf(stdout,"%u bytes written to %s\n",outlength,outfile);
  197. }
  198. free(outbuffer);
  199. }
  200. free(buffer);
  201. fclose(ofp);
  202. if (strip) remove(tmpfilename);
  203. return n;
  204. }
  205. #endif
  206. //#############################################################################
  207. //###################### NO MORE FAKES BEYOND THIS LINE #######################
  208. //#############################################################################
  209. //
  210. //=============================================================================
  211. // Revision History
  212. //=============================================================================
  213. //
  214. // Revision 1.11 2009/01/25 Lionel Debroux
  215. // Changes by Romain Liévin and/or me for 64-bit compatibility.
  216. // Adapt to new version display (revtools.h).
  217. //
  218. // Revision 1.10 2002/05/07 16:33:46 tnussb
  219. // generic commit
  220. //
  221. // Revision 1.9 2002/05/07 15:58:46 tnussb
  222. // usage text modified (NOTE3 added)
  223. //
  224. // Revision 1.8 2002/03/14 09:01:44 tnussb
  225. // (1) supports now between 1 and 4 characters as extension
  226. // (2) new flag "-quiet" added (suppress standard messages)
  227. // (3) new flag "-strip" added (strip TI header before OTH generation)
  228. // With this new flag it is possible to convert any TI file in
  229. // one step into an OTH file without the need of using TTStrip
  230. // before.
  231. // (4) Usage text completely rewritten
  232. //
  233. // Revision 1.7 2002/03/04 14:32:41 tnussb
  234. // now tool can be used as embedded version from within other tools
  235. // by defining EMBEDDED_VERSION before including the sourcefile
  236. //
  237. // Revision 1.6 2002/02/07 09:49:36 tnussb
  238. // all local includes changed, because header files are now located in pctools folder
  239. //
  240. // Revision 1.5 2000/11/28 00:41:15 Thomas Nussbaumer
  241. // unnecessary local function PrintId() removed
  242. //
  243. // Revision 1.4 2000/11/28 00:04:21 Thomas Nussbaumer
  244. // using now USAGE_OUT stream for usage info
  245. //
  246. // Revision 1.3 2000/08/23 19:40:44 Thomas Nussbaumer
  247. // using now automatic version display (revtools.h)
  248. //
  249. // Revision 1.2 2000/08/23 01:13:35 Thomas Nussbaumer
  250. // wrapper function exported to own file bin2oth.c
  251. //
  252. // Revision 1.1 2000/08/14 21:43:16 Thomas Nussbaumer
  253. // initial version
  254. //
  255. //
  256. //
  257. //