home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / database / dialbook / dbtest.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-05-18  |  5.0 KB  |  205 lines

  1. Program dbTest;
  2. {────────────────────────────────────────────────────────────────────┐
  3.   This program creates a Phonebook file from an ASCII fixed length
  4.   format text file.  The Text file must be in the same format as the
  5.   records: Name (20 char);
  6.            Phone(27 char);
  7.            FAX  (27 char);
  8.            Polling Password ( 8 char);
  9.  
  10.   Both phone numbers may have the following characters ONLY:
  11.     0..9    ,;  ()    -
  12.   Spaces may fill after the number but should not be included in the
  13.   number itself.  In particular the folling are illegal!
  14.   408 434-9701      ; illegal space after 408
  15.   (408) 434-9701    ; illegal space after (408)
  16.   408/434-9701      ; illegal /
  17.   [408]434-9701     : illegal []
  18. └────────────────────────────────────────────────────────────────────}
  19.  
  20. Uses Ascii, CFAX;
  21.  
  22. { TpString is used procedure ParseRecord to modify string after it is
  23. parsed.  It only uses two functions from the Unit, but I like them and
  24. they are fast.  Re-Write them if you must or buy Turbo Power's Turbo
  25. Professional 5.0.  }
  26.  
  27.  
  28. TYPE
  29.  
  30. { This describes a name record for working storage.  It will be
  31. implemented as a linked list. }
  32.   InRecPtr = ^InRec;
  33.   InRec = RECORD
  34.     Name     : NameStr;
  35.     CAMPhone : PhoneStr;
  36.     FAXPhone : PhoneStr;
  37.     PollPWD  : PollStr;
  38.     Next     : InRecPtr
  39.   END;
  40.  
  41. CONST
  42. {───────────────────────────────────────────────────────────────────────┐
  43. This section defines head and tail sentinals for linked list usage.
  44. Although this technique uses a little more memory, it sure makes coding
  45. linked lists a LOT easier.
  46. └───────────────────────────────────────────────────────────────────────}
  47.  
  48.   ListHead  : InRec = ( Name   : '^A';
  49.                       CAMPhone : '';
  50.                       FAXPhone : '^A';
  51.                       PollPWD  : '';
  52.                       Next     : Nil  );
  53.  
  54.   ListTail  : InRec = ( Name   : '~~~~~~~~~~~~~~~~~~~';
  55.                       CAMPhone : '~~~~~~~~~~~~~~~~~~~~~~~~~~';
  56.                       FAXPhone : '~~~~~~~~~~~~~~~~~~~~~~~~~~';
  57.                       PollPWD  : '~~~~~~~~';
  58.                       Next     : Nil  );
  59.  
  60.   LASTNAME  =  '~~~~~~~~~~~~~~~~~~~';
  61.  
  62. VAR
  63.   InputFile  : SDFile;
  64.   OutputFile : PhoneBook;
  65.   ListIR     : InRecPtr;
  66.   IRHead     : InRecPtr;
  67.   IRTail     : InRecPtr;
  68.   InFileName : FNType;
  69.   p          : Pointer;
  70.  
  71.  
  72.  
  73. PROCEDURE AddRecord(NewPtr : InRecPtr;
  74.                     VAR ListIR : InRecPtr );
  75. VAR
  76.   CurPtr,
  77.   NxtPtr   : InRecPtr;
  78.  
  79. BEGIN
  80.   CurPtr := ListIR;
  81.   NxtPtr := ListIR^.Next;
  82.   While NewPtr^.Name > NxtPtr^.Name DO
  83.     Begin
  84.       CurPtr := NxtPtr;
  85.       NxtPtr := NxtPtr^.Next;
  86.     End;
  87.   NewPtr^.Next := NxtPtr;
  88.   CurPtr^.Next := NewPtr;
  89. END;
  90.  
  91.  
  92. PROCEDURE ReadInput(Var RecCount : Word);
  93.  
  94. VAR
  95.   TextRec  : SDFRec;
  96.   InRecord : InRecPtr;
  97.   p        : Pointer;
  98.  
  99. BEGIN
  100.   New(IRHead);
  101.   IRHead^ := ListHead;
  102.   ListIR  := IRHead;
  103.   New(IRHead^.Next);
  104.   IRHead^.Next^ := ListTail;
  105.   SDFCount := 0;
  106.   While Not EOF(InputFile) Do Begin
  107.     TextRec := GetSDF(InputFile);
  108.     New(InRecord);
  109.     ParseSDF(InRecord^.Name,
  110.              InRecord^.CamPhone,
  111.              InRecord^.FaxPhone,
  112.              InRecord^.PollPwd,
  113.              TextRec);
  114.     AddRecord(InRecord, ListIR);
  115.     Inc(SDFCount);
  116.     Write(SDFCount:3, ' lines read.', ^M);
  117.   End;
  118.   WriteLn;
  119. END;
  120.  
  121.  
  122. PROCEDURE MakePBook;
  123.  
  124. CONST
  125.   ok = True;
  126.  
  127. VAR
  128.   CurPtr,
  129.   NxtPtr   : InRecPtr;
  130.   CntByte  : Byte;
  131.   Error    : Integer;
  132.   PBRecord : PBRec;     { PBRec defined in CFAX unit interface }
  133.  
  134. BEGIN
  135.   CurPtr := ListIR;
  136.   NxtPtr := ListIR^.Next;
  137.   RecCount := SDFCount;
  138.   If not WritePBHdr(OutputFile, RecCount) then begin
  139.     WriteLn;
  140.     WriteLn( 'Error writing the phonebook header' );
  141.     Halt;
  142.   End;
  143.   While NxtPtr^.Name < LASTNAME DO
  144.     Begin
  145.       CurPtr := NxtPtr;
  146.       NxtPtr := NxtPtr^.Next;
  147.       Write(CurPtr^.Name);
  148.       WriteLn(CurPtr^.FAXPhone);
  149.       PBRecord.Name     := CurPtr^.Name;
  150.       PBRecord.CAMPhone := CurPtr^.CamPhone;
  151.       PBRecord.FAXPhone := CurPtr^.FaxPhone;
  152.       PBRecord.PollPWD  := CurPtr^.PollPwd;
  153.       PutPBRec(OutputFile, PBRecord);
  154.     End;
  155. END;
  156.  
  157.  
  158. PROCEDURE OpenFiles;
  159.  
  160. VAR
  161.   IRError, PBError : Integer;
  162.  
  163. BEGIN
  164.   If ParamCount < 1 Then
  165.      Begin
  166.        WriteLn( 'You must supply the name of the data file!');
  167.        Halt
  168.      End
  169.     Else InFileName := ParamStr(1);
  170.   RecCount := 0;
  171.   SDFCount := 0;
  172.   IRError := OpenSDF(InputFile, InFileName);
  173.   IF ( IRError > 0 )
  174.     then begin
  175.       WriteLn;
  176.       WriteLn('Error', IRError, ' opening ', InFileName, ';  Program Halted');
  177.       WriteLn('Please write down the above line!');
  178.       exit;
  179.     end;
  180.   PBError := OpenPB(OutputFile, 'DIALBOOK.DIR');
  181.   IF PBError > 0 then
  182.     begin
  183.       WriteLn('Error ', PBError, ' opening DIALBOOK.DIR;  Program Halted');
  184.       halt;
  185.     end;
  186. END;
  187.  
  188.  
  189. PROCEDURE CloseFiles;
  190.  
  191. BEGIN
  192.   CloseSDF( InputFile );
  193.   ClosePB( OutputFile );
  194. END;
  195.  
  196.  
  197. BEGIN
  198.   OpenFiles;
  199.   Mark(p);
  200.   ReadInput(RecCount);
  201.   MakePBook;
  202.   Release(p);
  203.   CloseFiles;
  204. END.
  205.