NepomukDaemons
tstring.cpp
Go to the documentation of this file.00001 /* 00002 * This file is part of Soprano Project. 00003 * 00004 * Copyright (C) 2007 Sebastian Trueg <trueg@kde.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "tstring.h" 00023 00024 class TString::Private : public QSharedData 00025 { 00026 public: 00027 Private() 00028 : data( 0 ), 00029 wrap( false ) { 00030 } 00031 00032 ~Private() { 00033 if ( !wrap ) { 00034 free( data ); 00035 } 00036 } 00037 00038 TCHAR* data; 00039 bool wrap; 00040 }; 00041 00042 00043 TString::TString() 00044 : d( new Private() ) 00045 { 00046 } 00047 00048 00049 TString::TString( const TString& s ) 00050 { 00051 d = s.d; 00052 } 00053 00054 00055 TString::TString( const TCHAR* s, bool wrap ) 00056 : d( new Private() ) 00057 { 00058 d->wrap = wrap; 00059 if ( wrap ) { 00060 d->data = const_cast<TCHAR*>( s ); 00061 } 00062 else { 00063 operator=( s ); 00064 } 00065 } 00066 00067 00068 TString::TString( const QString& s ) 00069 : d( new Private() ) 00070 { 00071 operator=( s ); 00072 } 00073 00074 00075 TString::~TString() 00076 { 00077 } 00078 00079 00080 TString& TString::operator=( const TString& s ) 00081 { 00082 d = s.d; 00083 return *this; 00084 } 00085 00086 00087 TString& TString::operator=( const TCHAR* s ) 00088 { 00089 #ifdef _UCS2 00090 size_t len = wcslen( s ); 00091 d->data = ( TCHAR* )calloc( len+1, sizeof( TCHAR ) ); 00092 if ( d->data ) { 00093 wcscpy( d->data, s ); 00094 } 00095 #else 00096 d->data = strdup( s ); 00097 #endif 00098 d->wrap = false; 00099 return *this; 00100 } 00101 00102 00103 TString& TString::operator=( const QString& s ) 00104 { 00105 #ifdef _UCS2 00106 d->data = ( TCHAR* )calloc( s.length()+1, sizeof( TCHAR ) ); 00107 s.toWCharArray( d->data ); 00108 #else 00109 d->data = strdup( s.toUtf8().data() ); 00110 #endif 00111 d->wrap = false; 00112 return *this; 00113 } 00114 00115 00116 bool TString::isEmpty() const 00117 { 00118 return d->data == 0; 00119 } 00120 00121 00122 int TString::length() const 00123 { 00124 return _tcslen( d->data ); 00125 } 00126 00127 00128 const TCHAR* TString::data() const 00129 { 00130 return d->data; 00131 } 00132 00133 00134 TString::operator QString() const 00135 { 00136 return toQString(); 00137 } 00138 00139 00140 QString TString::toQString() const 00141 { 00142 if ( d->data ) { 00143 #ifdef _UCS2 00144 return QString::fromWCharArray( d->data ); 00145 #else 00146 return QString::fromUtf8( d->data ); 00147 #endif 00148 } 00149 else { 00150 return QString(); 00151 } 00152 } 00153 00154 00155 bool TString::operator==( const TString& other ) const 00156 { 00157 return _tcscmp( d->data, other.d->data ) == 0; 00158 } 00159 00160 00161 bool TString::operator==( const QString& other ) const 00162 { 00163 return toQString() == other; 00164 } 00165 00166 00167 bool TString::operator!=( const TString& other ) const 00168 { 00169 return !operator==( other ); 00170 } 00171 00172 00173 bool TString::operator!=( const QString& other ) const 00174 { 00175 return toQString() != other; 00176 } 00177 00178 00179 TString TString::fromUtf8( const char* data ) 00180 { 00181 TString s; 00182 #ifdef _UCS2 00183 s.d->data = ( TCHAR* )calloc( strlen( data )+1, sizeof( TCHAR ) ); // like this we are on the safe side 00184 QString::fromUtf8( data ).toWCharArray( s.d->data ); 00185 #else 00186 s.d->data = strdup( data ); 00187 #endif 00188 return s; 00189 }