Top Up Prev Next Bottom Contents Index Search

3.4 String Functions and Classes

The Ptolemy kernel defines some ordinary functions (not classes) plus two classes that are useful for building and manipulating strings. The non-class string functions are summarized in table 3-6
.. These include functions for copying strings, adding strings to a system-wide hash table, creating temporary file names. The non-class pathname functions are summarized in table 3-7
. These functions are for expanding file names that might begin with a reference to a user's home directory ("~username") or an shell environment variable ("$VARIABLE"). Also provided is a function for verifying that an external program to be invoked is available, and a function for searching the user's path.

Two classes are provided for manipulating strings, InfString, and StringList, these classes are summarized in figure 3-8

.

Although these two classes are almost identical in design, their recommended uses are quite different. The first is designed for building up strings without having to be concerned about the ultimate size of the string. New characters can be appended to the string at any time, and memory will be allocated to accommodate them. When you are ready to use the string, perhaps by passing it to a function that expects the standard character array representation of the string, then simply cast the object to char*.

In fact, InfString is publicly derived from StringList, adding only the cast to char*. StringList is implemented as a list of strings, where the size of the list is not bounded ahead of time. StringList is recommended for applications where the list structure is to be preserved. The cast to char* in InfString destroys the list structure, consolidating all its strings into one contiguous string.

The most useful methods for both classes are summarized in table . Since InfString differs by only one operator, we show only that one operator.

A word of warning is in order. If a function or expression returns a StringList or InfString, and that value is not assigned to a StringList or InfString variable or reference, and the (const char*) or (char*) cast is used, it is possible (likely under g++) that the StringList or InfString temporary will be destroyed too soon, leaving the const char* or char* pointer pointing to garbage. The solution is to assign the returned value to a local StringList or InfString before performing the cast. Suppose, for example, that the function foo returns an InfString. Further, suppose the function bar takes a char* argument. Then the following code will fail:

bar(foo()); (Note that the cast to char* is implicit). The following code will succeed:

InfString x = foo();
bar(x);


Top Up Prev Next Bottom Contents Index Search

Copyright © 1990-1997, University of California. All rights reserved.