home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 344b.lha / PLT_v1.3a / cf.c < prev    next >
C/C++ Source or Header  |  1990-01-28  |  6KB  |  254 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3.  
  4. #define ASTR    0;
  5. #define DIGIT   1;
  6. #define WSPC    2;
  7. #define NEWLINE 3;
  8. #define DASH    4;
  9. #define MISC    5;
  10.  
  11. long ftell();
  12.  
  13. struct state_rec
  14.     {
  15.         short next;
  16.         short action;
  17.     };
  18.     
  19. struct state_rec s_table[8][6] =
  20.     {
  21.         /* state 0 */
  22.         {
  23.             { 1, 0 },   /* ASTR */
  24.             { 0, 0 },   /* DIGIT */
  25.             { 0, 0 },   /* WSPC */
  26.             { 0, 0 },   /* NEWLINE */
  27.             { 0, 99},   /* DASH */
  28.             { 0, 99 }    /* MISC */
  29.         },
  30.         /* state 1 */
  31.         {
  32.             { 0, 99 },   /* ASTR */
  33.             { 2, 2 },   /* DIGIT */
  34.             { 1, 0 },   /* WSPC */
  35.             { 0, 99 },   /* NEWLINE */
  36.             { 0, 99},   /* DASH */
  37.             { 0, 99 }    /* MISC */
  38.         },
  39.         /* state 2 */
  40.         {
  41.             { 3, 4 },   /* ASTR */
  42.             { 2, 3 },   /* DIGIT */
  43.             { 3, 4 },   /* WSPC */
  44.             { 4, 4 },   /* NEWLINE */
  45.             { 0, 99},   /* DASH */
  46.             { 3, 4 }    /* MISC */
  47.         },
  48.         /* state 3 */
  49.         {
  50.             { 3, 0 },   /* ASTR */
  51.             { 3, 0 },   /* DIGIT */
  52.             { 3, 0 },   /* WSPC */
  53.             { 4, 0 },   /* NEWLINE */
  54.             { 3, 0},   /* DASH */
  55.             { 3, 0 }    /* MISC */
  56.         },
  57.         /* state 4 */
  58.         {
  59.             { 0, 99 },   /* ASTR */
  60.             { 5, 2 },   /* DIGIT */
  61.             { 4, 0 },   /* WSPC */
  62.             { 4, 0 },   /* NEWLINE */
  63.             { 5, 7},   /* DASH */
  64.             { 0, 99 }    /* MISC */
  65.         },
  66.         /* state 5 */
  67.         {
  68.             { 0, 6 },   /* ASTR */
  69.             { 5, 3 },   /* DIGIT */
  70.             { 6, 5 },   /* WSPC */
  71.             { 6, 5 },   /* NEWLINE */
  72.             { 0, 99},   /* DASH */
  73.             { 0, 99 }    /* MISC */
  74.         },
  75.         /* state 6 */
  76.         {
  77.             { 1, 1 },   /* ASTR */
  78.             { 5, 2 },   /* DIGIT */
  79.             { 6, 0 },   /* WSPC */
  80.             { 6, 0 },   /* NEWLINE */
  81.             { 5, 7},   /* DASH */
  82.             { 0, 99 }    /* MISC */
  83.         }
  84.     };
  85.  
  86. FILE *infile;
  87. int outfile;
  88.  
  89. char inname[100];
  90. char outname[100];
  91. char linebuf[100];
  92.  
  93. unsigned long d_index[256];
  94. short data[30000];
  95.  
  96.  
  97. main(argc,argv)
  98.     int argc;
  99.     char *argv[];
  100. {
  101.     short CellWidth,CellHeight;
  102.     short HighChar;
  103.     unsigned long pos;
  104.     short val;
  105.     char c;
  106.     short class, i;
  107.     short state,nextstate;
  108.     short action;
  109.     short sign;
  110.     
  111.     state=0;
  112.     pos=0;
  113.     HighChar=0;
  114.  
  115.     if(argc!=2)
  116.     {
  117.         printf("bad args\n");
  118.         exit();
  119.     }
  120.     
  121.     strcpy(inname,argv[1]);
  122.     strcat(inname,".fnt");
  123.     
  124.     strcpy(outname,"pltdata:");
  125.     strcat(outname,argv[1]);
  126.     
  127.     infile=fopen(inname,"r");
  128.     if( infile == NULL )
  129.     {
  130.         printf("can't open %s\n",inname);
  131.         exit();
  132.     }
  133.     
  134.     outfile=open(outname,O_CREAT | O_WRONLY);
  135.     if( outfile == -1 )
  136.     {
  137.         printf("can't open %s\n",outname);
  138.         exit();
  139.     }
  140.     
  141.     printf("Creating %s.\n",outname);
  142.  
  143.     for (i=0; i<256; i++)
  144.         d_index[i]=-1;
  145.  
  146.     linebuf[0]=0;
  147.     fgets(linebuf,99,infile);
  148.     
  149.     if( sscanf(linebuf,"%d %d",&CellWidth,&CellHeight) != 2 )
  150.     {
  151.         printf("error in width or height\n");
  152.         exit();
  153.     }
  154.     
  155.     while( feof(infile) == 0 )
  156.     {
  157.         c=fgetc(infile);
  158.         if( c == EOF )
  159.             break;
  160.         
  161.         switch(c)
  162.         {
  163.             case '*':   class=ASTR;
  164.                         break;
  165.             
  166.             case '0':
  167.             case '1':
  168.             case '2':
  169.             case '3':
  170.             case '4':
  171.             case '5':
  172.             case '6':
  173.             case '7':
  174.             case '8':
  175.             case '9':   class=DIGIT;
  176.                         break;
  177.             
  178.             case ' ':
  179.             case '\t':  class=WSPC;
  180.                         break;
  181.             
  182.             case '\n':  class=NEWLINE;
  183.                         break;
  184.             
  185.             case '-':   class=DASH;
  186.                         break;
  187.             
  188.             default:    class=MISC;
  189.         }
  190.         
  191.         nextstate=s_table[state][class].next;
  192.         action=s_table[state][class].action;
  193.  
  194. #ifdef DEBUG
  195.         printf("cs %d c '%c' ns %d ac %d\n",state,c,nextstate,action);
  196. #endif
  197.  
  198.         
  199.         switch(action)
  200.         {
  201.             case 1: data[pos]=-1;
  202.                     pos++;
  203.                     break;
  204.                     
  205.             case 2: val = c - '0';
  206.                     sign=1;
  207.                     break;
  208.             
  209.             case 3: val = (val * 10) + (c - '0');
  210.                     break;
  211.             
  212.             case 4: d_index[val] = pos;
  213.                     if(HighChar < val)
  214.                         HighChar = val;
  215.                     break;
  216.             
  217.             case 5: data[pos] = sign*val;
  218.                     pos++;
  219.                     break;
  220.                     
  221.             case 6: data[pos] = sign*val;
  222.                     pos++;
  223.                     data[pos] = -1;
  224.                     pos++;
  225.                     break;
  226.             
  227.             case 7: sign=-1;
  228.                     val=0;
  229.                     break;
  230.             
  231.             case 99: printf("\nerror: c='%c' pos=%ld\n",c,(long)ftell(infile));
  232.                      exit();
  233.                      break;
  234.                      
  235.         }
  236.         
  237.         state=nextstate;
  238.     }
  239.     
  240.     data[pos++]=-1;
  241.     write(outfile,&HighChar,sizeof(short));
  242.     write(outfile,&CellWidth,sizeof(short));
  243.     write(outfile,&CellHeight,sizeof(short));
  244.     
  245.     write(outfile,d_index,(int)((HighChar+1)*sizeof(long)));
  246.     
  247.     write(outfile,&pos,sizeof(long));
  248.     
  249.     write(outfile,data,(int)(pos*sizeof(short)));
  250.     
  251.     fclose(infile);
  252.     close(outfile);
  253. }
  254.