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

  1. (****************************************************************)
  2. (*                     DATABASE TOOLBOX 4.0                     *)
  3. (*     Copyright (c) 1984, 87 by Borland International, Inc.    *)
  4. (*                                                              *)
  5. (*                        SetConst                              *)
  6. (*                                                              *)
  7. (*  Purpose: Calculates Turbo Access configuration constants    *)
  8. (*           for specified Record and Key Sizes.  SetConst      *)
  9. (*           is used by the TABuild program.                    *)
  10. (*                                                              *)
  11. (****************************************************************)
  12. unit SetConst;
  13.  
  14. interface
  15. uses
  16.   CRT,
  17.   MiscTool,
  18. { If a compiler error occurs here, the Turbo Pascal compiler cannot
  19.   find the TAccess unit.  You can compile and configure the TAccess
  20.   unit for your database project by using the TABuild utility. See
  21.   the manual for detailed instructions. }
  22.  
  23.   EditLn;
  24.  
  25. const
  26.   UseDefaults : boolean = true;
  27.   TypeFileNm : string = '';
  28.   DefFileNm : String = '';
  29.   LargestVar = 65521;
  30.  
  31. var
  32.   DefaultMaxRecSize,
  33.   DefaultMaxKeyLen : integer;
  34.   MaxMemory : Word;
  35.  
  36. procedure TAConstants;
  37.  
  38. implementation
  39.  
  40. type
  41.   Rectangle = record
  42.                 X1, Y1, X2, Y2 : byte;
  43.               end;
  44.  
  45.   WindowRec = record
  46.                 Border,
  47.                 Vis : Rectangle;
  48.                 ForeColor,
  49.                 BackColor : byte;
  50.                 WTitle : String;
  51.               end;
  52.  
  53. type
  54.   LineStr = String;
  55.  
  56. const
  57.   EmptyStr = '';
  58.   Space = ' ';
  59.   Tab = ^I;
  60.   Blanks : CharSet = [Space, Tab];
  61.   EndPunct : CharSet = ['!', '?', '.'];
  62.   Delimeters  : CharSet = [Space, Tab, '!', '?', ',', ';'];
  63.  
  64. procedure SetColor(Fore, Back : byte);
  65. begin
  66.   TextColor(Fore);
  67.   TextBackground(Back);
  68. end; { SetColor }
  69.  
  70. procedure SetWindowColor(var W : WindowRec);
  71. begin
  72.   SetColor(W.ForeColor, W.BackColor);
  73. end;
  74.  
  75. function Center(Len, Left, Right : integer) : integer;
  76. begin
  77.   Center := (succ(Right - Left) div 2) - (Len div 2);
  78. end;
  79.  
  80. procedure Box(var W : WindowRec);
  81. const
  82.   UpLeft = #201;
  83.   UpRight = #187;
  84.   LoLeft =  #200;
  85.   LoRight = #188;
  86.   HWall = #205;
  87.   VWall = #186;
  88.  
  89. var
  90.   x, y : integer;
  91.  
  92. begin
  93.   with W, Border do
  94.   begin
  95.     Window(X1, Y1, X2, Y2);
  96.     TextColor(Yellow);
  97.     TextBackground(BackColor);
  98.     ClrScr;
  99.     Window(1, 1, 80, 25);
  100.     GotoXY(X1, Y1);
  101.     Write(UpLeft);
  102.     for x := succ(X1) to pred(X2) do
  103.       Write(HWall);
  104.     GotoXY(X2, Y1);
  105.     Write(UpRight);
  106.     for Y := succ(Y1) to pred(Y2) do
  107.     begin
  108.       GotoXY(X2, y);
  109.       Write(VWall);
  110.     end;
  111.     GotoXY(X1, Y2);
  112.     Write(LoLeft);
  113.     for x := succ(X1) to pred(X2) do
  114.       Write(HWall);
  115.     Write(LoRight);
  116.     for Y := pred(Y2) downto succ(Y1) do
  117.     begin
  118.       GotoXY(X1, y);
  119.       Write(VWall);
  120.     end;
  121.     Window(X1, Y1, X2, Y2);
  122.     GotoXY(Center(Length(WTitle) + 2, X1, X2), 1);
  123.     TextColor(Yellow);
  124.     Write(' ', WTitle, ' ');
  125.     SetWindowColor(W);
  126.   end;
  127. end; { Box }
  128.  
  129. procedure DisplayWindow(var W : WindowRec);
  130. begin
  131.   with W, Vis do
  132.   begin
  133.     Box(W);
  134.     Window(X1, Y1, X2, Y2);
  135.     GotoXY(1, 1);
  136.   end;
  137. end; { DisplayWindow }
  138.  
  139. procedure EraseWindow(var W : WindowRec);
  140. begin
  141.   with W, Border do
  142.   begin
  143.     Window(X1, Y1, X2, Y2);
  144.     NormVideo;
  145.     ClrScr;
  146.   end;
  147.   Window(1, 1, 80, 25);
  148. end; { EraseWindow }
  149.  
  150. procedure NewWindow(var W : WindowRec;
  151.                     Title : String;
  152.                     X1, Y1, X2, Y2 : integer;
  153.                     Fore, Back : byte);
  154. begin
  155.   FillChar(W, SizeOf(W), 0);
  156.   with W do
  157.   begin
  158.     Border.X1 := X1; Border.Y1 := Y1;
  159.     Border.X2 := x2; Border.Y2 := Y2;
  160.     Vis.X1 := X1 + 2; Vis.X2 := x2 - 2;
  161.     Vis.Y1 := y1 + 1;
  162.     if Y2 > succ(Vis.Y1) then
  163.       Vis.Y2 := y2 - 1
  164.     else
  165.       Vis.Y2 := y2;
  166.     ForeColor := Fore; BackColor := Back;
  167.     WTitle := Title;
  168.     DisplayWindow(W);
  169.   end;
  170. end; { NewWindow }
  171.  
  172. procedure SetWindow(var W : WindowRec);
  173. begin
  174.   with W.Vis do
  175.     Window(X1, Y1, X2, Y2);
  176.   SetWindowColor(W);
  177. end;
  178.  
  179. function GlobToLocX(var W : WindowRec; GlobalX : byte) : byte;
  180. begin
  181.   with W.Vis do
  182.     GlobToLocX := succ(GlobalX - X1);
  183. end;
  184.  
  185. function GlobToLocY(var W : WindowRec; GlobalY : byte) : byte;
  186. begin
  187.   with W.Vis do
  188.     GlobToLocY := succ(GlobalY - Y1);
  189. end;
  190.  
  191.  
  192. type
  193.   ConstIndex = (MaxRecords, PSize, PStackSize, DataFSize, IndexFSize,
  194.                 StackMemory, KeySearch, InMem, OnDisk);
  195.   ParamIndex = MaxRecords..PStackSize;
  196.   DispIndex = DataFSize..OnDisk;
  197.  
  198.   DBoxParameters = record
  199.                      case integer of
  200.                      0 : ( MaxRecs        : real;
  201.                            PageSize       : real;
  202.                            PageStackSize  : real;
  203.  
  204.                            DxSize         : real;
  205.                            IxSize         : real;
  206.                            StackMem       : real;
  207.  
  208.                            MaxSearch      : real;
  209.                            MemSearch      : real;
  210.                            DiskSearch     : real;
  211.  
  212.                            MaxDataRecSize : integer;
  213.                            MaxKeyLen      : integer;
  214.                            Order          : integer;
  215.                            MaxHeight      : integer
  216.                          );
  217.                      1 : ( ItemIndex : array[ConstIndex] of real;
  218.                            MaxData        : integer;
  219.                            MaxKey         : integer;
  220.                            TOrder         : integer;
  221.                            TMaxHeight     : integer
  222.                          )
  223.                    end;
  224. var
  225.   DBoxParams :  DBoxParameters;
  226.  
  227.  
  228.  
  229. const
  230.   DefaultMaxRecs        =  1000;
  231.   DefaultPageSize       =  24;
  232.   DefaultPageStackSize  =  10;
  233.   DefaultOrder          =  10;
  234.   DefaultMaxHeight      =   5;
  235.  
  236.  
  237. procedure StoreDefaults(var DBoxParams :  DBoxParameters);
  238.  
  239. function CheckResults(MaxKeyLen, PageSize, PageStackSize : real;
  240.                       var StackMem : real) : real;
  241. const
  242.   Density        = 0.75;
  243.  
  244. var
  245.   M    : real;
  246.   Temp : Real;
  247.   I    : Integer;
  248.   NumRecs : real;
  249.  
  250.   PerPage,
  251.   MaxSearch,
  252.   MemSearch,
  253.   IrSize,
  254.   TotalPages : real;
  255.  
  256. begin
  257.   CheckResults := 0;
  258.   IrSize:=(MaxKeyLen+9) * PageSize + 5;
  259.   StackMem := (IrSize + 9) * PageStackSize;
  260.   if StackMem > MaxMemory then
  261.     Exit;
  262.   PerPage:=PageSize*Density;
  263.   NumRecs := 1000;
  264.   MaxSearch := Ln(NumRecs)/Ln(PerPage);
  265.   TotalPages:=Int(NumRecs/PerPage+1.0);
  266.   Temp:=1.0;
  267.   M:=PerPage;
  268.   I:=1;
  269.   while Temp+M<PageStackSize do
  270.   begin
  271.     Temp:=Temp + M;
  272.     I:= succ(I);
  273.     M:=Exp(Ln(PerPage) * I);
  274.   End;
  275.   If Temp + M > TotalPages
  276.    then M:= TotalPages - Temp + 1;
  277.   MemSearch:= I + (PageStackSize-Temp)/M;
  278.   MemSearch := (MemSearch / MaxSearch);
  279.   if MemSearch > 1 then
  280.     MemSearch := 0.990;
  281.   CheckResults := MemSearch / (MaxSearch * 100);
  282. end;  { CheckResults }
  283.  
  284. procedure CalcDefaults(var DBoxParams : DBoxParameters);
  285. const
  286.   Density        = 0.75;
  287.  
  288. var
  289.   BestResult,
  290.   CurResult,
  291.   CurPSize,
  292.   CurPStack,
  293.   CurStackMem : real;
  294.  
  295. begin
  296.   with DBoxParams do
  297.   begin
  298.     CurPSize := 4;
  299.     CurPStack := 3;
  300.     CurStackMem := 0;
  301.     BestResult := 0;
  302.     while (CurStackMem <= MaxMemory) do
  303.     begin
  304.       CurResult := CheckResults(MaxKeyLen, CurPSize, CurPStack, CurStackMem);
  305.       if CurResult > BestResult then
  306.       begin
  307.         BestResult := CurResult;
  308.         PageSize := CurPSize;
  309.         PageStackSize := CurPStack;
  310.       end;
  311.       CurPStack := CurPStack + 1;
  312.       CurPSize := CurPSize + 2;
  313.     end;
  314.   end;
  315. end; { CalcDefaults }
  316.  
  317. begin { StoreDefaults }
  318.   with DBoxParams do
  319.   begin
  320.     MaxDataRecSize := DefaultMaxRecSize;
  321.     MaxKeyLen := DefaultMaxKeyLen;
  322.     PageSize := DefaultPageSize;
  323.     PageStackSize := DefaultPageStackSize;
  324.     Order := DefaultOrder;
  325.     MaxHeight := DefaultMaxHeight;
  326.     MaxRecs := DefaultMaxRecs;
  327.   end;
  328.   CalcDefaults(DBoxParams);
  329. end; { StoreDefaults }
  330.  
  331. var
  332.   MaxMaxHeight   : integer;
  333.   PerPage        : real;
  334.   TotalPages     : real;
  335.   IrSize         : real;
  336.  
  337. function GetMaxHeight(DBoxParams : DBoxParameters) : integer;
  338. var
  339.   MaxMaxHeight   : integer;
  340.   i : integer;
  341.   NumRecs : real;
  342. begin
  343.   MaxMaxHeight := 0;
  344.   for i := 2 to 4 do
  345.   with DBoxParams do
  346.   begin
  347.      Order:= Trunc(PageSize / 2.0);
  348.      PerPage:=PageSize* (i * 0.25);
  349.     if MaxRecs < 1000.0 then
  350.       NumRecs := 1000.0
  351.     else
  352.       NumRecs := MaxRecs;
  353.     MaxSearch := Ln(NumRecs)/Ln(PerPage);
  354.     MaxHeight :=Trunc(MaxSearch+1.0);
  355.     if MaxHeight > MaxMaxHeight then
  356.       MaxMaxHeight := MaxHeight;
  357.   end;
  358.   GetMaxHeight := MaxMaxHeight;
  359. end;
  360.  
  361. procedure DoCalculations(var DBoxParams : DBoxParameters);
  362. const
  363.   Density        = 0.75;
  364.  
  365. var
  366.   M    : real;
  367.   Temp : Real;
  368.   I    : Integer;
  369.   NumRecs : real;
  370.  
  371. begin
  372.   with DBoxParams do
  373.   begin
  374.     Order:= Trunc(PageSize / 2.0);
  375.     PerPage:=PageSize*Density;
  376.     if MaxRecs < 1000.0 then
  377.       NumRecs := 1000.0
  378.     else
  379.       NumRecs := MaxRecs;
  380.     MaxSearch := Ln(NumRecs)/Ln(PerPage);
  381.     MaxHeight:=Trunc(MaxSearch+1.0);
  382.     TotalPages:=Int(NumRecs/PerPage+1.0);
  383.     Temp:=1.0;
  384.     M:=PerPage;
  385.     I:=1;
  386.     while Temp+M<PageStackSize do
  387.     begin
  388.       Temp:=Temp + M;
  389.       I:=I + 1;
  390.       M:=Exp(Ln(PerPage) * I);
  391.     End;
  392.     If Temp+M>TotalPages Then M:=TotalPages-Temp+1;
  393.     MemSearch:=I+(PageStackSize-Temp)/M;
  394.     DiskSearch:=MaxSearch-MemSearch;
  395.     IrSize:=(MaxKeyLen+9) * PageSize + 5;
  396.     IxSize:=IrSize*TotalPages;
  397.     DxSize:=MaxDataRecSize*(MaxRecs+1);
  398.     StackMem:= IrSize * PageStackSize;
  399.     MemSearch := (MemSearch / MaxSearch) * 100.0;
  400.     DiskSearch := (DiskSearch / MaxSearch)  * 100.0;
  401.     if MemSearch > 100.0 then
  402.      begin
  403.        MemSearch := 99.00;
  404.        DiskSearch := 1.00;
  405.      end;
  406.     MaxHeight := GetMaxHeight(DBoxParams);
  407.   end;
  408. end;  { DoCalculations }
  409.  
  410. procedure SaveConstants(var Results : text;
  411.                         DBoxParams : DBoxParameters);
  412. const
  413.   Tab = 2;
  414. begin
  415.   Write(Results, '{ Turbo Access constants ');
  416.   Write(Results, 'for ', TypeFileNm, ' ');
  417.   Writeln(Results, '}');
  418.   Writeln(Results , 'const');
  419.   with DBoxParams do
  420.   begin
  421.     Writeln(Results, ' ':Tab, 'MaxDataRecSize = ', MaxDataRecSize:5, ';');
  422.     Writeln(Results, ' ':Tab, 'MaxKeyLen      = ', MaxKeyLen:5, ';');
  423.     Writeln(Results, ' ':Tab, 'PageSize       = ', PageSize:5:0, ';');
  424.     Writeln(Results, ' ':Tab, 'PageStackSize  = ', PageStackSize:5:0, ';');
  425.     Writeln(Results, ' ':Tab, 'Order          = ', Order:5, ';');
  426.     Writeln(Results, ' ':Tab, 'MaxHeight      = ', MaxHeight:5, ';');
  427.   end;
  428.   Close(Results);
  429. end; { SaveConstants }
  430.  
  431. procedure Wait;
  432. var
  433.   ch : char;
  434. begin
  435.   ch := ScanKey;
  436. end; { wait }
  437.  
  438. procedure FinishUp;
  439. var
  440.   t : text;
  441. begin
  442.   Assign(t, DefFileNm);
  443.   Rewrite(t);
  444.   SaveConstants(t, DBOXParams);
  445. end; { FinishUp }
  446.  
  447. procedure Error(var W : WindowRec; Message : String);
  448. var
  449.   StartX,
  450.   HomeX, HomeY : integer;
  451.  
  452. begin
  453.   HomeX := WhereX;
  454.   HomeY := WhereY;
  455.   Beep;
  456.   SetColor(White, Red);
  457.   with W.Vis do
  458.   begin
  459.     StartX := Center(Length(Message) + 2, X1, X2);
  460.     GotoXY(StartX, Y2 - 3);
  461.     Write(' ', Message, ' ');
  462.     GotoXY(HomeX, HomeY);
  463.     Wait;
  464.     SetWindowColor(W);
  465.     GotoXY(X1, Y2 - 3);
  466.     Write(' ':succ(X2 - X1));
  467.   end;
  468. end; { Error }
  469.  
  470. procedure LightFirst(var W : WindowRec;
  471.                      S : String; NumChars : integer);
  472. var
  473.   i : integer;
  474. begin
  475.   SetColor(Black, White);
  476.   for i := 1 to NumChars do
  477.     Write(S[i]);
  478.   SetWindowColor(W);
  479.   for i := NumChars + 1 to Length(S) do
  480.     Write(S[i]);
  481. end; { LightFirst }
  482.  
  483. const
  484.   LeftCol = 12;
  485.   TopRow = 2;
  486.  
  487. procedure InitWorkSheet(var W : WindowRec;
  488.                         var DBoxParams : DBoxParameters);
  489. const
  490.   Prompts : array[1..9] of String =
  491.    ('Estimated total records in the Database',
  492.     'Max. Record Size          Data File Size',
  493.     'Max. Key Length           Index File Size',
  494.     'Page Size - Max. number of keys on a page',
  495.     'Page Stack Size - Max. pages in memory',
  496.     'Page Stack memory requirements',
  497.     'Avg. comparisons in a key search',
  498.     'Searches satisfied in memory',
  499.     'Disk searches needed');
  500. var
  501.   i : integer;
  502.  
  503. begin
  504.   NewWindow(W, 'TABuild Constants WorkSheet', 1, 1, 80, 24, white, Blue);
  505.   GotoXY(LeftCol, TopRow);
  506.   for i := 1 to 9 do
  507.   begin
  508.     Write(Prompts[i]);
  509.     GotoXY(LeftCol, WhereY + 2);
  510.   end;
  511.   GotoXY(LeftCol - 5, WhereY + 2);
  512.   LightFirst(W, 'Defaults', 1);
  513.   GotoXY(WhereX + 5, WhereY);
  514.   LightFirst(W, 'F2 - Save and Quit', 2);
  515.   GotoXY(WhereX + 5, WhereY);
  516.   LightFirst(W, 'Esc - Exit', 3);
  517.   GotoXY(WhereX + 6, WhereY);
  518.   LightFirst(W, 'Calculate', 1);
  519.   SetColor(Yellow, W.BackColor);
  520.   with DBoxParams do
  521.   begin
  522.     GotoXY(LeftCol + 16, TopRow + 2);
  523.     Write(MaxDataRecSize:6);
  524.     GotoXY(LeftCol + 16, TopRow + 4);
  525.     Write(MaxKeyLen:6);
  526.   end;
  527.   SetWindowColor(W);
  528. end; { InitWorkSheet }
  529.  
  530.  
  531. type
  532.   ParamRec = record
  533.                 x, y : byte;
  534.                 Min,
  535.                 Max,
  536.                 MaxLen : LongInt;
  537.                 Value : real;  { change to LongInt later }
  538.                 ErrorStr : string;
  539.              end;
  540.  
  541. const
  542.   ConstParams : array[ParamIndex] of ParamRec =
  543.     ((x : 55; y : 2; Min : 1; Max : MaxLongInt;
  544.       MaxLen : 8; Value : DefaultMaxRecs;
  545.       ErrorStr : 'The maximum records must be greater than 0'),
  546.      (x : 60; y : 8; Min : 4; Max : 254;
  547.       MaxLen : 3; Value : DefaultPageSize;
  548.       ErrorStr : 'The Page Size must be an even number between 4 and 254'),
  549.      (x : 60; y : 10; Min : 3; Max : 255;
  550.       MaxLen : 3; Value :DefaultPageStackSize;
  551.       ErrorStr : 'The Page Stack size must be between 3 and 255'));
  552.  
  553. type
  554.   DisplayRec = record
  555.                  x, y, Prec  : byte;
  556.                  Units : string[5];
  557.                end;
  558.   DisplayDialog = array[DispIndex] of DisplayRec;
  559.  
  560. const
  561.   ConstDialog : DisplayDialog =
  562.                  ((x : 55; y : 4; Prec  : 0; Units : 'bytes'),
  563.                   (x : 55; y : 6; Prec  : 0; Units : 'bytes'),
  564.                   (x : 55; y : 12; Prec : 0; Units : 'bytes'),
  565.                   (x : 55; y : 14; Prec : 2; Units : ''),
  566.                   (x : 55; y : 16; Prec : 2; Units : '%'),
  567.                   (x : 55; y : 18; Prec : 2; Units : '%'));
  568.  
  569. procedure DisplayResults(var WorkSheet : WindowRec;
  570.                          var DBoxParams : DBoxParameters);
  571. var
  572.   CurIndex :  ConstIndex;
  573.   Prec : integer;
  574. begin
  575.   with WorkSheet, Vis do
  576.   begin
  577.     SetColor(Yellow, BackColor);
  578.     for CurIndex := DataFSize to OnDisk do
  579.     with ConstDialog[CurIndex] do
  580.     begin
  581.       GotoXY(x, y);
  582.       Write(DBoxParams.ItemIndex[CurIndex]:8:Prec, ' ', Units);
  583.       Write(' ':X2 - WhereX);
  584.     end;
  585.     SetWindowColor(WorkSheet);
  586.   end;
  587. end; { DisplayResults }
  588.  
  589. procedure ShowDefaults(var WorkSheet : WindowRec);
  590. var
  591.   CurIndex : ConstIndex;
  592. begin
  593.   SetColor(Black, White);
  594.   for CurIndex := MaxRecords to PStackSize do
  595.   begin
  596.     GotoXY(ConstParams[CurIndex].x, ConstParams[CurIndex].y);
  597.     Write(' ':ConstParams[CurIndex].MaxLen);
  598.     GotoXY(ConstParams[CurIndex].x, ConstParams[CurIndex].y);
  599.     Write(ConstParams[CurIndex].Value:1:0);
  600.   end;
  601.   SetWindowColor(WorkSheet);
  602. end; { ShowDefaults }
  603.  
  604. procedure ReadNum(CurParam : ConstIndex;
  605.                   Terminators : CharSet;
  606.                   var TC : char);
  607. var
  608.   NumStr : String;
  609.   Code : integer;
  610. begin
  611.   with ConstParams[CurParam] do
  612.   begin
  613.     Str(Value:1:0, NumStr);
  614.     EditLine(NumStr, MaxLen, x, y, ['0'..'9'],
  615.              Terminators, TC);
  616.     if Length(NumStr) > 0 then
  617.       Val(NumStr, Value, Code)
  618.   end;
  619. end; { ReadNum }
  620.  
  621. procedure ParamToDialog;
  622. begin
  623.   with DBoxParams do
  624.   begin
  625.     ConstParams[MaxRecords].Value := MaxRecs;
  626.     ConstParams[PSize].Value := PageSize;
  627.     ConstParams[PStackSize].Value := PageStackSize;
  628.   end;
  629. end;
  630.  
  631. procedure ResetDefaults(var WorkSheet : WindowRec);
  632. begin
  633.   StoreDefaults(DBoxParams);
  634.   ParamToDialog;
  635.   ShowDefaults(WorkSheet);
  636.   DoCalculations(DBoxParams);
  637.   DisplayResults(WorkSheet, DBoxParams);
  638. end;
  639.  
  640. procedure DialogToParam;
  641. begin
  642.   with DBoxParams do
  643.   begin
  644.     MaxRecs := ConstParams[MaxRecords].Value;
  645.     PageSize := ConstParams[PSize].Value;
  646.     PageStackSize := ConstParams[PStackSize].Value;
  647.   end;
  648. end; { DialogToParam }
  649.  
  650. function OkStackMem(DBoxParams : DBoxParameters ) : boolean;
  651. begin
  652.   with DBoxParams do
  653.   begin
  654.     IrSize := (MaxKeyLen + 9) * PageSize+5;
  655.     StackMem := (IrSize + 9) * PageStackSize;
  656.     OkStackMem := StackMem <= LargestVar;
  657.   end;
  658. end;
  659.  
  660. function LegalValues(var ErrorS : String) : boolean;
  661. const
  662.   PageS = 5;
  663. var
  664.   Legal : boolean;
  665.   Index : ConstIndex;
  666. begin
  667.   Legal := true;
  668.   index := MaxRecords;
  669.   while (Index <= PStackSize) and Legal do
  670.   with ConstParams[index] do
  671.   begin
  672.     Legal := (Value >= Min) and (Value <= Max);
  673.     if Legal and (index = PSize) then
  674.       Legal := not odd(trunc(Value));
  675.     if Legal then
  676.       Index := succ(Index);
  677.   end;
  678.   if Legal then
  679.   begin
  680.     Legal := OkStackMem(DBoxParams);
  681.     if not Legal then
  682.     begin
  683.       Str(MaxMemory, ErrorS);
  684.       ErrorS := 'Page Stack is greater than ' + ErrorS;
  685.       ErrorS := ErrorS +  '  Reduce the Page or Page Stack Size.';
  686.     end;
  687.   end
  688.   else
  689.     ErrorS := ConstParams[index].ErrorStr;
  690.   LegalValues := Legal;
  691. end;
  692.  
  693. function Calculate(var WorkSheet : WindowRec) : boolean;
  694. var
  695.   ErrorStr : string;
  696. begin
  697.   DialogToParam;
  698.   if not LegalValues(ErrorStr) then
  699.   begin
  700.     Calculate := false;
  701.     Error(WorkSheet, ErrorStr)
  702.   end
  703.   else
  704.   begin
  705.     DoCalculations(DBoxParams);
  706.     DisplayResults(WorkSheet, DBoxParams);
  707.     Calculate := true;
  708.   end;
  709. end; { Calculate }
  710.  
  711. const
  712.   CalcKey = 'C';
  713.   DefaultsKey = 'D';
  714.   Terminators : CharSet = [CR, UpKey, DownKey, CalcKey, DefaultsKey, F2, Esc];
  715.  
  716. procedure SetConstants(var WorkSheet : WindowRec;
  717.                        var DBoxParams : DBoxParameters);
  718.  
  719. var
  720.   TermChar : char;
  721.   CurItem : ConstIndex;
  722.   Ok : boolean;
  723.  
  724. begin
  725.   InitWorkSheet(WorkSheet, DBoxParams);
  726.   ResetDefaults(WorkSheet);
  727.   CurItem := MaxRecords;
  728.   repeat
  729.     with ConstParams[CurItem] do
  730.     begin
  731.       SetColor(Black, White);
  732.       ReadNum(CurItem, Terminators, TermChar);
  733.       case TermChar of
  734.         DownKey,
  735.         CR : if CurItem = PStackSize then
  736.                 CurItem := MaxRecords
  737.               else
  738.                 CurItem := Succ(CurItem);
  739.         UpKey : if CurItem = MaxRecords then
  740.                   CurItem := PStackSize
  741.                 else
  742.                   CurItem := pred(CurItem);
  743.         DefaultsKey : ResetDefaults(WorkSheet);
  744.         CalcKey,
  745.         F2 : OK := Calculate(WorkSheet);
  746.         Esc : Abort('');
  747.       end;
  748.     end;
  749.   until (TermChar = F2) and OK;
  750.   EraseWindow(WorkSheet);
  751. end; { SetConstants }
  752.  
  753. procedure InitSetConst;
  754. begin
  755.   StoreDefaults(DBoxParams);
  756.   DoCalculations(DBoxParams);
  757. end; { InitSetConst }
  758.  
  759. var
  760.   WorkSheet : WindowRec;
  761.  
  762. procedure TAConstants;
  763. begin
  764.   InitSetConst;
  765.   if not UseDefaults then
  766.     SetConstants(WorkSheet, DBoxParams);
  767.   FinishUp;
  768. end; { TAConstants }
  769.  
  770. begin
  771.   MaxMemory := LargestVar;
  772. end.