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

  1. Unit Ascii;
  2.  
  3. {───────────────────────────────────────────────────────────────────────┐
  4. This unit reads and writes to/from an Ascii Text file. It will work with
  5. any version of Turbo Pascal 4.0 or later.  The TPString Unit upon which
  6. it depends is from Turbo Power's Turbo Professional Series. The only
  7. functions used from it are Pad(),  and Trim() which are easy to
  8. reproduce.  I just used the Turbo Power unit because they did them in
  9. ASM and they are fast.
  10. └───────────────────────────────────────────────────────────────────────}
  11.  
  12. {───────────────────────────────────────────────────────────────────────┐
  13. }                              INTERFACE                                {
  14. └───────────────────────────────────────────────────────────────────────}
  15.  
  16.  
  17. Uses TpString;
  18. { TpString is used procedure ParseRecord to modify string after it is
  19. parsed.  It only uses two functions from the Unit, but I like them and
  20. they are fast.  Re-Write them if you must or buy Turbo Power's Turbo
  21. Professional 5.0.  }
  22.  
  23. CONST
  24.   NameLen  = 20;
  25.   PhoneLen = 27;
  26.   PollLen  = 8;
  27.   TEXTLEN  = 82;
  28.  
  29. TYPE
  30.   NameStr= String[NameLen];
  31.   PhoneStr = String[PhoneLen];
  32.   PollStr = String[PollLen];
  33.  
  34.   SDFRec = String[TEXTLEN];    { Text file record receiving string }
  35.   CDFRec = String[TEXTLEN];    { Text file record receiving string }
  36.   FNType = String[25];         { File Name type }
  37.   SDFile = Text;
  38.   CDFile = Text;
  39.  
  40.  
  41. VAR
  42.   SDFCount  : Word;
  43.   CDFCount  : Word;
  44.  
  45. {.PA}
  46. {═══════════════════════════════════════════════════════════════════╗
  47.                         Standard Delimited Files
  48. ╚═══════════════════════════════════════════════════════════════════}
  49.  
  50. Procedure ParseSDF(VAR Name     : NameStr;
  51.                    VAR CAMPhone : PhoneStr;
  52.                    VAR FAXPhone : PhoneStr;
  53.                    VAR PollPwd  : PollStr;
  54.                        InText   : SDFRec  );
  55. { Splits a text line into its parts }
  56.  
  57. Procedure BuildSDF(Name       : String;
  58.                    FAXPhone   : String;
  59.                    CAMPhone   : String;
  60.                    PollPwd    : String;
  61.                    VAR OutText : SDFRec  );
  62. { Assembles a Text line from its parts }
  63.  
  64. Function GetSDF(VAR SDFile   : SDFile) : String;
  65. { Gets a string from the SDF file }
  66.  
  67. Procedure PutSDF(VAR SDFile  : SDFile;
  68.                     TextRec : SDFRec );
  69. { Writes an SDF record out }
  70.  
  71. Function OpenSDF( VAR SDFile : SDFile;
  72.                   SDFileName : FNType ) : Integer;
  73. { Opens a Standard Delimited File for Read/Write }
  74.  
  75. Procedure CloseSDF( VAR SDFile : SDFile) ;
  76. { Closes the file }
  77.  
  78. {.PA}
  79. {═══════════════════════════════════════════════════════════════════╗
  80.                          Comma Delimited Files
  81. ╚═══════════════════════════════════════════════════════════════════}
  82.  
  83. Procedure ParseCDF(VAR Name     : NameStr;
  84.                    VAR CAMPhone : PhoneStr;
  85.                    VAR FAXPhone : PhoneStr;
  86.                    VAR PollPwd  : PollStr;
  87.                        InText   : CDFRec  );
  88.  
  89. Procedure BuildCDF(Name       : String;
  90.                    FAXPhone   : String;
  91.                    CAMPhone   : String;
  92.                    PollPwd    : String;
  93.                    VAR OutText : CDFRec  );
  94.  
  95. Function GetCDF(VAR CDFile   : CDFile) : String;
  96.  
  97. Procedure PutCDF(VAR CDFile  : CDFile;
  98.                     TextRec : CDFRec );
  99.  
  100. Function OpenCDF( VAR CDFile : CDFile;
  101.                   CDFileName : FNType ) : Integer;
  102.  
  103. Procedure CloseCDF( VAR CDFile : CDFile) ;
  104.  
  105.  
  106. {.PA}
  107. {───────────────────────────────────────────────────────────────────────┐
  108. }                           IMPLEMENTATION                              {
  109. └───────────────────────────────────────────────────────────────────────}
  110.  
  111.        {════════════════════════════════════════════════════════╗
  112.        ║                Standard Delimited Files                ║
  113.        ╚════════════════════════════════════════════════════════}
  114.  
  115. {.cp 25}
  116. Procedure ParseSDF(VAR Name : NameStr;
  117.                    VAR CAMPhone : PhoneStr;
  118.                    VAR FAXPhone : PhoneStr;
  119.                    VAR PollPwd  : PollStr;
  120.                        InText   : SDFRec  );
  121. VAR
  122.   Index : Integer;
  123.  
  124. BEGIN
  125.   Index := 1;
  126.   InText := Pad(InText, TextLen);
  127.   Name := StUpCase(Copy(InText, Index, NameLen));
  128.   Index := Index + NameLen;
  129.   CAMPhone := Copy(InText, Index, PhoneLen);
  130.   Index := Index + PhoneLen;
  131.   FAXPhone := Copy(InText, Index, PhoneLen);
  132.   Index := Index + PhoneLen;
  133.   PollPWD := Pad(StUpCase(Copy(InText, Index, PollLen)), PollLen);
  134. END;
  135.  
  136.  
  137. {.cp 12}
  138. Procedure BuildSDF(Name       : String;
  139.                    FAXPhone   : String;
  140.                    CAMPhone   : String;
  141.                    PollPwd    : String;
  142.                    VAR OutText : SDFRec  );
  143.  
  144. BEGIN
  145.   OutText := Pad(Name, NameLen) +
  146.              Pad(CamPhone, PhoneLen) +
  147.              Pad(FaxPhone, PhoneLen) +
  148.              Pad(PollPwd,  PollLen )
  149. END;
  150.  
  151.  
  152. {.cp 18}
  153. Function GetSDF(VAR SDFile   : SDFile ) : String;
  154. VAR
  155.    TextRec : String;
  156. BEGIN
  157.     ReadLn(SDFile, TextRec);
  158.     GetSDF := TextRec
  159. END;
  160.  
  161.  
  162. Procedure PutSDF(VAR SDFile  : SDFile;
  163.                     TextRec : SDFRec );
  164. BEGIN
  165.   WriteLn(SDFile, TextRec);
  166. END;
  167.  
  168.  
  169. {.cp 17}
  170. Function OpenSDF( VAR SDFile : SDFile;
  171.                   SDFileName : FNType ) : Integer;
  172.  
  173. VAR
  174.   SDFError : Integer;
  175.  
  176. BEGIN
  177.   OpenSDF := 110;
  178.   SDFCount := 0;
  179.   {$I-}
  180.   Assign(SDFile, SDFileName);
  181.   Reset(SDFile);
  182.   SDFError := IoResult;
  183.   IF SDFError > 0 Then Rewrite(SDFile);
  184.   {$I+}
  185.   OpenSDF := IoResult;
  186. END;
  187.  
  188.  
  189. {.cp 5}
  190. PROCEDURE CloseSDF( var SDFile : SDFile) ;
  191.  
  192. BEGIN
  193.   Close(SDFile);
  194. END;
  195.  
  196. {.PA}
  197.        {════════════════════════════════════════════════════════╗
  198.        ║                Comma Delimited Files                   ║
  199.        ╚════════════════════════════════════════════════════════}
  200.  
  201.  
  202. {.cp 25}
  203. Procedure ParseCDF(VAR Name     : NameStr;
  204.                    VAR CAMPhone : PhoneStr;
  205.                    VAR FAXPhone : PhoneStr;
  206.                    VAR PollPwd  : PollStr;
  207.                        InText   : CDFRec  );
  208. VAR
  209.   Index : Byte;
  210.   i     : Integer;
  211.  
  212. BEGIN
  213.   Delete(InText, 1, 1);            { delete the leading " }
  214.   Index := Pos('"', InText) - 1;   { Name ends just before next " }
  215.   Name := Pad(StUpCase(Copy(InText, 1, Index)), NameLen);
  216.   Index := Index + 3;              { Setup the delete to include }
  217.   Delete(InText, 1, Index);          { the "," between name and cam }
  218.   Index := Pos('"', InText) - 1;
  219.   CAMPhone := Pad(StUpCase(Copy(InText, 1, Index)), PhoneLen);
  220.   Index := Index + 3;
  221.   Delete(InText, 1, Index);
  222.   FAXPhone := Pad(StUpCase(Copy(InText, 1, Index)), PhoneLen);
  223.   Index := Index + 3;
  224.   Delete(InText, 1, Index);
  225.   Index := Pos('"', InText) - 1;
  226.   PollPWD := Pad(StUpCase(Copy(InText, 1, Index)), PollLen);
  227. END;
  228.  
  229.  
  230. {.cp 12}
  231. Procedure BuildCDF(Name       : String;
  232.                    FAXPhone   : String;
  233.                    CAMPhone   : String;
  234.                    PollPwd    : String;
  235.                    VAR OutText : CDFRec  );
  236. CONST
  237.   Quote = '"';
  238.   Delim = '","';
  239.  
  240. BEGIN
  241.   OutText := Quote + Trim(Name)     + Delim +
  242.                      Trim(CamPhone) + Delim +
  243.                      Trim(FaxPhone) + Delim +
  244.                      Trim(PollPwd)          + Quote;
  245. END;
  246.  
  247.  
  248. {.cp 7}
  249. Function GetCDF(VAR CDFile   : CDFile ) : String;
  250. VAR
  251.    TextRec : String[TextLen];
  252. BEGIN
  253.     ReadLn(CDFile, TextRec);
  254.     GetCDF := TextRec
  255. END;
  256.  
  257.  
  258. {.cp 5}
  259. Procedure PutCDF(VAR CDFile  : CDFile;
  260.                      TextRec : CDFRec );
  261. BEGIN
  262.   WriteLn(CDFile, TextRec);
  263. END;
  264.  
  265.  
  266. {.cp 17}
  267. Function OpenCDF( VAR CDFile : CDFile;
  268.                   CDFileName : FNType ) : Integer;
  269. VAR
  270.   CDFError : Integer;
  271.  
  272. BEGIN
  273.   CDFCount := 0;
  274.   {$I-}
  275.   Assign(CDFile, CDFileName);
  276.   Reset(CDFile);
  277.   CDFError := IoResult;
  278.   IF CDFError > 0 Then Rewrite(CDFile);
  279.   {$I+}
  280.   OpenCDF := IoResult;
  281. END;
  282.  
  283.  
  284. {.cp 5}
  285. PROCEDURE CloseCDF( var CDFile : CDFile) ;
  286.  
  287. BEGIN
  288.   Close(CDFile);
  289. END;
  290.  
  291.  
  292. END.     { Unit }
  293.