1 /* 2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 * 19 */ 20 21 #include "config.h" 22 #include "FormDataList.h" 23 24 namespace WebCore { 25 26 FormDataList::FormDataList(const TextEncoding& c) 27 : m_encoding(c) 28 { 29 } 30 31 void FormDataList::appendString(const CString& s) 32 { 33 m_list.append(s); 34 } 35 36 // Change plain CR and plain LF to CRLF pairs. 37 static CString fixLineBreaks(const CString& s) 38 { 39 // Compute the length. 40 unsigned newLen = 0; 41 const char* p = s.data(); 42 while (char c = *p++) { 43 if (c == '\r') { 44 // Safe to look ahead because of trailing '\0'. 45 if (*p != '\n') { 46 // Turn CR into CRLF. 47 newLen += 2; 48 } 49 } else if (c == '\n') { 50 // Turn LF into CRLF. 51 newLen += 2; 52 } else { 53 // Leave other characters alone. 54 newLen += 1; 55 } 56 } 57 if (newLen == s.length()) { 58 return s; 59 } 60 61 // Make a copy of the string. 62 p = s.data(); 63 char* q; 64 CString result = CString::newUninitialized(newLen, q); 65 while (char c = *p++) { 66 if (c == '\r') { 67 // Safe to look ahead because of trailing '\0'. 68 if (*p != '\n') { 69 // Turn CR into CRLF. 70 *q++ = '\r'; 71 *q++ = '\n'; 72 } 73 } else if (c == '\n') { 74 // Turn LF into CRLF. 75 *q++ = '\r'; 76 *q++ = '\n'; 77 } else { 78 // Leave other characters alone. 79 *q++ = c; 80 } 81 } 82 return result; 83 } 84 85 void FormDataList::appendString(const String& s) 86 { 87 CString cstr = fixLineBreaks(m_encoding.encode(s.characters(), s.length(), EntitiesForUnencodables)); 88 m_list.append(cstr); 89 } 90 91 } // namespace 92