program trans;

{$I "include:utils/stringlib.i"}

var ligne,nom_dep,nom_arr:string;
    fichier_dep,fichier_arr:text;
    result,i,l,k,n:integer;
    cond,long:boolean;
    t:array [1..2,1..13] of char;
    c:char;

procedure init;
begin
 write('');
 cond:=true;
 n:=13;
 t[1,1]:='é';t[2,1]:='‚';
 t[1,2]:='è';t[2,2]:='Š';
 t[1,3]:='à';t[2,3]:='…';
 t[1,4]:='ê';t[2,4]:='ˆ';
 t[1,5]:='ç';t[2,5]:='‡';
 t[1,6]:='â';t[2,6]:='ƒ';
 t[1,7]:='ù';t[2,7]:='—';
 t[1,8]:='î';t[2,8]:='Œ';
 t[1,9]:='«';t[2,9]:='®';
 t[1,10]:='»';t[2,10]:='¯';
 t[1,11]:='°';t[2,11]:='ø';
 t[1,12]:='ô';t[2,12]:='“';
 t[1,13]:='û';t[2,13]:='–';
end;

procedure texte;
begin
 writeln;
 writeln('                                 TRANS v1.5');
 writeln;
 writeln('  TRANS est un programme qui permet de transformer dans un fichier les');
 writeln('lettres qui ne sont pas compatible avec les imprimantes (é,è,à,ç,°,...).');
 writeln('On definit le fichier au lancement dans la ligne de commande : ');
 writeln('trans \'nom_du_fichier\'. Le programme crée automatiquement un fichier');
 writeln('\'nom_du_fichier.trans\'. Si le nom du fichier est help, se petit texte');
 writeln('apparaîtra à l\'ecran. Il faudra alors recommencer l\'opération en precisant');
 writeln('le chemin du fichier (par exemple : \'df0:help\').');
 writeln;
 cond:=false;
 if l=0 then
 begin
   write('appuyez 2 fois sur entrée');
   readln(c);
 end;
end;

procedure entre_nom;
begin
 l:=strlen(commandline);
 strncpy(nom_dep,commandline,l-1);
 if (stricmp(nom_dep,"help")=0) or (l=1) or (l=0) then texte;
 strcpy(nom_arr,nom_dep);
 strcat(nom_arr,".trans");
end;

procedure ouvre_fichiers;
begin
 if cond=true then
 begin
  writeln('Ouverture du fichier source ... ');
  {$I-}
   reset(fichier_dep,nom_dep);
  {$I+}
  result:=ioresult;
  if result<>0 then
  begin
   if result=205 then
   begin
    writeln('Fichier non trouvé!');
    writeln('Veuillez réessayer');
    cond:=false;
   end;
   if result=202 then
   begin
    writeln('Fichier en utilisation!');
    writeln('Veuillez réessayer');
    cond:=false;
   end;
   if result=224 then
   begin
    writeln('Fichier protégé en lecture!');
    writeln('Veuillez réessayer');
    cond:=false;
   end;
  end;
  if cond=true then
  begin
   writeln('Ouverture reussie');
   writeln;
   writeln('Ouverture du fichier cible ... ');
   {$I-}
    rewrite(fichier_arr,nom_arr);
   {$I+}
   result:=ioresult;
   if result<>0 then
   begin
    if result=213 then
    begin
     writeln('Disk non validé!');
     writeln('Veuillez réessayer');
     cond:=false;
    end;
    if result=214 then
    begin
     writeln('Disk protége!');
     writeln('Veuillez réessayer');
     cond:=false;
    end;
   end;
   if cond=true then writeln('Ouverture reussie');
  end;
  writeln;
 end;
end;

procedure lit_ligne;
begin
 readln(fichier_dep,ligne);
 l:=strlen(ligne);
end;

procedure check;
begin
 c:=ligne[i];
 for k:=1 to n do
  if c=t[1,k] then c:=t[2,k];
 ligne[i]:=c;
end;

procedure ecrit_ligne;
begin
 writeln(fichier_arr,ligne);
end;

begin
 init;
 ligne   := allocstring(255);
 nom_dep := allocstring(80);
 nom_arr := allocstring(80);
 entre_nom;
 ouvre_fichiers;
 if cond=true then
 begin
  writeln('Transformation des caractéres parasites ...');
  while not eof(fichier_dep) do
  begin
   lit_ligne;
   for i:=0 to l do
    check;
   ecrit_ligne;
  end;
  writeln('Transformation terminée!');
 end;
 freestring(ligne);
 freestring(nom_dep);
 freestring(nom_arr);
end.
