summaryrefslogtreecommitdiff
path: root/cosv/inc/cosv/streamstr.hxx
blob: 75b1c1bbdd0b4b2a121c59c1f8043fab4afd5f79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: streamstr.hxx,v $
 * $Revision: 1.7 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#ifndef CSV_STREAMSTR_HXX
#define CSV_STREAMSTR_HXX

// BASE CLASSES
#include <cosv/bstream.hxx>
// USED SERVICES
#include <cosv/str_types.hxx>
#include <string.h>




namespace csv
{
    class String;


void c_str();   // Dummy needed for StreamStr::operator<<(StreamStr::F_CSTR);


/** A string buffer class for all kinds of string manipulation.
*/
class StreamStr : public bostream
{
   public:
    typedef StreamStr       self;

    typedef str::size           size_type;
    typedef str::position       position_type;
    typedef intt                seek_type;
    typedef str::insert_mode    insert_mode;

    typedef const char *        const_iterator;
    typedef char *              iterator;

    typedef void (*F_CSTR)();


    /** Represents an area within a string.
    */
    struct Area
    {
        typedef str::size           size_type;

                        Area(
                            const char *        i_str = "",
                            size_type           i_nLength = str::maxsize )
                                                :   sStr(i_str),
                                                    nLength( i_nLength == str::maxsize
                                                                ?   strlen(i_str)
                                                                :   i_nLength ) {}
        const char *    sStr;
        size_type       nLength;
    };

    // LIFECYCLE
                        StreamStr(
                            size_type           i_nCapacity );
                        StreamStr(
                            const char *        i_sInitStr,
                            size_type           i_nCapacity ); /// Only used if > strlen(i_sInitStr).
                        StreamStr(
                            size_type           i_nGuessedCapacity,
                            const char *        str1,   // [!= 0]
                            const char *        str2,   // [!= 0]
                            ...                 );      // Has to end with NIL .
                        StreamStr(
                            csv::bstream &      i_source );
    /// Copies also insert_mode and current position.
                        StreamStr(
                            const self &        i_rOther );
                        ~StreamStr();

    // OPERATORS
    /// Copies also insert_mode and current position.
    self &              operator=(
                            const self &        i_rOther );

    self &              operator<<(
                            const char *        i_s );
    self &              operator<<(
                            const String &      i_s );
    self &              operator<<(
                            char                i_c );
    self &              operator<<(
                            unsigned char       i_c );
    self &              operator<<(
                            signed char         i_c );

    self &              operator<<(
                            short               i_n );
    self &              operator<<(
                            unsigned short      i_n );
    self &              operator<<(
                            int                 i_n );
    self &              operator<<(
                            unsigned int        i_n );
    self &              operator<<(
                            long                i_n );
    self &              operator<<(
                            unsigned long       i_n );

    self &              operator<<(
                            float               i_n );
    self &              operator<<(
                            double              i_n );

    /** This operator is used to finish a sequence of streaming
        oeprators by returning the c-string of the complete string.

        @return The same as ->c_str().

        @example
        csv::StreamStr  s(100);
        const char *
            fullname =  s << qualifier() << "::" << name() << csv::c_str;
    */
    const char *        operator<<(
                            F_CSTR              i_f );

    const char &        operator[](
                            position_type       i_nPosition ) const;
    char &              operator[](
                            position_type       i_nPosition );

    //  OPERATIONS
    void                resize(
                            size_type           i_nMinimumCapacity );

    void                clear();
    void                swap(
                            StreamStr &         io_swap );

    /** Sets start point for the next operator<<() call.
        if the intended position is not reachable, nothing happens.
    */
    self &              seekp(
                            seek_type           i_nCount,
                            seek_dir            i_eDirection = ::csv::beg );
    self &              reset()                 { return seekp(0); }
    /** Sets the insertion mode of all and only the operator<<() calls.

        Default is str::overwrite:
        str::overwrite: seekp() always sets the cur end of the string.
                        operator<<() calls push the end of the string forward.
        str::insert:    seekp() only sets the insertion point.
                        operator<<() calls insert their text at the tellp()
                        position and keep the rest of the string. tellp() is
                        then after the inserted text, on the beginning of the
                        rest of the string.
    */
    self &              set_insert_mode(
                            insert_mode         i_eMode );

    void                push_front(
                            const char *        i_str );
    void                push_front(
                            char                i_c );
    void                push_back(
                            const char *        i_str );
    void                push_back(
                            char                i_c );
    void                pop_front(
                            size_type           i_nCount );
    void                pop_back(
                            size_type           i_nCount );

