Subj : sorry for posting C source To : All From : Jasen Betts Date : Sun Aug 17 2003 10:29 am Hi All. Subj. I missed the change from C_ECHO to PASCAL.... oops! Here's the whole program converted to pascal sort-of it's not the most efficient way to do it, (ram or speed) that would be to do a byte-by-byte comparison of the two files and to rewrite resync to understand chracters streams... as a bonus there'd be no line length limits that way program diff; const LENGTH= 1000; { max line length } type line = array [0..length] of char; procedure MyFgets(var s:line;var f:FILE); var space:word; p:pchar;c,d:char; pos:longint; procedure addchar(c:char); begin p^:=c; inc(p); dec(space); end; begin space:=length; p:=@s[0]; if eof(f) then begin s[0]:=#0; exit;end; while length >0 do begin if eof(f) then begin {terminate unterminated lines} addchar(#10); break; end; blockread(f,c,1); if c in [#10,#13] then {handle #10#13 #13#10 #10 and #13 as EOL} begin { addchar(#10); } pos:=filepos(f); if not eof(f) then begin blockread(f,d,1); if (d=c) or not (d in [#10,#13]) then seek(f,pos); end; break; end; addchar(c); end; addchar(#0); end; function mystrcmp(var s,t:line):boolean; {false if lines match} var p, q:pchar; begin p:=@s[0];q:=@t[0]; mystrcmp:=true; repeat if p^ <> q^ then exit; inc(p); inc(q); until p^=#0; mystrcmp:=p^ <> q^; end; procedure resync(var f1,f2:FILe ;matches:integer); { attempts to resynchronise two file pointers, ** succeeds with both pointing to the next matching line ** fails with poth pointing to EOF - could be considered succes. } const l1:line='';l2:line='';lc1:line='';lc2:line=''; var po1,po2,pm1,pm2,pl1,pl2:longint; n,depth,y:integer; begin po1:=filepos(f1); po2:=filepos(f2); for depth:=1 to maxint do begin { assertion: for all n,m <= depth : line[f1,m] != line[f2,n] * * so read the next line from file1 and compare it with the * * depth lines from file2 starting at po2 } pl1:=filepos(f1); MyFgets(l1,f1); seek(f2,po2 ); for y:=1 to depth-1 do begin pl2:=filepos(f2); MyFgets(l2,f2); if not MyStrcmp(l1,l2)then begin { we have a match! } pm1:=filepos(f1); { store file pointers } pm2:=filepos(f2); for n:=1 to matches do begin { check next matches-1 lines also match } if n= matches then break; MyFgets(lc1,f1); MyFgets(lc2,f2); if MyStrcmp(lc1,lc2) then break; end; if n>=matches then begin { success! } seek(f1,pl1 ); seek(f2,pl2 ); exit ; end; seek(f1,pm1 ); { failure: restore pointers, keep looking } seek(f2,pm2 ); end; end; pl2:=filepos(f2); { this section is a mirror image of the } MyFgets(l2,f2); { section above, check for a match from } seek(f1,po1 ); { from the other side } for y:=1 to depth-2 do begin pl1:=filepos(f1); MyFgets(l1,f1); if not MyStrcmp(l1,l2) then begin pm1:=filepos(f1); pm2:=filepos(f2); for n:=1 to matches do begin if n= matches then break; MyFgets(lc1,f1); MyFgets(lc2,f2); if MyStrcmp(lc1,lc2) then break; end; if(n>=matches) then begin seek(f1,pl1 ); seek(f2,pl2 ); exit; end; seek(f1,pm1 ); seek(f2,pm2 ); end; end; end; end; procedure process( s1,s2:string;context:integer); var f1,f2:file; line1,line2: line; pos1,pos2,match1,match2,matches,ld1,ld2:longint; buf:pchar; begin assign(f1,s1); reset(f1,1); if(IOResult <> 0 ) then halt(1); assign(f2,s2); reset(f2,1); if(IOResult <> 0 ) then halt(1); { Can't do this in pascal-- major slow-up! if(NULL==(buf=malloc(BUFFERSIZE))) return -3; setvbuf(f1,buf,_IOFBF,BUFFERSIZE); if(NULL==(buf=malloc(BUFFERSIZE))) return -3; setvbuf(f2,buf,_IOFBF,BUFFERSIZE); } writeln('comparing[',s1,'] and [',s2,']'); matches:=0; while true do begin pos1:=filepos(f1); pos2:=filepos(f2); if( eof(f1) and eof(f2)) then break; MyFgets(line1,f1); MyFgets(line2,f2); if(MyStrcmp(line1,line2)) then begin seek(f1,pos1); seek(f2,pos2); resync(f1,f2,context); match1:=filepos(f1); seek(f1,pos1 ); ld1:=0; while filepos(f1) 0) then writeln ('C ',matches); if(ld1 <> 0 ) then writeln ('D ', ld1); if(ld2 <> 0 ) then begin writeln('A ',ld2); seek(f2,pos2 ); while(filepos(f2)0 then writeln ('C ',matches); end; begin process(paramstr(1),paramstr(2),1) end. *---/ -=> Bye <=- --- * Origin: Entropy isn't what it used to be. (3:640/1042) .