% File "main.t" % The file contains a driver program whose purpose is merely to test % the rope class (and indirectly, to test the Node, internalNode and % leafNode classes. % To run this entire program, one simply runs main.t % Imports % ------------------------------------------------------------------ % This program (directly) uses only the rope class. Indirectly, % it uses also Node, internalNode, and leafNode. import rope in "rope.tu" % Constants % ------------------------------------------------------------------ % These special stream numbers refer to the standard input and % the standard output. const stdoutStream : int := -1 const stdinStream : int := -2 % countChars % ------------------------------------------------------------------ % Counts the number of characters in the file whose stream number is % `inFile', which must already be open. This count is needed before % calling rope->Get. If called, rope->Get also has to read the file, % which means that it will be read twice in total. The extra cost % of reading the file twice buys us the ability to build a balanced % tree. % An alternative approach would be to read the file only once, % creating an imbalanced tree, and then to go back and shuffle the % tree around to balance it. Another approach would be to require % the input to begin with a count of the number of characters in the % long string. % Note that this procedure should not be called if input is % to come from the keyboard. The user would have to type the input % once to be counted, and once again to be read by rope->Get. procedure countChars(inFile : int, var numChars : int) var c : string(1) numChars := 0 loop exit when eof(inFile) get :inFile, c : 1 if c not= "\n" then numChars += 1 end if end loop end countChars % Main Program % ------------------------------------------------------------------ %