File FixUp using TFileStream

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math Topics]   [Library]   [Utilities]

 

Search

 

Search DelphiForFun.org only

Support DFF

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

In Association with Amazon.com

 

Support DFF

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

 

 

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

 

Search DelphiForFun.org only

 

 

 

 

 

Here's a program illustrating the use of FileStream (TFileStream component) to manipulate a text file.   The specific task at hand is to eliminate redundant carriage return characters from a text file.  The problem occurs in several fonts included with the  LEDSign  package - a Java implementation simulating  scrolling LED signage. 

How the extra carriage returns originate is  anyone's guess.  A Google search on "extra carriage return characters" returns 33,000 hits so it's not exactly a unique problem.     I've seen blame placed on Unix, Mac, Netscape and a site named furryterror.org.  

 My theory is  this:  Many utilities exist to insert an extra carriage return (Cr) in front of a linefeed (Lf) to convert a Unix format file to Windows format.  If the utility isn't smart enough to check if a Cr already exists, running such a program twice would result in the Cr-Cr-Lf sequence. 

My specific instance of the problem occurred with a couple of the font files included with the LEDSign download.  LEDSign is a Java based package which simulates LED signage.  I didn't run the package, but did want to borrow the fonts for my Delphi version of the same project.  

OK - the pattern we are looking for is Cr-Cr-Lf  (Cr=carriage return=13=hex 0D,  Lf=linefeed=10=hex 0A).    TFileStream effectively replaces the old Blockread/Blockwrite procedures even though Blockread and Blockwrite are still available.   The general strategy for the program is 

Use TOpenDialog and TSaveDialog to get input and output file names,  
assign these file names to TFileStream controls,  
 use the input filestream read a bunch of data into a buffer, 
scan for Cr-Cr-Lf sequence, eliminate one of the Crs if the sequence is found, 
and write the resulting buffer to a new corrected version of the file using the output filestream. 

 If we could be assured that the entire file  fits into a single buffer,  the problem would be simplified - but I'm assuming that that is not the case.   So the tricky part of the program is to detect Cr-Cr-Lf that may cross a buffer boundary (for example a buffer fill ends with a Cr and then next buffer fill begins with a Cr-Lf).    My solution is to not write the last 2 characters of the buffer, but move them to the beginning of the buffer and read the next chunk of data starting at position 3.   Don't forget to write out those last 2 bytes of data when all the data has been processed.  

The download includes a test file named Snow.font demonstrating the problem. 

I had thoughts of generalizing the program to replace all occurrences of an arbitrary string with another string.   Two problems I didn't feel like addressing, since I didn't really need the more powerful version:  1) How to enter and display non-character data (like Cr and Lf)  in the strings  and 2) writing string processing type procedures that would work for a buffer which is not actually a string  (or perhaps make the buffer a real string?).   If anyone tackles this, let me know.  

Addendum:  September 5, 2003:  Steve Moller from the land down-under made a worthwhile addition to the program by incorporating Delphi's AdjustLineBreaks function into the program.   This function corrects other common line break errors by inserting missing Cr or Lf characters to make valid line endings.   It also reverses Lf-Cr sequences to  Cr-Lf.    I also added a display unit to show hexadecimal and character  view of the input and output files.  Just for testing I've included  file  PasDoc2.txt  with missing carriage returns in the downloads.    

Download Source or Executable 

bulletDownload source
bulletDownload executable 

 

Created:  July 17, 2001

                        Modified: May 18, 2009

 

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2013, Gary Darby    All rights reserved.