~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty-proposed

« back to all changes in this revision

Viewing changes to yo/iostreams/src/strings.yo.OFF

  • Committer: Package Import Robot
  • Author(s): Frank B. Brokken
  • Date: 2013-05-30 13:32:18 UTC
  • mfrom: (1.1.24)
  • Revision ID: package-import@ubuntu.com-20130530133218-k39mr5uredd093jr
Tags: 9.7.2-1
New upstream release, repairs several minor left-over flaws.
This release incorporates 9.7.0 and 9.7.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Strings can be processed similarly to tt(iostream) objects, if objects of the
2
 
class tt(istrstream, ostrstream) or tt(strstream) are constructed. Objects of
3
 
these classes can be used to, respectively, read information from memory,
4
 
write information to memory, or both.
5
 
 
6
 
These objects can be created by constructors expecting the address
7
 
of a block of memory (and its size) as its argument. It is also possible to
8
 
let the objects to the memory management themselves.
9
 
 
10
 
Let's go through some examples. To write something into a block of memory
11
 
using a tt(ostrstream) object, the following code could be used:
12
 
        verb(
13
 
    char
14
 
        buffer[100];
15
 
    ostrstream
16
 
        os(buffer, 100);    // construct the ostrstream object
17
 
 
18
 
                            // fill 'buffer' with a well-known text
19
 
    os << "Hello world " << '\n' << ends;
20
 
 
21
 
    cout << os.str(); // display the string
22
 
        )
23
 
    Note the final tt(ends) that is appended: When an ascii-z string is
24
 
inserted into an tt(ostrstream) object it will not automatically write a
25
 
trailing ascii-z sentinel (comparable to the way tt(ostream) objects
26
 
behave). In order to append a terminating ascii-z, the symbolic value tt(ends)
27
 
can be used. After inserting an tt(ends) further insertions into the
28
 
tt(ostrstream) object will succeed, but they will not normally be visible:
29
 
        verb(
30
 
    char
31
 
        buffer[100];
32
 
    ostrstream
33
 
        os(buffer, 100);    // construct the ostrstream object
34
 
 
35
 
    os << "Hello world " << ends;
36
 
 
37
 
    os << " More text " << ends;
38
 
 
39
 
    cout << os.str() << '\n'; // this only shows 'Hello world'
40
 
        )
41
 
    The information, however, em(is) stored in the string, as shown by the
42
 
following example:
43
 
        verb(
44
 
    void bytes(ostrstream &str)
45
 
    {
46
 
        char
47
 
            *cp = str.str();
48
 
 
49
 
        cout << str.pcount() << ": ";
50
 
 
51
 
        for (int idx = 0; idx < 10; ++idx)
52
 
            cout << setw(3) << static_cast<int>(cp[idx]) << " ";
53
 
        cout << '\n';
54
 
    }
55
 
 
56
 
    int main()
57
 
    {
58
 
        char buffer[10];
59
 
 
60
 
        memset(buffer, 10, 10);
61
 
 
62
 
        ostrstream
63
 
            str(buffer, 100);
64
 
 
65
 
        bytes(str);
66
 
 
67
 
        str << "A";
68
 
 
69
 
        bytes(str);
70
 
 
71
 
        str << "B" << ends;
72
 
 
73
 
        bytes(str);
74
 
 
75
 
        str << "C";
76
 
 
77
 
        bytes(str);
78
 
 
79
 
        return (0);
80
 
    }
81
 
        )
82
 
    This little program produces the following output:
83
 
        verb(
84
 
    0:  10  10  10  10  10  10  10  10  10  10
85
 
    1:  65  10  10  10  10  10  10  10  10  10
86
 
    3:  65  66   0  10  10  10  10  10  10  10
87
 
    4:  65  66   0  67  10  10  10  10  10  10
88
 
        )
89
 
    This output shows that all insertions succeed, but the tt(ends) writes an
90
 
ascii-z character. This effectively creating an ascii-z string, preventing the
91
 
display of the information beyond when the contents of the tt(ostrstream)
92
 
object are inserted into tt(cout).
93
 
 
94
 
    Furthermore, note the use of the member function tt(str()), returning the
95
 
string the tt(ostrstream) object operates on. Using tt(str()) the existence of
96
 
