home *** CD-ROM | disk | FTP | other *** search
/ ticalc.org / ticalc_org_rev_b.iso / archives / 85 / asm / source / zshell / string85.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-01  |  4.1 KB  |  126 lines

  1. // Here is the source for String85.  I used Turnbo C++ to compile it.
  2. // Do whatever you want with it, but if you use a major portion of it,
  3. // give me a little credit, please.     -Dan Eble (eble.2@osu.edu)
  4. // -------------------------------------------------------------------
  5.  
  6. #include <dos.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. void fputc_and_sum(char byte, FILE *file);
  12.  
  13. unsigned checksum = 0; char checksum2 = 0;
  14.  
  15. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!**
  16. void main(int argc, char *argv[])
  17. {
  18.         FILE *infile, *outfile;
  19.         char stringname[9], filename[13], len, makelib = 0;
  20.         int ch;
  21.         unsigned i, num;
  22.         fpos_t infilebottom;
  23.  
  24.         if (argc != 2 && argc != 3)
  25.         {
  26.                 puts("\nSTRING85  Copyright 1995 Dan Eble"
  27.                      "\nUsage: STRING85 inputfile [-L]"
  28.                      "\n       Don't put an extension on input file."
  29.                      "\n       Output file will be .85s\n");
  30.                 return;
  31.         }
  32.  
  33.         if (argc == 3 && (!strcmp(argv[2], "-L") || !strcmp(argv[2], "-l")))
  34.                 makelib = 1;
  35.  
  36.         strncpy(stringname, argv[1], 9);        // copy up to 8 bytes into stringname[]
  37.         strcpy(filename, stringname);
  38.         strcat(filename, ".85s");
  39.  
  40.         if ((infile = fopen(stringname, "rb")) == NULL)
  41.         {
  42.                 puts("\nCannot open input file.\n");
  43.                 return;
  44.         }
  45.  
  46.         if ((outfile = fopen(filename, "wb")) == NULL)
  47.         {
  48.                 puts("\nCannot open output file.\n");
  49.                 return;
  50.         }
  51.  
  52.         fseek(infile,0,SEEK_END);
  53.         fgetpos(infile, &infilebottom);
  54.         fseek(infile,0,SEEK_SET);
  55.         infilebottom += 4;      // add 4 bytes for 00,FF,titlelength,checksum2
  56.  
  57.         fputs("**TI85**", outfile);     // id string
  58.         fputc(26, outfile);             // ascii eof
  59.  
  60.         fputc(12, outfile);     // doesn't seem to change
  61.         fputc(0, outfile);
  62.  
  63.         fputs("Machine code stored as a string.        DE", outfile);   // 42-bytes
  64.  
  65.         num = infilebottom+(unsigned)strlen(stringname)+10;
  66.         fputc((char)num, outfile);      // next two things + 4
  67.         fputc(num>>8, outfile);
  68.  
  69.         num = (unsigned)strlen(stringname)+4;
  70.         fputc_and_sum((char)num, outfile);      // name length + 4
  71.         fputc_and_sum(num>>8, outfile);
  72.  
  73.         num = infilebottom+2;
  74.         fputc_and_sum((char)num, outfile);      // string length + 2
  75.         fputc_and_sum(num>>8, outfile);         // (same as below)
  76.  
  77.         fputc_and_sum(0x0C, outfile);           // data type (string)
  78.  
  79.         fputc_and_sum(strlen(stringname), outfile);     // name length
  80.  
  81.         for (i = 0; i < strlen(stringname); i++)        // name
  82.                 fputc_and_sum(stringname[i], outfile);
  83.  
  84.         fputc_and_sum((char)(infilebottom+2), outfile); // string length + 2
  85.         fputc_and_sum((infilebottom+2)>>8, outfile);    // (same as above)
  86.  
  87.         fputc_and_sum((char)infilebottom, outfile);     // string length
  88.         fputc_and_sum(infilebottom>>8, outfile);
  89.  
  90.         fputc(0, outfile);      // program signature bytes
  91.  
  92.         if (makelib)
  93.                 fputc(0, outfile);      // library string
  94.         else
  95.                 fputc_and_sum(0xfe, outfile);   // ff = zshell 2.0 and 1.0
  96.                                                 // fe = zshell 3.0 and later
  97.  
  98.         for (i = 0; (ch = fgetc(infile)) != EOF && ch != 0; i++)
  99.                 ;
  100.         fseek(infile, 0, SEEK_SET);     // go back to file start
  101.  
  102.         fputc_and_sum((char)i, outfile);        // long name length
  103.         checksum2 = (char)i;
  104.  
  105.         // copy the data
  106.         for (i = 0; (ch = fgetc(infile)) != EOF; i++)
  107.         {
  108.                 fputc_and_sum((char)ch, outfile);
  109.                 checksum2 += (char)ch;
  110.         }
  111.  
  112.         fputc_and_sum(checksum2, outfile);
  113.  
  114.         fputc((char)checksum, outfile);
  115.         fputc(checksum>>8, outfile);
  116.         fclose(infile);
  117.         fclose(outfile);
  118. }
  119.  
  120. void fputc_and_sum(char byte, FILE *file)
  121. {
  122.         fputc(byte, file);
  123.         checksum += byte;
  124. }
  125.  
  126.