home *** CD-ROM | disk | FTP | other *** search
- Unit Ascii;
-
- {───────────────────────────────────────────────────────────────────────┐
- This unit reads and writes to/from an Ascii Text file. It will work with
- any version of Turbo Pascal 4.0 or later. The TPString Unit upon which
- it depends is from Turbo Power's Turbo Professional Series. The only
- functions used from it are Pad(), and Trim() which are easy to
- reproduce. I just used the Turbo Power unit because they did them in
- ASM and they are fast.
- └───────────────────────────────────────────────────────────────────────}
-
- {───────────────────────────────────────────────────────────────────────┐
- } INTERFACE {
- └───────────────────────────────────────────────────────────────────────}
-
-
- Uses TpString;
- { TpString is used procedure ParseRecord to modify string after it is
- parsed. It only uses two functions from the Unit, but I like them and
- they are fast. Re-Write them if you must or buy Turbo Power's Turbo
- Professional 5.0. }
-
- CONST
- NameLen = 20;
- PhoneLen = 27;
- PollLen = 8;
- TEXTLEN = 82;
-
- TYPE
- NameStr= String[NameLen];
- PhoneStr = String[PhoneLen];
- PollStr = String[PollLen];
-
- SDFRec = String[TEXTLEN]; { Text file record receiving string }
- CDFRec = String[TEXTLEN]; { Text file record receiving string }
- FNType = String[25]; { File Name type }
- SDFile = Text;
- CDFile = Text;
-
-
- VAR
- SDFCount : Word;
- CDFCount : Word;
-
- {.PA}
- {═══════════════════════════════════════════════════════════════════╗
- Standard Delimited Files
- ╚═══════════════════════════════════════════════════════════════════}
-
- Procedure ParseSDF(VAR Name : NameStr;
- VAR CAMPhone : PhoneStr;
- VAR FAXPhone : PhoneStr;
- VAR PollPwd : PollStr;
- InText : SDFRec );
- { Splits a text line into its parts }
-
- Procedure BuildSDF(Name : String;
- FAXPhone : String;
- CAMPhone : String;
- PollPwd : String;
- VAR OutText : SDFRec );
- { Assembles a Text line from its parts }
-
- Function GetSDF(VAR SDFile : SDFile) : String;
- { Gets a string from the SDF file }
-
- Procedure PutSDF(VAR SDFile : SDFile;
- TextRec : SDFRec );
- { Writes an SDF record out }
-
- Function OpenSDF( VAR SDFile : SDFile;
- SDFileName : FNType ) : Integer;
- { Opens a Standard Delimited File for Read/Write }
-
- Procedure CloseSDF( VAR SDFile : SDFile) ;
- { Closes the file }
-
- {.PA}
- {═══════════════════════════════════════════════════════════════════╗
- Comma Delimited Files
- ╚═══════════════════════════════════════════════════════════════════}
-
- Procedure ParseCDF(VAR Name : NameStr;
- VAR CAMPhone : PhoneStr;
- VAR FAXPhone : PhoneStr;
- VAR PollPwd : PollStr;
- InText : CDFRec );
-
- Procedure BuildCDF(Name : String;
- FAXPhone : String;
- CAMPhone : String;
- PollPwd : String;
- VAR OutText : CDFRec );
-
- Function GetCDF(VAR CDFile : CDFile) : String;
-
- Procedure PutCDF(VAR CDFile : CDFile;
- TextRec : CDFRec );
-
- Function OpenCDF( VAR CDFile : CDFile;
- CDFileName : FNType ) : Integer;
-
- Procedure CloseCDF( VAR CDFile : CDFile) ;
-
-
- {.PA}
- {───────────────────────────────────────────────────────────────────────┐
- } IMPLEMENTATION {
- └───────────────────────────────────────────────────────────────────────}
-
- {════════════════════════════════════════════════════════╗
- ║ Standard Delimited Files ║
- ╚════════════════════════════════════════════════════════}
-
- {.cp 25}
- Procedure ParseSDF(VAR Name : NameStr;
- VAR CAMPhone : PhoneStr;
- VAR FAXPhone : PhoneStr;
- VAR PollPwd : PollStr;
- InText : SDFRec );
- VAR
- Index : Integer;
-
- BEGIN
- Index := 1;
- InText := Pad(InText, TextLen);
- Name := StUpCase(Copy(InText, Index, NameLen));
- Index := Index + NameLen;
- CAMPhone := Copy(InText, Index, PhoneLen);
- Index := Index + PhoneLen;
- FAXPhone := Copy(InText, Index, PhoneLen);
- Index := Index + PhoneLen;
- PollPWD := Pad(StUpCase(Copy(InText, Index, PollLen)), PollLen);
- END;
-
-
- {.cp 12}
- Procedure BuildSDF(Name : String;
- FAXPhone : String;
- CAMPhone : String;
- PollPwd : String;
- VAR OutText : SDFRec );
-
- BEGIN
- OutText := Pad(Name, NameLen) +
- Pad(CamPhone, PhoneLen) +
- Pad(FaxPhone, PhoneLen) +
- Pad(PollPwd, PollLen )
- END;
-
-
- {.cp 18}
- Function GetSDF(VAR SDFile : SDFile ) : String;
- VAR
- TextRec : String;
- BEGIN
- ReadLn(SDFile, TextRec);
- GetSDF := TextRec
- END;
-
-
- Procedure PutSDF(VAR SDFile : SDFile;
- TextRec : SDFRec );
- BEGIN
- WriteLn(SDFile, TextRec);
- END;
-
-
- {.cp 17}
- Function OpenSDF( VAR SDFile : SDFile;
- SDFileName : FNType ) : Integer;
-
- VAR
- SDFError : Integer;
-
- BEGIN
- OpenSDF := 110;
- SDFCount := 0;
- {$I-}
- Assign(SDFile, SDFileName);
- Reset(SDFile);
- SDFError := IoResult;
- IF SDFError > 0 Then Rewrite(SDFile);
- {$I+}
- OpenSDF := IoResult;
- END;
-
-
- {.cp 5}
- PROCEDURE CloseSDF( var SDFile : SDFile) ;
-
- BEGIN
- Close(SDFile);
- END;
-
- {.PA}
- {════════════════════════════════════════════════════════╗
- ║ Comma Delimited Files ║
- ╚════════════════════════════════════════════════════════}
-
-
- {.cp 25}
- Procedure ParseCDF(VAR Name : NameStr;
- VAR CAMPhone : PhoneStr;
- VAR FAXPhone : PhoneStr;
- VAR PollPwd : PollStr;
- InText : CDFRec );
- VAR
- Index : Byte;
- i : Integer;
-
- BEGIN
- Delete(InText, 1, 1); { delete the leading " }
- Index := Pos('"', InText) - 1; { Name ends just before next " }
- Name := Pad(StUpCase(Copy(InText, 1, Index)), NameLen);
- Index := Index + 3; { Setup the delete to include }
- Delete(InText, 1, Index); { the "," between name and cam }
- Index := Pos('"', InText) - 1;
- CAMPhone := Pad(StUpCase(Copy(InText, 1, Index)), PhoneLen);
- Index := Index + 3;
- Delete(InText, 1, Index);
- FAXPhone := Pad(StUpCase(Copy(InText, 1, Index)), PhoneLen);
- Index := Index + 3;
- Delete(InText, 1, Index);
- Index := Pos('"', InText) - 1;
- PollPWD := Pad(StUpCase(Copy(InText, 1, Index)), PollLen);
- END;
-
-
- {.cp 12}
- Procedure BuildCDF(Name : String;
- FAXPhone : String;
- CAMPhone : String;
- PollPwd : String;
- VAR OutText : CDFRec );
- CONST
- Quote = '"';
- Delim = '","';
-
- BEGIN
- OutText := Quote + Trim(Name) + Delim +
- Trim(CamPhone) + Delim +
- Trim(FaxPhone) + Delim +
- Trim(PollPwd) + Quote;
- END;
-
-
- {.cp 7}
- Function GetCDF(VAR CDFile : CDFile ) : String;
- VAR
- TextRec : String[TextLen];
- BEGIN
- ReadLn(CDFile, TextRec);
- GetCDF := TextRec
- END;
-
-
- {.cp 5}
- Procedure PutCDF(VAR CDFile : CDFile;
- TextRec : CDFRec );
- BEGIN
- WriteLn(CDFile, TextRec);
- END;
-
-
- {.cp 17}
- Function OpenCDF( VAR CDFile : CDFile;
- CDFileName : FNType ) : Integer;
- VAR
- CDFError : Integer;
-
- BEGIN
- CDFCount := 0;
- {$I-}
- Assign(CDFile, CDFileName);
- Reset(CDFile);
- CDFError := IoResult;
- IF CDFError > 0 Then Rewrite(CDFile);
- {$I+}
- OpenCDF := IoResult;
- END;
-
-
- {.cp 5}
- PROCEDURE CloseCDF( var CDFile : CDFile) ;
-
- BEGIN
- Close(CDFile);
- END;
-
-
- END. { Unit }