Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef CSV_STREAMSTR_HXX
21 : #define CSV_STREAMSTR_HXX
22 :
23 : #include "sal/config.h"
24 :
25 : #include "sal/types.h"
26 :
27 : // BASE CLASSES
28 : #include <cosv/bstream.hxx>
29 : // USED SERVICES
30 : #include <cosv/str_types.hxx>
31 : #include <string.h>
32 :
33 :
34 :
35 :
36 : namespace csv
37 : {
38 : class String;
39 :
40 :
41 : void c_str(); // Dummy needed for StreamStr::operator<<(StreamStr::F_CSTR);
42 :
43 :
44 : /** A string buffer class for all kinds of string manipulation.
45 : */
46 : class StreamStr : public bostream
47 : {
48 : public:
49 : typedef StreamStr self;
50 :
51 : typedef str::size size_type;
52 : typedef str::position position_type;
53 : typedef intt seek_type;
54 : typedef str::insert_mode insert_mode;
55 :
56 : typedef const char * const_iterator;
57 : typedef char * iterator;
58 :
59 : typedef void (*F_CSTR)();
60 :
61 :
62 : /** Represents an area within a string.
63 : */
64 : struct Area
65 : {
66 : typedef str::size size_type;
67 :
68 : Area(
69 : const char * i_str = "",
70 : size_type i_nLength = str::maxsize )
71 : : sStr(i_str),
72 : nLength( i_nLength == str::maxsize
73 : ? strlen(i_str)
74 : : i_nLength ) {}
75 : const char * sStr;
76 : size_type nLength;
77 : };
78 :
79 : // LIFECYCLE
80 : StreamStr(
81 : size_type i_nCapacity );
82 : /// Copies also insert_mode and current position.
83 : StreamStr(
84 : const self & i_rOther );
85 : ~StreamStr();
86 :
87 : // OPERATORS
88 : /// Copies also insert_mode and current position.
89 : self & operator=(
90 : const self & i_rOther );
91 :
92 : self & operator<<(
93 : const char * i_s );
94 : self & operator<<(
95 : const String & i_s );
96 : self & operator<<(
97 : char i_c );
98 : self & operator<<(
99 : unsigned char i_c );
100 : self & operator<<(
101 : signed char i_c );
102 :
103 : self & operator<<(
104 : short i_n );
105 : self & operator<<(
106 : unsigned short i_n );
107 : self & operator<<(
108 : int i_n );
109 : self & operator<<(
110 : unsigned int i_n );
111 : self & operator<<(
112 : long i_n );
113 : self & operator<<(
114 : unsigned long i_n );
115 :
116 : self & operator<<(
117 : float i_n );
118 : self & operator<<(
119 : double i_n );
120 :
121 : /** This operator is used to finish a sequence of streaming
122 : oeprators by returning the c-string of the complete string.
123 :
124 : @return The same as ->c_str().
125 :
126 : @example
127 : csv::StreamStr s(100);
128 : const char *
129 : fullname = s << qualifier() << "::" << name() << csv::c_str;
130 : */
131 : const char * operator<<(
132 : F_CSTR i_f );
133 :
134 : const char & operator[](
135 : position_type i_nPosition ) const;
136 : char & operator[](
137 : position_type i_nPosition );
138 :
139 : // OPERATIONS
140 : void resize(
141 : size_type i_nMinimumCapacity );
142 :
143 : void clear();
144 :
145 : /** Sets start point for the next operator<<() call.
146 : if the intended position is not reachable, nothing happens.
147 : */
148 : self & seekp(
149 : seek_type i_nCount,
150 : seek_dir i_eDirection = ::csv::beg );
151 301184 : self & reset() { return seekp(0); }
152 : /** Sets the insertion mode of all and only the operator<<() calls.
153 :
154 : Default is str::overwrite:
155 : str::overwrite: seekp() always sets the cur end of the string.
156 : operator<<() calls push the end of the string forward.
157 : str::insert: seekp() only sets the insertion point.
158 : operator<<() calls insert their text at the tellp()
159 : position and keep the rest of the string. tellp() is
160 : then after the inserted text, on the beginning of the
161 : rest of the string.
162 : */
163 : self & set_insert_mode(
164 : insert_mode i_eMode );
165 :
166 : void pop_front(
167 : size_type i_nCount );
168 : void pop_back(
169 : size_type i_nCount );
170 :
171 : /// Works like operator<<(). Does the same as Perl's join().
172 : self & operator_join(
173 : std::vector<String>::const_iterator
174 : i_rBegin,
175 : std::vector<String>::const_iterator
176 : i_rEnd,
177 : const char * i_sLink );
178 : /// Works like operator<<()
179 : self & operator_add_substr(
180 : const char * i_sText,
181 : size_type i_nLength );
182 : /// Works like operator<<()
183 : self & operator_add_token(
184 : const char * i_sText,
185 : char i_cDelimiter );
186 : /// Works like operator<<()
187 : self & operator_read_line(
188 : bstream & i_src );
189 :
190 : void strip_front_whitespace(); /// removes space, tab and crlf.
191 : void strip_back_whitespace();
192 : void strip_frontback_whitespace();
193 :
194 : void replace_all(
195 : char i_cCarToSearch,
196 : char i_cReplacement );
197 :
198 : // INQUIRY
199 : const char * c_str() const;
200 : const char * data() const;
201 :
202 : bool empty() const;
203 : size_type size() const;
204 : size_type length() const;
205 :
206 : size_type capacity() const;
207 :
208 : position_type tellp() const;
209 :
210 : const_iterator begin() const;
211 : const_iterator cur() const;
212 : const_iterator end() const;
213 :
214 : // ACCESS
215 : iterator begin();
216 : iterator cur();
217 : iterator end();
218 :
219 : private:
220 : // Interface bostream
221 : virtual UINT32 do_write(
222 : const void * i_pSrc,
223 : UINT32 i_nNrofBytes);
224 : // Locals
225 : void ProvideAddingSize(
226 : size_type i_nSize2Add );
227 : /// Resizes with the factor 2.0 (under 128), 1.5 or until i_nMinimumCapacity, whatever is bigger.
228 : void Resize(
229 : size_type i_nMinimumCapacity = 0 );
230 : void Advance(
231 : size_type i_nAddedSize );
232 : void MoveData(
233 : char * i_pStart,
234 : char * i_pEnd,
235 : seek_type i_nDiff );
236 : // DATA
237 : size_type nCapacity1; /// Capacity of characters to contain + 1 for terminating 0.
238 : DYN char * dpData;
239 : char * pEnd;
240 : char * pCur;
241 : insert_mode eMode;
242 : };
243 :
244 :
245 :
246 : class StreamStrLock
247 : {
248 : public:
249 : StreamStrLock(
250 : uintt i_nMinimalSize );
251 : ~StreamStrLock();
252 :
253 281915 : StreamStr & operator()() { return *pStr; }
254 :
255 : private:
256 : StreamStr * pStr;
257 : };
258 :
259 : // IMPLEMENTATION
260 :
261 : inline const char *
262 166357 : StreamStr::operator<<( SAL_UNUSED_PARAMETER F_CSTR )
263 166357 : { return dpData; }
264 : inline void
265 0 : StreamStr::clear()
266 0 : { pEnd = pCur = dpData; *pEnd = '\0'; }
267 : inline const char *
268 281407 : StreamStr::c_str() const
269 281407 : { return dpData; }
270 : inline const char *
271 : StreamStr::data() const
272 : { return dpData; }
273 : inline bool
274 12568 : StreamStr::empty() const
275 12568 : { return dpData == pEnd; }
276 : inline StreamStr::size_type
277 15115936 : StreamStr::size() const
278 15115936 : { return pEnd - dpData; }
279 : inline StreamStr::size_type
280 15100274 : StreamStr::length() const
281 15100274 : { return size(); }
282 : inline StreamStr::size_type
283 14770488 : StreamStr::capacity() const
284 14770488 : { return nCapacity1-1; }
285 : inline StreamStr::position_type
286 538608 : StreamStr::tellp() const
287 538608 : { return size_type(pCur-dpData); }
288 : inline StreamStr::const_iterator
289 : StreamStr::begin() const
290 : { return dpData; }
291 : inline StreamStr::const_iterator
292 : StreamStr::cur() const
293 : { return pCur; }
294 : inline StreamStr::const_iterator
295 : StreamStr::end() const
296 : { return pEnd; }
297 : inline StreamStr::iterator
298 3471 : StreamStr::begin()
299 3471 : { return dpData; }
300 : inline StreamStr::iterator
301 : StreamStr::cur()
302 : { return pCur; }
303 : inline StreamStr::iterator
304 0 : StreamStr::end()
305 0 : { return pEnd; }
306 :
307 : } // namespace csv
308 : #endif
309 :
310 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|