home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / DBT.ZIP / DBMENU.ARC / DBMENU.PAS next >
Encoding:
Pascal/Delphi Source File  |  1988-02-21  |  21.6 KB  |  522 lines

  1. Unit Dbmenu;
  2.  
  3. {This is offered for comments, criticism, etc. for all and sundry
  4.  on a limited basis. It is _NOT_ as well commented as I would like,
  5.  but it is a working copy! Three caveats:
  6.  1 : No liability is assumed or implied. If you like it, donations [time or money]
  7.      are accepted
  8.  2 : The date field is in the form of YYYY/MM/DD.
  9.  3 : Many thanks and appelations to Kim and Brian, without whose TPRO units
  10.      that this depends on, it would have been much harder
  11.  
  12. James C Walker <Cap'n> 72255,1616 }
  13.  
  14.  
  15. Interface
  16. Uses
  17.   TPCrt,
  18.   TPMenu,
  19.   TpEdit,
  20.   GetFld,
  21.   taccess,
  22.   Mulkey4;
  23.  
  24. Procedure DisplayR(Var F : File_Type);
  25. Procedure GetR(Var F : File_Type; Var R);
  26. Procedure AddRec(Var F : File_Type; Var R);
  27. Procedure EditR(Var F : File_Type; Var R);
  28. Procedure DeleteR(Var F : File_Type; Var R);
  29. Procedure FindR(Var F : File_Type; Var R; Var KeyNum : Integer);
  30. Procedure FindMatch(Var F : File_Type; Var R; Var KeyNum : Integer);
  31. Procedure NextR(Var F : File_Type; Var R; Var KeyNum : Integer);
  32. Procedure PreviousR(Var F : File_Type; Var R; Var KeyNum : Integer);
  33. Procedure ReportToFile(Var F : File_Type; Var R);
  34.  
  35. Procedure RunMenu(Var F : File_Type; Var R);
  36.  
  37. Implementation
  38.  
  39. Var
  40.   CH : Char;
  41.   main : Menu;
  42.   Key : MenuKey;
  43.   MStackP : MenuStackP;
  44.   KeyNum, I : Integer;
  45.   ScrBufPtr : Pointer;
  46.  
  47.   Procedure DisplayR(Var F : File_Type);
  48.   Var
  49.     I : Integer;
  50.     Date : String[10];
  51.  
  52.   Begin
  53.     With F Do
  54.       Begin
  55.         For I := 1 To NumOfFields Do
  56.           Begin
  57.             With Field[I] Do
  58.               Begin
  59.                 Date := '';
  60.                 FastWriteWindow(ScreenPrompt, YCoord, XCoord, PromptAttribute);
  61.                 If (FieldType = Date_Field) or (FieldType = Valid_Date_Field)
  62.                  Then
  63.                     begin
  64.                       Date := Copy(FieldData^, 6, 2)
  65.                             + '/'
  66.                             + Copy(FieldData^, 9, 2)
  67.                             + '/'
  68.                             + Copy(FieldData^, 1, 4) ;
  69.                       FastWriteWindow(Date, YCoord, XCoord + Length(ScreenPrompt) + 2, DisplayAttribute);
  70.                     end
  71.                     else
  72.                 FastWriteWindow(FieldData^, YCoord, XCoord + Length(ScreenPrompt) + 2, DisplayAttribute);
  73.               End;      {With Field[I]}
  74.           End;               {For I :=}
  75.       End;                     {With F}
  76.   End;
  77.  
  78.   Procedure GetR(Var F : File_Type; Var R);
  79.   Var
  80.     I, KeyVal, Code : Integer;
  81.     CH : Char;
  82.     TestReal : Real;
  83.     TestVal : LongInt;
  84.     Month, Day, Year : Integer;
  85.     TestDate : String[4];
  86.     NewDate : String[10];
  87.   Const
  88.     Escape = 27;
  89.     CtrlEnter = 10;
  90.   Label
  91.     Egress, NextField;
  92.   Begin
  93.  
  94.     DisplayR(F);
  95.     With F Do
  96.       Begin
  97.         I := 1;
  98.         While I <= NumOfFields Do
  99.           Begin
  100.             With Field[I] Do
  101.               Begin
  102.                 KeyVal := 0;
  103.                 GoToXY(1, 24); ClrEol;
  104.                 FastWriteWindow(HelpPrompt, 24, 1, HelpAttribute);
  105.                 Code := 1;
  106.                 Case FieldType Of
  107.                   Date_Field : Begin
  108.                                  LegalChars := '0123456789';
  109.                                  TestDate := Copy(FieldData^, 6, 2);
  110.                                  GetField(KeyVal, LegalChars, TestDate,
  111.                                           GetAttribute, YCoord,
  112.                                           XCoord + Length(ScreenPrompt) + 2,
  113.                                           2, [1, 5, 7]);
  114.                                  If KeyVal = Escape Then
  115.                                    Begin
  116.                                      TestDate := '';
  117.                                      GoTo Egress;
  118.                                    End;
  119.                                  If KeyVal = CtrlEnter Then GoTo NextField;
  120.                                  Val(TestDate, Month, Code);
  121.                                  Str(Month:2, TestDate);
  122.                                  TestDate := Copy(FieldData^, 9, 2);
  123.                                  GetField(KeyVal, LegalChars, TestDate,
  124.                                           GetAttribute, YCoord,
  125.                                           XCoord + Length(ScreenPrompt) + 5,
  126.                                           2, [1, 5, 7]);
  127.                                  If KeyVal = CtrlEnter Then GoTo NextField;
  128.                                  If KeyVal = Escape Then
  129.                                    Begin
  130.                                      TestDate := '';
  131.                                      GoTo Egress;
  132.                                    End;
  133.                                  Val(TestDate, Day, Code);
  134.                                  Str(Day:2, TestDate);
  135.                                  TestDate := Copy(FieldData^, 1, 4);
  136.                                  GetField(KeyVal, LegalChars, TestDate,
  137.                                           GetAttribute, YCoord,
  138.                                           XCoord + Length(ScreenPrompt) + 8,
  139.                                           4, [1, 5, 7]);
  140.                                  If KeyVal = CtrlEnter Then GoTo NextField;
  141.                                  If KeyVal = Escape Then
  142.                                    Begin
  143.                                      TestDate := '';
  144.                                      GoTo Egress;
  145.                                    End;
  146.                                  Val(TestDate, Year, Code);
  147.                                  Str(Year:4, TestDate);
  148.                                  FieldData^ := TestDate + '/';
  149.                                  Str(Month:2, TestDate);
  150.                                  FieldData^ := FieldData^ + TestDate + '/';
  151.                                  Str(Day:2, TestDate);
  152.                                  FieldData^ := FieldData^ + TestDate;
  153.                                End;
  154.                   Valid_Date_Field : Begin
  155.                                        Month := 00; Day := 00; Year := 0000;
  156.                                        LegalChars := '0123456789';
  157.                                        TestDate := Copy(FieldData^, 6, 2);
  158.                                        Repeat
  159.                                          GetField(KeyVal, LegalChars, TestDate,
  160.                                                   GetAttribute, YCoord,
  161.                                                   XCoord + Length(ScreenPrompt) + 2,
  162.                                                   2, [1, 5, 7]);
  163.                                          If KeyVal = Escape Then
  164.                                            Begin
  165.                                              TestDate := '';
  166.                                              GoTo Egress;
  167.                                            End;
  168.                                          If KeyVal = CtrlEnter Then GoTo NextField;
  169.                                          Val(TestDate, Month, Code);
  170.                                          Str(Month:2, TestDate);
  171.                                          If (Month < 1) Or (Month > 12) Then
  172.                                            Begin
  173.                                              GoToXY(XCoord + Length(ScreenPrompt) + 2 + 15, YCoord);
  174.                                              ClrEol;
  175.                                              Write('Month must be between 1 and 12 ');
  176.                                              ClrEol;
  177.                                            End;
  178.                                        Until ((Month > 0) And (Month < 13)) Or (Length(TestDate) = 0);
  179.                                        TestDate := Copy(FieldData^, 9, 2);
  180.                                        Repeat
  181.                                          GetField(KeyVal, LegalChars, TestDate,
  182.                                                   GetAttribute, YCoord,
  183.                                                   XCoord + Length(ScreenPrompt) + 5,
  184.                                                   2, [1, 5, 7]);
  185.                                          If KeyVal = CtrlEnter Then GoTo NextField;
  186.                                          If KeyVal = Escape Then
  187.                                            Begin
  188.                                              TestDate := '';
  189.                                              GoTo Egress;
  190.                                            End;
  191.                                          Val(TestDate, Day, Code);
  192.                                          Str(Day:2, TestDate);
  193.                                          If (Day < 1) Or (Day > 31) Then
  194.                                            Begin
  195.                                              GoToXY(XCoord + Length(ScreenPrompt) + 2 + 15, YCoord);
  196.                                              ClrEol;
  197.                                              Write('Day must be between 1 and 31');
  198.                                              ClrEol;
  199.                                            End;
  200.                                        Until ((Day > 0) And (Day < 32)) Or (Length(TestDate) = 0);
  201.                                        TestDate := Copy(FieldData^, 1, 2);
  202.                                        Repeat
  203.                                          GetField(KeyVal, LegalChars, TestDate,
  204.                                                   GetAttribute, YCoord,
  205.                                                   XCoord + Length(ScreenPrompt) + 8,
  206.                                                   4, [1, 5, 7]);
  207.                                          If KeyVal = CtrlEnter Then GoTo NextField;
  208.                                          If KeyVal = Escape Then
  209.                                            Begin
  210.                                              TestDate := '';
  211.                                              GoTo Egress;
  212.                                            End;
  213.                                          Val(TestDate, Year, Code);
  214.                                          Str(Year:4, TestDate);
  215.                                          If (Year < 1000) Or (Year > 3100) Then
  216.                                            Begin
  217.                                              {GoToXY(XCoord + LENGTH(ScreenPrompt) + 2 + 15, YCoord);}
  218.                                              GoToXY(1, 23);
  219.                                              ClrEol;
  220.                                              Write('Year must be between 1000 and 3100');
  221.                                              ClrEol;
  222.                                            End;
  223.                                        Until ((Year > 999) And (Year < 3101)) Or (Length(TestDate) = 0);
  224.                                        Val(TestDate, Year, Code);
  225.                                  Str(Year:4, TestDate);
  226.                                  FieldData^ := TestDate + '/';
  227.                                  Str(Month:2, TestDate);
  228.                                  FieldData^ := FieldData^ + TestDate + '/';
  229.                                  Str(Day:2, TestDate);
  230.                                  FieldData^ := FieldData^ + TestDate;
  231.                                      End;
  232.                   String_Field : Begin
  233.                                    GetField(KeyVal, LegalChars, FieldData^,
  234.                                             GetAttribute, YCoord,
  235.                                             XCoord + Length(ScreenPrompt) + 2,
  236.                                             FieldLength - 1, [1, 5, 7]);
  237.                                    If KeyVal = CtrlEnter Then GoTo NextField;
  238.                                    If KeyVal = Escape Then
  239.                                      Begin
  240.                                        FieldData^ := '';
  241.                                        GoTo Egress;
  242.                                      End;
  243.                                  End;
  244.                   Real_Field : Begin
  245.                                  LegalChars := '0123456789.';
  246.                                  Repeat
  247.                                    GetField(KeyVal, LegalChars, FieldData^,
  248.                                             GetAttribute, YCoord,
  249.                                             XCoord + Length(ScreenPrompt) + 2,
  250.                                             FieldLength - 4, [1, 5, 7]);
  251.                                    If KeyVal = CtrlEnter Then GoTo NextField;
  252.                                    If KeyVal = Escape Then
  253.                                      Begin
  254.                                        FieldData^ := '';
  255.                                        GoTo Egress;
  256.                                      End;
  257.                                    Val(FieldData^, TestReal, Code);
  258.                                    If Code <> 0 Then
  259.                                      Begin
  260.                                        Delete(FieldData^, Code, 1);
  261.                                        {GoToXY(XCoord + LENGTH(ScreenPrompt) + 2 + FieldLength + 2, YCoord);}
  262.                                        GoToXY(1, 23);
  263.                                        Write('This must be a real number');
  264.                                        ClrEol;
  265.                                      End;
  266.                                  Until (Code = 0) Or (Length(FieldData^) = 0);
  267.                                  Str(TestReal:FieldLength - 4:2, FieldData^);
  268.                                End;
  269.                   Integer_Field : Begin
  270.                                     LegalChars := '0123456789.';
  271.                                     Repeat
  272.                                       GetField(KeyVal, LegalChars, FieldData^,
  273.                                                GetAttribute, YCoord,
  274.                                                XCoord + Length(ScreenPrompt) + 2,
  275.                                                5, [1, 5, 7]);
  276.                                       If KeyVal = CtrlEnter Then GoTo NextField;
  277.                                       If KeyVal = Escape Then
  278.                                         Begin
  279.                                           FieldData^ := '';
  280.                                           GoTo Egress;
  281.                                         End;
  282.                                       Val(FieldData^, TestVal, Code);
  283.                                       If (Code <> 0) or (TestVal > 32767) Then
  284.                                         Begin
  285.                                           Delete(FieldData^, Code, 1);
  286.                                           {GoToXY(XCoord + LENGTH(ScreenPrompt) + 2 + FieldLength + 2, YCoord);}
  287.                                           GoToXY(1, 23);
  288.                                           Write('This must be an integer between 0 and 32767');
  289.                                           ClrEol;
  290.                                         End;
  291.                                     Until ((Code = 0) Or (Length(FieldData^) = 0)) and (TestVal < 32768);
  292.                                     Str(TestVal:5, FieldData^);
  293.                                   End;
  294.                   Non_Blank : Begin
  295.                                 Repeat
  296.                                   GetField(KeyVal, LegalChars, FieldData^,
  297.                                            GetAttribute, YCoord,
  298.                                            XCoord + Length(ScreenPrompt) + 2,
  299.                                            FieldLength - 1, [1, 5, 7]);
  300.                                   If KeyVal = CtrlEnter Then GoTo NextField;
  301.                                   If KeyVal = Escape Then
  302.                                     Begin
  303.                                       FieldData^ := '';
  304.                                       GoTo Egress;
  305.                                     End;
  306.                                   If Length(FieldData^) = 0 Then
  307.                                     Begin
  308.                                       {GoToXY(XCoord + LENGTH(ScreenPrompt) + 2 + FieldLength + 2, YCoord);}
  309.                                       GoToXY(1, 23);
  310.                                       Write('This must be not be blank');
  311.                                       ClrEol;
  312.                                     End;
  313.                                 Until Length(FieldData^) <> 0;
  314.                               End;
  315.                 Else
  316.                   Begin
  317.                     GetField(KeyVal, LegalChars, FieldData^, GetAttribute,
  318.                              YCoord, XCoord + Length(ScreenPrompt) + 2,
  319.                              FieldLength - 1, [1, 5, 7]);
  320.                     If KeyVal = Escape Then
  321.                       Begin
  322.                         FieldData^ := '';
  323.                         GoTo Egress;
  324.                       End;
  325.                   End;
  326.                 End;
  327. NextField:
  328.                 If KeyVal = 328 Then
  329.                   If I = 1 Then
  330.                     I := 1
  331.                   Else
  332.                     I := I - 1
  333.                 Else
  334.                   I := I + 1;
  335.               End;      {With Field[I]}
  336.           End;               {For I :=}
  337. Egress:
  338.         DisplayR(F);
  339.       End;                     {With F}
  340.   End;
  341.  
  342.   Procedure AddRec(Var F : File_Type; Var R);
  343.   Begin
  344.     DisplayR(F);
  345.     GetR(F, R);
  346.     If YesOrNo('Confirm addition ?', 25, 1, 5, 'Y') Then
  347.       ADD_RECORD(F, R);
  348.     DisplayR(F);
  349.   End;
  350.  
  351.   Procedure EditR(Var F : File_Type; Var R);
  352.   Begin
  353.     DisplayR(F);
  354.     GetR(F, R);
  355.     If YesOrNo('Confirm edit ?', 25, 1, 5, 'N') Then
  356.       UPDATE_RECORD(F, R);
  357.     DisplayR(F);
  358.   End;
  359.  
  360.   Procedure DeleteR(Var F : File_Type; Var R);
  361.   Begin
  362.     DisplayR(F);
  363.     If YesOrNo('Confirm deletion ?', 25, 1, 5, 'N') Then
  364.       DELETE_RECORD(F);
  365.     NEXT_RECORD(F, 1, R);
  366.     DisplayR(F);
  367.   End;
  368.  
  369.   Procedure FindR(Var F : File_Type; Var R; Var KeyNum : Integer);
  370.   Var
  371.     I : Integer;
  372.     DummyChar : Char;
  373.     Dummy : Boolean;
  374.     KeyStr : String[10];
  375.     REC : Array[0..MaxDataRecSize] Of Char Absolute R;
  376.   Begin
  377.     FillChar(R, F.RecSize, 0);
  378.     DisplayR(F);
  379.     GetR(F, R);
  380.     If YesOrNo('Confirm choice ?', 25, 1, 5, 'Y') Then
  381.       Begin
  382.         With F Do
  383.           Begin
  384.             I := F.NUMBER_OF_KEYS;
  385.             KeyNum := 1;
  386.             While I <> 0 Do
  387.               Begin
  388.                 If REC[Key[I].Offset] <> #0 Then
  389.                   Begin
  390.                     KeyNum := I;
  391.                     I := 0;
  392.                   End
  393.                 Else
  394.                   Begin
  395.                     I := I - 1
  396.                   End;
  397.               End;            {While I}
  398.           End;              {with F do}
  399.         If KeyNum = 0 Then KeyNum := 1;
  400.         READ_RECORD(F, KeyNum, R);
  401.         Str(KeyNum, KeyStr);
  402.         DisplayR(F);
  403.       End                  {If YESORNO}
  404.     Else
  405.       Begin
  406.         DisplayR(F);
  407.       End;
  408.   End;                          {FindR}
  409.  
  410.   Procedure FindMatch(Var F : File_Type; Var R; Var KeyNum : Integer);
  411.   Var
  412.     I : Integer;
  413.     DummyChar : Char;
  414.     Dummy : Boolean;
  415.     REC : Array[0..MaxDataRecSize] Of Char Absolute R;
  416.   Begin
  417.     With F Do
  418.       Begin
  419.         I := F.NUMBER_OF_KEYS;
  420.         KeyNum := 1;
  421.         While I <> 0 Do
  422.           Begin
  423.             If REC[Key[I].Offset] <> #0 Then
  424.               Begin
  425.                 KeyNum := I;
  426.                 I := 0;
  427.               End
  428.             Else
  429.               Begin
  430.                 I := I - 1
  431.               End;
  432.           End;                {While I}
  433.       End;                  {with F do}
  434.     If KeyNum = 0 Then KeyNum := 1;
  435.     READ_RECORD(F, KeyNum, R);
  436.   End;                          {FindR}
  437.  
  438.   Procedure NextR(Var F : File_Type; Var R; Var KeyNum : Integer);
  439.   Begin
  440.     NEXT_RECORD(F, KeyNum, R);
  441.     DisplayR(F);
  442.   End;
  443.  
  444.   Procedure PreviousR(Var F : File_Type; Var R; Var KeyNum : Integer);
  445.   Begin
  446.     PREVIOUS_RECORD(F, KeyNum, R);
  447.     DisplayR(F);
  448.   End;
  449.  
  450.   Procedure ReportToFile(Var F : File_Type; Var R);
  451.   Var
  452.     I : Integer;
  453.     FTxt : Text;
  454.   Begin
  455.     If YesOrNo('Proceed ?', 25, 1, 5, 'Y') Then
  456.       Begin
  457.         With F Do
  458.           Begin
  459.             Assign(FTxt, Name + '.PRN');
  460.             Rewrite(FTxt);
  461.             For I := 1 To NumOfFields Do
  462.               Begin
  463.                 WriteLn(FTxt, '"', Field[I].FieldData^, '",');
  464.               End;
  465.           End;
  466.         Close(FTxt);
  467.       End;
  468.   End;
  469.  
  470.  
  471.   Procedure InitMenu1(Var M : Menu);
  472.   Const
  473.     Color1 : MenuColorArray = ($0F, $0F, $06, $03, $04, $02);
  474.     Frame1 : FrameArray = '╔╚╗╝═║';
  475.  
  476.   Begin
  477.     {Customize this call for special exit characters and custom item displays}
  478.     M := NewMenu([], Nil);
  479.  
  480.     SubMenu(1, 2, 25, Horizontal, Frame1, Color1, '');
  481.     MenuItem('ADD RECORDS', 2, 1, 100, 'Add NEW records');
  482.     MenuItem('EDIT RECORDS', 20, 1, 200, 'Change the contents of a record');
  483.     SubMenu(15, 4, 25, Horizontal, Frame1, Color1, '');
  484.     MenuItem('Edit', 3, 1, 210, 'Change the current record');
  485.     MenuItem('Delete', 8, 1, 220, 'Delete the current record');
  486.     MenuItem('Find', 15, 1, 230, 'Find another record');
  487.     MenuItem('Next', 20, 1, 232, 'Goto next record');
  488.     MenuItem('Previous', 25, 1, 233, 'Goto previous record');
  489.     PopSubLevel;
  490.     MenuItem('REPORT TO FILE', 50, 1, 400, 'Create a comma delimited file');
  491.     MenuItem('QUIT', 73, 1, 1000, 'Exit the program');
  492.     PopSubLevel;
  493.  
  494.     ResetMenu(M);
  495.   End;
  496.  
  497.   Procedure RunMenu(Var F : File_Type; Var R);
  498.   Begin
  499.     OPEN_FILE(F);
  500.     KeyNum := 1;
  501.     InitMenu1(main);
  502.     Repeat
  503.       Key := MenuChoice(main, CH);
  504.       EraseMenuOntoStack(main, MStackP);
  505.       ClrScr;
  506.  
  507.       Case Key Of
  508.         100 : AddRec(F, R);
  509.         210 : EditR(F, R);
  510.         220 : DeleteR(F, R);
  511.         230 : FindR(F, R, KeyNum);
  512.         232 : NextR(F, R, KeyNum);
  513.         233 : PreviousR(F, R, KeyNum);
  514.         400 : ReportToFile(F, R);
  515.       End;
  516.       DrawMenuFromStack(main, MStackP);
  517.     Until (CH = ^M) And (Key = 1000);
  518.     CLOSE_FILE(F);
  519.   End;
  520.  
  521. End.
  522.