Metropoli BBS
VIEWER: unix2dos.pas MODE: TEXT (ASCII)
program unix2dos;

{ Tiny hack to convert unix style text files into DOS by inserting CR's     }
{ This one showes handling of huge files by processing them entirely in     }
{ memory (so they fit).                                                     }

var 
    f:file of byte;
    p,pcrlf:pointer;
    k,l,m,n,o:integer;
    mem:array [0..$7ffffffe] of byte absolute 0;
    crlf:integer;
begin
  if paramcount = 0 then writeln ('Argument expected') else begin
    assign (f,paramstr(1));
    {$I-} reset(f) {$I+};
    if ioresult <> 0 then writeln ('Error opening file ',paramstr(1)) else begin
      l:=filesize(f);
      if l > 0 then begin
        getmem (p,l);
        asm		{ since a pointer basically is nothing but a dword }
          mov eax,p     { we can cast it into integers with this trick     }
          mov m,eax
          mov n,eax
          mov o,eax
        end;
        if p = nil then writeln ('Not enough memory') else begin
          blockread(f,p^,l);
          close(f);
          {$I-} rewrite(f) {$I+};
          if ioresult <> 0 then writeln ('Error writing file ',paramstr(1)) else begin
            crlf:=$a0d;
            pcrlf:=@crlf;
            k:=0;
            while ((m-o) < l) do begin
              if (mem[m] = 10) then begin
                if (o = m) or (mem[m-1] <> 13) then begin
                  asm
                    mov eax,n
                    mov p,eax
                  end;
                  if ((m-n) > 0) then blockwrite(f,p^,m-n);
                  blockwrite(f,pcrlf^,2);
		{ some sort of progress indicator... }
                  inc(k);
                  if k=100 then begin
                    write('.');
                    k:=0;
                  end;
                  n:=m+1;
                end;
              end;
              inc(m);
            end;
            asm
              mov eax,n
              mov p,eax
            end;
            if l>(n-o) then blockwrite (f,p^,l-(n-o));
            close(f);
          end;
        end;
      end;
    end;
  end;
end.
[ RETURN TO DIRECTORY ]