cfgfile 0.2.11
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1
31#ifndef CFGFILE__TYPES_HPP__INCLUDED
32#define CFGFILE__TYPES_HPP__INCLUDED
33
34#ifndef CFGFILE_DISABLE_STL
35// C++ include.
36#include <string>
37#include <iostream>
38#include <cwctype>
39#include <cctype>
40#include <vector>
41#endif // CFGFILE_DISABLE_STL
42
43#ifdef CFGFILE_QT_SUPPORT
44// Qt include.
45#include <QString>
46#include <QTextStream>
47#endif // CFGFILE_QT_SUPPORT
48
49
50namespace cfgfile {
51
52#ifndef CFGFILE_DISABLE_STL
53
54//
55// wstring_trait_t
56//
57
61 using string_t = std::wstring;
62
64 using char_t = string_t::value_type;
65
67 using ostream_t = std::wostream;
68
70 using istream_t = std::wistream;
71
73 using pos_t = std::streamoff;
74
76 using buf_t = std::vector< char_t >;
77
78 static inline string_t to_string( pos_t pos )
79 {
80 return std::to_wstring( pos );
81 }
82
83 static inline string_t from_ascii( const std::string & str )
84 {
85 std::wstring res;
86 res.assign( str.cbegin(), str.cend() );
87
88 return res;
89 }
90
91 static inline char_t from_ascii( char ch )
92 {
93 return (char_t) ch;
94 }
95
96 static inline void noskipws( istream_t & stream )
97 {
98 stream >> std::noskipws;
99 }
100
101 static inline bool is_at_end( istream_t & stream )
102 {
103 char_t tmp = 0;
104
105 stream >> tmp;
106
107 if( tmp )
108 {
109 stream.putback( tmp );
110
111 return false;
112 }
113 else
114 return true;
115 }
116
117 static inline bool is_space( char_t ch )
118 {
119 return ( std::iswspace( ch ) != 0 );
120 }
121
122 static inline void to_begin( istream_t & stream )
123 {
124 stream.seekg( 0 );
125 }
126
128 {
129 stream.seekg( 0, std::ios::end );
130
131 const auto s = stream.tellg();
132
133 stream.seekg( 0 );
134
135 return s;
136 }
137
138 static inline void fill_buf( istream_t & stream, buf_t & buf, pos_t buf_size, pos_t & pos, pos_t size )
139 {
140 pos_t actual_size = ( size - pos < buf_size ? size - pos : buf_size );
141
142 buf = buf_t( actual_size, 0x00 );
143
144 if( buf.size() > 0 )
145 stream.read( &buf[ 0 ], actual_size );
146
147 std::size_t skip = 0;
148
149 for( auto it = buf.crbegin(), last = buf.crend(); it != last; ++it )
150 {
151 if( *it == 0x00 )
152 ++skip;
153 else
154 break;
155 }
156
157 if( skip > 0 )
158 buf.resize( actual_size - skip );
159
160 pos += actual_size;
161 }
162}; // struct wstring_trait_t
163
164
165//
166// string_trait_t
167//
168
172 using string_t = std::string;
173
175 using char_t = string_t::value_type;
176
178 using istream_t = std::istream;
179
181 using pos_t = std::streamoff;
182
184 using ostream_t = std::ostream;
185
187 using buf_t = std::vector< char_t >;
188
189 static inline string_t to_string( pos_t pos )
190 {
191 return std::to_string( pos );
192 }
193
194 static inline string_t from_ascii( const std::string & str )
195 {
196 return str;
197 }
198
199 static inline char_t from_ascii( char ch )
200 {
201 return ch;
202 }
203
204 static inline void noskipws( istream_t & stream )
205 {
206 stream >> std::noskipws;
207 }
208
209 static inline bool is_at_end( istream_t & stream )
210 {
211 char_t tmp = 0;
212
213 stream >> tmp;
214
215 if( tmp )
216 {
217 stream.putback( tmp );
218
219 return false;
220 }
221 else
222 return true;
223 }
224
225 static inline bool is_space( char_t ch )
226 {
227 return ( std::isspace( (int) ch ) != 0 );
228 }
229
230 static inline void to_begin( istream_t & stream )
231 {
232 stream.seekg( 0 );
233 }
234
236 {
237 stream.seekg( 0, std::ios::end );
238
239 const auto s = stream.tellg();
240
241 stream.seekg( 0 );
242
243 return s;
244 }
245
246 static inline void fill_buf( istream_t & stream, buf_t & buf, pos_t buf_size, pos_t & pos, pos_t size )
247 {
248 pos_t actual_size = ( size - pos < buf_size ? size - pos : buf_size );
249
250 buf = buf_t( actual_size, 0x00 );
251
252 if( buf.size() > 0 )
253 stream.read( &buf[ 0 ], actual_size );
254
255 std::size_t skip = 0;
256
257 for( auto it = buf.crbegin(), last = buf.crend(); it != last; ++it )
258 {
259 if( *it == 0x00 )
260 ++skip;
261 else
262 break;
263 }
264
265 if( skip > 0 )
266 buf.resize( actual_size - skip );
267
268 pos += actual_size;
269 }
270}; // struct string_trait_t
271
272#endif // CFGFILE_DISABLE_STL
273
274
275#ifdef CFGFILE_QT_SUPPORT
276
277//
278// qstring_wrapper_t
279//
280
283public:
284 using size_type = int;
285
287 {
288 }
289
291 : m_str( size, ch )
292 {
293 }
294
295 qstring_wrapper_t( const char * str )
296 : m_str( str )
297 {
298 }
299
301 : m_str( other )
302 {
303 }
304
305
307 : m_str( unicode, size )
308 {
309 }
310
312 : m_str( ch )
313 {
314 }
315
317 : m_str( str )
318 {
319 }
320
322 : m_str( ba )
323 {
324 }
325
326 operator QString ()
327 {
328 return m_str;
329 }
330
331 operator QString () const
332 {
333 return m_str;
334 }
335
336 inline bool empty() const
337 {
338 return m_str.isEmpty();
339 }
340
341 static const int npos = -1;
342
343 inline int find( QChar ch ) const
344 {
345 return m_str.indexOf( ch );
346 }
347
348 inline int find( const qstring_wrapper_t & str ) const
349 {
350 return m_str.indexOf( str.m_str );
351 }
352
353 inline int rfind( const qstring_wrapper_t & str ) const
354 {
355 return m_str.lastIndexOf( str.m_str );
356 }
357
358 inline int rfind( QChar ch ) const
359 {
360 return m_str.lastIndexOf( ch );
361 }
362
364 const qstring_wrapper_t & v )
365 {
366 m_str.replace( pos, count, v.m_str );
367
368 return *this;
369 }
370
371 inline QString::iterator begin()
372 {
373 return m_str.begin();
374 }
375
376 inline QString::iterator end()
377 {
378 return m_str.end();
379 }
380
381 inline QString::const_iterator begin() const
382 {
383 return m_str.begin();
384 }
385
386 inline QString::const_iterator end() const
387 {
388 return m_str.end();
389 }
390
391 inline QString::const_iterator cbegin() const
392 {
393 return m_str.begin();
394 }
395
396 inline QString::const_iterator cend() const
397 {
398 return m_str.end();
399 }
400
401 inline size_type length() const
402 {
403 return m_str.length();
404 }
405
406
407 inline const QChar at( int position ) const
408 {
409 return m_str.at( position );
410 }
411
412
414 {
415 return m_str.mid( pos, count );
416 }
417
418 friend bool operator == ( const qstring_wrapper_t & s1,
419 const qstring_wrapper_t & s2 )
420 {
421 return ( s1.m_str == s2.m_str );
422 }
423
425 const qstring_wrapper_t & s2 )
426 {
427 return qstring_wrapper_t( s1.m_str + s2.m_str );
428 }
429
431 const char * s2 )
432 {
433 return qstring_wrapper_t( s1.m_str + s2 );
434 }
435
436 friend qstring_wrapper_t operator + ( const char * s1,
437 const qstring_wrapper_t & s2 )
438 {
439 return qstring_wrapper_t( s1 + s2.m_str );
440 }
441
443 const char ch )
444 {
445 return qstring_wrapper_t( s1.m_str + ch );
446 }
447
448 friend qstring_wrapper_t operator + ( const char ch,
449 const qstring_wrapper_t & s2 )
450 {
451 return qstring_wrapper_t( ch + s2.m_str );
452 }
453
455 const qstring_wrapper_t & what )
456 {
457 to << what.m_str;
458
459 return to;
460 }
461
462 inline const QChar operator [] ( size_type pos ) const
463 {
464 return m_str[ pos ];
465 }
466
468 {
469 m_str.append( other.m_str );
470
471 return *this;
472 }
473
475 {
476 m_str.append( QString( count, ch ) );
477
478 return *this;
479 }
480
481 inline void clear()
482 {
483 m_str.clear();
484 }
485
486 inline void push_back( QChar ch )
487 {
488 m_str.append( ch );
489 }
490
491private:
493 QString m_str;
494}; // class qstring_wrapper_t
495
496
497//
498// qstring_trait_t
499//
500
505
507 using char_t = QChar;
508
511
514
516 using pos_t = qint64;
517
519 using buf_t = QString;
520
521 static inline string_t to_string( pos_t pos )
522 {
523 return QString::number( pos );
524 }
525
526 static inline string_t from_ascii( const std::string & str )
527 {
528 return qstring_wrapper_t( QString( str.c_str() ) );
529 }
530
531 static inline char_t from_ascii( char ch )
532 {
533 return QLatin1Char( ch );
534 }
535
536 static inline void noskipws( istream_t & )
537 {
538 }
539
540 static inline bool is_at_end( istream_t & stream )
541 {
542 return stream.atEnd();
543 }
544
545 static inline bool is_space( char_t ch )
546 {
547 return ch.isSpace();
548 }
549
550 static inline void to_begin( istream_t & stream )
551 {
552 stream.seek( 0 );
553 }
554
556 {
557 return 0;
558 }
559
560 static inline void fill_buf( istream_t & stream, buf_t & buf, pos_t buf_size, pos_t & pos, pos_t size )
561 {
562 buf = stream.read( buf_size );
563 pos += buf.size();
564 }
565}; // struct qstring_trait_t
566
567#endif // CFGFILE_QT_SUPPORT
568
569
570#if defined(CFGFILE_DISABLE_STL) && defined(CFGFILE_QT_SUPPORT)
571using string_trait_t = qstring_trait_t;
572#endif
573
574
575//
576// DISABLE_COPY
577//
578
580#define DISABLE_COPY( Class ) \
581 Class( const Class & ) = delete; \
582 Class & operator= ( const Class & ) = delete;
583
584} /* namespace cfgfile */
585
586#endif // CFGFILE__TYPES_HPP__INCLUDED
Exception in the library.
Definition exceptions.hpp:51
QString wrapper.
Definition types.hpp:282
int find(QChar ch) const
Definition types.hpp:343
qstring_wrapper_t(const QChar *unicode, size_type size=-1)
Definition types.hpp:306
static const int npos
Definition types.hpp:341
QString::const_iterator begin() const
Definition types.hpp:381
const QChar operator[](size_type pos) const
Definition types.hpp:462
int rfind(QChar ch) const
Definition types.hpp:358
void clear()
Definition types.hpp:481
QString::const_iterator end() const
Definition types.hpp:386
qstring_wrapper_t & append(const qstring_wrapper_t &other)
Definition types.hpp:467
QString::iterator end()
Definition types.hpp:376
qstring_wrapper_t(size_type size, QChar ch)
Definition types.hpp:290
qstring_wrapper_t(const QString &other)
Definition types.hpp:300
friend qstring_wrapper_t operator+(const qstring_wrapper_t &s1, const qstring_wrapper_t &s2)
Definition types.hpp:424
friend QTextStream & operator<<(QTextStream &to, const qstring_wrapper_t &what)
Definition types.hpp:454
void push_back(QChar ch)
Definition types.hpp:486
qstring_wrapper_t(QChar ch)
Definition types.hpp:311
bool empty() const
Definition types.hpp:336
qstring_wrapper_t & append(size_type count, QChar ch)
Definition types.hpp:474
qstring_wrapper_t & replace(size_type pos, size_type count, const qstring_wrapper_t &v)
Definition types.hpp:363
const QChar at(int position) const
Definition types.hpp:407
QString::iterator begin()
Definition types.hpp:371
friend bool operator==(const qstring_wrapper_t &s1, const qstring_wrapper_t &s2)
Definition types.hpp:418
qstring_wrapper_t(const char *str)
Definition types.hpp:295
qstring_wrapper_t(const QByteArray &ba)
Definition types.hpp:321
QString::const_iterator cbegin() const
Definition types.hpp:391
qstring_wrapper_t substr(size_type pos, size_type count=npos) const
Definition types.hpp:413
int rfind(const qstring_wrapper_t &str) const
Definition types.hpp:353
int find(const qstring_wrapper_t &str) const
Definition types.hpp:348
qstring_wrapper_t()
Definition types.hpp:286
qstring_wrapper_t(QLatin1String str)
Definition types.hpp:316
size_type length() const
Definition types.hpp:401
QString::const_iterator cend() const
Definition types.hpp:396
Definition const.hpp:38
Trait for QString support.
Definition types.hpp:502
static bool is_at_end(istream_t &stream)
Definition types.hpp:540
static string_t from_ascii(const std::string &str)
Definition types.hpp:526
static void to_begin(istream_t &stream)
Definition types.hpp:550
static bool is_space(char_t ch)
Definition types.hpp:545
static char_t from_ascii(char ch)
Definition types.hpp:531
static void fill_buf(istream_t &stream, buf_t &buf, pos_t buf_size, pos_t &pos, pos_t size)
Definition types.hpp:560
static string_t to_string(pos_t pos)
Definition types.hpp:521
static void noskipws(istream_t &)
Definition types.hpp:536
static pos_t size_of_file(istream_t &stream)
Definition types.hpp:555
Trait for std::string support.
Definition types.hpp:170
static char_t from_ascii(char ch)
Definition types.hpp:199
static void fill_buf(istream_t &stream, buf_t &buf, pos_t buf_size, pos_t &pos, pos_t size)
Definition types.hpp:246
std::istream istream_t
Input stream type.
Definition types.hpp:178
std::streamoff pos_t
Type of pos in stream.
Definition types.hpp:181
std::string string_t
String type.
Definition types.hpp:172
static pos_t size_of_file(istream_t &stream)
Definition types.hpp:235
static bool is_at_end(istream_t &stream)
Definition types.hpp:209
static void to_begin(istream_t &stream)
Definition types.hpp:230
static string_t from_ascii(const std::string &str)
Definition types.hpp:194
string_t::value_type char_t
Char type.
Definition types.hpp:175
static string_t to_string(pos_t pos)
Definition types.hpp:189
static bool is_space(char_t ch)
Definition types.hpp:225
std::ostream ostream_t
Output stream type.
Definition types.hpp:184
std::vector< char_t > buf_t
Type of the buffer.
Definition types.hpp:187
static void noskipws(istream_t &stream)
Definition types.hpp:204
Trait for std::wstring support.
Definition types.hpp:59
static pos_t size_of_file(istream_t &stream)
Definition types.hpp:127
static void noskipws(istream_t &stream)
Definition types.hpp:96
static void fill_buf(istream_t &stream, buf_t &buf, pos_t buf_size, pos_t &pos, pos_t size)
Definition types.hpp:138
static string_t to_string(pos_t pos)
Definition types.hpp:78
static bool is_at_end(istream_t &stream)
Definition types.hpp:101
static char_t from_ascii(char ch)
Definition types.hpp:91
std::wostream ostream_t
Out stream type.
Definition types.hpp:67
std::vector< char_t > buf_t
Type of the buffer.
Definition types.hpp:76
std::wstring string_t
String type.
Definition types.hpp:61
static string_t from_ascii(const std::string &str)
Definition types.hpp:83
std::wistream istream_t
In stream type.
Definition types.hpp:70
std::streamoff pos_t
Type of pos in stream.
Definition types.hpp:73
static void to_begin(istream_t &stream)
Definition types.hpp:122
string_t::value_type char_t
Char type.
Definition types.hpp:64
static bool is_space(char_t ch)
Definition types.hpp:117