    /// Works like operator<<(). Does the same as Perl's join().
    self &              operator_join(
                            std::vector<String>::const_iterator
                                                i_rBegin,
                            std::vector<String>::const_iterator
                                                i_rEnd,
                            const char *        i_sLink );
    /// Works like operator<<()
    self &              operator_add_substr(
                            const char *        i_sText,
                            size_type           i_nLength );
    /// Works like operator<<()
    self &              operator_add_token(
                            const char *        i_sText,
                            char                i_cDelimiter );
    /// Works like operator<<()
    self &              operator_read_line(
                            bstream &           i_src );

    void                strip_front(
                            char                i_cToRemove );
    void                strip_back(
                            char                i_cToRemove );
    void                strip_frontback(
                            char                i_cToRemove );
    void                strip_front_whitespace();    /// removes space, tab and crlf.
    void                strip_back_whitespace();
    void                strip_frontback_whitespace();

    /** @precond i_begin is valid
        @precond i_end is valid
        @precond i_end >= i_begin
    */
    void                remove(
                            iterator            i_begin,
                            iterator            i_end );
    void                replace(
                            position_type       i_nStart,
                            size_type           i_nSize,
                            Area                i_aReplacement );

    void                replace_all(
                            char                i_cCarToSearch,
                            char                i_cReplacement );
    void                replace_all(
                            Area                i_aStrToSearch,
                            Area                i_aReplacement );

    StreamStr &         to_lower(
                            position_type       i_nStart = 0,
                            size_type           i_nLength = str::maxsize );
    StreamStr &         to_upper(
                            position_type       i_nStart = 0,
                            size_type           i_nLength = str::maxsize );

    // INQUIRY
    const char *        c_str() const;
    const char *        data() const;

    bool                empty() const;
    size_type           size() const;
    size_type           length() const;

    size_type           capacity() const;

    position_type       tellp() const;

    const_iterator      begin() const;
    const_iterator      cur() const;
    const_iterator      end() const;

    size_type           token_count(
                            char                i_cSplit ) const;
    String              token(
                            position_type       i_nNr,      /// Starting with 0.
                            char                i_cSpli ) const;

    // ACCESS
    iterator            begin();
    iterator            cur();
    iterator            end();

   private:
    // Interface bostream
    virtual UINT32      do_write(
                            const void *    i_pSrc,
                            UINT32          i_nNrofBytes);
    // Locals
    void                ProvideAddingSize(
                            size_type           i_nSize2Add );
    /// Resizes with the factor 2.0 (under 128), 1.5 or until i_nMinimumCapacity, whatever is bigger.
    void                Resize(
                            size_type           i_nMinimumCapacity = 0 );
    void                Advance(
                            size_type           i_nAddedSize );
    void                MoveData(
                            char *              i_pStart,
                            char *              i_pEnd,
                            seek_type           i_nDiff );
    // DATA
    size_type           nCapacity1;     /// Capacity of characters to contain + 1 for terminating 0.
    DYN char *          dpData;
    char *              pEnd;
    char *              pCur;
    insert_mode         eMode;
};



class StreamStrLock
{
  public:
                        StreamStrLock(
                            uintt               i_nMinimalSize );
                        ~StreamStrLock();

    StreamStr &         operator()()            { return *pStr; }

  private:
    StreamStr *         pStr;
};

/** Splits a string into tokens by whitespace.

    The tokens are added to the end of o_list.
*/
void                Split(
                        std::vector<String> &
                                            o_list,
                        const char *        i_text );
inline void         Join(
                        StreamStr &         o_text,
                        std::vector<String> &
                                            i_list,
                        const char *        i_sLink = " ");

// IMPLEMENTATION

inline const char *
StreamStr::operator<<( F_CSTR )
    { return dpData; }
inline void
StreamStr::clear()
    { pEnd = pCur = dpData; *pEnd = '\0'; }
inline const char *
StreamStr::c_str() const
    { return dpData; }
inline const char *
StreamStr::data() const
    { return dpData; }
inline bool
StreamStr::empty() const
    { return dpData == pEnd; }
inline StreamStr::size_type
StreamStr::size() const
    { return pEnd - dpData; }
inline StreamStr::size_type
StreamStr::length() const
    { return size(); }
inline StreamStr::size_type
StreamStr::capacity() const
    { return nCapacity1-1; }
inline StreamStr::position_type
StreamStr::tellp() const
    { return size_type(pCur-dpData); }
inline StreamStr::const_iterator
StreamStr::begin() const
    { return dpData; }
inline StreamStr::const_iterator
StreamStr::cur() const
    { return pCur; }
inline StreamStr::const_iterator
StreamStr::end() const
    { return pEnd; }
inline StreamStr::iterator
StreamStr::begin()
    { return dpData; }
inline StreamStr::iterator
StreamStr::cur()
    { return pCur; }
inline StreamStr::iterator
StreamStr::end()
    { return pEnd; }

inline void
Join( StreamStr &           o_text,
      std::vector<String> & i_list,
      const char *          i_sLink )
{
    o_text.operator_join(i_list.begin(),i_list.end(),i_sLink);
}




}   // namespace csv
#endif