update credits
[LibreOffice.git] / include / cosv / streamstr.hxx
blobec13f8ec63c61285684d4d8fc758759619966faf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef CSV_STREAMSTR_HXX
21 #define CSV_STREAMSTR_HXX
23 #include "sal/config.h"
25 #include "sal/types.h"
27 // BASE CLASSES
28 #include <cosv/bstream.hxx>
29 // USED SERVICES
30 #include <cosv/str_types.hxx>
31 #include <string.h>
36 namespace csv
38 class String;
41 void c_str(); // Dummy needed for StreamStr::operator<<(StreamStr::F_CSTR);
44 /** A string buffer class for all kinds of string manipulation.
46 class StreamStr : public bostream
48 public:
49 typedef StreamStr self;
51 typedef str::size size_type;
52 typedef str::position position_type;
53 typedef intt seek_type;
54 typedef str::insert_mode insert_mode;
56 typedef const char * const_iterator;
57 typedef char * iterator;
59 typedef void (*F_CSTR)();
62 /** Represents an area within a string.
64 struct Area
66 typedef str::size size_type;
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;
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();
87 // OPERATORS
88 /// Copies also insert_mode and current position.
89 self & operator=(
90 const self & i_rOther );
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 );
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 );
116 self & operator<<(
117 float i_n );
118 self & operator<<(
119 double i_n );
121 /** This operator is used to finish a sequence of streaming
122 oeprators by returning the c-string of the complete string.
124 @return The same as ->c_str().
126 @example
127 csv::StreamStr s(100);
128 const char *
129 fullname = s << qualifier() << "::" << name() << csv::c_str;
131 const char * operator<<(
132 F_CSTR i_f );
134 const char & operator[](
135 position_type i_nPosition ) const;
136 char & operator[](
137 position_type i_nPosition );
139 // OPERATIONS
140 void resize(
141 size_type i_nMinimumCapacity );
143 void clear();
145 /** Sets start point for the next operator<<() call.
146 if the intended position is not reachable, nothing happens.
148 self & seekp(
149 seek_type i_nCount,
150 seek_dir i_eDirection = ::csv::beg );
151 self & reset() { return seekp(0); }
152 /** Sets the insertion mode of all and only the operator<<() calls.
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.
163 self & set_insert_mode(
164 insert_mode i_eMode );
166 void pop_front(
167 size_type i_nCount );
168 void pop_back(
169 size_type i_nCount );
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 );
190 void strip_front_whitespace(); /// removes space, tab and crlf.
191 void strip_back_whitespace();
192 void strip_frontback_whitespace();
194 void replace_all(
195 char i_cCarToSearch,
196 char i_cReplacement );
198 // INQUIRY
199 const char * c_str() const;
200 const char * data() const;
202 bool empty() const;
203 size_type size() const;
204 size_type length() const;
206 size_type capacity() const;
208 position_type tellp() const;
210 const_iterator begin() const;
211 const_iterator cur() const;
212 const_iterator end() const;
214 // ACCESS
215 iterator begin();
216 iterator cur();
217 iterator end();
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;
246 class StreamStrLock
248 public:
249 StreamStrLock(
250 uintt i_nMinimalSize );
251 ~StreamStrLock();
253 StreamStr & operator()() { return *pStr; }
255 private:
256 StreamStr * pStr;
259 // IMPLEMENTATION
261 inline const char *
262 StreamStr::operator<<( SAL_UNUSED_PARAMETER F_CSTR )
263 { return dpData; }
264 inline void
265 StreamStr::clear()
266 { pEnd = pCur = dpData; *pEnd = '\0'; }
267 inline const char *
268 StreamStr::c_str() const
269 { return dpData; }
270 inline const char *
271 StreamStr::data() const
272 { return dpData; }
273 inline bool
274 StreamStr::empty() const
275 { return dpData == pEnd; }
276 inline StreamStr::size_type
277 StreamStr::size() const
278 { return pEnd - dpData; }
279 inline StreamStr::size_type
280 StreamStr::length() const
281 { return size(); }
282 inline StreamStr::size_type
283 StreamStr::capacity() const
284 { return nCapacity1-1; }
285 inline StreamStr::position_type
286 StreamStr::tellp() const
287 { 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 StreamStr::begin()
299 { return dpData; }
300 inline StreamStr::iterator
301 StreamStr::cur()
302 { return pCur; }
303 inline StreamStr::iterator
304 StreamStr::end()
305 { return pEnd; }
307 } // namespace csv
308 #endif
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */