home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / answcode / answers.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-10-27  |  17.5 KB  |  640 lines

  1. {$R-,S-,I-,D+,T-,F+,V-,B+,N+,L+ }
  2. {$M 65520,0,655360 }
  3. Program Answers10; { Version 4.0 }
  4.  
  5. (*********************************************************)
  6. (********** ANSWERS ! Version 4.0 October 27, 1988 *******)
  7. (********** Copyright 1988, Brian Corll ******************)
  8. (********** All Rights Reserved         ******************)
  9. (*********************************************************)
  10.  
  11. (*************************************************************)
  12. (* This is the original source code to ANSWERS ! Version     *)
  13. (* 4.0, which I distributed last summer.  ANSWERS ! is a     *)
  14. (* plain and simple text file indexing and retrieval sys-    *)
  15. (* tem.  For more information, download the original file    *)
  16. (* ANSWER40.arc.                                             *)
  17. (* This program is now in the public domain.  If you make    *)
  18. (* any improvements on it ( and many could be made; this     *)
  19. (* is just the nucleus) please share them with me.           *)
  20. (* Questions or comments may be left on these boards:        *)
  21. (*      Cliffside Park  201-886-8041                         *)
  22. (*      MicroSellar     201-239-1346                         *)
  23. (*      Turbo Source Search  617-545-9131                    *)
  24. (*                                                           *)
  25. (*                    Brian Corll                            *)
  26. (*                    102 West Locust Street                 *)
  27. (*                    Mechanicsburg, PA 17055                *)
  28. (*                    717-691-0286                           *)
  29. (*                    October 27, 1988                       *)
  30. (*************************************************************)
  31. (* Compilation requires Jim LeMay's Qwik,Wndw, and Wndwvars; *)
  32. (* Juan Vegarra's DER12.arc, and the Turbo Database Toolbox  *)
  33. (*************************************************************)
  34.  
  35. Uses CRT,Dos,Turbo3,TAccess,TAHigh,Qwik,Wndw,Wndwvars,Der,Utils;
  36.  
  37. Const
  38.  MaxWndw = 100;
  39.  
  40. Type
  41.    String80 = String[80];
  42.    IndexType = String[6];
  43.    Pointers = Record
  44.             IndexWord : IndexType;
  45.             PtrArray : Array[1..200] of Integer;
  46.             end;
  47.    TextData = record
  48.             TextLine : String80;
  49.             end;
  50.  
  51.    Regs =
  52.              Record Case Boolean of
  53.              True : (al,ah,bl,bh,cl,ch,dl,dh : Byte);
  54.              False : (ax, bx, cx, dx, bp,si,di,ds,es,Flags : Registers)
  55.                      end;
  56.  
  57.     Config = Record
  58.            Colors : Array[1..18] of Byte;
  59.           end;
  60.    String6 = String[6];
  61.    Elements = Array[1..100] of Integer;
  62.    Element = Integer;
  63.    String20 = String[20];
  64. MaxDataType = Pointers;
  65. MaxKeyType = IndexType;
  66.  
  67. Var
  68.   Color : Array[1..4] of Byte;
  69.   ConfigFile : DataFile;
  70.   ConfigRec : Config;
  71.   TextDataFile : File of TextData;
  72.   TextDataRec : TextData;
  73.   PointerFile : DataSet;
  74.   PointerRec : Pointers;
  75.   Times : Byte;
  76.   TheCowsComeHome : Boolean;
  77.   FS : Integer;
  78.   St : String[5];
  79.   Quit : string[1];
  80.   TC,GoAhead : Char;
  81.   Ch : Char;
  82.   Root : String[8];
  83.   NewStr,TextKey,Line6 : String[6];
  84.   H,TotLines,RecordNum,Lines,X,Z,G,P,M,A,Pntr,Stop,Number,Comma,LineCount,Position,Result,I,K : Integer;
  85.   Row,Col,IndexSize,Spot,Head,Foot,RedLine,EndPoint,NumChars : Integer;
  86.   Query : String[55];
  87.   Message,ParseWord : String[80];
  88.   ParseStr : String[6];
  89.   WordIn : String20;
  90.   Answers : Array[1..2,1..1000] of Integer;
  91.   FinalWord : Elements;
  92.   ChoiceIn : String[5];
  93.   NumLines,Code : Integer;
  94.   WhyNot,Choice : Byte;
  95.   ChoiceNumbers : Array[1..24] of Integer;
  96.   BC,ZZ,Q : Integer;
  97.   Color1,Color2 : String[12];
  98.  
  99. Procedure Wait;
  100. Var
  101.    Ch : Char;
  102. begin
  103.      ModCursor(CursorOff);
  104.      QWrite(WhereR,WhereC,White+RedBG,'Press any key to continue....');
  105.      Read(kbd,Ch);
  106.      gotoxy(1,WhereY);
  107.      ClrEol;
  108.      ModCursor(CursorOn);
  109. end;
  110.  
  111. Procedure Block;
  112. Var
  113.    Reg : Registers;
  114.  
  115. begin
  116.      with Reg do
  117.      begin
  118.      ch := 01;
  119.      cl := 12;
  120.      ah := 1;
  121.      Intr($10,Reg)
  122.      end
  123. end;
  124.  
  125. Procedure Configuration;
  126.    Var
  127.       Finished : Byte;
  128.       TC : Char;
  129.    begin
  130.           InitWindow(0,True);
  131.         ModCursor(CursorOff);
  132.         MakeWindow(7,10,7,60,White+BlackBG,White+BlackBG,DoubleBrdr,Window1);
  133.         TitleWindow(Top,Center,'Color Selection');
  134.         Repeat
  135.         MakeFile(ConfigFile,'answers.cfg',SizeOf(ConfigRec));
  136.           For Times := 1 to 18 do
  137.                 ConfigRec.Colors[Times] := 0;
  138.         ClearWindow;
  139.         QWrite(8,12,White+BlackBG,'Select a Color for each of the following:');
  140.         QWrite(9,12,White+BlackBG,'Main Text Color: ');
  141.         QWrite(10,12,White+BlackBG,'Error Message Color: ');
  142.         QWrite(11,12,White+BlackBG,'HighLight Color: ');
  143.         ConfigRec.Colors[1] := ColorSelect(9,30,1,1);
  144.         QWrite(9,12,ConfigRec.Colors[1],'Main Text Color: ');
  145.         ConfigRec.Colors[2] := ColorSelect(10,33,1,1);
  146.         QWrite(10,12,ConfigRec.Colors[2],'Error Message Color: ');
  147.         ConfigRec.Colors[3] := ColorSelect(11,30,1,1);
  148.         QWrite(11,12,ConfigRec.Colors[3],'HighLight Color: ');
  149.           PutRec(ConfigFile,1,ConfigRec);
  150.         CloseFile(ConfigFile);
  151.           Finished := 0;
  152.         QWrite(12,12,White+BlackBg,'Are you satisfied with these colors ? (Y or N)');
  153.         NormalAtt  := Attr(White,Blue);
  154.         ReverseAtt := Attr(White,Blue);
  155.         TC := SelectBoolean(Finished,'Y','N',62,12);
  156.         Block;
  157.         Until Finished = 1;
  158.         ModCursor(CursorOff);
  159.         RemoveWindow;
  160.         QWriteC(12,1,80,White+RedBG,'Color Selections have been saved.');
  161. end; (* Procedure Configuration *)
  162.  
  163. Procedure AnswerQuestion;
  164. { $B+}
  165.      Function Clean(var Dirty : String6) : string;
  166.      Var
  167.     Ch : Char;
  168.     OutStr : string[6];
  169.     I : Integer;
  170.      begin
  171.     OutStr := '';
  172.     For I := 1 to Length(TrimL(TrimR(Dirty))) do
  173.     begin
  174.          Ch := Dirty[I];
  175.        If (UpCase(Ch) in ['A'..'Z']) or (Ch in ['0'..'9']) then
  176.        OutStr := OutStr + Ch;
  177.        end;
  178.            Clean := PadR(TrimR(TrimL(OutStr)),6);
  179.      end;
  180. { $B-}
  181.  
  182.      Procedure ScrollDown;Forward;
  183.      Procedure ScrollUp;
  184.  
  185.      begin
  186.           while (Ch = #72) or (ch = #27) do
  187.           begin
  188.           gotoxy(1,1);
  189.           DelLine;
  190.           Foot := Foot + 1;
  191.           Head := Head + 1;
  192.           If Foot>FS then
  193.           begin
  194.           while Foot>FS do
  195.           begin
  196.           Beep;
  197.           QWrite(24,1,Color[2],'End of File.');
  198.           Head := FS-(FS-Head-1);
  199.           Read(kbd,Ch);
  200.           If Ch = #27 then
  201.           begin
  202.           Read(kbd,Ch);
  203.           if ch = #80 then ScrollDown;
  204.           end;
  205.           end;
  206.           end
  207.           else
  208.           begin
  209.           Seek(TextDataFile,Foot-1);
  210.           Read(TextDataFile,TextDataRec);
  211.           with TextDataRec do
  212.           begin
  213.           Message := TextLine;
  214.           If Foot-1 = RedLine then
  215.           begin
  216.           QWrite(23,1,Color[3],PadR(Message,80));
  217.           end
  218.           else
  219.           QWrite(23,1,Color[1],Message);
  220.           Read(kbd,Ch);
  221.           end;
  222.           end;
  223.           end
  224.           end;
  225.  
  226.           Procedure ScrollDown;
  227.  
  228.           begin
  229.           while (Ch = #80) or (Ch = #27) do
  230.           begin
  231.           gotoxy(1,1);
  232.           InsLine;
  233.           Head := Head - 1;
  234.           Foot := Foot - 1;
  235.           If Head<0 then
  236.           begin
  237.           WHILE Head<0 do
  238.           begin
  239.           Beep;
  240.           QWrite(1,1,Color[2],'Beginning of File.');
  241.           Read(kbd,Ch);
  242.           If Ch = #27 then begin
  243.           Read(kbd,Ch);
  244.           If ch = #72 then ScrollUp;
  245.           end;
  246.           end;
  247.           end
  248.           else
  249.           begin
  250.           Seek(TextDataFile,Head);
  251.           Read(TextDataFile,TextDataRec);
  252.           with TextDataRec do
  253.           begin
  254.           Message := TextLine;
  255.           If Head = RedLine then
  256.           begin
  257.           QWrite(1,1,Color[3],PadR(Message,80));
  258.           end
  259.           else
  260.           QWrite(1,1,Color[1],Message);
  261.           Read(kbd,Ch);
  262.           end;
  263.           end;
  264.           end;
  265.           end;
  266.  
  267.           Procedure SortInts(Var FinalWord : Elements; c : Integer);
  268.  
  269.           Var
  270.              d,f,Foot,Head,Middle : Integer;
  271.              Temp : Element;
  272.  
  273.              begin
  274.                   for d := 2 to c do
  275.                   begin
  276.                   Temp := FinalWord[d];Foot := 1;Head := d-1;
  277.                   while Foot<=Head do
  278.                   begin
  279.                   Middle := (Foot+Head) div 2;
  280.                   If Temp<FinalWord[Middle]
  281.                   Then Head := Middle -1
  282.                   else Foot := Middle+1
  283.                   end;
  284.                   for f := d-1 downto Foot do
  285.                   FinalWord[f+1] := FinalWord[f];
  286.                   FinalWord[Foot] := Temp
  287.                   end
  288.              end;
  289.  
  290. begin
  291.      InitWindow(Blue+BlueBG,True);
  292.      Block;
  293.      For I := 1 to 1000 do
  294.      begin
  295.      Answers[1,I] := 0;
  296.      Answers[2,I] := 0;
  297.      end;
  298.      for I := 1 to 100 do
  299.      FinalWord[I] := 0;
  300.      A := 1;
  301.      SetWindowModes($00);
  302.      MakeWindow(4,12,16,60,White+BlueBG,White+BlueBG,DoubleBrdr,Window25);
  303.      QWriteC(22,1,80,Black+GreenBG,'Copyright 1988 Brian Corll');
  304.      gotoxy(3,6);
  305.      write('What topic are you looking for ?');
  306.      gotoxy(3,10);
  307.      write('Press ESCape Key to End Program.');
  308.      Message := '';
  309.      For Q := 1 to 52 do
  310.      begin
  311.      Message := Message+chr(219);
  312.      end;
  313.      gotoxy(3,14);
  314.      write('         ANSWERS !                 VERSION 4.0     ');
  315.      SetWindowModes(ShadowRight);
  316.      MakeWindow(3,28,3,27,Black+GreenBG,Black+GreenBG,HdoubleBrdr,Window9);
  317.      QWriteC(4,28,55,Black+GreenBG,UpperCase(ParamStr(1)));
  318.      TextColor(White);
  319.      TextBackGround(Blue);
  320.      NormalAtt  := Attr(White,Blue);
  321.      ReverseAtt := Attr(White,Blue);
  322.      Block;
  323.      Query := '';
  324.      TC := SelectString(Query,55,14,12);
  325.      If TC = #27 then
  326.      begin
  327.       InitWindow(0,True);
  328.       ModCursor(CursorOn);
  329.      Close(TextDataFile);
  330.      TAClose(PointerFile);
  331.       Halt(1);
  332.       end;
  333.      ModCursor(CursorOff);
  334.       RemoveWindow;
  335.      MakeWindow(3,28,3,27,White+RedBG,White+RedBG,DoubleBrdr,Window11);
  336.      gotoxy(3,2);
  337.      write('      Searching... ');
  338.      Query := UpperCase(TrimR(TrimL(Query)));
  339.      K := 1;
  340.      Number := Words(Query);
  341.      while K<=Number do
  342.       begin
  343.       ParseStr := WordOne(Query,K);
  344.       ParseStr := PadR(copy(Clean(ParseStr),1,6),6);
  345.        TARead(PointerFile,PointerRec,ParseStr,PartialMatch);
  346.      if not Ok then
  347.         begin
  348.         end
  349.      else
  350.      begin
  351.      with PointerRec do
  352.      begin
  353.      Pntr := 1;
  354.      while PtrArray[Pntr]>0 do
  355.      begin
  356.      Answers[1,A] := PtrArray[Pntr];
  357.      Pntr := Pntr + 1;
  358.      A := A + 1;
  359.      end;
  360.      end;
  361.      end;
  362.      K := K + 1;
  363.      end;
  364.      If Answers[1,1]>0 then
  365.      begin
  366.      for Z := 1 to A do
  367.      begin
  368.      G := Answers[1,Z];
  369.      for P := 1 to A do
  370.      begin
  371.      if Answers[1,P] = G then Answers[2,Z] := Answers[2,Z]+1;
  372.      end;
  373.      end;
  374.      G := Answers[2,1];
  375.      for Z := 1 to A do
  376.      begin
  377.      If Answers[2,Z]>G then G := Answers[2,Z];
  378.      end;
  379.      end;
  380.      X := 1;
  381.      for Z := 1 to A do
  382.      begin
  383.      If Answers[2,Z] = G then
  384.      begin
  385.      P := Answers[1,Z];
  386.      FinalWord[X] := P;
  387.      X := X +1;
  388.      end;
  389.      end;
  390.      SortInts(FinalWord,100);
  391.      If FinalWord[100] = 0 then
  392.      begin
  393.      TextBackGround(White);
  394.      ModCursor(CursorOff);
  395.      For Times := 1 to 3 do
  396.      Beep;
  397.      MakeWindow(5,11,13,60,White+RedBG,White+RedBG,DoubleBrdr,Window12);
  398.      gotoxy(12,3);
  399.      QWriteC(8,11,71,White+RedBG,'No answers found to match');
  400.      Message := '"'+UpperCase(Query)+'"';
  401.      QWriteC(11,11,71,White+RedBG,Message);
  402.      QWriteC(14,11,71,White+RedBG,'Press any key to continue.....');
  403.      Read(kbd,Ch);
  404.      RemoveWindow;
  405.      RemoveWindow;
  406.      ModCursor(CursorOn);
  407.      end
  408.      else
  409.      begin
  410.      RemoveWindow;
  411.       InitWindow(0,True);
  412.      GoAhead := 'Y';
  413.      while GoAhead = 'Y' do
  414.       begin
  415.      M := 1;
  416.      ClrScr;
  417.      ModCursor(CursorOff);
  418.      QWrite(1,1,LightCyan+BlackBG,'The following line(s) of text pertain to your question: ');
  419.      H := 0;
  420.      Spot := 2;
  421.      for Z := 99-(x) to 100 do
  422.      begin
  423.      if FinalWord[Z]>0 then
  424.      begin
  425.      If FinalWord[Z]>H then
  426.      begin
  427.      ChoiceNumbers[M] := FinalWord[Z];
  428.      Seek(TextDataFile,FinalWord[Z]-1);
  429.      Read(TextDataFile,TextDataRec);
  430.      with TextDataRec do
  431.      begin
  432.      Str(M,St);
  433.      Message := St + ': '+ TrimL(TextLine);
  434.      QWrite(Spot,1,LightCyan+BlackBG,Message);
  435.      M := M + 1;
  436.      Spot := Spot + 1;
  437.      If (M mod 21) = 0 then
  438.      begin
  439.      gotoxy(1,Spot+1);
  440.      Spot := 2;
  441.      wait;
  442.      ClrScr;
  443.      QWrite(1,1,LightCyan+BlackBG,'These additional line(s) of text pertain to your question: ');
  444.      end;
  445.      end;
  446.      H := FinalWord[Z];
  447.      end;
  448.      end;
  449.      end;
  450.      Choice := 0;
  451.      QWrite(Spot+2,1,White+RedBg,'Enter line number to begin display >>> ');
  452.      Block;
  453.      TextColor(White);
  454.      NormalAtt  := Attr(White,Blue);
  455.      ReverseAtt := Attr(White,Blue);
  456.      TC := SelectByte(Choice,0,M-1,3,42,Spot+2);
  457.      If Choice = 0 then Exit;
  458.      If Choice>0 then
  459.      begin
  460.         ClrScr;
  461.         MakeWindow(1,1,25,80,Color[1],Color[1],NoBrdr,Window2);
  462.          ModCursor(CursorOff);
  463.          Lines :=1;
  464.          RecordNum := ChoiceNumbers[Choice]-1;
  465.          Head := RecordNum;
  466.          RedLine := RecordNum;
  467.              if RecordNum<0 then exit;
  468.              If RecordNum+23>=FS then EndPoint := FS-RecordNum
  469.              else EndPoint := 23;
  470.          while Lines <=EndPoint do
  471.              begin
  472.              Seek(TextDataFile,RecordNum);
  473.          Read(TextDataFile,TextDataRec);
  474.                  with TextDataRec do
  475.                  begin
  476.                  If Lines = 1 then
  477.                  begin
  478.                  Message := TextLine;
  479.                  QWrite(Lines,1,Color[3],PadR(Message,80));
  480.                  end
  481.                  else
  482.             begin
  483.                  Message := TextLine;
  484.                  QWrite(Lines,1,Color[1],Message);
  485.                  end;
  486.              Lines := Lines + 1;
  487.              RecordNum := RecordNum + 1;
  488.          end;
  489.         end;
  490.      Foot := RecordNum;
  491.      QWrite(24,1,Color[4],'     Press '+#24+' Key to Scroll Up , '+#25+' Key to Scroll Down , or Enter Key to Exit     ');
  492.      Read(kbd,Ch);
  493.          If Ch = #27 then
  494.              begin
  495.                 Read(kbd,Ch);
  496.              if (ch = #72) or (ch = #80) then
  497.              begin
  498.              gotoxy(1,24);
  499.              ClrEol;
  500.              while (Ch = #72) or (Ch = #80) do
  501.              begin
  502.              case Ch of
  503.               #72 : ScrollUp;
  504.                #80 : ScrollDown;
  505.                end;
  506.              end;
  507.              end;
  508.          end;
  509.      MakeWindow(12,24,3,37,White+RedBG,White+RedBG,DoubleBrdr,Window20);
  510.      Block;
  511.      write(' Choose another line ? (Y or N) ');
  512.       WhyNot := 0;
  513.           NormalAtt  := Attr(White,Blue);
  514.           ReverseAtt := Attr(White,Blue);
  515.      TC := SelectBoolean(WhyNot,'Y','N',57,13);
  516.       If WhyNot = 2 then Exit;
  517.      RemoveWindow;
  518.      RemoveWindow;
  519.      ClrScr;
  520.       end;
  521.      end;
  522.      ModCursor(CursorOn);
  523.      RemoveWindow;
  524.      RemoveWindow;
  525. end;
  526. end;
  527.  
  528. Procedure Initialize;
  529.  
  530. Procedure OpenFiles;
  531. Var
  532.    Times : Byte;
  533.  
  534. begin
  535.      ModCursor(CursorOff);
  536.      If UpperCase(ParamStr(1))='COLORS' then
  537.      begin
  538.      Configuration;
  539.      Halt(1);
  540.      end;
  541.      TAOpen(PointerFile,ParamStr(1)+'.ptr',SizeOf(PointerRec),ParamStr(1)+'.ndx',SizeOf(IndexType)-1);
  542.      If (not OK) then
  543.      begin
  544.      InitWindow(Black+BlackBG,True);
  545.      MakeWindow(5,15,15,55,Black+LightGrayBG,Black+LightGrayBG,DoubleBrdr,Window30);
  546.      For Times := 1 to 3 do
  547.      Beep;
  548.      gotoxy(2,4);
  549.      write('No files with a root name of ',UpperCase(ParamStr(1)),' were found !');
  550.      gotoxy(2,6);
  551.      write('You will be returned to the DOS prompt in a moment.');
  552.      gotoxy(3,8);
  553.      write('Start the program again using a valid file name.');
  554.      Delay(5000);
  555.      RemoveWindow;
  556.      InitWindow(0,True);
  557.      Halt;
  558.      end
  559.      else
  560.      begin
  561.      Assign(TextDataFile,ParamStr(1)+'.dat');
  562.      Reset(TextDataFile);
  563.      FS := FileSize(TextDataFile);
  564.      end;
  565. end;
  566.  
  567. Procedure StartUp;
  568. begin
  569.      QInit;
  570.      CRTCols := CrtColumns;
  571.      CheckSnow := QSnow;
  572.      TheCowsComeHome := False;
  573.      CheckBreak := False;
  574. end;
  575.  
  576. Procedure GetConfig;
  577. Var
  578.    H,Times : Byte;
  579. begin
  580.      If ParamCount = 0 then
  581.      begin
  582.      For Times := 1 to 3 do
  583.      Beep;
  584.      TextColor(White);
  585.      writeln('Syntax: ANSWERS <filename>');
  586.      Halt;
  587.      end
  588.       else
  589.      If UpperCase(ParamStr(1)) = 'COLORS' then
  590.      begin
  591.      Configuration;
  592.       Halt(1);
  593.      end
  594.       else
  595.       begin
  596.      ClrScr;
  597.      For H := 1 to 4 do
  598.      Color[H] := 0;
  599.       OpenFile(ConfigFile,'answers.cfg',SizeOf(ConfigRec));
  600.       GetRec(ConfigFile,1,ConfigRec);
  601.      Color[1] := ConfigRec.Colors[1];
  602.      Color[2] := ConfigRec.Colors[2];
  603.      Color[3] := ConfigRec.Colors[3];
  604.       Color[4] := Black+LightGrayBG;
  605.      CloseFile(ConfigFile);
  606. end;
  607. end;
  608.  
  609. Procedure NoFiles;
  610. Var
  611.    Times : Byte;
  612.     begin
  613.      ModCursor(CursorOff);
  614.      InitWindow(White+BlackBG,True);
  615.      SetWindowModes(ShadowRight+ZoomMode);
  616.      MakeWindow(10,17,5,50,White+BlackBG,White+BlackBG,DoubleBrdr,Window30);
  617.      For Times:= 1 to 3 do
  618.      Beep;
  619.      QWrite(12,20,White+BlackBG,'NO FILES WERE SPECIFIED ON THE COMMAND LINE.');
  620.      Delay(3000);
  621.      RemoveWindow;
  622.      InitWindow(Black+BlackBG,True);
  623.      ModCursor(CursorOn);
  624.      Halt;
  625. end;
  626.  
  627. begin (* Initialize *)
  628.      If ParamCount=0 then NoFiles;
  629.      StartUp;
  630.      GetConfig;
  631.      OpenFiles;
  632. end; (* Initialize *)
  633.  
  634. begin (* Main Program *)
  635.      Initialize;
  636.      Repeat
  637.      AnswerQuestion
  638.      Until TheCowsComeHome;
  639. end. (* Main Program *)
  640.