add more spacing
[personal-kdebase.git] / runtime / nepomuk / strigibackend / tstring.cpp
blob0976de10171a3f19aa8e0680921d0200229535b7
1 /*
2 * This file is part of Soprano Project.
4 * Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "tstring.h"
24 class TString::Private : public QSharedData
26 public:
27 Private()
28 : data( 0 ),
29 wrap( false ) {
32 ~Private() {
33 if ( !wrap ) {
34 free( data );
38 TCHAR* data;
39 bool wrap;
43 TString::TString()
44 : d( new Private() )
49 TString::TString( const TString& s )
51 d = s.d;
55 TString::TString( const TCHAR* s, bool wrap )
56 : d( new Private() )
58 d->wrap = wrap;
59 if ( wrap ) {
60 d->data = const_cast<TCHAR*>( s );
62 else {
63 operator=( s );
68 TString::TString( const QString& s )
69 : d( new Private() )
71 operator=( s );
75 TString::~TString()
80 TString& TString::operator=( const TString& s )
82 d = s.d;
83 return *this;
87 TString& TString::operator=( const TCHAR* s )
89 #ifdef _UCS2
90 size_t len = wcslen( s );
91 d->data = ( TCHAR* )calloc( len+1, sizeof( TCHAR ) );
92 if ( d->data ) {
93 wcscpy( d->data, s );
95 #else
96 d->data = strdup( s );
97 #endif
98 d->wrap = false;
99 return *this;
103 TString& TString::operator=( const QString& s )
105 #ifdef _UCS2
106 d->data = ( TCHAR* )calloc( s.length()+1, sizeof( TCHAR ) );
107 s.toWCharArray( d->data );
108 #else
109 d->data = strdup( s.toUtf8().data() );
110 #endif
111 d->wrap = false;
112 return *this;
116 bool TString::isEmpty() const
118 return d->data == 0;
122 int TString::length() const
124 return _tcslen( d->data );
128 const TCHAR* TString::data() const
130 return d->data;
134 TString::operator QString() const
136 return toQString();
140 QString TString::toQString() const
142 if ( d->data ) {
143 #ifdef _UCS2
144 return QString::fromWCharArray( d->data );
145 #else
146 return QString::fromUtf8( d->data );
147 #endif
149 else {
150 return QString();
155 bool TString::operator==( const TString& other ) const
157 return _tcscmp( d->data, other.d->data ) == 0;
161 bool TString::operator==( const QString& other ) const
163 return toQString() == other;
167 bool TString::operator!=( const TString& other ) const
169 return !operator==( other );
173 bool TString::operator!=( const QString& other ) const
175 return toQString() != other;
179 TString TString::fromUtf8( const char* data )
181 TString s;
182 #ifdef _UCS2
183 s.d->data = ( TCHAR* )calloc( strlen( data )+1, sizeof( TCHAR ) ); // like this we are on the safe side
184 QString::fromUtf8( data ).toWCharArray( s.d->data );
185 #else
186 s.d->data = strdup( data );
187 #endif
188 return s;