Pascal source: MERGE.PAS


Program merge;
{$APPTYPE CONSOLE}

uses
   SysUtils, Classes;

var
    txt1,nsdvar2:TStringList;
    txt2,nsdvar1:TStringList;
    varfil:TStringList;


Function Button1Click(FileName:String):integer;
var
    s:string;
    i,hash:integer;
    vtmp:TStringList;
    nvars,nplus:integer;
begin

    txt1:=TStringList.Create();
    vtmp:=TStringList.Create();
    varfil:=TStringList.Create();
    txt1.Sorted:=true;
    txt1.LoadFromFile(FileName);
    s:=ChangeFileExt(FileName,'.var');
    vtmp.Sorted:=false;
    vtmp.LoadFromFile(s);
    varfil.addstrings(vtmp);
    nsdvar1:=TStringList.Create();
    nvars:=0;
    for i:=0 to vtmp.count-1 do begin
        hash:=pos('#',vtmp.strings[i]);
        if (hash>0) then begin
           nsdvar1.add(copy(vtmp.strings[i],1,hash-1));
           inc(nvars);
        end;
    end;
    write('Fil1: ',vtmp.count);
    vtmp.Free;
    Button1Click:=nvars;
 end;


//---------------------------------------------------------------------------

Function Button2Click(FileName:string):integer;
var
    vtmp:TStringList;
    s:string;
    hash,i:integer;
    nvars,nplus:integer;

begin
    vtmp := TStringList.Create();
    txt2 := TStringList.Create();
    txt2.Sorted:=true;
    txt2.LoadFromFile(FileName);
    s:=ChangeFileExt(FileName,'.var');
    vtmp.Sorted:=false;
    vtmp.LoadFromFile(s);
    varfil.addstrings(vtmp);
    nsdvar2:=TStringList.Create();
    nvars:=0;
    for i:=0 to vtmp.count-1 do begin
        hash:=pos('#',vtmp.strings[i]);
        if (hash>0) then begin
           nsdvar2.add(copy(vtmp.strings[i],1,hash-1));
           inc(nvars);
        end;
    end;
    write(' Fil2: ',vtmp.count);
    vtmp.free;
    Button2Click:=nvars;
end;

procedure Button3Click(from,top:integer; FileName:string; checkbox1:boolean);
var
    i, ix1, ix2, maxi1, maxi2:integer;

    Merge :TStringList;

    key1,key2,mrgstr1, mrgstr2:string;
    str1,str2:string;
begin
    if (top=from) then inc(top);
    Merge:=TStringList.Create();
    ix1:=0;
    ix2:=0;
    maxi1:=txt1.Count-1;
    maxi2:=txt2.Count-1;
    write(' maxi1 maxi2: ',maxi1,' ',maxi2);
    mrgstr2:='';
    for i:=1 to nsdvar2.count-1 do
       mrgstr2:=mrgstr2+nsdvar2.strings[i]+chr(9);
    mrgstr2:=trim(mrgstr2);
    mrgstr1:='';
    for i:=1 to nsdvar1.count-1 do
       mrgstr1:=mrgstr1+nsdvar1.strings[i]+chr(9);
    mrgstr1:=trim(mrgstr1);
    repeat
//    while (ix1<=maxi1) and (ix2<=maxi2) do begin
      key1:=copy(txt1.Strings[ix1],from,top-from);
      key2:=copy(txt2.Strings[ix2],from,top-from);
      str1:=trim(txt1.Strings[ix1]);
      str2:=trim(txt2.Strings[ix2]);
      if (key1<key2) then begin
         if (ix1<maxi1) then
           Merge.Add(str1+chr(9)+key1+chr(9)+mrgstr2);
         inc(ix1);
      end else if (key1=key2) then  begin
         if (ix2<=maxi2) and (ix1<=maxi1) then
           Merge.Add(str1+chr(9)+str2);
         inc(ix1);inc(ix2);
      end else begin
(*    Key1 > key2 *)
         if (ix2<maxi2) and (checkbox1=false) then
           Merge.Add(key2+chr(9)+mrgstr1+chr(9)+str2);
         inc(ix2);
      end;
//    end;
    until (ix1>maxi1) or (ix2>maxi2);
    if (ix1<=maxi1) then
       for i:=ix1 to maxi1 do begin
         key1:=copy(txt1.Strings[i],from,top-from);
         str1:=trim(txt1.Strings[i]);
         Merge.Add(str1+chr(9)+key1+chr(9)+mrgstr2)
       end
    else if (ix2<=maxi2) and (checkbox1=false) then
       for i:=ix2 to maxi2 do begin
         key2:=copy(txt2.Strings[i],from,top-from);
         str2:=trim(txt2.Strings[i]);
         Merge.Add(key2+chr(9)+mrgstr1+chr(9)+str2);
       end;
    write(' ix1 ix2: ',ix1,' ',ix2);
    Merge.SaveToFile(FileName);
    varfil.SaveToFile(ChangeFileExt(FileName,'.var'));
    Merge.free;
end;

var i1, i2, uf, fras, lengde, lu:string;
    oppslag:boolean;
    nvar1,nvar2:integer;

Procedure NSDdokfil(fn1,fn2,uf:string;nvar1,nvar2:integer);
var i:integer;
begin
    nsdvar1.Clear;
    nsdvar2.clear;
    varfil.Clear;
    nsdvar1.LoadFromFile(ChangeFileExt(fn1,'.dok'));
    nsdvar2.LoadFromFile(ChangeFileExt(fn2,'.dok'));
    varfil.addstrings(nsdvar1);
    varfil.addstrings(nsdvar2);
    varfil.SaveToFile(ChangeFileExt(uf,'.dok'));
    nsdvar1.free;
    nsdvar2.free;
    varfil.free;
end;

begin
  if (ParamCount<5) then
     writeln('MERGE innfil1 innfil2 utfil fra lengde lookup=TRUE/FALSE')
  else begin
     i1:=paramstr(1);
     i2:=paramstr(2);
     uf:=paramstr(3);
     fras:=paramstr(4);
     lengde:=paramstr(5);
     lu:=paramstr(6);
     nvar1:=Button1Click(i1);
     nvar2:=Button2Click(i2);
     if lu='FALSE' then
        oppslag:=false
     else
        oppslag:=true;
     Button3Click(Strtoint(fras),Strtoint(lengde),uf,oppslag);
     NSDdokfil(i1,i2,uf,nvar1,nvar2);
     writeln('Ferdig');
  end;
end.

Generated by PAS2HTML, copyrights © 1996,97 by COAS, All rights reserved.