home *** CD-ROM | disk | FTP | other *** search
/ ticalc.org / ticalc_org_rev_b.iso / archives / amiga / amigati85prog.lha / src / string85.c < prev   
Encoding:
C/C++ Source or Header  |  1997-06-14  |  2.9 KB  |  148 lines

  1. /* String85.c - Converts a binary file into a .85s file suitable for ZShell 
  2.  * on the TI-85 calculator.  Adds the checksum byte automatically, but does
  3.  * not add the neccessary stuff to the beinging ($00FD<DescLen><Desc>00)
  4.  *
  5.  * Copyright ⌐1997 Shawn D'Alimonte - This program is freeware
  6.  * Please don't charge more than media costs for it
  7.  * If you modify it than please give me credit and clearly show what you
  8.  * changed.
  9.  *
  10.  * Note to PC users: This code assumes Motorolla word ordering
  11.  * although you should be able to use it if you change fputw()
  12.  * I think that is all the changes that it should need
  13.  * Also the data type UBYTE is the same as unsigned char
  14.  *
  15.  * Sorry that the code is uncommented, but I wrote it in a hurry and don't
  16.  * feel like going back over it.
  17.  */
  18.  
  19. #include<stdio.h>
  20. #include<stdlib.h>
  21. #include<exec/types.h> 
  22. #include<string.h>
  23.  
  24. /* Write a word to the file in correct order for TI85*/
  25. void fputw(int c, FILE *f) 
  26. {
  27.   fputc((c)&0x00ff,(f));
  28.   fputc(((c)&0xff00)>>8,(f));
  29. }
  30.  
  31. void print_usage(void)
  32. {
  33.   printf("String85 for Amiga\nConverts binary files to TI85 type strings for ZShell\nUsage: string85 File Name Desc\n");
  34.   exit(5);
  35. }
  36.  
  37. int main(int argc, char **argv)
  38. {
  39.   const char Ident[]="**TI85**";          /*TI85 File ID string*/
  40.   const char Sig[]  ={0x1a,0x0c,0x00};    
  41.   const char Comment[]="ZShell string file                        ";
  42.  
  43.   int FLength, DLength;
  44.   UBYTE Name_length;
  45.   int Skip_len, checksum, i;
  46.   char OutName[8+4+1];
  47.   
  48.   FILE *infile, *outfile;
  49.   
  50.   UBYTE *buffer;
  51.  
  52.   UBYTE ZSChk;
  53.   
  54.   if(argc != 3)
  55.     {
  56.       print_usage();
  57.     }
  58.   
  59.   if(strlen(argv[2])>12) 
  60.     {
  61.       printf("Variable name too long!\n");
  62.       abort();
  63.     }
  64.   
  65.   if(!(infile=fopen(argv[1],"r")))
  66.     {
  67.       printf("Error opening %s for reading\n", argv[1]);
  68.       abort();
  69.     }
  70.   
  71.   
  72.   strcpy(OutName, argv[2]);
  73.   strcat(OutName, ".85s");
  74.   
  75.   if(!(outfile=fopen(OutName,"w")))
  76.     {
  77.       printf("Error opening %s for writing\n", OutName);
  78.       fclose(infile);
  79.       abort();
  80.     }
  81.  
  82.   
  83.   fputs(Ident, outfile);
  84.   fputs(Sig, outfile);
  85.   fputc(0x00, outfile);
  86.   fputs(Comment,outfile);
  87.   
  88.   DLength=0;
  89.   while(fgetc(infile)!=EOF)
  90.     DLength++;
  91.  
  92.   DLength += 2;
  93.   DLength ++;
  94.  
  95.   rewind(infile);
  96.   
  97.   FLength = DLength+strlen(argv[2])+8;
  98.   fputw(FLength, outfile);
  99.   
  100.   Skip_len = 2+1+1+strlen(argv[2]);
  101.   fputw(Skip_len, outfile);
  102.   
  103.   fputw(DLength,outfile);
  104.   
  105.   fputc(0x0c, outfile);
  106.   
  107.   fputc(strlen(argv[2]),outfile);
  108.   
  109.   fputs(argv[2],outfile);
  110.   
  111.   fputw(DLength, outfile);
  112.   
  113.   fputw(DLength-2, outfile);
  114.  
  115.   ZSChk=0;
  116.   for(i=0;i<(DLength-3);i++)
  117.     {
  118.       int temp=fgetc(infile);
  119.       fputc(temp,outfile);
  120.       ZSChk+=temp;
  121.     }
  122.   
  123.   fputc((ZSChk-0xfd)&0xff, outfile);
  124.  
  125.   fclose(outfile);
  126.   outfile=fopen(OutName,"r");
  127.  
  128.   fseek(outfile , 0x37, SEEK_SET);
  129.  
  130.   checksum=0;
  131.   while (!feof(outfile))
  132.     checksum+=fgetc(outfile);
  133.   checksum++;
  134.  
  135.   fclose(outfile);
  136.   outfile=fopen(OutName,"a");
  137.  
  138.   fputw(checksum,outfile);
  139.  
  140.   fclose(infile);fclose(outfile);
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.