-- convert linux/unix 2 dos

-- Dit is de (normale) Bottum-Top versie van TDl2d.exp welke een Top-Down versie is

integer filein , fileout
sequence tekens , cmd
-- with define JTP

procedure cl2d()
integer pos_LF
fileout=open("klad","wb")
while sequence(tekens) do
     pos_LF=find('\n',tekens)
     while pos_LF do
     tekens=tekens[1..pos_LF-1]&'\r'&tekens[pos_LF..]
     pos_LF=find_from('\n',tekens,pos_LF+1)
     end while
     for i=1 to length(tekens) do
         putc(fileout,tekens[i])
     end for
     tekens=get_bytes(filein,100)
end while
end procedure

function convert()
if(filein<3) 
then puts(1,"file niet gevonden\n")
     return 0
else
     tekens=get_bytes(filein,100) 
     if find('\r',tekens) 
     then puts(1,"Dos format\n")
          -- do nothing 
          close(filein)
          return 0
     else     
          puts(1,"Linux format\n")
          cl2d() -- convert to dos           
          close(filein)
          close(fileout)
          return 1
     end if
end if
end function

procedure convert2file(integer index; same)
 filein=open(cmd[index],"rb")
 if convert()
 then removef(cmd[index+same])
      renamef("klad",cmd[index+same])
 end if
end procedure

-- main prog

ifdef JTP -- conditioneel vertalen voor Judo Toernooi Programma

cmd="norm.vrd"
convert2fl(1,0)
cmd="poule.vrd"
convert2fl(1,0)

elsedef --  conditioneel anders

cmd=command_line()
filein=length(cmd)
if match("peu",cmd[1])
then -- interpretor
 if filein < 3 
 then -- no file given give help
 puts(1,"Use : peu l2d fileinout or peu l2d filein fileout\n")
 elsif filein < 4
 then -- input is output file
  convert2file(3,0)
 else -- input differend output file
  convert2file(3,1)
 end if
else -- compiled (bind)
 if filein < 2 
 then -- no file given give help
 puts(1,"Use : l2d fileinout or l2d filein fileout\n")
 elsif filein < 3
 then -- input is output file
  convert2file(2,0)
 else -- input differend output file
  convert2file(2,1)
 end if
end if                         

enddef -- einde conditie
  
abort(0)