Online Book Reader

Home Category

Beautiful Code [323]

By Root 5325 0
first.

The code for SaveReverse is shown in Example 30-2.

Example 30-2. Adding text to the start of a text file, without duplication

Code View: Scroll / Show All

Sub SaveReverse(ByVal filest As String, ByVal stringtoadd As String) 'not

'an append, a prepend...

'with elimination of duplicates

Dim starray(HistoryLength) As String

Dim i As Long

Dim arrlength As Long

Dim st As String

Dim filenum As Long

starray(0) = stringtoadd

filenum = FreeFile

i = 1

On Error GoTo err1

Open filest For Input As #filenum

While Not EOF(filenum) And (i < HistoryLength)

Line Input #filenum, st

If (st <> stringtoadd) Then 'only save non-duplicates

starray(i) = st

i = i + 1

End If

Wend

arrlength = i - 1

Close #filenum

Open filest For Output As #filenum 'this deletes the existing file contents

For i = 0 To arrlength

Print #filenum, starray(i)

Next

Close #filenum

Exit Sub

err1:

' MsgBox "error with file " + filest

Open filest For Output As #filenum

Close #filenum

Open filest For Input As #filenum 'this creates an empty file if one does 'not

exist

Resume Next

End Sub

30.2.8. Common Words and Favorites

Frequently used words are collected in the "common words" subtree, which has two components. Part of this subtree is static, consisting of very frequently used words such as a, and, but, etc. The dynamic part contains additional words frequently used by the user, which are found under its "favouritechoices" subtree.

The last 20 words found by the user in Speller can be found under its "favouritespeller" subtree. Likewise, if a node exists in the vocabulary tree called "cities," the user needs only to create a blank file, favouritecities.txt. Thereafter, the last 20 selections made by the user of words found under the cities subtree will be available under "favouritecities" in the "favouritechoices" subtree. In this way, the user can decide himself what kind of words, if used frequently, are worth remembering, and how they should be slotted.

Example 30-3 shows the subroutine that creates a new "favourites" and inserts it into the tree. Please note that stfavourite is the constant favorite, and MakeFullFileName returns a proper filename including the path, filename, and .txt extension.

Example 30-3. How eLocutor files words already typed under "favourites"

Code View: Scroll / Show All

Public Sub AddToFavourites(parentnode As Node, stAdd As String)

Dim tempfilename As String

If parentnode.Text = stStart Then

Exit Sub

End If

tempfilename = MakeFullFileName(App.Path, stfavourite + parentnode.Text)

If FileExists(tempfilename) Then

SaveReverse tempfilename, stAdd

Else

AddToFavourites parentnode.Parent, stAdd

End If

End Sub

Whenever a word is typed, eLocutor looks to see whether it also can be found in the vocabulary tree. Suppose the word Boston has just been typed. In that case, Boston is inserted at the top of the file favouritecities.txt, if it exists, using the subroutine SaveReverse. If not, eLocutor looks for favouriteplaces.txt, because the parent of Cities is Places. If that file doesn't exist, eLocutor tries a higher ancestor. If favouriteplaces.txt does exist, Boston is added to that file using the same subroutine. This provides the user with some control over what the software should consider her "favourites." By creating a file called favouritecities.txt, she is telling eLocutor that she uses city names a lot.

30.2.9. Retracing Paths

To aid in rapid navigation in a rather large tree, eLocutor automatically remembers, for each subtree in which the user has made a selection, what the user did the last 20 times after making a selection here. These destinations are presented conveniently to the user. Each parent node x has a subtree x_Next. After selecting a leaf node, the user should look under the sibling _Next node and select a destination close to where she wants to go next. Effectively, eLocutor detects patterns in operations performed by the user and allows her to repeat them easily. The software also remembers the last 20 files that were opened, the last 20 items of text

Return Main Page Previous Page Next Page

®Online Book Reader