home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / database / dialbook / cfax.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-09-19  |  18.7 KB  |  524 lines

  1.  
  2. Unit CFAX;
  3.  
  4. {───────────────────────────────────────────────────────────────────────┐
  5. This unit reads and writes to/from a The Complete PC's DIALBOOK.DIR
  6. Phonebook file.  It also will read/write to Group files: These have the
  7. extension .GRP.  It checks for the maximum number of entries allowed for
  8. both types when allowing adding entries.  It will work with any version
  9. of Turbo Pascal 4.0 or later.  The TPString Unit upon which it depends
  10. is from Turbo Power's Turbo Professional Series. The only functions used
  11. from it are Pad(),  Trim() and DefaultExtension().  The last is trivial
  12. and may be dropped if so desired.  Pad and Trim are easy to reproduce. I
  13. just used the Turbo Power unit because they did them in ASM and they are
  14. fast.  DefaultExtension adds a default extension to the file names if
  15. one doesn't already exist.
  16. └───────────────────────────────────────────────────────────────────────}
  17.  
  18. {───────────────────────────────────────────────────────────────────────}
  19.                               INTERFACE
  20. {───────────────────────────────────────────────────────────────────────}
  21.  
  22. Uses TPString;
  23.  
  24. CONST
  25.   NameLen  = 20;
  26.   PhoneLen = 27;
  27.   PollLen  = 8;
  28.  
  29. TYPE
  30.  
  31.   PhoneBook = File;       { The PhoneBook File: DIALBOOK.DIR }
  32.   GroupFile = File;       { Count of Phonebook file records }
  33.   PBRec = Record
  34.         Name     : String[NameLen];
  35.         CAMPhone : String[PhoneLen];
  36.         FAXPhone : String[PhoneLen];
  37.         PollPwd  : String[PhoneLen]
  38.   End;
  39.   GrpRec = Record
  40.         Name     : String[NameLen];
  41.         FAXPhone : String[PhoneLen];
  42.         PollPwd  : String[PhoneLen]
  43.   End;
  44.  
  45.  
  46. VAR
  47.  
  48.   RecCount  : Word;       { Count of Phonebook records }
  49.   GrpCount  : Word;       { Count of Group records }
  50.  
  51. {.PA}
  52. {───────────────────────────────────────────────────────────────────────┐
  53.                   PHONEBOOK FILE SUBROUTINE INTERFACE
  54. └───────────────────────────────────────────────────────────────────────}
  55. Function WritePBHdr( VAR PhoneBook : PhoneBook;
  56.                          RecCount : Word ) : Boolean;
  57.  
  58. {───────────────────────────────────────────────────────────────────────┐
  59. This function writes the record count into the header. It returns True
  60. if it was successful and False if not.
  61. └───────────────────────────────────────────────────────────────────────}
  62.  
  63. Function ReadPBHdr( VAR PhoneBook : PhoneBook)  : Word;
  64. {───────────────────────────────────────────────────────────────────────┐
  65. This function reads the record count into from the header.  It returns
  66. True if it was successful and False if not.
  67. └───────────────────────────────────────────────────────────────────────}
  68.  
  69. Procedure GetPBRec( VAR PhoneBook : PhoneBook;
  70.                     VAR  PBRecord : PBRec     );
  71. {───────────────────────────────────────────────────────────────────────┐
  72. This Procedure reads a record from the phonebook file.  Note that the
  73. phonebook file must already be open.
  74. └───────────────────────────────────────────────────────────────────────}
  75.  
  76. Procedure PutPBRec( VAR PhoneBook : PhoneBook;
  77.                         PBRecord  : PBRec     );
  78. {───────────────────────────────────────────────────────────────────────┐
  79. This procedure writes a PhoneBook Record.  There must be a valid phone
  80. number in at least one of the fields.  Also, the Name field cannot be
  81. blank.  The Phonebook must be open and the current RecCount must have
  82. been written to the Phonebook.
  83. └───────────────────────────────────────────────────────────────────────}
  84.  
  85. Function OpenPB(VAR PhoneBook : PhoneBook;
  86.                     FName : String        ) : Integer;
  87. {───────────────────────────────────────────────────────────────────────┐
  88. This Function opens the phonebook file.  It returns True if ok.  The
  89. name of the file will be extended to .DIR if none supplied.
  90. └───────────────────────────────────────────────────────────────────────}
  91.  
  92. Procedure ClosePB( VAR PhoneBook : PhoneBook);
  93. {───────────────────────────────────────────────────────────────────────┐
  94. This procedure Closes the Phonebook.  It should be called before exiting
  95. to DOS.
  96. └───────────────────────────────────────────────────────────────────────}
  97.  
  98. {.PA}
  99. {───────────────────────────────────────────────────────────────────────┐
  100.                     GROUP FILE SUBROUTINE INTERFACE
  101. └───────────────────────────────────────────────────────────────────────}
  102. Function ReadGrpHdr(VAR GroupFile : GroupFile) : Word;
  103. {───────────────────────────────────────────────────────────────────────┐
  104. This function reads the record count into the header.  It return True if
  105. it was successful and False if not.
  106. └───────────────────────────────────────────────────────────────────────}
  107.  
  108. Function WriteGrpHdr( VAR GroupFile : GroupFile;
  109.                           GroupCount : Word     ) : Boolean;
  110. {───────────────────────────────────────────────────────────────────────┐
  111. This function writes the record count into the header.  It returns True
  112. if it was successful and False if not.
  113. └───────────────────────────────────────────────────────────────────────}
  114.  
  115. Procedure GetGrpRec( VAR GroupFile : Groupfile;
  116.                      VAR GrpRecord : GrpRec  );
  117. {───────────────────────────────────────────────────────────────────────┐
  118. This Procedure reads a record from the Group file.  Note that the
  119. Group File must already be open.
  120. └───────────────────────────────────────────────────────────────────────}
  121.  
  122. Procedure PutGrpRec( VAR GroupFile : GroupFile;
  123.                          GrpRecord : GrpRec );
  124. {───────────────────────────────────────────────────────────────────────┐
  125. This procedure writes a GroupFile Record.  There must be a valid phone
  126. number in at least one of the fields.  Also, the Name field cannot be
  127. blank.  If either of these conditions occur then the procedure will exit
  128. immediately.  The Resulting GroupRecord will contain both blank Name and
  129. blank FAX phone numbers.  The GroupFile must be open and the current
  130. GroupCount must have been written.
  131. └───────────────────────────────────────────────────────────────────────}
  132.  
  133. Function OpenGrp(VAR GroupFile : GroupFile;
  134.                      FName     : String    ) : Integer;
  135. {───────────────────────────────────────────────────────────────────────┐
  136. This Function opens the Group file.  It returns True if ok.  The name of
  137. the file will be extended to .GRP if none supplied.
  138. └───────────────────────────────────────────────────────────────────────}
  139.  
  140. Procedure CloseGrp(VAR GroupFile : GroupFile );
  141. {───────────────────────────────────────────────────────────────────────┐
  142. This procedure Closes the GroupFile.  It should be called before exiting
  143. to DOS.
  144. └───────────────────────────────────────────────────────────────────────}
  145.  
  146. {.PA}
  147. {───────────────────────────────────────────────────────────────────────}
  148.                              IMPLEMENTATION
  149. {───────────────────────────────────────────────────────────────────────}
  150.  
  151. {═══════════════════════════════════════════════════════════════════════╗
  152.                        Phonebook file functions.
  153. ╚═══════════════════════════════════════════════════════════════════════}
  154.  
  155. {.CP 27}
  156. Function WritePBHdr( VAR PhoneBook : PhoneBook;
  157.                      RecCount : Word ) : Boolean;
  158. {───────────────────────────────────────────────────────────────────────┐
  159. This function writes the record count into the header.  It must reverse
  160. the order of the bytes in the write.  It return True if it was
  161. successful and False if not.
  162. └───────────────────────────────────────────────────────────────────────}
  163. CONST
  164.   Count : Word = 1;
  165.   MaxCount = 999;
  166.  
  167. VAR
  168.   CntByte : Byte;
  169.  
  170. BEGIN
  171.   If RecCount > MaxCount
  172.   then WritePBHdr := False
  173.   else begin
  174.     {$I-}
  175.     CntByte := Lo(RecCount);
  176.     BlockWrite(PhoneBook, CntByte, Count);
  177.     CntByte := Hi(RecCount);
  178.     BlockWrite(PhoneBook, CntByte, Count);
  179.     {$I+}
  180.     WritePBHdr := (IoResult = 0)
  181.   end;
  182. END;
  183.  
  184.  
  185. {.CP 25}
  186. Function ReadPBHdr( VAR PhoneBook : PhoneBook) : Word;
  187. {───────────────────────────────────────────────────────────────────────┐
  188. This function reads the record count into the header.  It must reverse
  189. the order of the bytes in the read.  It return True if it was successful
  190. and False if not.
  191. └───────────────────────────────────────────────────────────────────────}
  192. CONST
  193.   Count : Word = 1;
  194.   MaxCount = 999;
  195.  
  196. VAR
  197.   LoByte, HiByte : Byte;
  198.   i : Integer;
  199.  
  200. BEGIN
  201.   {$I-}
  202.   BlockRead(PhoneBook, LoByte, Count);
  203.   BlockWrite(PhoneBook, HiByte, Count);
  204.   {$I+}
  205.   RecCount := HiByte;
  206.   RecCount := HiByte Shl 8;
  207.   RecCount := RecCount OR LoByte;
  208.   ReadPBHdr := RecCount;
  209. END;
  210.  
  211.  
  212. {.CP 43}
  213. Procedure GetPBRec( VAR PhoneBook : PhoneBook;
  214.                     VAR PBRecord  : PBRec );
  215. {───────────────────────────────────────────────────────────────────────┐
  216. This Function reads a record from the phonebook file.  Note that the
  217. phonebook file must already be open.  If it is a succesful read, then
  218. the function will return True.
  219. └───────────────────────────────────────────────────────────────────────}
  220.  
  221. CONST
  222.   Count : Word = 1;           { How many bytes to Read    }
  223.  
  224. VAR
  225.    i     : integer;
  226.    Name, CAMPhone, FAXPhone, PollPWD  : String;
  227.    Zero, Terminator  : Byte;
  228.  
  229.  
  230. BEGIN
  231.     For i := 1 to NameLen Do                          { Name     }
  232.       BlockRead( PhoneBook, Name[i], count );
  233.     PBRecord.Name := Trim(Name);
  234.     BlockRead( PhoneBook, Zero, Count );
  235.     For i := 1 to PhoneLen Do                         { CAMPhone }
  236.       BlockRead( PhoneBook, CAMPhone[i], count );
  237.     PBRecord.CAMPhone := Trim(CAMPhone);
  238.     BlockRead( PhoneBook, Zero, Count );
  239.     For i := 1 to PhoneLen Do                         { FAXPhone }
  240.       BlockRead( PhoneBook, FAXPhone[i], count );
  241.     PBRecord.FAXPhone := Trim(FAXPhone);
  242.     BlockRead( PhoneBook, Zero, Count );
  243.     For i := 1 to PollLen Do                          { PollPWD  }
  244.       BlockRead( PhoneBook, PollPWD[i], count );
  245.     PBRecord.PollPWD := Trim(PollPWD);
  246.     BlockRead( PhoneBook, Zero, Count );
  247.     BlockRead(PhoneBook, Terminator, Count);
  248.     If Terminator > 1 then begin
  249.        WriteLn( 'Error Reading Record.');
  250.        Halt;
  251.     end;
  252.     BlockRead(PhoneBook, Zero, Count)
  253. END;  { function GetPBRec  }
  254.  
  255.  
  256. {.PA}
  257. Procedure PutPBRec( VAR PhoneBook : PhoneBook;
  258.                         PBRecord  : PBRec );
  259. {───────────────────────────────────────────────────────────────────────┐
  260. This function writes a PhoneBook Record.  It will return True if
  261. successful and False if not.  It may return False if there is
  262. insufficient information to write the record.  For instance, there must
  263. be a valid phone number in at least one of the fields.  Also, the Name
  264. field cannot be blank.  The Phonebook must be open and the current
  265. RecCount must have been written to the Phonebook.
  266. └───────────────────────────────────────────────────────────────────────}
  267.  
  268. CONST
  269.   Terminator : Byte = 1;      { Last Byte of Each Record }
  270.   Zero  : Byte = 0;
  271.   Count : Word = 1;
  272.  
  273. VAR
  274.    i     : integer;
  275.    Name  : String[NameLen];
  276.    CAMPhone : String[PhoneLen];
  277.    FAXPhone : String[PhoneLen];
  278.    PollPwd  : String[PollLen];
  279.  
  280. BEGIN
  281.     Name     := PBRecord.Name;
  282.     CAMPhone := PBRecord.CAMPhone;
  283.     FAXPhone := PBRecord.FAXPhone;
  284.     PollPWD  := PBRecord.PollPWD;
  285.     If (Length(Name) = 0) or
  286.        ( (Length(CAMPhone) = 0) and (Length(FAXPhone) = 0) ) Then  Exit;
  287.     Name := Pad(Trim(Name), NameLen);
  288.     For i := 1 to NameLen Do
  289.       BlockWrite( PhoneBook, Name[i], count );
  290.     BlockWrite( PhoneBook, Zero, Count );
  291.     CAMPhone := Pad(Trim(CAMPhone), PhoneLen);
  292.     For i := 1 to PhoneLen Do
  293.       BlockWrite( PhoneBook, CAMPhone[i], count );
  294.     BlockWrite( PhoneBook, Zero, Count );
  295.     FAXPhone := Pad(Trim(FAXPhone), PhoneLen);
  296.     For i := 1 to PhoneLen Do
  297.       BlockWrite( PhoneBook, FAXPhone[i], count );
  298.     BlockWrite( PhoneBook, Zero, Count );
  299.     PollPWD := Pad(Trim(PollPWD), PollLen);
  300.     For i := 1 to PollLen Do
  301.       BlockWrite( PhoneBook, PollPWD[i], count );
  302.     BlockWrite( PhoneBook, Zero, Count );
  303.     BlockWrite(PhoneBook, Terminator, Count);
  304.     BlockWrite(PhoneBook, Zero, Count)
  305. END;
  306.  
  307. {.cp 20}
  308. FUNCTION OpenPB( VAR PhoneBook : PhoneBook;
  309.                      FName : String ) : Integer;
  310. {───────────────────────────────────────────────────────────────────────┐
  311. This Function opens the phonebook file.  It returns 0 if ok.  The
  312. name of the file will be extended to .DIR if none supplied.
  313. └───────────────────────────────────────────────────────────────────────}
  314.  
  315. VAR
  316.   PBError : Integer;
  317.  
  318. BEGIN
  319.   OpenPB := 110;
  320.   RecCount := 0;
  321.   {$I-}
  322.   FName := DefaultExtension( FName, 'DIR');
  323.   Assign(PhoneBook, FName);
  324.   Rewrite(PhoneBook, 1);
  325.   {$I+}
  326.   OpenPB := IoResult;
  327. END;
  328.  
  329. {.cp 9}
  330. PROCEDURE ClosePB( VAR PhoneBook : PhoneBook);
  331. {───────────────────────────────────────────────────────────────────────┐
  332. This procedure Closes the Phonebook.  It MUST be called before exiting
  333. to DOS.
  334. └───────────────────────────────────────────────────────────────────────}
  335.  
  336. BEGIN
  337.   Close(PhoneBook)
  338. END;
  339.  
  340. {.PA}
  341. {═══════════════════════════════════════════════════════════════════════╗
  342.                          Group file functions.
  343. ╚═══════════════════════════════════════════════════════════════════════}
  344.  
  345. {.CP 17}
  346. Function WriteGrpHdr( VAR GroupFile : GroupFile;
  347.                           GroupCount : Word ) : Boolean;
  348. {───────────────────────────────────────────────────────────────────────┐
  349. This function writes the record count into the header.  It must do this
  350. in reverse order.  The file must be open.
  351. └───────────────────────────────────────────────────────────────────────}
  352. CONST
  353.   Count : Word = 1;
  354.   MaxCount = 99;
  355.  
  356. VAR
  357.   CntByte : Byte;
  358.  
  359. BEGIN
  360.   If GroupCount > MaxCount then
  361.     WriteGrpHdr := False
  362.   else begin
  363.     {$I-}
  364.     CntByte := Lo(GroupCount);
  365.     BlockWrite(GroupFile, CntByte, Count);
  366.     CntByte := Hi(GroupCount);
  367.     BlockWrite(GroupFile, CntByte, Count);
  368.     {$I+}
  369.     WriteGrpHdr := (IoResult = 0)
  370.   end;
  371. END;
  372.  
  373. {.CP 17}
  374. Function ReadGrpHdr( VAR GroupFile : GroupFile ) : Word;
  375. {───────────────────────────────────────────────────────────────────────┐
  376. This function reads the record count of the header.  It must reverse
  377. the order of the bytes in the read.  The GroupFile must be open.
  378. └───────────────────────────────────────────────────────────────────────}
  379. CONST
  380.   Count : Word = 1;
  381.   MaxCount = 99;
  382.  
  383. VAR
  384.   LoByte, HiByte : Byte;
  385.   i : Integer;
  386.   GroupCount : Word;
  387.  
  388. BEGIN
  389.   {$I-}
  390.   BlockRead(GroupFile, LoByte, Count);
  391.   BlockWrite(GroupFile, HiByte, Count);
  392.   {$I+}
  393.   GroupCount := HiByte;
  394.   GroupCount := HiByte Shl 8;
  395.   GroupCount := RecCount OR LoByte;
  396.   ReadGrpHdr := GroupCount;
  397. END;
  398.  
  399.  
  400. {.CP 43}
  401. Procedure GetGrpRec( VAR GroupFile : GroupFile;
  402.                      VAR GrpRecord : GrpRec );
  403. {───────────────────────────────────────────────────────────────────────┐
  404. This Function reads a record from the Group file.  Note that the Group
  405. file must already be open, and the header must have already been
  406. written.  If it is a succesful read, then the function will return True.
  407. └───────────────────────────────────────────────────────────────────────}
  408.  
  409. CONST
  410.   Count : Word = 1;           { How many bytes to Read    }
  411.   TermChar = $4A;
  412.  
  413. VAR
  414.    i     : integer;
  415.    Zero, Terminator  : Byte;
  416.    Name : String[NameLen];
  417.    FAXPhone : String[PhoneLen];
  418.    PollPwd : String[PollLen];
  419.  
  420. BEGIN
  421.     For i := 1 to NameLen Do                          { Name     }
  422.       BlockRead( GroupFile, Name[i], count );
  423.     GrpRecord.Name := Trim(Name);
  424.     BlockRead( GroupFile, Zero, Count );
  425.     For i := 1 to PhoneLen Do                         { FAXPhone }
  426.       BlockRead( GroupFile, FAXPhone[i], count );
  427.     GrpRecord.FAXPhone := Trim(FAXPhone);
  428.     BlockRead( GroupFile, Zero, Count );
  429.     For i := 1 to PollLen Do                          { PollPWD  }
  430.       BlockRead( GroupFile, PollPWD[i], count );
  431.     GrpRecord.PollPWD := Trim(PollPWD);
  432.     BlockRead( GroupFile, Zero, Count );
  433.     BlockRead(GroupFile, Terminator, Count);
  434.     If (Terminator <> TermChar) then
  435.        WriteLn( 'Trouble Reading Group File Records' );
  436.     For i := 1 to 4 Do
  437.       BlockRead(GroupFile, Zero, Count)
  438. END;  { function GetPBRec  }
  439.  
  440.  
  441. {.CP 48}
  442. Procedure PutGrpRec( VAR GroupFile : GroupFile;
  443.                          GrpRecord : GrpRec    );
  444. {───────────────────────────────────────────────────────────────────────┐
  445. This procedure writes a GroupFile Record.  There must be a valid phone
  446. number in at least one of the fields.  Also, the Name field cannot be
  447. blank.  If either of these conditions occur then the procedure will exit
  448. immediately.  The Resulting GroupRecord will contain both blank Name and
  449. blank FAX phone numbers.  The GroupFile must be open and the current
  450. GroupCount must have been written.
  451. └───────────────────────────────────────────────────────────────────────}
  452.  
  453. CONST
  454.   TermChar : Byte= $4A;      { Last Byte of Each Record }
  455.   Zero  : Byte = 0;
  456.   Count : Word = 1;
  457.  
  458. VAR
  459.    i     : integer;
  460.    Name     : String[NameLen];
  461.    FAXPhone : String[PhoneLen];
  462.    PollPwd  : String[PollLen];
  463.  
  464. BEGIN
  465.     Name     := GrpRecord.Name;
  466.     FAXPhone := GrpRecord.FAXPhone;
  467.     PollPwd  := GrpRecord.PollPwd;
  468.     If (Length(Name) = 0) or (Length(FAXPhone) = 0)
  469.       Then Exit;
  470.     Name := Pad(Trim(Name), NameLen);
  471.     For i := 1 to NameLen Do
  472.       BlockWrite( GroupFile, Name[i], count );
  473.     BlockWrite( GroupFile, Zero, Count );
  474.     FAXPhone := Pad(Trim(FAXPhone), PhoneLen);
  475.     For i := 1 to PhoneLen Do
  476.       BlockWrite( GroupFile, FAXPhone[i], count );
  477.     BlockWrite( GroupFile, Zero, Count );
  478.     PollPWD := Pad(Trim(PollPWD), PollLen);
  479.     For i := 1 to PollLen Do
  480.       BlockWrite( GroupFile, PollPWD[i], count );
  481.     BlockWrite( GroupFile, Zero, Count );
  482.     BlockWrite(GroupFile, TermChar, Count);
  483.     For i := 1 to 4 Do
  484.       BlockWrite(GroupFile, Zero, Count)
  485. END;
  486.  
  487.  
  488. {.cp 20}
  489. FUNCTION OpenGrp( VAR GroupFile : GroupFile;
  490.                       FName : String        ) : Integer;
  491. {───────────────────────────────────────────────────────────────────────┐
  492. This Function opens the Group file.  It returns 0 if everything went
  493. well, otherwise it returns the Turbo Pascal DOS error.  The name of the
  494. file will be extended to .GRP if none supplied.
  495. └───────────────────────────────────────────────────────────────────────}
  496.  
  497. VAR
  498.   GrpError : Integer;
  499.  
  500. BEGIN
  501.   OpenGrp := 110;
  502.   GrpCount := 0;
  503.   {$I-}
  504.   FName := DefaultExtension( FName, 'GRP');
  505.   Assign(GroupFile, FName);
  506.   Rewrite(GroupFile, 1);
  507.   {$I+}
  508.   OpenGrp := IoResult;
  509. END;
  510.  
  511.  
  512. {.cp 12}
  513. PROCEDURE CloseGrp( VAR GroupFile : GroupFile);
  514. {───────────────────────────────────────────────────────────────────────┐
  515. This procedure Closes the GroupFile.  It MUST be called before exiting
  516. to DOS.
  517. └───────────────────────────────────────────────────────────────────────}
  518.  
  519. BEGIN
  520.   Close(GroupFile)
  521. END;
  522.  
  523. END.  { End of Unit }
  524.