Sunday, December 18, 2005

TraverToy1 and Removing Blank Lines

TraverToy1 has arrived, and its purpose is show how to remove all blank lines from the source code. Before you use it, you have to save the RB Project you want to process as an XML file. (Cheer up you won't need to know anything about XML in order to understand the program.) An XML file is just a simple text file in a particular format. In it you'll see "tagged" text, similar to what you see in an HTML or XHTML file. Well, that's the alternate way you can save an RB Project, but we will be looking at it as just a bunch of lines of text (strings) that we will manipulate as strings.

Here's all you need to know for the moment: There are two types of lines in the XML file: lines the contain source code and lines that don't. The source code lines start with a "<SourceLine>" tag and end with a "</SourceLine>" tag. What is between the two tags is the source code for the program. If the tags have no text between them (other than, say, blank spaces), then that is a blank source code line.

To remove the blank lines of source code, then, all you need to do is to go through the lines of the file and check each line. If a line has "
<SourceLine>" and"</SourceLine>" tags it is source code. If there's no real text between the two tags (but just "empty space"), then remove that line. Then save the result back to disk.

There are a number of ways to read in the information from the file. You can read it in a line at a time, or you can (as this program does) load the whole file in at once as a super-long string, and then break that super-long string up into an array of smaller strings, each of which represents one line in the original XML file. Thus in TraverToy1, FileStreamA.ReadAll reads the whole file into memory as a super-long string and then uses the RB's built-in Split Function to break it up into individual lines, each of which becomes an element in an array. (In the program, the super-long string is called "MainBuffer" and the array of lines is called "XMLLines.")

For the processing, it's a simple matter of looping through the lines in the array, one line at a time, and removing from the array those elements that are blank lines. Then the lines of the array (XMLLines) are put back together in a super-long string (MainBuffer).

Finally, the result is saved back to disk. There are a number of ways this could be done. It could be written a line at a time, but the program saves the whole super-lone string Miainbuffer using FileStreamB.Write MainBuffer. That's all there is to it!

When you've finished, simply load the modified XML file into the RB IDE, from which it can be saved as a normal RB Project. Look at the source code, and you will indeed see that all of the blank lines have now been removed!

Similarly, it would not be difficult to write a short TraverToy to make other changes in line spacing, such as double- or triple-spacing the source code (to make more room for annotating source code that is printed out) or using blank lines to separate out logical units of code (e.g., putting a blank line before and after If/Then/Else/EndIf statements or For/Next loops). We may in fact do that in a future TraverToy or two, but we may do some other interesting things before then.

Barry Traver



Blog Home Page: http://traverrb.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrb/

0 Comments:

Post a Comment

<< Home