tt(buffer) can be hidden from the users of the tt(ostrstream) object.
97
 
 
98
 
    When an tt(ostrstream) object is created without an external memory buffer
99
 
(e.g., `tt(ostrstream str;)' is defined), the tt(ostrstream) object allocates
100
 
the required memory itself. In that case using the tt(str()) member function
101
 
will result in the em(freezing) of the tt(ostrstream) object: it will no
102
 
longer create room for new characters when additional text is inserted into
103
 
the object, and, most important, it will em(not delete allocated memory) when
104
 
the object itself is deleted.
105
 
 
106
 
To prevent memory leakage here, the program using the tt(str()) member function
107
 
can take two actions:
108
 
    itemization(
109
 
        it() First, as tt(str()) returns a tt(char *) rather than a tt(char
110
 
const *) the caller of tt(str()) may consider the returned string its own.
111
 
Consequently, the caller of tt(str()) is responsible for deleting
112
 
the string returned by tt(str()). E.g.,
113
 
        verb(
114
 
    ostrstream
115
 
        ostr;
116
 
 
117
 
    ostr << "Hello world" << ends;
118
 
 
119
 
    char
120
 
        *cp = ostr.gets();  // freezes ostr
121
 
 
122
 
    cout << cp;             // use ostr's string
123
 
    delete cp;              // caller deletes ostr's string
124
 
        )
125
 
        it() Alternatively, the string can be em(unfrozen), after which
126
 
insertions are again possible. Now, when the tt(ostrstream) object is
127
 
destroyed the tt(ostrstream)'s internally stored string is destroyed
128
 
too. E.g.,
129
 
        verb(
130
 
    ostrstream
131
 
        ostr;
132
 
 
133
 
    ostr << "Hello world" << ends;
134
 
 
135
 
    char
136
 
        *cp = ostr.gets();  // freezes ostr
137
 
 
138
 
    cout << cp;             // use ostr's string
139
 
 
140
 
    ostr.freeze(0);         // ostr will now delete its own string, cp
141
 
                            // should leave the memory it points to alone.
142
 
        )
143
 
    )
144
 
 
145
 
    The following member functions are available for tt(strstream) objects:
146
 
    itemization(
147
 
    itt(istrstream::istrstream(const char *str [, int size])): This
148
 
constructor creates an input string class tt(istrstream) object, associating
149
 
it with an existing buffer starting at tt(str), of size tt(size).
150
 
If tt(size) is not specified, the buffer is treated as a null-terminated
151
 
string.
152
 
    itt(ostrstream::ostrstream()): This constructor creates a new stream for
153
 
output to a dynamically managed string, which will grow as needed.
154
 
    itt(ostrstream::ostrstream(char *str, int size [, int mode])): This
155
 
constructor creates a new stream for output to a statically defined string of
156
 
length tt(size), starting at tt(str).  The tt(mode) parameter may
157
 
optionally be specified as one of the iostream modes. By default tt(ios::out)
158
 
is used.
159
 
    itt(int ostrstream::pcount()): returns the current length of the string
160
 
associated with this tt(ostrstream) object.
161
 
    itt(char *ostrstream::str()): The member function returns a pointer to the
162
 
string managed by this tt(ostrstream) object. This function implies
163
 
tt(freeze()), see below:
164
 
    itt(void ostrstream::freeze ([int n])): If tt(n) is nonzero (the default),
165
 
the string associated with this tt(ostrstream) object must not change
166
 
dynamically anymore.  While frozen, it will not be reallocated if it needs
167
 
more space, and it will not be deallocated when the tt(ostrstream) object is
168
 
destroyed.  tt(freeze(1)) can be used to refer to the string as a pointer
169
 
after creating it via tt(ostrstream) facilities. tt(freeze(0)) can be used to
170
 
unfreeze (thaw ?) the object again. Following tt(freeze(0)) the tt(ostrstream)
171
 
object will delete memory it allocated when the object itself is deleted.
172
 
    itt(int ostrstream::frozen()): This member can be used to
173
 
test whether tt(freeze(1)) is in effect for this string.
174
 
    )
175
 
    In order to use the tt(strstream) classes, the header file tt(strstream)
176
 
must be included.