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

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