home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l041 / 2.ddi / REFLEX.ARC / BUILDKEY.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-12-31  |  7.4 KB  |  272 lines

  1. (****************************************************************)
  2. (*                     DATABASE TOOLBOX 4.0                     *)
  3. (*     Copyright (c) 1984, 87 by Borland International, Inc.    *)
  4. (*                                                              *)
  5. (*     Purpose:   Builds index files from .DAT files and .FD    *)
  6. (*                file specs                                    *)
  7. (*                                                              *)
  8. (****************************************************************)
  9. program BuildKey;
  10. uses DOS,
  11.      CRT,
  12.      TAccess,
  13. {    If a compiler error occurs here, the Turbo Pascal compiler cannot
  14.      find the TAccess unit.  You can compile and configure the TAccess
  15.      unit for your database project by using the TABuild utility. See
  16.      the manual for detailed instructions. }
  17.  
  18.      MiscTool,
  19. {    If a compiler error occurs here, you need to unpack the source
  20.      to the MiscTool unit from the archived file Tools.arc.  See the
  21.      README file on disk 1 for detailed instructions. }
  22.  
  23.      FileUtil,
  24.      EditLn,
  25.      FieldDef;
  26. {$V-}
  27. { $define debug}
  28. const
  29.   ProgName = 'BuildKey';
  30.   Version = '4.00';
  31.   CopyrightMsg = 'Copyright (C)';
  32.   Year = '1987';
  33.   Company = 'Borland International';
  34.   Description = ' creates Turbo Access Index file(s) from a TA data file';
  35.  
  36. type
  37.   LongIntPtr = ^LongInt;
  38.   IndexFilePtr = ^IndexFile;
  39.   BuildInfoRec = record
  40.                    FD : FieldDirectory;
  41.                    DatF : DataFile;
  42.                    DataRec : Pointer;
  43.                    CurRecNum : LongInt;
  44.                    Indexes : array[0..MaxKeys] of IndexFilePtr;
  45.                  end;
  46. var
  47.   DefFileNm : FileSpec;
  48.   InputFileNm : FileName;
  49.   BuildInfo : BuildInfoRec;
  50.  
  51.   FD : FieldDirectory;
  52.   DatF : DataFile;
  53.  
  54. procedure ProgramInfo;
  55. begin
  56.   Writeln(ProgName, ' version ', Version);
  57.   Writeln(CopyrightMsg, ' ', Year, ' ', Company);
  58.   Writeln;
  59.   Writeln(ProgName, ' ', Description);
  60.   Writeln;
  61. end; { ProgramInfo }
  62.  
  63. const
  64.   FileChars : CharSet = [#32..#127];
  65.   Terminators : CharSet = [CR, Esc];
  66.  
  67. function GetFileName(prompt : String;
  68.                      var F : FileName;
  69.                      ExistCheck: boolean) : boolean;
  70. var
  71.   SaveX,
  72.   SaveY : byte;
  73.   TC : Char;
  74.   AllDone : boolean;
  75.  
  76. begin
  77.   GetFileName := false;
  78.   Write(Prompt, ' ');
  79.   SaveX := WhereX; { Save Initial X coordinate }
  80.   SaveY := WhereY;
  81.   repeat
  82.     EditLine(F, SizeOf(FileName) - 1, SaveX, SaveY,
  83.              FileChars, Terminators, TC);
  84.     case TC of
  85.       CR : if ExistCheck then
  86.              Alldone := Exist(F)
  87.            else
  88.              Alldone := true;
  89.       Esc : Alldone := true;
  90.     end;
  91.     if not AllDone then
  92.       Beep;
  93.   until Alldone;
  94.   GetFileName := TC <> Esc;
  95.   Writeln;
  96. end; { GetFileName }
  97.  
  98. procedure OpenDataBase(var BuildInfo : BuildInfoRec;
  99.                        FileNm : FileName);
  100. var
  101.   CurIndexF : integer;
  102. begin
  103.   with BuildInfo, FD do
  104.   begin
  105.     Writeln;
  106.     if not GetFileName('Turbo Access data file:', FileNm, true) then
  107.       Abort('User Terminated')
  108.     else
  109.       Writeln;
  110.     OpenFile(DatF, FileNm, GetPascalRecSize(FD));
  111.     if not Ok then
  112.       Abort('Could not open Turbo Access data file: '+ FileNm);
  113.     GetMem(DataRec, RecordSize);
  114.     for CurIndexF := 0 to pred(NumberOfKeys) do
  115.     begin
  116.       new(Indexes[CurIndexF]);
  117.       with KeyEntries[CurIndexF]^ do
  118.       begin
  119.         if IndexFileNm = '' then
  120.           IndexFileNm := Copy(KeyName, 1, 8) + '.IDX';
  121.         if not GetFileName(KeyName + ' index file:', IndexFileNm, false) then
  122.           Abort('User Terminated');
  123.         MakeIndex(Indexes[CurIndexF]^, IndexFileNm, pred(Size), Status);
  124.         if not Ok then
  125.           Abort('Could not create Turbo Access index file: ' + IndexFileNm);
  126.        end;
  127.     end;
  128.   end;
  129. end;  { OpenDatabase }
  130.  
  131. procedure CloseDatabase(var BuildInfo : BuildInfoRec);
  132. var
  133.   CurIndexF : integer;
  134. begin
  135.   with BuildInfo, FD do
  136.   begin
  137.     CloseFile(DatF);
  138.     for CurIndexF := 0 to pred(NumberOfKeys) do
  139.       CloseIndex(Indexes[CurIndexF]^);
  140.   end;
  141. end; { CloseDatabase }
  142.  
  143. function Min(x, y : integer) : integer;
  144. begin
  145.   if x < y then
  146.     Min := x
  147.   else
  148.     Min := y;
  149. end; { Min }
  150.  
  151. procedure RebuildKeys(var BuildInfo : BuildInfoRec);
  152. type
  153.   StringPtr = ^String;
  154.   BytePtr = ^byte;
  155. var
  156.   Key,
  157.   SubKeyStr : String;
  158.   CurSubKey : integer;
  159.   MaxBytes : integer;
  160.   LenPtr : BytePtr;
  161.   StrPtr : StringPtr;
  162.   CurIndexF : integer;
  163.   RecOffset : integer;
  164.  
  165. begin
  166.   with BuildInfo, FD do
  167.   begin
  168.   {$IFDEF DEBUG}
  169.   Writeln('Current Record : ', CurRecNum);
  170.   {$ENDIF}
  171.     for CurIndexF := 0 to pred(NumberOfKeys) do
  172.     with KeyEntries[CurIndexF]^ do
  173.     begin
  174.       Key := '';
  175.       for CurSubKey := 0 to pred(NumSubKeys) do
  176.       with SubKeys[CurSubKey] do
  177.       begin
  178.         SubKeyStr := '';
  179.         MaxBytes := Min(pred(Size), NumBytes);
  180.         (* Dec(MaxBytes, Length(Key)); *)
  181.         with FieldEntries[FieldNum]^ do
  182.           LenPtr := BytePtr(Ptr(Seg(DataRec^) , Ofs(DataRec^) + RecOffset));
  183.         {$IFDEF DEBUG}
  184.         Write('Length of Str: ', LenPtr^);
  185.         Readln;
  186.         {$ENDIF}
  187.         MaxBytes := Min(MaxBytes, LenPtr^);
  188.         StrPtr := StringPtr(succ(LongInt(LenPtr)));
  189.         {$IFDEF DEBUG}
  190.         Write('Max Bytes: ', MaxBytes);
  191.         Readln;
  192.         {$ENDIF}
  193.         Move(StrPtr^, SubKeyStr[1], MaxBytes);
  194.         Length(SubKeyStr) := MaxBytes;
  195.         SubKeyStr := UpCaseStr(SubKeyStr);
  196.         {$IFDEF DEBUG}
  197.         Write('Current SubKey : ', SubKeyStr);
  198.         Readln;
  199.         {$ENDIF}
  200.         Key := Key + SubKeyStr;
  201.       end;
  202.       {$IFDEF DEBUG}
  203.       Write('Current Key ', Key);
  204.       Readln;
  205.       {$ENDIF}
  206.       AddKey(Indexes[CurIndexF]^, CurRecNum, Key);
  207.       if not Ok then
  208.         Abort('Error adding key ' + Key);
  209.     end;
  210.   end;
  211. end; { RebuildKeys }
  212.  
  213. procedure Rebuild(var DefFileNm : FileName);
  214. var
  215.   CurRec : LongInt;
  216.   DefFileSpec,
  217.   DataFName : FileSpec;
  218.  
  219. begin
  220.   GetFileSpec(DefFileSpec, DefFileNm);
  221.   with DefFileSpec do
  222.   begin
  223.     if (Ext = '') and not (Name = '') then
  224.       Ext := FDExt;
  225.      DefFileNm := Path + Name + Ext;
  226.      if not GetFileName('Field definition file:', DefFileNm, true) then
  227.        Abort('Could not open ' + Path + Name + Ext)
  228.      else
  229.        with BuildInfo do
  230.       begin
  231.         LoadFD(FD, DefFileNm);
  232.         GetFileSpec(DefFileSpec, DefFileNm);
  233.         with FD, DataFName do
  234.         begin
  235.           if DataFileNm <> '' then
  236.           begin
  237.             GetFileSpec(DataFName, DataFileNm);
  238.             if Path = '' then
  239.               Path := DefFileSpec.Path;
  240.           end
  241.           else
  242.           begin
  243.             DataFName := DefFileSpec;
  244.             Ext := '';
  245.           end;
  246.           if Ext = '' then
  247.             Ext := FileDefaults[TAccessFile].Ext;
  248.           OpenDatabase(BuildInfo, Path + Name + Ext);
  249.         end;
  250.         Writeln('Building keys: ');
  251.         for CurRecNum := 1 to pred(FileLen(DatF)) do
  252.         begin
  253.           GetRec(DatF, CurRecNum, DataRec^);
  254.           Write(CurRecNum:10, #13);
  255.           if LongIntPtr(DataRec)^ = 0 then
  256.             RebuildKeys(BuildInfo);
  257.         end;
  258.         Writeln;
  259.         CloseDatabase(BuildInfo);
  260.       end;
  261.     end;
  262. end; { Rebuild }
  263.  
  264. begin
  265.   ProgramInfo;
  266.   if ParamCount = 0 then
  267.     InputFileNm := ''
  268.   else
  269.     InputFileNm := ParamStr(1);
  270.   Rebuild(InputFileNm);
  271. end.
  272.