kmail
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 Qt library by Trolltech AS, Norway (or with modified versions 00029 ** of Qt that use the same license as Qt), 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 ** Qt. 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 <qcstring.h> 00042 00043 size_t KMail::Util::crlf2lf( char* str, const size_t strLen ) 00044 { 00045 if ( !str || strLen == 0 ) 00046 return 0; 00047 00048 const char* source = str; 00049 const char* sourceEnd = source + strLen; 00050 00051 // search the first occurrence of "\r\n" 00052 for ( ; source < sourceEnd - 1; ++source ) { 00053 if ( *source == '\r' && *( source + 1 ) == '\n' ) 00054 break; 00055 } 00056 00057 if ( source == sourceEnd - 1 ) { 00058 // no "\r\n" found 00059 return strLen; 00060 } 00061 00062 // replace all occurrences of "\r\n" with "\n" (in place) 00063 char* target = const_cast<char*>( source ); // target points to '\r' 00064 ++source; // source points to '\n' 00065 for ( ; source < sourceEnd; ++source ) { 00066 if ( *source != '\r' || *( source + 1 ) != '\n' ) 00067 * target++ = *source; 00068 } 00069 *target = '\0'; // terminate result 00070 return target - str; 00071 } 00072 00073 QCString KMail::Util::lf2crlf( const QCString & src ) 00074 { 00075 QCString result( 1 + 2*src.length() ); // maximal possible length 00076 00077 QCString::ConstIterator s = src.begin(); 00078 QCString::Iterator d = result.begin(); 00079 // we use cPrev to make sure we insert '\r' only there where it is missing 00080 char cPrev = '?'; 00081 while ( *s ) { 00082 if ( ('\n' == *s) && ('\r' != cPrev) ) 00083 *d++ = '\r'; 00084 cPrev = *s; 00085 *d++ = *s++; 00086 } 00087 result.truncate( d - result.begin() ); // adds trailing NUL 00088 return result; 00089 }