home *** CD-ROM | disk | FTP | other *** search
/ messroms.de / 2007-01-13_www.messroms.de.zip / VZ200 / TOOLS / TEXT2BAS.ZIP / text2bas.c < prev    next >
C/C++ Source or Header  |  1999-12-08  |  8KB  |  340 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. /* set to 1 for Colour Genie tokenizer, 0 for VZ200/300 */
  6. #define CGENIE  0
  7.  
  8. typedef struct  tagLINE {
  9.     unsigned short next;
  10.     unsigned short num;
  11.     unsigned char text[254];
  12. }    LINE;
  13.  
  14. LINE line = {0, 0, {0, }};
  15.  
  16. unsigned short lineaddr = 0;
  17. unsigned short linenum    = 1;
  18. int flag_tokenize = 1;
  19. int flag_squeeze_blanks = 1;
  20.  
  21. #if CGENIE
  22. #define EXT ".cas"
  23. #define ADR 0x5801
  24.  
  25. static char *token[128+26+1] = {
  26.     "END","FOR","RESET","SET","CLS","CMD","RANDOM","NEXT",
  27.     "DATA","INPUT","DIM","READ","LET","GOTO","RUN","IF",
  28.     "RESTORE","GOSUB","RETURN","REM","STOP","ELSE","TRON","TROFF",
  29.     "DEFSTR","DEFINT","DEFSNG","DEFDBL","LINE","EDIT","ERROR","RESUME",
  30.     "OUT","ON","OPEN","FIELD","GET","PUT","CLOSE","LOAD",
  31.     "MERGE","NAME","KILL","LSET","RSET","SAVE","SYSTEM","LPRINT",
  32.     "DEF","POKE","PRINT","CONT","LIST","LLIST","DELETE","AUTO",
  33.     "CLEAR","CLOAD","CSAVE","NEW","TAB(","TO","FN", "USING",
  34.     "VARPTR","USR","ERL","ERR","STRING$","INSTR","CHECK","TIME$",
  35.     "MEM","INKEY$","THEN","NOT","STEP","+","-","*",
  36.     "/","[","AND","OR",">","=","<","SGN",
  37.     "INT","ABS","FRE","INP","POS","SQR","RND","LOG",
  38.     "EXP","COS","SIN","TAN","ATN","PEEK","CVI","CVS",
  39.     "CVD","EOF","LOC","LOF","MKI$","MKS$","MKD$","CINT",
  40.     "CSNG","CDBL","FIX","LEN","STR$","VAL","ASC","CHR$",
  41.     "LEFT$","RIGHT$","MID$","'","","","","",
  42.     "COLOUR","FCOLOU","KEYPAD","JOY","PLOT","FGR","LGR","FCLS",
  43.     "PLAY","CIRCLE","SCALE","SHAPE","NSHAPE","XSHAPE","PAINT","CPOINT",
  44.     "NPLOT","SOUND","CHAR","RENUM","SWAP","FKEY","CALL","VERIFY",
  45.     "BGRD","NBGRD", NULL};
  46. #else
  47. #define EXT ".vz"
  48. #define ADR 0x7ae9
  49.  
  50. static char *token[128+1] = {
  51.     "END","FOR","RESET","SET","CLS",""/* CMD */,"RANDOM","NEXT",
  52.     "DATA","INPUT","DIM","READ","LET","GOTO","RUN","IF",
  53.     "RESTORE","GOSUB","RETURN","REM","STOP","ELSE","COPY","COLOR",
  54.     "VERIFY","DEFINT","DEFSNG","DEFDBL","CRUN","MODE","SOUND","RESUME",
  55.     "OUT","ON","OPEN","FIELD","GET","PUT","CLOSE","LOAD",
  56.     "MERGE","NAME","KILL","LSET","RSET","SAVE","SYSTEM","LPRINT",
  57.     "DEF","POKE","PRINT","CONT","LIST","LLIST","DELETE","AUTO",
  58.     "CLEAR","CLOAD","CSAVE","NEW","TAB(","TO","FN", "USING",
  59.     "VARPTR","USR","ERL","ERR","STRING$","INSTR","POINT","TIME$",
  60.     "MEM","INKEY$","THEN","NOT","STEP","+","-","*",
  61.     "/","^","AND","OR",">","=","<","SGN",
  62.     "INT","ABS","FRE","INP","POS","SQR","RND","LOG",
  63.     "EXP","COS","SIN","TAN","ATN","PEEK","CVI","CVS",
  64.     "CVD","EOF","LOC","LOF","MKI$","MKS$","MKD$","CINT",
  65.     "CSNG","CDBL","FIX","LEN","STR$","VAL","ASC","CHR$",
  66.     "LEFT$","RIGHT$","MID$","'","","","","",NULL};
  67. #endif
  68.  
  69. void tokenize(int i)
  70. {
  71.     int t;
  72.     int len;
  73.  
  74.     for( t = 0x80; token[t-0x80]; t++ )
  75.     {
  76.         len = strlen(token[t-0x80]);
  77.         if( len && strncasecmp(line.text+i,token[t-0x80],len) == 0 )
  78.         {
  79.             if( t > 0xFF )
  80.             {
  81.                 strcpy(line.text + i + 2, line.text + i + len);
  82.                 line.text[i+0] = 0xFF;
  83.                 line.text[i+1] = t - 0x80;
  84.                 line.next -= len - 2;
  85.             }
  86.             else
  87.             {
  88.                 strcpy(line.text + i + 1, line.text + i + len);
  89.                 line.text[i+0] = t;
  90.                 line.next -= len - 1;
  91.             }
  92.         }
  93.     }
  94. }
  95.  
  96. void squeeze_blanks(int i)
  97. {
  98.     int j;
  99.  
  100.     if( line.text[i] != ' ' )
  101.         return;
  102.  
  103.     for( j = i+1; j < line.next; j++ )
  104.     {
  105.         if( line.text[j] != ' ' )
  106.             break;
  107.     }
  108.     if( j > i+1 )
  109.     {
  110.         strcpy(line.text + i + 1, line.text + j);
  111.         line.next -= j - i - 1;
  112.     }
  113. }
  114.  
  115. void outline(FILE * out)
  116. {
  117.     int i;
  118.     int len;
  119.     char str = 0;
  120.  
  121.     if( line.next )
  122.     {
  123.         if( line.next == 1 )            /* avoid empty lines */
  124.         {
  125.             line.text[0] = 'R';
  126.             line.text[1] = 'E';
  127.             line.text[2] = 'M';
  128.             line.text[3] = 0;
  129.             line.next     = 4;
  130.         }
  131.         for (i = 0; i < line.next; i++)
  132.         {
  133.             if (str && (line.text[i] != str))
  134.                 continue;
  135.             if( line.text[i] == str )
  136.             {
  137.                 str = 0;
  138.             }
  139.             else
  140.             if( line.text[i] == 0x22 )
  141.             {
  142.                 str = 0x22;
  143.             }
  144.             else
  145.             if( line.text[i] == 0x27 )
  146.             {
  147.                 str = 0x27;
  148.             }
  149.             else
  150.             if( flag_tokenize )
  151.                 tokenize(i);
  152.             if( flag_squeeze_blanks )
  153.                 squeeze_blanks(i);
  154.         }
  155.  
  156.         len = 4 + line.next;
  157.         lineaddr += len;
  158.         line.next = lineaddr;
  159.         line.num = linenum++;
  160.         fwrite(&line, 1, len, out);
  161.         line.next = 0;
  162.     }
  163. }
  164.  
  165. void outbyte(FILE * out, char c)
  166. {
  167.     if( c == 0 && line.next > 0 && line.text[line.next-1] == 0 )
  168.         return;
  169.  
  170.     line.text[line.next] = c;
  171.  
  172.     if( ++line.next == 254 )
  173.         outline(out);
  174. }
  175.  
  176. int main(int ac, char ** av)
  177. {
  178.     char inpfilename[256];
  179.     char outfilename[256];
  180.     char *p;
  181.     FILE *inp, *out;
  182.     int num = 1;
  183.     char str = 0;
  184.     char c;
  185.  
  186.     memset(inpfilename, 0, sizeof(inpfilename));
  187.     memset(outfilename, 0, sizeof(outfilename));
  188.  
  189.     if (ac < 2)
  190.     {
  191.         fprintf(stderr, "usage: text2bas input_filename [output_filename[%s]]\n", EXT);
  192.         fprintf(stderr, "\tconvert text file to basic image (extension %s)\n", EXT);
  193.         return 1;
  194.     }
  195.  
  196.     strcpy(inpfilename, av[1]);
  197.     if (ac < 3)
  198.     {
  199.         strcpy(outfilename, av[1]);
  200.         p = strrchr(outfilename, '.');
  201.         if (p)
  202.             strcpy(p, EXT);
  203.     }
  204.     else
  205.     {
  206.         strcpy(outfilename, av[2]);
  207.     }
  208.  
  209.     p = strrchr(outfilename, '.');
  210.     if (!p)
  211.         strcat(outfilename, EXT);
  212.  
  213.     p = strrchr(outfilename, '/');
  214.     if (!p)
  215.         p = strrchr(outfilename, '/');
  216.     if (!p)
  217.         p = outfilename - 1;
  218.  
  219.     inp = fopen(inpfilename, "rb");
  220.     if( !inp )
  221.     {
  222.         fprintf(stderr, "%s not found\n", inpfilename);
  223.         return 1;
  224.     }
  225.  
  226.     out = fopen(outfilename,"wb");
  227.     if( !out )
  228.     {
  229.         fprintf(stderr, "could not create %s\n", outfilename);
  230.         return 1;
  231.     }
  232.  
  233. #if CGENI
  234.     /* Colour Genie header */
  235.     fputc(0x66, out);               
  236.     /* Basic name is first char of outfilename */
  237.     fputc(toupper(p[1]), out);      
  238. #else
  239.     /* VZ magic header */
  240.     fwrite("VZF0", 1, 4, out);      
  241.     /* write input filename */
  242.     for( p = inpfilename; *p != '.'; p++ )
  243.         *p = toupper(*p);
  244.     memset(p, 0, sizeof(inpfilename) - (int)(p - inpfilename));
  245.     fwrite(inpfilename, 1, 17, out); 
  246.     /* VZ magic value 0xf0 */
  247.     fputc(0xf0, out);
  248.     /* VZ basic load address **/
  249.     fputc(ADR & 0xff, out);
  250.     fputc(ADR >> 8, out);
  251. #endif
  252.  
  253.     lineaddr = ADR;
  254.     while( !feof(inp) )
  255.     {
  256.         c = fgetc(inp);
  257.         if( str )                    /* inside string/comment ? */
  258.         {
  259.             if( c == 0x0d )         /* new line ? */
  260.             {
  261.                 outbyte(out, 0x00);
  262.                 outline(out);
  263.                 num = 1;
  264.             }
  265.             else
  266.             {
  267. #if CGENIE
  268.                 /* put any char */
  269.                 outbyte(out, c);
  270. #else
  271.                 /* upper case letters only */
  272.                 outbyte(out, toupper(c));
  273. #endif
  274.             }
  275.             if( c == str )            /* end of string/comment ? */
  276.                 str = 0;
  277.         }
  278.         else                        /* normal text */
  279.         {
  280.             if( num )                /* expecting line number ? */
  281.             {
  282.                 if( isdigit(c) )    /* digit follows ?? */
  283.                 {
  284.                     linenum = c - '0';
  285.                     while( isdigit(c = fgetc(inp)) )
  286.                     {
  287.                         linenum *= 10;
  288.                         linenum += c - '0';
  289.                     }
  290.                     if( c == ' ' )  /* skip one space after the line number */
  291.                         c = fgetc(inp);
  292.                 }
  293.                 num = 0;
  294.             }
  295.             switch (c)
  296.             {
  297.             case 0x0a:                /* no linefeeds */
  298.                 num = 1;
  299.                 break;
  300.             case 0x0d:                /* convert newline into EOL */
  301.             case 0x1a:                /* convert end of text into EOL */
  302.                 outbyte(out, 0x00);
  303.                 outline(out);
  304.                 num = 1;
  305.                 break;
  306.             case 9:                 /* expand tabs */
  307.                 outbyte(out, 0x20);
  308.                 while( line.next & 7 )
  309.                     outbyte(out, 0x20);
  310.                 break;
  311.             case 0x22:                /* inside string ? */
  312.             case 0x27:
  313.                 str = c;
  314.                 outbyte(out, c);
  315.                 break;
  316.             case ';':               /* comment line ? */
  317.                 str = 0x0d;         /* skip until next return */
  318.                 outbyte(out, c);
  319.                 break;
  320.             default:
  321. #if CGENIE
  322.                 outbyte(out, c);
  323. #else
  324.                 outbyte(out, toupper(c));
  325. #endif
  326.                 break;
  327.             }
  328.         }
  329.     }
  330.  
  331.     line.next = 0;
  332.     fwrite(&line, 1, 2, out);
  333.  
  334.     fclose(inp);
  335.     fclose(out);
  336.  
  337.     return 0;
  338. }
  339.  
  340.