util.cpp
00001 /******************************************************************************* 00002 ** 00003 ** Filename : util 00004 ** Created on : 03 April, 2005 00005 ** Copyright : (c) 2005 Till Adam 00006 ** Email : <adam@kde.org> 00007 ** 00008 *******************************************************************************/ 00009 00010 /******************************************************************************* 00011 ** 00012 ** This program is free software; you can redistribute it and/or modify 00013 ** it under the terms of the GNU General Public License as published by 00014 ** the Free Software Foundation; either version 2 of the License, or 00015 ** (at your option) any later version. 00016 ** 00017 ** It is distributed in the hope that it will be useful, but 00018 ** WITHOUT ANY WARRANTY; without even the implied warranty of 00019 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 ** General Public License for more details. 00021 ** 00022 ** You should have received a copy of the GNU General Public License 00023 ** along with this program; if not, write to the Free Software 00024 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00025 ** 00026 ** In addition, as a special exception, the copyright holders give 00027 ** permission to link the code of this program with any edition of 00028 ** the TQt library by Trolltech AS, Norway (or with modified versions 00029 ** of TQt that use the same license as TQt), and distribute linked 00030 ** combinations including the two. You must obey the GNU General 00031 ** Public License in all respects for all of the code used other than 00032 ** TQt. If you modify this file, you may extend this exception to 00033 ** your version of the file, but you are not obligated to do so. If 00034 ** you do not wish to do so, delete this exception statement from 00035 ** your version. 00036 ** 00037 *******************************************************************************/ 00038 #include "util.h" 00039 00040 #include <stdlib.h> 00041 #include <tqcstring.h> 00042 #include <mimelib/string.h> 00043 00044 size_t KMail::Util::crlf2lf( char* str, const size_t strLen ) 00045 { 00046 if ( !str || strLen == 0 ) 00047 return 0; 00048 00049 const char* source = str; 00050 const char* sourceEnd = source + strLen; 00051 00052 // search the first occurrence of "\r\n" 00053 for ( ; source < sourceEnd - 1; ++source ) { 00054 if ( *source == '\r' && *( source + 1 ) == '\n' ) 00055 break; 00056 } 00057 00058 if ( source == sourceEnd - 1 ) { 00059 // no "\r\n" found 00060 return strLen; 00061 } 00062 00063 // replace all occurrences of "\r\n" with "\n" (in place) 00064 char* target = const_cast<char*>( source ); // target points to '\r' 00065 ++source; // source points to '\n' 00066 for ( ; source < sourceEnd; ++source ) { 00067 if ( *source != '\r' || *( source + 1 ) != '\n' ) 00068 * target++ = *source; 00069 } 00070 *target = '\0'; // terminate result 00071 return target - str; 00072 } 00073 00074 TQCString KMail::Util::lf2crlf( const TQCString & src ) 00075 { 00076 TQCString result( 1 + 2*src.size() ); // maximal possible length 00077 00078 TQCString::ConstIterator s = src.begin(); 00079 TQCString::Iterator d = result.begin(); 00080 // we use cPrev to make sure we insert '\r' only there where it is missing 00081 char cPrev = '?'; 00082 while ( *s ) { 00083 if ( ('\n' == *s) && ('\r' != cPrev) ) 00084 *d++ = '\r'; 00085 cPrev = *s; 00086 *d++ = *s++; 00087 } 00088 result.truncate( d - result.begin() ); // adds trailing NUL 00089 return result; 00090 } 00091 00092 TQByteArray KMail::Util::lf2crlf( const TQByteArray & src ) 00093 { 00094 const char* s = src.data(); 00095 if ( !s ) 00096 return TQByteArray(); 00097 00098 TQByteArray result( 2 * src.size() ); // maximal possible length 00099 TQByteArray::Iterator d = result.begin(); 00100 // we use cPrev to make sure we insert '\r' only there where it is missing 00101 char cPrev = '?'; 00102 const char* end = src.end(); 00103 while ( s != end ) { 00104 if ( ('\n' == *s) && ('\r' != cPrev) ) 00105 *d++ = '\r'; 00106 cPrev = *s; 00107 *d++ = *s++; 00108 } 00109 result.truncate( d - result.begin() ); // does not add trailing NUL, as expected 00110 return result; 00111 } 00112 00113 TQCString KMail::Util::CString( const DwString& str ) 00114 { 00115 const int strLen = str.size(); 00116 TQCString cstr( strLen + 1 ); 00117 memcpy( cstr.data(), str.data(), strLen ); 00118 cstr[ strLen ] = 0; 00119 return cstr; 00120 } 00121 00122 TQByteArray KMail::Util::ByteArray( const DwString& str ) 00123 { 00124 const int strLen = str.size(); 00125 TQByteArray arr( strLen ); 00126 memcpy( arr.data(), str.data(), strLen ); 00127 return arr; 00128 } 00129 00130 DwString KMail::Util::dwString( const TQCString& str ) 00131 { 00132 if ( !str.data() ) // DwString doesn't like char*=0 00133 return DwString(); 00134 return DwString( str.data(), str.size() - 1 ); 00135 } 00136 00137 DwString KMail::Util::dwString( const TQByteArray& str ) 00138 { 00139 if ( !str.data() ) // DwString doesn't like char*=0 00140 return DwString(); 00141 return DwString( str.data(), str.size() ); 00142 } 00143 00144 void KMail::Util::append( TQByteArray& that, const TQByteArray& str ) 00145 { 00146 that.detach(); 00147 uint len1 = that.size(); 00148 uint len2 = str.size(); 00149 if ( that.resize( len1 + len2, TQGArray::SpeedOptim ) ) 00150 memcpy( that.data() + len1, str.data(), len2 ); 00151 } 00152 00153 void KMail::Util::append( TQByteArray& that, const char* str ) 00154 { 00155 if ( !str ) 00156 return; // nothing to append 00157 that.detach(); 00158 uint len1 = that.size(); 00159 uint len2 = tqstrlen(str); 00160 if ( that.resize( len1 + len2, TQGArray::SpeedOptim ) ) 00161 memcpy( that.data() + len1, str, len2 ); 00162 } 00163 00164 void KMail::Util::append( TQByteArray& that, const TQCString& str ) 00165 { 00166 that.detach(); 00167 uint len1 = that.size(); 00168 uint len2 = str.size() - 1; 00169 if ( that.resize( len1 + len2, TQGArray::SpeedOptim ) ) 00170 memcpy( that.data() + len1, str.data(), len2 ); 00171 } 00172 00173 // Code taken from TQCString::insert, but trailing nul removed 00174 void KMail::Util::insert( TQByteArray& that, uint index, const char* s ) 00175 { 00176 int len = tqstrlen(s); 00177 if ( len == 0 ) 00178 return; 00179 uint olen = that.size(); 00180 int nlen = olen + len; 00181 if ( index >= olen ) { // insert after end of string 00182 that.detach(); 00183 if ( that.resize(nlen+index-olen, TQGArray::SpeedOptim ) ) { 00184 memset( that.data()+olen, ' ', index-olen ); 00185 memcpy( that.data()+index, s, len ); 00186 } 00187 } else { 00188 that.detach(); 00189 if ( that.resize(nlen, TQGArray::SpeedOptim ) ) { // normal insert 00190 memmove( that.data()+index+len, that.data()+index, olen-index ); 00191 memcpy( that.data()+index, s, len ); 00192 } 00193 } 00194 }