home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TPBROWSE.ZIP / BRSETUP1.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-02-03  |  9.6 KB  |  336 lines

  1. {
  2. :-------------------------------------------------------------------
  3. : BrSetUp1 is the companion to BrowseF1.
  4. : Copyright (c) - Ronald C. Schultz
  5. : RR 1, Nolalu, Ontario, Canada
  6. :-------------------------------------------------------------------
  7. :  This unit is called by BrowseF1. It contains the initialization
  8. :  code that is used in the browseF1 unit.
  9. :-------------------------------------------------------------------
  10. }
  11.  
  12. Unit BrSetUp1;
  13. INTERFACE
  14. uses Crt;
  15.  
  16. const
  17.  FgBarColor        = Black;
  18.  BgBarColor        = LightGray;
  19.  FgColHeadColor    = Blue;
  20.  BgColHeadColor    = Black;
  21.  FgNormalColor     = White;
  22.  BgNormalColor     = Black;
  23.  EUpperCase        = false;
  24.  ELowerCase        = false;
  25.  ENoCasePreference = true;
  26.  
  27.  MaxFields = 10;
  28.  
  29.  FileName1 = 'samplef1.dat';
  30.  BrowseFile1 : boolean = false;
  31.  FileName2 = 'yourfile.dat';
  32.  BrowseFile2 : boolean = false; (* use with Rec2 *)
  33.  
  34.  type
  35.     str10 = string[10];
  36.     FieldArray = array[1..MaxFields] of string[80];
  37.     DataFields  = record (* global variables *)
  38.          fieldNm  : str10;
  39.          Width : byte;
  40.     end;
  41.  
  42.       Rec1 = record
  43.          FName     : string[10];
  44.          LName     : string[15];
  45.          age   : byte;
  46.          StAddress : string[25];
  47.          City      : string[20];
  48.          Country   : string[20];
  49.          AcctNum   : string[4];
  50.          Balance   : real;
  51.          Comments  : string[40];
  52.          Deleted   : string[1];
  53.       end;
  54.  
  55.       (* put your data below *)
  56.        rec2 = record
  57.        end;
  58.  
  59.     BrFile1 = file of Rec1;
  60.     BrFile2 = file of Rec2;
  61.  
  62.     FieldTypes = array[1..128] of DataFields;
  63.  
  64. var
  65.     FileVar1      : BrFile1;
  66.     TempFile1     : BrFile1;
  67.     RecVar1       : Rec1;
  68.  
  69.     FileVar2      : BrFile2; (* use with your data for rec2 *)
  70.     TempFile2     : BrFile2;
  71.     RecVar2       : Rec2;
  72.  
  73.     FieldVal      : FieldArray; (* assign values of each record var *)
  74.     Flds          : FieldTypes; (* chars of each rec field *)
  75.     NumFields     : byte;
  76.     FPos          : longint;
  77.  
  78.     AppendPrompt  : array[1..MaxFields] of string;
  79.     AppendVar     : FieldArray;
  80.     AppendLength  : array[1..MaxFields] of integer;
  81.  
  82.  
  83. Procedure InitBrowseVars;
  84. procedure GetRec;
  85. procedure Get_WriteEditRec(FldNum            : integer;
  86.                            EditStr       : string;
  87.                            OldFilePointerPos : LongInt;
  88.                            NewFilePointerPos : LongInt);
  89. Procedure AppendRec(AppendStrArray : FieldArray);
  90. Procedure DeleteRecs;
  91.  
  92. IMPLEMENTATION
  93.  
  94. {
  95. :-----------------------------------------------------------
  96. : DeleteRecs will delete those records that are marked with "*".
  97. : The "deleted" field should be marked for deletion in the
  98. : BrowseF1 unit. Fields will not be deleted unit they are packed
  99. : by selecting F4 while in the BrowseF1 unit.
  100. :-----------------------------------------------------------
  101. }
  102. Procedure DeleteRecs;
  103. begin
  104.   if BrowseFile1 then
  105.    begin
  106.      assign(TempFile1,'temp.dat');
  107.      rewrite(TempFile1);
  108.      reset(FileVar1);
  109.      while not Eof(FileVar1 ) do
  110.        begin
  111.          read(FileVar1,RecVar1);
  112.          if RecVar1.Deleted <> '*' then write(TempFile1,RecVar1);
  113.        end;
  114.       reset(TempFile1);
  115.       if BrowseFile1 then
  116.       rewrite(FileVar1);
  117.       while not eof(TempFile1) do
  118.       begin
  119.         read(TempFile1,RecVar1);
  120.         write(FileVar1,RecVar1);
  121.       end;
  122.       erase(TempFile1);
  123.      end;
  124.  
  125.  (* insert code below for second record *)
  126.  (***************************************)
  127.  (*  if BrowseFile2 then *)
  128.  (*     begin *)
  129.  (*     end;  *)
  130.  
  131. end; (* DeleteRecs *)
  132. {
  133. :---------------------------------------------------
  134. : InitRec1 is called by BrowseF1 unit to set up column
  135. : heading names and to set the size of the fields displayed
  136. : on the screen.
  137. :----------------------------------------------------
  138. }
  139. Procedure InitRec1;
  140. begin
  141.   fillchar(flds,sizeof(flds),0);
  142.   flds[1].fieldNm  := 'FNAME';
  143.   flds[1].width    := 10;
  144.   flds[2].fieldNm  := 'LNAME';
  145.   flds[2].width    := 15;
  146.   flds[3].fieldNm  := 'AGE';
  147.   flds[3].width    := 3;
  148.   flds[4].fieldNm  := 'ADDRESS';
  149.   flds[4].width    := 25;
  150.   flds[5].fieldNm  := 'CITY';
  151.   flds[5].width    := 20;
  152.   flds[6].fieldNm  := 'COUNTRY';
  153.   flds[6].width    := 20;
  154.   flds[7].fieldNm  := 'ACCTNUM';
  155.   flds[7].width    := 4;
  156.   flds[8].fieldNm  := 'BAL';
  157.   flds[8].width    := 9;
  158.   flds[9].fieldNm  := 'COMMENTS';
  159.   flds[9].width    := 40;
  160.   flds[10].fieldNm := 'DELSTAT';
  161.   flds[10].width   := 1;
  162.   (* This is limited to 21 fields *)
  163.   NumFields := 10;
  164.  
  165.   {
  166.   :------------------------------------------------------------
  167.   : The BrowseF1 unit can be used to append new records.
  168.   : The following code is used to label each data entry prompt
  169.   : The append screen can only accommodate 21 fields which is
  170.   : basically one screen of fields. The prompt and field length
  171.   : must be entered below for each field.
  172.   :------------------------------------------------------------
  173.   }
  174.   AppendPrompt[1]  := 'First Name......';
  175.   AppendLength[1]  := 10;
  176.   AppendPrompt[2]  := 'Last Name.......';
  177.   AppendLength[2]  := 15;
  178.   AppendPrompt[3]  := 'Age.........';
  179.   AppendLength[3]  := 2;
  180.   AppendPrompt[4]  := 'Address.........';
  181.   AppendLength[4]  := 25;
  182.   AppendPrompt[5]  := 'City............';
  183.   AppendLength[5]  := 20;
  184.   AppendPrompt[6]  := 'Country.........';
  185.   AppendLength[6]  := 20;
  186.   AppendPrompt[7]  := 'Account number..';
  187.   AppendLength[7]  := 4;
  188.   AppendPrompt[8]  := 'Balance.........';
  189.   AppendLength[8]  := 9;
  190.   AppendPrompt[9]  := 'Comments........';
  191.   AppendLength[9]  := 40;
  192.   AppendPrompt[10] := 'Delete Rec.....';
  193.   AppendLength[10] := 1;
  194. end; (* initrec1 *)
  195.  
  196. (* insert code for rec2 below *)
  197. (******************************)
  198. Procedure InitRec2;
  199. begin
  200. end; (* InitRec2 *)
  201. (******************************)
  202.  
  203. Procedure InitBrowseVars;
  204. begin
  205.  if BrowseFile1 then InitRec1;
  206. (* if BrowseFile2 then initRec2; *)
  207. end;
  208.  
  209. (* GetRec1 reads records from file for display in Browsef1 Unit *)
  210. procedure GetRec1;
  211. begin
  212.   read(FileVar1,RecVar1);
  213.   {
  214.   :-------------------------------------------------------------
  215.   : all fields must be converted to a string before they are
  216.   : sent to the browse unit
  217.   : ------------------------------------------------------------
  218.   }
  219.   fillchar(FieldVal,sizeof(FieldVal),0);
  220.   FieldVal[1] := RecVar1.fname;
  221.   FieldVal[2] := RecVar1.lname;
  222.   str(RecVar1.age,FieldVal[3]);
  223.   FieldVal[4] := RecVar1.staddress;
  224.   FieldVal[5] := RecVar1.city;
  225.   FieldVal[6] := RecVar1.country;
  226.   FieldVal[7] := RecVar1.acctnum;
  227.   str(RecVar1.balance:6:2,FieldVal[8]);
  228.   FieldVal[9] := RecVar1.comments;
  229.   FieldVal[10] := RecVar1.Deleted;
  230.   AppendVar := FieldVal;
  231. end; (* GetRec1 *)
  232.  
  233. (* insert code for rec2 *)
  234. (************************)
  235. procedure GetRec2;
  236. begin
  237. end;
  238.  
  239. procedure GetRec;
  240. begin
  241.   if BrowseFile1 then GetRec1;
  242.   (*  if BrowseFile2 then GetRec2; *)
  243. end;
  244. {
  245. :-------------------------------------------------------------
  246. : AppendRec receives record staring data from BrowseF1 and
  247. : writes the data to the file.  All data must be re-converted
  248. : from string data to the appropriate data type before writing
  249. : to the file.
  250. :-------------------------------------------------------------
  251. }
  252. Procedure AppendRec(AppendStrArray : FieldArray);
  253. var
  254.  e : integer;
  255. begin
  256.   if BrowseFile1 then
  257.    begin
  258.      RecVar1.FName     := AppendStrArray[1];
  259.      RecVar1.LName     := AppendStrArray[2];
  260.      val(AppendStrArray[3],RecVar1.age,e);
  261.      RecVar1.StAddress := AppendStrArray[4];
  262.      RecVar1.City      := AppendStrArray[5];
  263.      RecVar1.Country   := AppendStrArray[6];
  264.      RecVar1.AcctNum   := AppendStrArray[7];
  265.      val(AppendStrArray[8],RecVar1.Balance,e);
  266.      RecVar1.Comments  := AppendStrArray[9];
  267.      RecVar1.Deleted   := AppendStrArray[10];
  268.      Seek(FileVar1, FileSize(FileVar1));
  269.      write(FileVar1,RecVar1);
  270.    end;
  271.   {
  272.   :-------------------------------
  273.   : insert code for rec2 here
  274.   (*****************************)
  275.   : if BrowseFile2 then
  276.   :   begin
  277.   :   end;
  278.   :-------------------------------
  279.   }
  280.  
  281. end; (* AppendRec *)
  282. {
  283. :---------------------------------------------------------
  284. : Get_WriteEditRec gets the edited record from BrowseF1.
  285. : Data comes from BrowseF1 as a string.  Data must be
  286. : converted to the appropriate data type before writing
  287. : to the file.
  288. :---------------------------------------------------------
  289. }
  290. procedure Get_WriteEditRec(FldNum            : integer;
  291.                            EditStr           : string;
  292.                            OldFilePointerPos : LongInt;
  293.                            NewFilePointerPos : LongInt);
  294. var
  295.   Rec1Copy : Rec1;
  296. (*  Rec2Copy : Rec2; *)
  297.   e : integer;
  298. begin
  299.   (* edit a single field *)
  300.   if  BrowseFile1 then
  301.   begin
  302.     seek(FileVar1,NewFilePointerPos-1);
  303.     read(FileVar1,Rec1Copy);
  304.      case FldNum of
  305.       1 :  Rec1Copy.FName := EditStr;
  306.       2 :  Rec1Copy.LName := EditStr;
  307.       3 :  val(EditStr,Rec1Copy.age,e);
  308.       4 :  Rec1Copy.StAddress := EditStr;
  309.       5 :  Rec1Copy.City := EditStr;
  310.       6 :  Rec1Copy.Country := EditStr;
  311.       7 :  Rec1Copy.AcctNum := EditStr;
  312.       8 :  val(EditStr,Rec1Copy.Balance,e);
  313.       9 :  Rec1Copy.Comments := EditStr;
  314.       10:  Rec1Copy.Deleted := EditStr[1];
  315.      end;
  316.     seek(FileVar1,NewFilePointerPos-1);
  317.     write(FileVar1,Rec1Copy);
  318.     seek(FileVar1,OldFilePointerPos);
  319.   end; (* BrowseFile1 *)
  320. {
  321. :----------------------------------
  322. :  (* insert code for rec2 below
  323. :  (******************************)
  324. :  if BrowseFile2 then
  325. :    begin
  326. :    end;
  327. :----------------------------------
  328. }
  329.  
  330. end; (* Get_WriteEditRec *)
  331.  
  332. begin
  333. end.
  334.  
  335.  
  336.