Rogue Wave banner
Previous fileTop of DocumentContentsNext file

15.2 An Example: Storing a Date Format String

Consider the inserter and extractor we defined for a date class in Section 11.3. The input and output operations were internationalized and relayed the task of date formatting and parsing to the stream's locale. Here, however, the rules for formatting and parsing were fixed, making them much more restricted than the features available in the standard C library, for example.

In the standard C library, you can specify format strings, similar to those for prinft() and scanf(), that describe the rules for parsing and formatting dates.23 For example, the format string "%A, %B %d, %Y" stands for the rule that a date must consist of the name of the weekday, the name of the month, the day of the month, and the year-as in Friday, July 12, 1996.

Now imagine you want to improve the input and output operations for the date class by allowing specification of such format strings. How can you do this? Other format information is stored in the stream's format state; consequently, you may want to store the format string for dates somewhere in the stream as well. And indeed, you can.

Streams have an array for private use. An array element is of a union type that allows access as a long or as a pointer to void.24 The array is of unspecified size, and new memory is allocated as needed. In principle, you can think of it as infinitely long.

You can use this array to store in a stream whatever additional information you might need. In our example, we would want to store the format string.

The array can be accessed by two functions: iword() and pword(). Both functions take an index to an array element and return a reference to the respective element. The function iword() returns a reference to long; the function pword() allows access to the array element as a pointer to void.

Indices into the array are maintained by the xalloc() function, a static function in class ios_base that returns the next free index into the array.


Previous fileTop of DocumentContentsNext file

©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.


OEM Release, June 1998