program Smartwork_File_To_Pad_Coordinate_File_Transformer;
const cell=1.27;
label zacetek;
type name = string[66];

var
 infile:file of byte;                { vhodna datoteka }
 OutFile:text;                       { izhodna text datoteka}
 infilename,outfilename:name;        { imena vhodnih in izhodnih datotek}
 Xcellsize,Ycellsize:integer;        { velikost tiskanega vezja v celicah}
 num,LeftByte,rightByte:byte;        {splosne spremenlivke iz vhodne datoteke}
 x,y,n,padnr:integer;                {general variables,X/Y poz in st. luknje}
 OK:boolean;
 vrsta:string[80];

function Exist(filename:name): Boolean;   {ali obstaja datoteka na disku}
Var
  fil:file;
  IOr:byte;
begin
  Assign(Fil,Filename);
  {$I-}
  reset(Fil);
  {$I+}
  IOr:=Ioresult;
   if IOr = 1 then
     begin
      Sound(440);
      GotoXY(1,25);
      Write('Datoteke ni mozno najti !');
      Delay(1000);
      Nosound;
      GotoXY(1,25);
      write('                         ')
     end;
   Exist := (IOr = 0);
end;

procedure naprej(step:byte);           {premik naprej po infile}
 begin
  for n:=1 to step do read(Infile,num);
 end;

function Word:integer;                 {vzame dvobyten zlog iz infile}
 var buff:integer;
 begin
   read(InFile,num);
   buff:=256*num;
   read(InFile,num);
   word:=buff+num
 end;

procedure getbyte;                    {vzame levi ni desni del byta iz infile}
  begin
    read(infile,num);
    leftbyte := num div 16;
    rightbyte:= num mod 16;
  end;

function FilenameOK(filename:name):boolean; { kontrola pravilnosti sintakse za}
label konec;                                { ime datoteke}
 var n:integer;
 OK:boolean;
 begin
   n:=1;
   OK:=FALSE;
   if length(filename)=0 then goto konec;
   repeat
      case copy(filename,n,1)  of
        'a'..'z'    :OK:=true;
        'A'..'Z'    :OK:=true;
        '0'..'9'    :OK:=true;
        '.'         :OK:=true;
        ':'         :OK:=true;
        '\'         :OK:=true;
        ' '         :OK:=true;
               else  OK:=FALSE
        end;

      n:=n+1;
   until ( n > Length(filename) ) or NOT OK  ;
  konec:
  if not OK then
       begin
              sound(500);
              delay(600);
              Nosound
       end;
   FilenameOK := OK;
end;

begin  {ZACETEK GLAVNEGA PROGRAMA}
zacetek:
ClrScr;
writeln('SMARTWORK > PAD coordinate transformer');
writeln;
writeln('(C) Copyright Leon Kos 1988');
writeln;

repeat
  GotoXY(1,5);
  write('Ime vhodne datoteke :                                                            ');
  GotoXY(22,5);
  read(Infilename);
until  Exist (Infilename) and  FilenameOK(InFileName);

repeat
   GotoXY(1,7);
   write('Ime izhodne datoteke:'             );
   write('                                                                ');
   GotoXY(22,7);
   read(Outfilename);
Until  FilenameOK(OutfileName);

assign(infile,Infilename);             {kontrola in datoteke}
reset(Infile);
OK:=true;
read(Infile,num); if chr(num) <> 'p' then OK:=false;
read(Infile,num); if chr(num) <> 'w' then OK:=false;
read(Infile,num); if chr(num) <> 'b' then OK:=false;
read(Infile,num); if chr(num) <> 'c' then OK:=false;
If OK then begin end else
  begin
    writeln;
    writeln;
    writeln(Infilename,' ni SMARTWORK datoteka !');
    repeat until keypressed;
    goto zacetek
  end;
naprej(5);
XcellSize:=Word;
YcellSize:=Word;
writeln;
writeln;
write('Minimalna velikost tiskanega vezja je ');
writeln((XcellSize-1)*cell:6:2,' x ',YcellSize*cell:6:2,' [mm]');

assign(outfile,Outfilename);
rewrite(outfile);
naprej(3);
padnr:=1;
window(1,11,80,19);
GotoXY(1,1);

for y:=0 to YcellSize-1 do
    begin
         x:=0;
         repeat
           getbyte;
           if leftbyte = 14  then
              begin
                    writeln(' ³ ',padnr:5,' ³ ',x*cell:10:2,y*cell:10:2,'    [mm]');
                    writeln(Outfile,' ³ ',padnr:5,' ³ ',x*cell:10:2,y*cell:10:2);
                    padnr:=padnr+1    { oblika izpisa za v OUT datoteko}
              end;
           if rightbyte = 14  then
              begin
                    writeln(' ³ ',padnr:5,' ³ ',(x+1)*cell:10:2,y*cell:10:2,'    [mm]');
                    writeln(Outfile,' ³ ',padnr:5,' ³ ',(x+1)*cell:10:2,y*cell:10:2);
                    padnr:=padnr+1
              end;
           x:=x+2
         until x >= Xcellsize
    end;

close(infile);
close(Outfile);
writeln;
writeln('Skupno',padnr-1:6,' lukenj na tiskanem vezju ',InFileName);
writeln;
writeln('     ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»');
writeln('     º    Transformacija koncana !   º');
writeln('     ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ');
for n:=1 to 3 do
  begin
    writeln;
    sound(random(800)+200);
    delay(random(600)+500);
  end;
NoSound;
end.