home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 495b.lha / Vortex_v1.5 / Vortex.c < prev    next >
C/C++ Source or Header  |  1991-04-06  |  12KB  |  347 lines

  1. /* Vortext v1.5 - Michel Laliberté, février 1991 - compilé sur Lattice 5.0
  2.     lc <source>.c
  3.     blink FROM lib:c.o <source>.o TO <source> 
  4.     LIB lib:lc.lib lib:amiga.lib SMALLCODE SMALLDATA NODEBUG VERBOSE
  5. */
  6. #include <stdio.h>
  7. #include <exec/types.h>
  8. #define LF 10
  9. #define CR 13
  10. #define TAB_DEF 5
  11. #define LIM_AMI 161
  12. #define LIM_PC  128    
  13. unsigned char b = 0;
  14. unsigned char a = 0;
  15. BOOL sl = TRUE;
  16. BOOL ca, ai, ia, am, ma, im, mi,  wp, aw, ascii, nc, Tab_size;
  17. FILE *fp;
  18. FILE *pf;
  19.  
  20. main(argc,argv)
  21. int argc;
  22. char *argv[];
  23. {
  24.     VOID conv_accents(), Am_C64(), TABtoSPC(), rien_a_foutre();
  25.     char outfile[32];
  26.      int NextArg; 
  27.                      /* Initialisation des tableaux de conversion */
  28.     static unsigned char tabAI_conv[96] = {
  29.         173,189,156,207,190, 46,245,249,184,166,174,170,240,169,238,248,
  30.         241,253,252,239,230,244,250, 44,251,167,175,171,172,243,168,183,
  31.         181,182,199,142,143,146,128,212,144,210,211,222,214,215,216,209,
  32.         165,227,224,226,229,153,158,157,235,233,234,154,237,231,225,133,
  33.         160,131,198,132,134,145,135,138,130,136,137,141,161,140,139,208,
  34.         164,149,162,147,228,148,246,155,151,163,150,129,236,232,152 };    
  35.  
  36.     static unsigned char tabAM_conv[96] = {
  37.         193,162,163, 46,180, 46,164,172,169,187,199,194, 46,168, 46,161,
  38.         177, 46, 46,171,181,166,165, 44, 46,188,200, 46, 46, 46,192,203,
  39.          46, 46,204,128,129,174,130, 46,131, 46, 46, 46, 46, 46, 46, 46,
  40.         132, 46, 46, 46,205,133, 46,175, 46, 46, 46,134, 46, 46,167,136,
  41.         135,137,139,138,140,190,141,143,142,144,145,147,146,148,149, 46,
  42.         150,152,151,153,155,154,214,191,157,156,158,159, 46, 46,216 };
  43.         
  44.     static unsigned char tabIA_conv[129] = {
  45.         199,252,233,226,228,224,229,231,234,235,232,239,238,236,196,197,
  46.         201,230,198,244,246,242,251,249,255,214,220,248,163,216,215, 46,
  47.         225,237,243,250,241,209,170,186,191,169,172,189,188,161,171,187,
  48.          46, 46, 46, 46, 46,193,194,192,169, 46, 46, 46, 46,162,165, 46,
  49.          46, 46, 46, 46, 46, 46,227,195, 46, 46, 46, 46, 46, 46, 46,164,
  50.         240,208,202,203,200, 46,205,206,207, 46, 46, 46, 46, 46,204, 46,
  51.         211,223,212,210,245,213,181,222,254,218,219,217,253,221,175,180,
  52.         173,177, 46,190,182,167,247, 46,176,168,183,185,179,178, 46, 46 };
  53.             
  54.     static unsigned char tabIM_conv[129] = {
  55.         130,159,142,137,138,136,140,141,144,145,143,149,148,147,128,129,
  56.         131,190,174,153,154,152,158,157,216,133,134,191,163,175, 46,196,
  57.         135,146,151,156,150,132,187,188,192,168,194, 46, 46,193,199,200,
  58.          46, 46, 46, 46, 46, 46, 46,203,169, 46, 46, 46, 46,162,180, 46,
  59.          46, 46, 46, 46, 46, 46,139,204, 46, 46, 46, 46, 46, 46, 46, 46,
  60.          46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
  61.          46,167, 46, 46,155,205,181, 46, 46, 46, 46, 46, 46, 46, 46,171,
  62.         209,177, 46, 46,166,164,214, 46,161,172,165, 46, 46, 46,215,202 };
  63.         
  64.     static unsigned char tabMA_conv[90] = {
  65.         196,197,199,201,209,214,220,225,224,226,228,227,229,231,233,232,
  66.         234,235,237,236,238,239,241,243,242,244,246,245,250,249,251,252,
  67.          46,176,162,163,167,183,182,223,174,169, 46,180,168, 46,198,216,
  68.          46,177, 46, 46,165,181, 46, 46, 46, 46, 46,170,186, 46,230,248,
  69.         191,161,172, 46, 46, 46, 46,171,187, 46, 46,192,195,213, 46, 46,
  70.          45, 46, 34, 34, 96, 39,247, 46,255 };
  71.         
  72.     static unsigned char tabMI_conv[90] = {
  73.         142,143,128,144,165,153,154,160,133,131,132,198,134,135,130,138,
  74.         136,137,161,141,140,139,164,162,149,147,148,228,163,151,150,129,
  75.          46,248,189,156,245,250,244,225,169,184, 46,239,249, 46,146,157,
  76.          46,241, 46, 46,190,230, 46, 46, 46, 46, 46,166,167, 46,145,155,
  77.         168,173,170, 46,159, 46, 46,174,175, 46,255,183,199,229, 46, 46,
  78.          45,240, 34, 34, 96, 39,246,254,152  };
  79.  
  80.     if (argc<2 || argv[1][0] == '?') {  /* Tableau d'aide */
  81.         printf("\f");
  82.         printf("                         Vortex v1.5 - February 1991\n\n");
  83.         printf("     Converts all accented characters in ASCII or Word Perfect\n");
  84.         printf("     files for Amiga, IBM-PC, MacIntosh and C-64 computers.\n");
  85.         printf("     NOTE: Does not convert the diskette format.\n");
  86.          printf("           Michel Laliberté, Montréal - tél.: (514) 274-0288\n\n");
  87.         printf(" USAGE: 1> %s source [destination] -direction [-options...] \n\n",argv[0]); 
  88.         printf("     Puts in \"destination\" the conversion of file \"source\".\n");
  89.         printf(" DIRECTION:     -ai/-ia:  Amiga  -> IBM-PC /or vice versa\n");
  90.         printf(" choose one:    -am/-ma:  Amiga  -> Mac /or vice versa\n");
  91.         printf("                -im/-mi:  IBM-PC -> Mac /or vice versa\n");
  92.         printf("                -ca:      C-64 (Traitex) -> Amiga\n");
  93.         printf(" OPTIONS:       -wp:      Word Perfect 4.x source file\n");
  94.         printf(" [optional]     -aw:      ASCII -> Word Perfect conversion\n");
  95.         printf("                -sl/-sl0: Converts line feeds /or keeps them \n");  
  96.         printf("                -nc:      No accent conversion\n");
  97.         printf("                -tx:      Replaces tabs by x spaces (1-20)\n");
  98.         printf("     ex.: 1> %s  df1:sourcefile  ram:destfile -ai -wp\n\n",argv[0]);
  99.         exit(0);
  100.     }
  101.     else {                        /* Lecture des options  */
  102.         for (NextArg=1; NextArg <argc; NextArg++) {
  103.             if (*argv[NextArg] == '-') {        
  104.                 switch (argv[NextArg][1]) {
  105.                     case 'a':    {            /* AI, AM et AW */
  106.                         if (argv[NextArg][2] == 'i')      ai = TRUE;
  107.                         else if (argv[NextArg][2] == 'm') am = TRUE;
  108.                         else if (argv[NextArg][2] == 'w') {
  109.                              aw = TRUE; sl = FALSE;
  110.                         }
  111.                         else rien_a_foutre(); 
  112.                         break;
  113.                     }
  114.                     case 'c': {               /* CA */
  115.                         if (argv[NextArg][2] == 'a') ca = TRUE;
  116.                         else rien_a_foutre(); 
  117.                         break;
  118.                     }
  119.                     case 'i':   {            /* IA et IM*/
  120.                         if (argv[NextArg][2] == 'a')      ia = TRUE; 
  121.                         else if (argv[NextArg][2] == 'm') im = TRUE;
  122.                         else rien_a_foutre(); 
  123.                         break;
  124.                     }
  125.                     case 'm':    {            /* MA et MI */
  126.                         if (argv[NextArg][2] == 'a')      ma = TRUE;
  127.                         else if (argv[NextArg][2] == 'i') mi = TRUE;
  128.                         else rien_a_foutre();
  129.                         break;
  130.                     }
  131.                     case 'n':    {            /* MA et MI */
  132.                         if (argv[NextArg][2] == 'c') {
  133.                             nc = TRUE; sl = FALSE;
  134.                         }
  135.                         else rien_a_foutre();
  136.                         break;
  137.                     }
  138.                     case 's':     {           /* SL */
  139.                         if (argv[NextArg][2] == 'l') { 
  140.                             sl = TRUE;
  141.                             if (argv[NextArg][3] == '0') sl = FALSE;
  142.                         }
  143.                         else rien_a_foutre(); 
  144.                         break;
  145.                     }
  146.                     case 't': {    /* conversion tabs -> espaces */
  147.                         Tab_size = atoi(&argv[NextArg][2]);
  148.                         if (Tab_size < 2 || Tab_size > 20) {
  149.                             Tab_size = TAB_DEF;
  150.                             printf("\tNombre d'espaces invalide; changé pour %d.\n",TAB_DEF);
  151.                         }
  152.                         break;
  153.                     }
  154.                     case 'w':     {           /* WP */
  155.                         if (argv[NextArg][2] == 'p') { 
  156.                             wp = TRUE; sl = FALSE; 
  157.                         }
  158.                         else rien_a_foutre();
  159.                         break;
  160.                     }
  161.                     default:
  162.                         rien_a_foutre();
  163.                 }
  164.             }
  165.         }
  166.     }                         /* On ouvre source et destination */
  167.  
  168.     if (argc >2 && argv[2][0] == '-') { /* Destination = <source>.vx */
  169.         strcpy(outfile,argv[1]);
  170.         strcat(outfile,".vx");
  171.     }
  172.     else if (argv[2][0] != '-')       /* Destination = argv[2] */
  173.         strcpy(outfile,argv[2]);
  174.     
  175.     if (argv[1][0] == '-')     {        /* Source != argv[1]! */            
  176.         printf("USAGE: 1> vortex source [destination] -direction [-options...]\n");
  177.         printf("\tPlease enter source file name first!\n");
  178.         exit(0);
  179.     }
  180.     if ((argv[2][0] != '-')&&(argv[3][0] != '-')
  181.             ||(!ai&&!ia&&!ma&&!am&&!im&&!mi&&!ca)) { /* Pas de direction! */
  182.         printf("USAGE: 1> vortex source [destination] -direction [-options...]\n");
  183.         printf("\tPlease entrer a direction!\n");
  184.         exit(0);
  185.     }
  186.     if((fp=fopen(argv[1],"r")) == NULL) {
  187.         printf("\tCan't open source file...\n");
  188.         exit(0);
  189.     }
  190.     if((pf=fopen(outfile,"w")) == NULL) {
  191.         printf("\tCan't open destination file...\n");
  192.         fclose(fp);
  193.         exit(0);
  194.     }
  195.     printf("    Destination = %s  Working...\n",outfile);
  196.  
  197.     if      (ca) Am_C64();       /* Jump table */
  198.     else if (ai) conv_accents(LIM_AMI, 256, tabAI_conv);
  199.     else if (am) conv_accents(LIM_AMI, 256, tabAM_conv);
  200.     else if (ia) conv_accents(LIM_PC, 256,  tabIA_conv);
  201.     else if (im) conv_accents(LIM_PC, 256,  tabIM_conv);
  202.     else if (ma) conv_accents(LIM_PC, 217,  tabMA_conv);
  203.     else if (mi) conv_accents(LIM_PC, 217,  tabMI_conv);
  204.  
  205.     fclose(fp);                 /* Sortie */
  206.     fclose(pf);
  207.     exit(0);
  208. }
  209.  
  210. VOID conv_accents(lim1, lim2, str) /* Routine principale  */
  211. int lim1, lim2;
  212. char *str;
  213. {
  214.     register int i, j;           /* Crée tableau de conversion */
  215.     static unsigned char tab_conv[257];
  216.     for(i=0; i<lim1;i++)     tab_conv[i] = (char)i; 
  217.     for(j=0; i<lim2;i++,j++) tab_conv[i] = (char)str[j]; 
  218.     for(   ; i<256;i++)         tab_conv[i] = (char)i; 
  219.     if (wp) tab_conv[225] = 225;     /* á */
  220.     b=getc(fp);
  221.     if (wp) {
  222.         if (ai || am) while (b == 128) b=getc(fp);     
  223.         if (ia) {
  224.             putc(128,pf);
  225.             putc(128,pf);
  226.         }
  227.     }
  228.     while (b!=EOF) {                  /* Boucle principale */
  229.         if (wp && a == 'á') b = tab_conv[b];
  230.         else if (!wp && !nc) b = tab_conv[b];
  231.         a = b;
  232.         if (Tab_size && b == 9) {  /* Tabs */
  233.             TABtoSPC(Tab_size);
  234.             continue;
  235.         }
  236.         if (sl){                         /* Saut de ligne */
  237.              if (conv_sl()) continue;
  238.         }
  239.         if (aw && ASCtoWP(lim1)) continue;  /* ASCII -> WP */
  240.         putc(b,pf);                
  241.         b=getc(fp); 
  242.     }
  243. }
  244.  
  245. conv_sl()
  246. {
  247.     switch (b) {                      /* Convertit saut de ligne */
  248.         case LF:
  249.             if     (ai) putc(CR,pf);  /* Amiga -> */
  250.             else if(am) b = CR;
  251.             break;
  252.         case CR:
  253.             if     (ia) {              /* IBM -> */
  254.                 b = getc(fp);
  255.                 return(TRUE);
  256.             }
  257.             else if(im) getc(fp);
  258.             else if(ma) b = LF;        /* Mac -> */
  259.             else if(mi) { 
  260.                 putc(CR,pf);
  261.                 b = LF;
  262.             }
  263.             break;
  264.         }
  265.     return(FALSE);
  266. }
  267.  
  268. ASCtoWP(lim1)          /* Conversion ASCII -> WP */
  269. int lim1;
  270. {
  271.     if( b >= lim1)      {   /* Caractère accentué WP à encadrer */
  272.         putc('á',pf);
  273.         putc(b,pf);
  274.         putc('á',pf);
  275.         b = getc(fp);
  276.         return(TRUE);
  277.     }
  278.     else return(FALSE);    
  279. }
  280.  
  281. VOID TABtoSPC(n)               /* Conversion des TAB en espaces */
  282. int n;
  283. {
  284.     int i;
  285.     b = 32;
  286.     for(i=0; i < n; i++) putc(b,pf);
  287.     b = getc(fp);
  288. }
  289.  
  290. VOID Am_C64()             /*Conversion Amiga -> C-64 */
  291. {
  292.     while (b == 0) b=getc(fp);
  293.     while (b!=EOF)    {        /* Boucle de lecture du fichier source */
  294.         if (b=='z') {    /* Enlève caractères de contrôle */
  295.             do    {
  296.                 b=getc(fp);
  297.             }while (b!=';' && b != 31);
  298.         }
  299.         if (b ==31) b=10;    /* Si b = 31, b = LF   */ 
  300.         else if (b == ';') b = getc(fp);
  301.         else if (b>0 && b<32) b=b+96;    /* Codes d'écran -> ASCII */
  302.         else switch(b) {            /* Convertit autres accents français */
  303.             case 't':        /* é */
  304.                 b=233;if (aw) ascii = TRUE; break;
  305.             case 'h':        /* à */        
  306.                 b=224;if (aw) ascii = TRUE; break;
  307.             case 'u':        /* è */
  308.                 b=232;if (aw) ascii = TRUE; break;
  309.             case 'v':        /* ê */
  310.                 b=234;if (aw) ascii = TRUE; break;
  311.             case 'j':        /* â */
  312.                 b=226;if (aw) ascii = TRUE; break;
  313.             case '\\':        /* ç */
  314.                 b=251;if (aw) ascii = TRUE; break;
  315.             case 'o':        /* î */
  316.                 b=231;if (aw) ascii = TRUE; break;
  317.             case 'w':        /* û */
  318.                 b=238;if (aw) ascii = TRUE; break;
  319.             case 'x':        /* ô */
  320.                 b=244;if (aw) ascii = TRUE; break;
  321.             case 'y':        /* ù */
  322.                 b=249;if (aw) ascii = TRUE; break;
  323.             case '^':        /* ë */
  324.                 b=235;if (aw) ascii = TRUE; break;
  325.             case '_':        /* ï */
  326.                 b=239;if (aw) ascii = TRUE; break;
  327.             case 'i':        /* ü */
  328.                 b=252;if (aw) ascii = TRUE; break;         
  329.             case '[':    /* Hard space Traitex -> espace */
  330.                 b=32; break;
  331.         }
  332.         if (ascii) putc('á',pf);    /* Imprime á */
  333.         putc(b,pf);                /* Envoie le caractère à l'output */
  334.         if (ascii) putc('á',pf);    /* Imprime á */
  335.         ascii = FALSE;
  336.         b=getc(fp);                /* Va chercher autre car. à l'input*/
  337.     } 
  338. }
  339.  
  340. VOID rien_a_foutre()            /* Erreur, on sort */
  341. {
  342.     printf("\tIllegal option!\n");
  343.     if (fp) fclose(fp); 
  344.     if (pf) fclose(pf); 
  345.     exit(0);
  346. }
  347.