kmail

util.cpp
1/*******************************************************************************
2**
3** Filename : util
4** Created on : 03 April, 2005
5** Copyright : (c) 2005 Till Adam
6** Email : <adam@kde.org>
7**
8*******************************************************************************/
9
10/*******************************************************************************
11**
12** This program is free software; you can redistribute it and/or modify
13** it under the terms of the GNU General Public License as published by
14** the Free Software Foundation; either version 2 of the License, or
15** (at your option) any later version.
16**
17** It is distributed in the hope that it will be useful, but
18** WITHOUT ANY WARRANTY; without even the implied warranty of
19** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20** General Public License for more details.
21**
22** You should have received a copy of the GNU General Public License
23** along with this program; if not, write to the Free Software
24** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25**
26** In addition, as a special exception, the copyright holders give
27** permission to link the code of this program with any edition of
28** the TQt library by Trolltech AS, Norway (or with modified versions
29** of TQt that use the same license as TQt), and distribute linked
30** combinations including the two. You must obey the GNU General
31** Public License in all respects for all of the code used other than
32** TQt. If you modify this file, you may extend this exception to
33** your version of the file, but you are not obligated to do so. If
34** you do not wish to do so, delete this exception statement from
35** your version.
36**
37*******************************************************************************/
38#include "util.h"
39
40#include <stdlib.h>
41#include <tqcstring.h>
42#include <mimelib/string.h>
43
44size_t KMail::Util::crlf2lf( char* str, const size_t strLen )
45{
46 if ( !str || strLen == 0 )
47 return 0;
48
49 const char* source = str;
50 const char* sourceEnd = source + strLen;
51
52 // search the first occurrence of "\r\n"
53 for ( ; source < sourceEnd - 1; ++source ) {
54 if ( *source == '\r' && *( source + 1 ) == '\n' )
55 break;
56 }
57
58 if ( source == sourceEnd - 1 ) {
59 // no "\r\n" found
60 return strLen;
61 }
62
63 // replace all occurrences of "\r\n" with "\n" (in place)
64 char* target = const_cast<char*>( source ); // target points to '\r'
65 ++source; // source points to '\n'
66 for ( ; source < sourceEnd; ++source ) {
67 if ( *source != '\r' || *( source + 1 ) != '\n' )
68 * target++ = *source;
69 }
70 *target = '\0'; // terminate result
71 return target - str;
72}
73
74TQCString KMail::Util::lf2crlf( const TQCString & src )
75{
76 TQCString result( 1 + 2*src.size() ); // maximal possible length
77
78 TQCString::ConstIterator s = src.begin();
79 TQCString::Iterator d = result.begin();
80 // we use cPrev to make sure we insert '\r' only there where it is missing
81 char cPrev = '?';
82 while ( *s ) {
83 if ( ('\n' == *s) && ('\r' != cPrev) )
84 *d++ = '\r';
85 cPrev = *s;
86 *d++ = *s++;
87 }
88 result.truncate( d - result.begin() ); // adds trailing NUL
89 return result;
90}
91
92TQByteArray KMail::Util::lf2crlf( const TQByteArray & src )
93{
94 const char* s = src.data();
95 if ( !s )
96 return TQByteArray();
97
98 TQByteArray result( 2 * src.size() ); // maximal possible length
99 TQByteArray::Iterator d = result.begin();
100 // we use cPrev to make sure we insert '\r' only there where it is missing
101 char cPrev = '?';
102 const char* end = src.end();
103 while ( s != end ) {
104 if ( ('\n' == *s) && ('\r' != cPrev) )
105 *d++ = '\r';
106 cPrev = *s;
107 *d++ = *s++;
108 }
109 result.truncate( d - result.begin() ); // does not add trailing NUL, as expected
110 return result;
111}
112
113TQCString KMail::Util::CString( const DwString& str )
114{
115 const int strLen = str.size();
116 TQCString cstr( strLen + 1 );
117 memcpy( cstr.data(), str.data(), strLen );
118 cstr[ strLen ] = 0;
119 return cstr;
120}
121
122TQByteArray KMail::Util::ByteArray( const DwString& str )
123{
124 const int strLen = str.size();
125 TQByteArray arr( strLen );
126 memcpy( arr.data(), str.data(), strLen );
127 return arr;
128}
129
130DwString KMail::Util::dwString( const TQCString& str )
131{
132 if ( !str.data() ) // DwString doesn't like char*=0
133 return DwString();
134 return DwString( str.data(), str.size() - 1 );
135}
136
137DwString KMail::Util::dwString( const TQByteArray& str )
138{
139 if ( !str.data() ) // DwString doesn't like char*=0
140 return DwString();
141 return DwString( str.data(), str.size() );
142}
143
144void KMail::Util::append( TQByteArray& that, const TQByteArray& str )
145{
146 that.detach();
147 uint len1 = that.size();
148 uint len2 = str.size();
149 if ( that.resize( len1 + len2, TQGArray::SpeedOptim ) )
150 memcpy( that.data() + len1, str.data(), len2 );
151}
152
153void KMail::Util::append( TQByteArray& that, const char* str )
154{
155 if ( !str )
156 return; // nothing to append
157 that.detach();
158 uint len1 = that.size();
159 uint len2 = tqstrlen(str);
160 if ( that.resize( len1 + len2, TQGArray::SpeedOptim ) )
161 memcpy( that.data() + len1, str, len2 );
162}
163
164void KMail::Util::append( TQByteArray& that, const TQCString& str )
165{
166 that.detach();
167 uint len1 = that.size();
168 uint len2 = str.size() - 1;
169 if ( that.resize( len1 + len2, TQGArray::SpeedOptim ) )
170 memcpy( that.data() + len1, str.data(), len2 );
171}
172
173// Code taken from TQCString::insert, but trailing nul removed
174void KMail::Util::insert( TQByteArray& that, uint index, const char* s )
175{
176 int len = tqstrlen(s);
177 if ( len == 0 )
178 return;
179 uint olen = that.size();
180 int nlen = olen + len;
181 if ( index >= olen ) { // insert after end of string
182 that.detach();
183 if ( that.resize(nlen+index-olen, TQGArray::SpeedOptim ) ) {
184 memset( that.data()+olen, ' ', index-olen );
185 memcpy( that.data()+index, s, len );
186 }
187 } else {
188 that.detach();
189 if ( that.resize(nlen, TQGArray::SpeedOptim ) ) { // normal insert
190 memmove( that.data()+index+len, that.data()+index, olen-index );
191 memcpy( that.data()+index, s, len );
192 }
193 }
194}
void append(TQByteArray &that, const TQByteArray &str)
Append a bytearray to a bytearray.
Definition: util.cpp:144
TQByteArray ByteArray(const DwString &str)
Construct a TQByteArray from a DwString.
Definition: util.cpp:122
TQCString lf2crlf(const TQCString &src)
Convert "\n" line endings to "\r\n".
Definition: util.cpp:74
TQCString CString(const DwString &str)
Construct a TQCString from a DwString.
Definition: util.cpp:113
DwString dwString(const TQCString &str)
Construct a DwString from a TQCString.
Definition: util.cpp:130
size_t crlf2lf(char *str, const size_t strLen)
Convert all sequences of "\r\n" (carriage return followed by a line feed) to a single "\n" (line feed...
Definition: util.cpp:44